forked from brandonmenc/znowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
113 lines (97 loc) · 3.13 KB
/
client.c
File metadata and controls
113 lines (97 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <inttypes.h>
#include <unistd.h>
#include <time.h>
#include <zmq.h>
#define TIME_BITLEN 39
#define MACHINE_BITLEN 15
#define SEQ_BITLEN 10
#define MACHINE_BITSHIFT SEQ_BITLEN
#define TIME_BITSHIFT SEQ_BITLEN + MACHINE_BITLEN
#define MACHINE_MASK (1ULL << MACHINE_BITLEN) - 1
#define SEQ_MASK (1ULL << SEQ_BITLEN) - 1
#define DEFAULT_PORT 23138
#define EPOCH 1337000000ULL
static uint64_t
id_recv (void *socket)
{
zmq_msg_t message;
zmq_msg_init (&message);
if (zmq_recv (socket, &message, 0)) {
printf ("Fatal: error receiving zmq message.\n");
exit (EXIT_FAILURE);
}
int size = zmq_msg_size (&message);
if (size != 8) {
printf ("Fatal: ID payload was not 64 bits.\n");
exit (EXIT_FAILURE);
}
uint64_t id;
memcpy (&id, zmq_msg_data (&message), 8);
zmq_msg_close (&message);
// Convert from network byte order
return (be64toh (id));
}
void
print_id (uint64_t id)
{
// Break it down
uint64_t ts = (EPOCH * 1000) + (id >> TIME_BITSHIFT);
uint64_t sec = ts / 1000;
uint64_t msec = ts - (sec * 1000);
uint64_t machine = (id >> MACHINE_BITSHIFT) & MACHINE_MASK;
uint64_t seq = id & SEQ_MASK;
// Print it out
time_t time = (time_t) sec;
char *timestr = ctime (&time);
printf ("id: %llu\n", id);
printf ("machine: %llu\n", machine);
printf ("datetime: %s", timestr);
printf ("timestamp: %llu\n", sec);
printf ("(msec, seq): (%llu, %llu)\n\n", msec, seq);
}
int
main (int argc, char **argv)
{
// Do some initial sanity checking
assert (TIME_BITLEN + MACHINE_BITLEN + SEQ_BITLEN == 64);
// Parse command-line arguments
int opt;
int has_port_opt = 0;
char *port;
while ((opt = getopt (argc, argv, "p:")) != -1) {
switch (opt) {
case 'p':
has_port_opt = 1;
port = optarg;
break;
}
}
// Build the ZMQ endpoint
char *zmq_endpoint = NULL;
if (!has_port_opt)
asprintf (&port, "%d", DEFAULT_PORT);
asprintf (&zmq_endpoint, "tcp://*:%s", port);
// Main loop
void *context = zmq_init (1);
void *socket = zmq_socket (context, ZMQ_REQ);
zmq_connect (socket, zmq_endpoint);
int request_nbr;
for (request_nbr = 0; request_nbr != 100; request_nbr++) {
// Send a zero-length message to the server
zmq_msg_t request;
zmq_msg_init_size (&request, 0);
memcpy (zmq_msg_data (&request), "", 0);
zmq_send (socket, &request, 0);
zmq_msg_close (&request);
uint64_t id = id_recv (socket);
print_id (id);
}
zmq_close (socket);
zmq_term (context);
exit (EXIT_SUCCESS);
}