void zctx_test (bool verbose) { printf (" * zctx: "); // @selftest // Create and destroy a context without using it zctx_t *ctx = zctx_new (); assert (ctx); zctx_destroy (&ctx); assert (ctx == NULL); // Create a context with many busy sockets, destroy it ctx = zctx_new (); assert (ctx); zctx_set_iothreads (ctx, 1); zctx_set_linger (ctx, 5); // 5 msecs void *s1 = zctx__socket_new (ctx, ZMQ_PAIR); void *s2 = zctx__socket_new (ctx, ZMQ_XREQ); void *s3 = zctx__socket_new (ctx, ZMQ_REQ); void *s4 = zctx__socket_new (ctx, ZMQ_REP); void *s5 = zctx__socket_new (ctx, ZMQ_PUB); void *s6 = zctx__socket_new (ctx, ZMQ_SUB); zsocket_connect (s1, "tcp://127.0.0.1:5555"); zsocket_connect (s2, "tcp://127.0.0.1:5555"); zsocket_connect (s3, "tcp://127.0.0.1:5555"); zsocket_connect (s4, "tcp://127.0.0.1:5555"); zsocket_connect (s5, "tcp://127.0.0.1:5555"); zsocket_connect (s6, "tcp://127.0.0.1:5555"); assert (zctx_underlying (ctx)); zctx_destroy (&ctx); // @end printf ("OK\n"); }
int main (int argc, char *argv []) { // Initialize context for talking to tasks zctx_t *ctx = zctx_new (); zctx_set_linger (ctx, 100); // Get number of interfaces to simulate, default 100 int max_interface = 100; int nbr_interface = 0; if (argc > 1) max_interface = atoi (argv [1]); // We address interfaces as an array of pipes void **pipes = zmalloc (sizeof (void *) * max_interface); for (nbr_interface = 0; nbr_interface < max_interface; nbr_interface++) { pipes [nbr_interface] = zthread_fork (ctx, interface_task, NULL); zclock_log ("I: Started interface (%d running)", nbr_interface + 1); } // We will randomly start and stop interface threads while (!zctx_interrupted) { zclock_sleep (1000); } zclock_log ("I: Stopped perl_remote"); zctx_destroy (&ctx); free (pipes); return 0; }
void send_czmq (char *buf, int len) { zctx_t *zctx; void *zs; zmsg_t *zmsg; if (!(zctx = zctx_new ())) log_err_exit ("C: zctx_new"); if (lopt) /* zctx linger default = 0 (flush none) */ zctx_set_linger (zctx, linger); if (!(zs = zsocket_new (zctx, ZMQ_DEALER))) log_err_exit ("C: zsocket_new"); //if (lopt) // doesn't work here // zsocket_set_linger (zs, linger); if (iopt) zsocket_set_immediate (zs, imm); //zsocket_set_sndhwm (zs, 0); /* unlimited */ if (zsocket_connect (zs, "%s", uri) < 0) log_err_exit ("C: zsocket_connect"); if (!(zmsg = zmsg_new ())) oom (); if (zmsg_pushmem (zmsg, buf, bufsize) < 0) oom (); if (zmsg_send (&zmsg, zs) < 0) log_err_exit ("C: zmsg_send"); if (sleep_usec > 0) usleep (sleep_usec); zctx_destroy (&zctx); }
int main (void) { zctx_t *ctx = zctx_new (); zctx_set_linger (ctx, 1000); void *pub = zsocket_new (ctx, ZMQ_XPUB); zsocket_set_hwm (pub, 0); zsocket_connect (pub, "tcp://127.0.0.1:9000"); // Wait for subscriber to subscribe zframe_t *frame = zframe_recv (pub); zframe_destroy (&frame); // Send HELLOs for five seconds size_t total = 0; int64_t finish_at = zclock_time () + 5000; while (zclock_time () < finish_at) { // Send 100K and then check time again int count = 0; for (count = 0; count < 100000; count++) zstr_send (pub, "HELLO"); total++; } printf ("%zd00000 messages sent\n", total); zstr_send (pub, "WORLD"); zctx_destroy (&ctx); return 0; }
static VALUE rb_czmq_nogvl_zctx_new(ZMQ_UNUSED void *ptr) { errno = 0; zctx_t *ctx = NULL; ctx = zctx_new(); zctx_set_linger(ctx, 1); return (VALUE)ctx; }
static VALUE rb_czmq_ctx_set_linger(VALUE obj, VALUE linger) { errno = 0; ZmqGetContext(obj); Check_Type(linger, T_FIXNUM); /* XXX: boundary checks */ zctx_set_linger(ctx->ctx, FIX2INT(linger)); return Qnil; }
int main(int argc, char const * const *argv) { int rc; zctx_t *context = zctx_new(); assert(context); zctx_set_rcvhwm(context, 1000); zctx_set_linger(context, 100); void *socket = zsocket_new(context, ZMQ_PULL); assert(socket); zsocket_set_rcvhwm(socket, 100000); zsocket_set_linger(socket, 500); zsocket_set_reconnect_ivl(socket, 100); // 100 ms zsocket_set_reconnect_ivl_max(socket, 10 * 1000); // 10 s const char* host = "localhost"; if (argc>1) host = argv[1]; rc = zsocket_connect(socket, "tcp://%s:9651", host); assert(rc==0); zmsg_t *msg = NULL; size_t received = 0; size_t lost = 0; size_t last_num = 0; while (1) { msg = zmsg_recv(socket); if (zsys_interrupted) break; assert(msg); received++; // assert(zmsg_size(msg) == 3); // zmsg_dump(msg); zframe_t *last_frame = zmsg_last(msg); char *str = zframe_strdup(last_frame); size_t n = atol(str); free(str); if (n > last_num + 1 && last_num != 0) { lost += n - (last_num + 1); } last_num = n; zmsg_destroy(&msg); } zsocket_destroy(context, socket); zctx_destroy(&context); printf("\nlost: %7zu\nreceived: %7zu\n", lost, received); return 0; }
zctx_t *new_context() { zctx_t *ctx; ctx = zctx_new(); assert(ctx != NULL); zctx_set_linger(ctx, 0); //** Disable the CZMQ SIGINT/SIGTERM signale handler apr_signal(SIGINT, NULL); apr_signal(SIGTERM, NULL); return(ctx); }
int main (int argc, char *argv []) { // Initialize context for talking to tasks zctx_t *ctx = zctx_new (); zctx_set_linger (ctx, 100); // Get number of nodes N to simulate // We need 3 x N x N + 3N file handles int max_nodes = 10; int nbr_nodes = 0; if (argc > 1) max_nodes = atoi (argv [1]); int max_iterations = -1; int nbr_iterations = 0; if (argc > 2) max_iterations = atoi (argv [2]); // We address nodes as an array of pipes void **pipes = zmalloc (sizeof (void *) * max_nodes); // We will randomly start and stop node threads while (!zctx_interrupted) { uint index = randof (max_nodes); // Toggle node thread if (pipes [index]) { zstr_send (pipes [index], "STOP"); zsocket_destroy (ctx, pipes [index]); pipes [index] = NULL; zclock_log ("I: Stopped node (%d running)", --nbr_nodes); } else { pipes [index] = zthread_fork (ctx, node_task, NULL); zclock_log ("I: Started node (%d running)", ++nbr_nodes); } nbr_iterations++; if (max_iterations > 0 && nbr_iterations >= max_iterations) break; // Sleep ~750 msecs randomly so we smooth out activity zclock_sleep (randof (500) + 500); } zclock_log ("I: Stopped tester (%d iterations)", nbr_iterations); // Does not actually terminate properly... :-/ // zctx_destroy (&ctx); free (pipes); return 0; }
mq_socket_context_t *zero_socket_context_new() { mq_socket_context_t *ctx; tbx_type_malloc_clear(ctx, mq_socket_context_t, 1); ctx->arg = zctx_new(); assert(ctx->arg != NULL); zctx_set_linger(ctx->arg, 0); ctx->create_socket = zero_create_socket; ctx->destroy = zero_socket_context_destroy; //** Disable the CZMQ SIGINT/SIGTERM signale handler apr_signal(SIGINT, NULL); apr_signal(SIGTERM, NULL); return(ctx); }
int main(int argc, char const * const *argv) { int socket_count = argc>1 ? atoi(argv[1]) : 1; int message_count = argc>2 ? atoi(argv[2]) : 100000; int rc; zctx_t *context = zctx_new(); assert(context); zctx_set_sndhwm(context, 1); zctx_set_linger(context, 100); // zmq 2.2 only supports up to 512 sockets assert_x(ZMQ_VERSION <= 20200 && socket_count > 512, "zeromq < 3.2 only supports up to 512 sockets"); void **sockets = zmalloc(socket_count * sizeof(void*)); int j; for (j=0; j<socket_count; j++) { void *socket = zsocket_new(context, ZMQ_PUSH); if (NULL==socket) { printf("Error occurred during %dth call to zsocket_new: %s\n", j, zmq_strerror (errno)); exit(1); } // zsocket_set_sndtimeo(socket, 10); zsocket_set_sndhwm(socket, 10); zsocket_set_linger(socket, 50); zsocket_set_reconnect_ivl(socket, 100); // 100 ms zsocket_set_reconnect_ivl_max(socket, 10 * 1000); // 10 s // printf("hwm %d\n", zsocket_hwm(socket)); // printf("sndbuf %d\n", zsocket_sndbuf(socket)); // printf("swap %d\n", zsocket_swap(socket)); // printf("linger %d\n", zsocket_linger(socket)); // printf("sndtimeo %d\n", zsocket_sndtimeo(socket)); // printf("reconnect_ivl %d\n", zsocket_reconnect_ivl(socket)); // printf("reconnect_ivl_max %d\n", zsocket_reconnect_ivl_max(socket)); rc = zsocket_connect(socket, "tcp://localhost:12345"); assert(rc==0); sockets[j] = socket; zclock_sleep(10); // sleep 10 ms } char data[MESSAGE_BODY_SIZE]; memset(data, 'a', MESSAGE_BODY_SIZE); char *exchanges[2] = {"zmq-device-1", "zmq-device-2"}; // char *keys[2] = {"1","2"}; int i = 0, queued = 0, rejected = 0; for (i=0; i<message_count; i++) { if (zsys_interrupted) break; zmsg_t *message = zmsg_new(); zmsg_addstr(message, exchanges[i%2]); zmsg_addstrf(message, "logjam.zmq.test.%d.%d", i%2, i); zmsg_addmem(message, data, MESSAGE_BODY_SIZE); // zmsg_dump(message); rc = zmsg_send_dont_wait(&message, sockets[i%socket_count]); assert(message == NULL); if (rc == 0) { queued++; } else { rejected++; } // usleep(20); usleep(1000); } printf("queued: %8d\n", queued); printf("rejected: %8d\n", rejected); for (i=0;i<socket_count;i++) { zsocket_destroy(context, sockets[i]); } free(sockets); zctx_destroy(&context); return 0; }
/* Creates a new instance of Device Information */ devio_t * devio_new (char *name, char *endpoint_dev, llio_type_e type, char *endpoint_broker, int verbose) { devio_t *self = (devio_t *) zmalloc (sizeof *self); ASSERT_ALLOC(self, err_self_alloc); /* Initialize the sockets structure to talk to nodes */ self->pipes = zmalloc (sizeof (void *) * NODES_MAX_LEN); ASSERT_ALLOC(self->pipes, err_pipes_alloc); /* 0 nodes for now... */ self->nnodes = 0; /* Nullify poller */ self->poller = zpoller_new (NULL); ASSERT_ALLOC(self->poller, err_poller_alloc); /* Initilize mdp_worrker last, as we need to have everything ready * when we attemp to register in broker. Actually, we still need * to parse the SDB strucutres and register the operations in the * hash tables... * TODO (FIXME?): Find a better initialitazion routine before registering * to the broker the request from clients */ self->name = strdup (name); ASSERT_ALLOC(self->name, err_name_alloc); self->endpoint_broker = strdup (endpoint_broker); ASSERT_ALLOC(self->endpoint_broker, err_endp_broker_alloc); self->verbose = verbose; /* Concatenate recv'ed name with a llio identifier */ char *llio_name = zmalloc (sizeof (char)*(strlen(name)+strlen(LLIO_STR)+1)); ASSERT_ALLOC(llio_name, err_llio_name_alloc); strcat (llio_name, name); strcat (llio_name, LLIO_STR); self->llio = llio_new (llio_name, endpoint_dev, type, verbose); ASSERT_ALLOC(self->llio, err_llio_alloc); /* We try to open the device */ int err = llio_open (self->llio, NULL); ASSERT_TEST(err==0, "Error opening device!", err_llio_open); /* We can free llio_name now, as llio copies the string */ free (llio_name); llio_name = NULL; /* Avoid double free error */ /* Init sm_io_thsafe_server_ops_h. For now, we assume we want zmq * for exchanging messages between smio and devio instances */ self->thsafe_server_ops = &smio_thsafe_zmq_server_ops; /* Init sm_io_h hash */ self->sm_io_h = zhash_new (); ASSERT_ALLOC(self->sm_io_h, err_sm_io_h_alloc); /* Init sm_io_thsafe_ops_h dispatch table */ self->disp_table_thsafe_ops = disp_table_new (); ASSERT_ALLOC(self->disp_table_thsafe_ops, err_disp_table_thsafe_ops_alloc); disp_table_func_fp *thsafe_server_fp = (disp_table_func_fp *) (self->thsafe_server_ops); const uint32_t *thsafe_opcodes_p = thsafe_opcodes; halutils_err_e halutils_err = disp_table_insert_all (self->disp_table_thsafe_ops, thsafe_server_fp, thsafe_opcodes_p, THSAFE_OPCODE_END); ASSERT_TEST(halutils_err==HALUTILS_SUCCESS, "Could not initialize dispatch table", err_disp_table_init); /* Finally, initialize mdp_worker with service being the BPM<board_number> */ /* self->worker = mdp_worker_new (endpoint_broker, name, verbose); ASSERT_ALLOC(self->worker, err_worker_alloc); */ /* Finally, initialize out zeroMQ context */ self->ctx = zctx_new (); ASSERT_ALLOC(self->ctx, err_ctx_alloc); /* Adjust linger time for Majordomo protocol (MDP) */ /* A non-zero linger value is required for DISCONNECT to be sent * when the worker is destroyed. 100 is arbitrary but chosen to be * sufficient for common cases without significant delay in broken ones. */ zctx_set_linger (self->ctx, 100); return self; err_ctx_alloc: err_disp_table_init: disp_table_destroy (&self->disp_table_thsafe_ops); err_disp_table_thsafe_ops_alloc: zhash_destroy (&self->sm_io_h); err_sm_io_h_alloc: llio_release (self->llio, NULL); err_llio_open: llio_destroy (&self->llio); err_llio_alloc: free (llio_name); err_llio_name_alloc: free (self->endpoint_broker); err_endp_broker_alloc: free (self->name); err_name_alloc: zpoller_destroy (&self->poller); err_poller_alloc: free (self->pipes); err_pipes_alloc: free (self); err_self_alloc: return NULL; }
char *CASGC_read( char *obj_name, char *writer_id, unsigned int op_num , // char *payload, char *servers_str, char *port ) { s_catch_signals(); int j; printf("Obj name : %s\n",obj_name); printf("Writer name : %s\n",writer_id); printf("Operation num : %d\n",op_num); /* char *myb64 = (char *)malloc(strlen(payload)); b64_decode(payload, myb64); printf("Encoded string : %s\n", payload); free(myb64); */ printf("Server string : %s\n", servers_str); printf("Port to Use : %s\n", port); int num_servers = count_num_servers(servers_str); printf("Num of Servers : %d\n",num_servers); // printf("Decoded string : %s\n", myb64); char **servers = create_server_names(servers_str); for(j=0; j < num_servers; j++) { printf("\tServer : %s\n", servers[j]); } printf("\n"); zctx_t *ctx = zctx_new(); void *sock_to_servers = zsocket_new(ctx, ZMQ_DEALER); zctx_set_linger(ctx, 0); assert (sock_to_servers); zsocket_set_identity(sock_to_servers, writer_id); for(j=0; j < num_servers; j++) { char *destination = create_destination(servers[j], port); int rc = zsocket_connect(sock_to_servers, destination); assert(rc==0); free(destination); } printf("READ %d\n", op_num); printf(" MAX_TAG_VALUE (READER)\n"); char *payload; TAG max_tag; get_max_tag_value_phase(obj_name, op_num, sock_to_servers, servers, num_servers, port, &max_tag, &payload); printf("\tmax tag (%d,%s)\n\n", max_tag.z, max_tag.id); printf(" WRITE_VALUE (READER)\n"); int size = strlen(payload); write_value_phase(obj_name, writer_id, op_num, sock_to_servers, servers, num_servers, port, payload, size, max_tag); zsocket_destroy(ctx, sock_to_servers); zctx_destroy(&ctx); return payload; }
// ABD write bool CASGC_write( char *obj_name, char *writer_id, unsigned int op_num , char *payload, unsigned int size, char *servers_str, char *port ) { s_catch_signals(); int j; printf("Obj name : %s\n",obj_name); printf("Writer name : %s\n",writer_id); printf("Operation num : %d\n",op_num); printf("Size : %d\n", size); printf("Size of : %u\n", (unsigned int)strlen(payload)); char *myb64 = (char *)malloc(strlen(payload)); b64_decode(payload, myb64); printf("Encoded string : %s\n", payload); printf("Server string : %s\n", servers_str); printf("Port to Use : %s\n", port); int num_servers = count_num_servers(servers_str); printf("Num of Servers : %d\n",num_servers); // printf("Decoded string : %s\n", myb64); char **servers = create_server_names(servers_str); for(j=0; j < num_servers; j++) { printf("\tServer : %s\n", servers[j]); } printf("\n"); free(myb64); zctx_t *ctx = zctx_new(); void *sock_to_servers = zsocket_new(ctx, ZMQ_DEALER); zctx_set_linger(ctx, 0); assert (sock_to_servers); zsocket_set_identity(sock_to_servers, writer_id); for(j=0; j < num_servers; j++) { char *destination = create_destination(servers[j], port); int rc = zsocket_connect(sock_to_servers, (const char *)destination); assert(rc==0); free(destination); } printf("WRITE %d\n", op_num); printf(" MAX_TAG (WRITER)\n"); TAG max_tag= get_max_tag_phase(obj_name, op_num, sock_to_servers, servers, num_servers, port); //Create a new tag TAG new_tag; new_tag.z = max_tag.z + 1; strcpy(new_tag.id, writer_id); printf(" WRITE_VALUE (WRITER)\n"); write_value_phase(obj_name, writer_id, op_num, sock_to_servers, servers, num_servers, port, payload, size, max_tag); zsocket_destroy(ctx, sock_to_servers); zctx_destroy(&ctx); return true; }
int main (int argc, char *argv []) { if (argc != 4) { printf ("usage: %s <bind-to> <message-size> " "<roundtrip-count>\n", argv[0]); return 1; } char* bind_to = argv [1]; long message_size = atoi (argv [2]); long roundtrip_count = atoi (argv [3]); char check_dropped_packets = 1; if(message_size < GetNumberOfDigits(roundtrip_count)) { printf("CAUTION: Message size too small to check for dropped packets\r\n"); check_dropped_packets = 0; } zctx_t *context = zctx_new (); void *publisher = zsocket_new (context, ZMQ_XPUB); zctx_set_linger(context, 1000); zsocket_set_sndhwm(publisher, 1000); int hwm = zsocket_sndhwm(publisher); printf("HMW=%d\r\n", hwm); #ifdef ZMQ_PUB_RELIABLE // set PUB_RELIABLE int pub_reliable = 1; int rc = zmq_setsockopt(publisher, ZMQ_PUB_RELIABLE, &pub_reliable, sizeof(pub_reliable)); if (rc != 0) { printf ("error in zmq_setsockopt (ZMQ_PUB_RELIABLE): %s\n", zmq_strerror (errno)); return -1; } #endif printf("Connecting to %s\r\n", bind_to); zsocket_bind (publisher, bind_to); //Wait for sub connection printf("Waiting for subscriber.\r\n"); zmsg_t* connection = zmsg_recv(publisher); zmsg_destroy(&connection); printf("Subscriber connected!\r\n"); int i = 0; while (i<roundtrip_count && !zctx_interrupted) { void* data = malloc(message_size); bzero(data, message_size); if(check_dropped_packets) { sprintf(data, "%d", i); } zmsg_t* msg = zmsg_new(); zmsg_addstr(msg, "TEST", 4); zmsg_addmem(msg, data, message_size); //zmsg_dump(msg); zmsg_send (&msg, publisher); i++; free(data); } zctx_destroy (&context); return 0; }