ProBInitialResponse prob_init(prob_client_t pc, int is_por) { Debugf("initializing ProB Zocket\n") zmsg_t *request = zmsg_new(); zmsg_addstr(request, "init"); zmsg_addstrf(request, "%d", pc->id_count); zmsg_addstrf(request, "%d", is_por); Debugf("sending message with length %zu, contents are:\n", zmsg_content_size(request)); #ifdef LTSMIN_DEBUG if (log_active(debug)) zmsg_print(request); #endif if (zmsg_send(&request, pc->zocket) != 0) Abort("Could not send message"); zmsg_destroy(&request); zmsg_t *response = zmsg_recv(pc->zocket); if (response == NULL) Abort("Did not receive valid response"); Debugf("received message with length %zu, contents are:\n", zmsg_content_size(response)); #ifdef LTSMIN_DEBUG if (log_active(debug)) zmsg_print(response); #endif ProBInitialResponse resp = prob_get_init_response(response); if (zmsg_size(response) != 0) Abort("Did not receive valid reponse size"); // puts("transition groups:"); // print_chunk_array(resp.transition_groups); // puts("variables"); // print_chunk_array(resp.variables); // for (size_t i = 0; i < resp.variables.size; i++) { // printf("%s (%s)\n", resp.variables.chunks[i].data, resp.variable_types.chunks[i].data); // } // puts("state labels"); // print_chunk_array(resp.state_labels); // // puts("May Write Matrix:"); // print_matrix(resp.may_write); // puts("Must Write Matrix:"); // print_matrix(resp.must_write); // puts("Reads Action Matrix:"); // print_matrix(resp.reads_action); // puts("Reads Guard Matrix:"); // print_matrix(resp.reads_guard); zmsg_destroy(&response); return resp; }
int main (int argc, char *argv []) { int verbose = (argc > 1 && streq (argv [1], "-v")); mdcli_t *session = mdcli_new ("tcp://localhost:5555", verbose); zmsg_t *request; zmsg_t *reply; int count; //for (count = 0; count < 1; count++) { request = zmsg_new (); zmsg_pushstr (request, "KILL"); reply = mdcli_send (session, "echo", &request); if (reply) { zframe_t * lastFrame = zmsg_last(reply); char* msg = zframe_strdup(lastFrame); printf("msg:%s\n", msg); printf("size:%d\n", zmsg_content_size(reply)); //zframe_print(lastFrame,"->"); zmsg_destroy (&reply); } //else // break; // Interrupt or failure //} //printf ("%d requests/replies processed\n", count); mdcli_destroy (&session); return 0; }
int zsock_wait (void *self) { assert (self); // A signal is a message containing one frame with our 8-byte magic // value. If we get anything else, we discard it and continue to look // for the signal message while (true) { zmsg_t *msg = zmsg_recv (self); if (!msg) return -1; if (zmsg_size (msg) == 1 && zmsg_content_size (msg) == 8) { zframe_t *frame = zmsg_first (msg); int64_t signal_value = *((int64_t *) zframe_data (frame)); if ((signal_value & 0xFFFFFFFFFFFFFF00L) == 0x7766554433221100L) { zmsg_destroy (&msg); return signal_value & 255; } } zmsg_destroy (&msg); } return -1; }
int zmsg_signal (zmsg_t *self) { if (zmsg_size (self) == 1 && zmsg_content_size (self) == 8) { zframe_t *frame = zmsg_first (self); int64_t signal_value = *((int64_t *) zframe_data (frame)); if ((signal_value & 0xFFFFFFFFFFFFFF00L) == 0x7766554433221100L) return signal_value & 255; } return -1; }
int main (int argc, char *argv []) { zmsg_t *reply = 0; mdwrk_t *session = 0; zframe_t * lastFrame = 0; char* lastMsg = 0; int verbose = (argc > 1 && streq (argv [1], "-v")); //if (argc = 2 && !streq (argv [1], "-v")) //{ // session = mdwrk_new ("tcp://localhost:5555", argv[1], verbose); //} //else if (argc = 3 && !streq (argv [2], "")) //{ // session = mdwrk_new ("tcp://localhost:5555", argv[2], verbose); //} //else //{ session = mdwrk_new ("tcp://localhost:5555", "echo", verbose); //} if (!session) return 1; while (true) { zmsg_t *request = mdwrk_recv (session, &reply); if (request == NULL) break; // Worker was interrupted ////////////////////////////////////加入自己的雷达逻辑////////////////////////////////// //解析收到的信息 lastFrame = zmsg_last(request); lastMsg = zframe_strdup(lastFrame); printf("zmsg_t::msg:%s\n", lastMsg); printf("zmsg_t::size:%d\n", zmsg_content_size(request)); // // // // //建立回复信息 reply = zmsg_new (); zmsg_pushstr (reply, "Done!"); //////////////////////////////////////////////////////////////////////////////////////// zmsg_destroy (&request); //reply = request; // Echo is complex... :-) } mdwrk_destroy (&session); return 0; }
int main(void) { zmsg_t *msg; zframe_t *frame; char *str; int i, rc; // create push/pull sockets zsock_t *push = zsock_new_push("inproc://example"); zsock_t *pull = zsock_new_pull("inproc://example"); // send multi-frame message msg = zmsg_new(); zmsg_addmem(msg, "apple", 5); zmsg_addmem(msg, "banana", 6); zmsg_addmem(msg, "cherry", 6); assert(zmsg_size(msg) == 3); assert(zmsg_content_size(msg) == 5+6+6); rc = zmsg_send(&msg, push); assert(msg == NULL); assert(rc == 0); // receive multi-frame message msg = zmsg_recv(pull); assert(msg); assert(zmsg_size(msg) == 3); assert(zmsg_content_size(msg) == 5+6+6); for (i = 0; i < 3; i++) { str = zmsg_popstr(msg); puts(str); } zmsg_destroy(&msg); // disconnect zsock_destroy(&push); zsock_destroy(&pull); return 0; }
int rrsvr_recv(rrsvr_t *self, void *buf, size_t len) { int nbytes = 0; if (zsocket_poll(self->socket, self->timeout)) { zmsg_t *msg = zmsg_recv(self->socket); if (self->mode == ASYNC_MODE) { //** Retrieves envelope free(self->cli_identity); self->cli_identity = zmsg_popstr(msg); } nbytes = zmsg_content_size(msg); size_t to_copy = (size_t)nbytes < len ? (size_t)nbytes : len; zframe_t *frame = zmsg_pop(msg); memcpy (buf, zframe_data(frame), to_copy); zframe_destroy(&frame); zmsg_destroy(&msg); } return nbytes; }
void zmsg_test (bool verbose) { printf (" * zmsg: "); int rc = 0; // @selftest // Create two PAIR sockets and connect over inproc zsock_t *output = zsock_new_pair ("@inproc://zmsg.test"); assert (output); zsock_t *input = zsock_new_pair (">inproc://zmsg.test"); assert (input); // Test send and receive of single-frame message zmsg_t *msg = zmsg_new (); assert (msg); zframe_t *frame = zframe_new ("Hello", 5); assert (frame); zmsg_prepend (msg, &frame); assert (zmsg_size (msg) == 1); assert (zmsg_content_size (msg) == 5); rc = zmsg_send (&msg, output); assert (msg == NULL); assert (rc == 0); msg = zmsg_recv (input); assert (msg); assert (zmsg_size (msg) == 1); assert (zmsg_content_size (msg) == 5); zmsg_destroy (&msg); // Test send and receive of multi-frame message msg = zmsg_new (); assert (msg); rc = zmsg_addmem (msg, "Frame0", 6); assert (rc == 0); rc = zmsg_addmem (msg, "Frame1", 6); assert (rc == 0); rc = zmsg_addmem (msg, "Frame2", 6); assert (rc == 0); rc = zmsg_addmem (msg, "Frame3", 6); assert (rc == 0); rc = zmsg_addmem (msg, "Frame4", 6); assert (rc == 0); rc = zmsg_addmem (msg, "Frame5", 6); assert (rc == 0); rc = zmsg_addmem (msg, "Frame6", 6); assert (rc == 0); rc = zmsg_addmem (msg, "Frame7", 6); assert (rc == 0); rc = zmsg_addmem (msg, "Frame8", 6); assert (rc == 0); rc = zmsg_addmem (msg, "Frame9", 6); assert (rc == 0); zmsg_t *copy = zmsg_dup (msg); assert (copy); rc = zmsg_send (©, output); assert (rc == 0); rc = zmsg_send (&msg, output); assert (rc == 0); copy = zmsg_recv (input); assert (copy); assert (zmsg_size (copy) == 10); assert (zmsg_content_size (copy) == 60); zmsg_destroy (©); msg = zmsg_recv (input); assert (msg); assert (zmsg_size (msg) == 10); assert (zmsg_content_size (msg) == 60); // create empty file for null test FILE *file = fopen ("zmsg.test", "w"); assert (file); fclose (file); file = fopen ("zmsg.test", "r"); zmsg_t *null_msg = zmsg_load (NULL, file); assert (null_msg == NULL); fclose (file); remove ("zmsg.test"); // Save to a file, read back file = fopen ("zmsg.test", "w"); assert (file); rc = zmsg_save (msg, file); assert (rc == 0); fclose (file); file = fopen ("zmsg.test", "r"); rc = zmsg_save (msg, file); assert (rc == -1); fclose (file); zmsg_destroy (&msg); file = fopen ("zmsg.test", "r"); msg = zmsg_load (NULL, file); assert (msg); fclose (file); remove ("zmsg.test"); assert (zmsg_size (msg) == 10); assert (zmsg_content_size (msg) == 60); // Remove all frames except first and last int frame_nbr; for (frame_nbr = 0; frame_nbr < 8; frame_nbr++) { zmsg_first (msg); frame = zmsg_next (msg); zmsg_remove (msg, frame); zframe_destroy (&frame); } // Test message frame manipulation assert (zmsg_size (msg) == 2); frame = zmsg_last (msg); assert (zframe_streq (frame, "Frame9")); assert (zmsg_content_size (msg) == 12); frame = zframe_new ("Address", 7); assert (frame); zmsg_prepend (msg, &frame); assert (zmsg_size (msg) == 3); rc = zmsg_addstr (msg, "Body"); assert (rc == 0); assert (zmsg_size (msg) == 4); frame = zmsg_pop (msg); zframe_destroy (&frame); assert (zmsg_size (msg) == 3); char *body = zmsg_popstr (msg); assert (streq (body, "Frame0")); free (body); zmsg_destroy (&msg); // Test encoding/decoding msg = zmsg_new (); assert (msg); byte *blank = (byte *) zmalloc (100000); assert (blank); rc = zmsg_addmem (msg, blank, 0); assert (rc == 0); rc = zmsg_addmem (msg, blank, 1); assert (rc == 0); rc = zmsg_addmem (msg, blank, 253); assert (rc == 0); rc = zmsg_addmem (msg, blank, 254); assert (rc == 0); rc = zmsg_addmem (msg, blank, 255); assert (rc == 0); rc = zmsg_addmem (msg, blank, 256); assert (rc == 0); rc = zmsg_addmem (msg, blank, 65535); assert (rc == 0); rc = zmsg_addmem (msg, blank, 65536); assert (rc == 0); rc = zmsg_addmem (msg, blank, 65537); assert (rc == 0); free (blank); assert (zmsg_size (msg) == 9); byte *buffer; size_t buffer_size = zmsg_encode (msg, &buffer); zmsg_destroy (&msg); msg = zmsg_decode (buffer, buffer_size); assert (msg); free (buffer); zmsg_destroy (&msg); // Test submessages msg = zmsg_new (); assert (msg); zmsg_t *submsg = zmsg_new (); zmsg_pushstr (msg, "matr"); zmsg_pushstr (submsg, "joska"); rc = zmsg_addmsg (msg, &submsg); assert (rc == 0); assert (submsg == NULL); submsg = zmsg_popmsg (msg); assert (submsg == NULL); // string "matr" is not encoded zmsg_t, so was discarded submsg = zmsg_popmsg (msg); assert (submsg); body = zmsg_popstr (submsg); assert (streq (body, "joska")); free (body); zmsg_destroy (&submsg); frame = zmsg_pop (msg); assert (frame == NULL); zmsg_destroy (&msg); // Test comparison of two messages msg = zmsg_new (); zmsg_addstr (msg, "One"); zmsg_addstr (msg, "Two"); zmsg_addstr (msg, "Three"); zmsg_t *msg_other = zmsg_new (); zmsg_addstr (msg_other, "One"); zmsg_addstr (msg_other, "Two"); zmsg_addstr (msg_other, "One-Hundred"); zmsg_t *msg_dup = zmsg_dup (msg); zmsg_t *empty_msg = zmsg_new (); zmsg_t *empty_msg_2 = zmsg_new (); assert (zmsg_eq (msg, msg_dup)); assert (!zmsg_eq (msg, msg_other)); assert (zmsg_eq (empty_msg, empty_msg_2)); assert (!zmsg_eq (msg, NULL)); assert (!zmsg_eq (NULL, empty_msg)); assert (!zmsg_eq (NULL, NULL)); zmsg_destroy (&msg); zmsg_destroy (&msg_other); zmsg_destroy (&msg_dup); zmsg_destroy (&empty_msg); zmsg_destroy (&empty_msg_2); // Test signal messages msg = zmsg_new_signal (0); assert (zmsg_signal (msg) == 0); zmsg_destroy (&msg); msg = zmsg_new_signal (-1); assert (zmsg_signal (msg) == 255); zmsg_destroy (&msg); // Now try methods on an empty message msg = zmsg_new (); assert (msg); assert (zmsg_size (msg) == 0); assert (zmsg_unwrap (msg) == NULL); assert (zmsg_first (msg) == NULL); assert (zmsg_last (msg) == NULL); assert (zmsg_next (msg) == NULL); assert (zmsg_pop (msg) == NULL); // Sending an empty message is valid and destroys the message assert (zmsg_send (&msg, output) == 0); assert (!msg); zsock_destroy (&input); zsock_destroy (&output); // @end printf ("OK\n"); }
/// // Return total size of all frames in message. size_t QmlZmsg::contentSize () { return zmsg_content_size (self); };
/// // Return total size of all frames in message. size_t QZmsg::contentSize () { size_t rv = zmsg_content_size (self); return rv; }
Z K1(zmsgcontentsize){PC(x); R kj(zmsg_content_size(VSK(x)));}
static void server_worker (void *_server_args, zctx_t *ctx) { void *worker = zsocket_new (ctx, ZMQ_DEALER); zsocket_connect(worker, "inproc://backend"); int64_t affinity = 5000; int rc = zmq_setsockopt(socket, ZMQ_SNDBUF, &affinity, sizeof affinity); rc = zmq_setsockopt(socket, ZMQ_RCVBUF, &affinity, sizeof affinity); char algorithm_name[BUFSIZE]; server_args = (Server_Args *)_server_args; printf("Initial value size %ld\n", strlen(server_args->init_data)); printf("Initial server str %s\n", server_args->servers_str); zmq_pollitem_t items[] = { { worker, 0, ZMQ_POLLIN, 0}}; // int count = 0; while (true) { printf("\twaiting for message\n"); int rc = zmq_poll(items, 1, -1); if( rc < 0 || s_interrupted==1) { exit(EXIT_FAILURE); } if (items[0].revents & ZMQ_POLLIN) { printf("\treceived message\n"); zmsg_t *msg = zmsg_recv(worker); // receive the frames status->network_data += (float)zmsg_content_size(msg) ; zlist_t *frames_list = zlist_new(); zhash_t *frames = receive_message_frames_at_server(msg, frames_list); get_string_frame(algorithm_name, frames, ALGORITHM); if( strcmp(algorithm_name, "ABD")==0) { printf(" [[[ %s\n", server_args->server_id); if(DEBUG_MODE) { print_out_hash_in_order(frames, frames_list); } algorithm_ABD(frames, worker, server_args); printf(" ]]]\n\n"); } else if( strcmp(algorithm_name, SODAW)==0) { printf(" [[[ %s\n",server_args->server_id); if(DEBUG_MODE) { print_out_hash_in_order(frames, frames_list); } algorithm_SODAW(frames, worker, server_args); printf(" ]]]\n\n"); } zlist_purge(frames_list); zlist_destroy(&frames_list); destroy_frames(frames); zmsg_destroy(&msg); } } printf("done\n"); }
// Main 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; int has_machine_opt = 0; int has_config_file_opt = 0; int has_daemonize_opt = 0; int machine_specified = 0; const char *config_file_path; int port = DEFAULT_PORT; uint64_t machine; while ((opt = getopt (argc, argv, "hp:m:f:d")) != -1) { switch (opt) { case 'h': print_help (); exit (EXIT_SUCCESS); case 'p': has_port_opt = 1; port = atoi (optarg); break; case 'm': has_machine_opt = 1; machine = atoll (optarg); machine_specified = 1; break; case 'f': has_config_file_opt = 1; config_file_path = optarg; break; case 'd': has_daemonize_opt = 1; break; } } // Read the config file if (has_config_file_opt) { config_t cfg; config_init (&cfg); if (!config_read_file (&cfg, config_file_path)) { config_destroy (&cfg); fprintf (stderr, "Invalid config file\n"); exit (EXIT_FAILURE); } long unsigned int machine_from_file; if (config_lookup_int (&cfg, "machine", &machine_from_file) && !has_machine_opt) { machine_specified = 1; machine = (uint64_t) machine_from_file; } long unsigned int port_from_file; if (config_lookup_int (&cfg, "port", &port_from_file) && !has_port_opt) port = (int) port_from_file; } // Sanity check the machine number if (!machine_specified) { fprintf (stderr, "No machine number specified.\n"); exit (EXIT_FAILURE); } else if (machine > MACHINE_MAX) { fprintf (stderr, "Machine number too large. Cannot be greater than %llu\n", MACHINE_MAX); exit (EXIT_FAILURE); } // Daemonize static char *pid_file_path = "/var/run/znowflaked.pid"; int pid_file; if (has_daemonize_opt) { pid_t pid, sid; pid = fork (); if (pid < 0) { exit (EXIT_FAILURE); } if (pid > 0) { exit (EXIT_SUCCESS); } umask (0); sid = setsid (); if (sid < 0) { exit (EXIT_FAILURE); } if ((chdir ("/")) < 0) { exit (EXIT_FAILURE); } // Create and lock the pid file pid_file = open (pid_file_path, O_CREAT | O_RDWR, 0666); if (pid_file > 0) { int rc = lockf (pid_file, F_TLOCK, 0); if (rc) { switch (errno) { case EACCES: case EAGAIN: fprintf (stderr, "PID file already locked\n"); break; case EBADF: fprintf (stderr, "Bad pid file\n"); break; default: fprintf (stderr, "Could not lock pid file\n"); } exit (EXIT_FAILURE); } char *pid_string = NULL; int pid_string_len = asprintf (&pid_string, "%ld", (long) getpid ()); write (pid_file, pid_string, pid_string_len); } close (STDIN_FILENO); close (STDOUT_FILENO); close (STDERR_FILENO); } // Sleep for 1ms to prevent collisions struct timespec ms; ms.tv_sec = 0; ms.tv_nsec = 1000000; nanosleep (&ms, NULL); // Initialize ZeroMQ zctx_t *context = zctx_new (); assert (context); void *socket = zsocket_new (context, ZMQ_REP); assert (socket); assert (streq (zsocket_type_str (socket), "REP")); int rc = zsocket_bind (socket, "tcp://*:%d", port); if (rc != port) { printf ("E: bind failed: %s\n", strerror (errno)); exit (EXIT_FAILURE); } // Start remembering the last timer tick uint64_t ts = 0; uint64_t last_ts = 0; uint64_t seq = 0; // Main loop s_catch_signals (); while (1) { // Wait for the next request zmsg_t *request_msg = zmsg_new (); assert (request_msg); request_msg = zmsg_recv (socket); assert (request_msg); zmsg_destroy (&request_msg); // Grab a time click last_ts = ts; ts = get_ts (); // Make sure the system clock wasn't reversed on us if (ts < last_ts) { // Wait until it catches up while (ts <= last_ts) { nanosleep (&ms, NULL); ts = get_ts (); } } // Increment the sequence number if (ts != last_ts) { // We're in a new time click, so reset the sequence seq = 0; } else if (seq == SEQ_MAX) { // Wrapped sequence, so wait for the next time tick seq = 0; nanosleep (&ms, NULL); } else { // Still in the same time click seq++; } // Build the ID and put it in network byte order uint64_t id = build_id (ts, machine, seq); uint64_t id_be64 = htobe64 (id); // Reply zmsg_t *reply_msg = zmsg_new (); assert (reply_msg); zframe_t *frame = zframe_new (&id_be64, 8); assert (frame); zmsg_push (reply_msg, frame); assert (zmsg_size (reply_msg) == 1); assert (zmsg_content_size (reply_msg) == 8); zmsg_send (&reply_msg, socket); assert (reply_msg == NULL); // Exit program if (s_interrupted) { printf ("interrupt received, killing server…\n"); break; } } zctx_destroy (&context); if (has_daemonize_opt) { if (pid_file > 0) { unlink (pid_file_path); close (pid_file); } } exit (EXIT_SUCCESS); }