bool CZmqWrapper::addDevice(std::string srcstring,std::string dststring) { zmq::socket_t* _pSrcSocket = new zmq::socket_t(m_context,ZMQ_ROUTER); if (!_pSrcSocket) { return false; } try { _pSrcSocket->bind(srcstring.c_str()); } catch(zmq::error_t err) { printf("%s,%d,error %s\n",__FUNCTION__,__LINE__,err.what()); } zmq::socket_t* _pDstSocket = new zmq::socket_t(m_context,ZMQ_DEALER); if (!_pDstSocket) { return false; } try { _pDstSocket->bind(dststring.c_str()); } catch(zmq::error_t err) { printf("%s,%d,error %s\n",__FUNCTION__,__LINE__,err.what()); } zmq_device(ZMQ_QUEUE,*_pSrcSocket,*_pDstSocket); }
void *glbColl_add_storage(void *arg) { void *context; void *frontend; void *backend; int ret; context = zmq_init(1); frontend = zmq_socket(context, ZMQ_SUB); if (!frontend) pthread_exit(NULL); zmq_bind(frontend, "tcp://127.0.0.1:6910"); zmq_setsockopt(frontend, ZMQ_SUBSCRIBE, "", 0); backend = zmq_socket(context, ZMQ_PUB); if (!backend) pthread_exit(NULL); zmq_bind(backend, "tcp://127.0.0.1:6911"); printf(" ++ Collection storage device ready....\n"); zmq_device(ZMQ_FORWARDER, frontend, backend); /* we never get here */ zmq_close(frontend); zmq_close(backend); zmq_term(context); return; }
void ciao_zmq_device(char *type_atom, char *frontend, char *backend) { ciao_zmq_socket_assoc *front_assoc= find_socket(frontend); if(front_assoc == NULL) { report_error(EINVAL, "socket_not_found", frontend); return; } ciao_zmq_socket_assoc *back_assoc= find_socket(backend); if(back_assoc==NULL) { report_error(EINVAL, "socket_not_found", backend); return; } ciao_zmq_atom_option *opt= find_option(device_options, type_atom); if(opt == NULL) { report_error(EINVAL, "invalid_device", type_atom); return; } zmq_device(opt->value, front_assoc->zmq_socket, back_assoc->zmq_socket); }
/** * glbCollection network service startup */ static int glbColl_start(struct glbColl *self) { void *context; void *frontend; void *backend; pthread_t storage_outlet; int ret; ret = pthread_create(&storage_outlet, NULL, glbColl_add_storage, (void*)self); context = zmq_init(1); frontend = zmq_socket(context, ZMQ_PULL); backend = zmq_socket(context, ZMQ_PUSH); zmq_bind(frontend, "tcp://127.0.0.1:6908"); zmq_bind(backend, "tcp://127.0.0.1:6909"); printf(" ++ Collection server is up and running!...\n\n"); zmq_device(ZMQ_QUEUE, frontend, backend); /* we never get here */ zmq_close(frontend); zmq_close(backend); zmq_term(context); return glb_OK; }
int main () { s_version_assert (2, 1); void *context = zmq_init (1); // Socket to talk to clients void *clients = zmq_socket (context, ZMQ_XREP); zmq_bind (clients, "tcp://*:5555"); // Socket to talk to workers void *workers = zmq_socket (context, ZMQ_XREQ); zmq_bind (workers, "inproc://workers"); // Launch pool of worker threads int thread_nbr; for (thread_nbr = 0; thread_nbr != 5; thread_nbr++) { pthread_t worker; pthread_create (&worker, NULL, worker_routine, context); } // Connect work threads to client threads via a queue zmq_device (ZMQ_QUEUE, clients, workers); // We never get here but clean up anyhow zmq_close (clients); zmq_close (workers); zmq_term (context); return 0; }
// Starts an automagical device // // - zqueue acts as broker, binds XREP and XREQ // - zforwarder acts as proxy, connects SUB, binds PUB // - zstreamer acts as proxy, binds PULL, connects PUSH // static void s_start_automagic_device (char *type, char *frontend, char *backend) { void *context = zmq_init (1); int device; // 0MQ defined type int frontend_type; // Socket types depending int backend_type; // on the device type s_parse_device_type (type, &device, &frontend_type, &backend_type); // Create and configure sockets void *frontend_socket = zmq_socket (context, frontend_type); assert (frontend_socket); void *backend_socket = zmq_socket (context, backend_type); assert (backend_socket); if (device == ZMQ_QUEUE) { printf ("I: Binding to %s for client connections\n", frontend); if (zmq_bind (frontend_socket, frontend)) { printf ("E: cannot bind to '%s': %s\n", frontend, zmq_strerror (errno)); exit (EXIT_FAILURE); } printf ("I: Binding to %s for service connections\n", backend); if (zmq_bind (backend_socket, backend)) { printf ("E: cannot bind to '%s': %s\n", backend, zmq_strerror (errno)); exit (EXIT_FAILURE); } } else if (device == ZMQ_FORWARDER) { printf ("I: Connecting to publisher at %s\n", frontend); if (zmq_connect (frontend_socket, frontend)) { printf ("E: cannot connect to '%s': %s\n", frontend, zmq_strerror (errno)); exit (EXIT_FAILURE); } printf ("I: Binding to %s for subscriber connections\n", backend); if (zmq_bind (backend_socket, backend)) { printf ("E: cannot bind to '%s': %s\n", backend, zmq_strerror (errno)); exit (EXIT_FAILURE); } } else if (device == ZMQ_STREAMER) { printf ("I: Binding to %s for upstream nodes\n", frontend); if (zmq_bind (frontend_socket, frontend)) { printf ("E: cannot bind to '%s': %s\n", frontend, zmq_strerror (errno)); exit (EXIT_FAILURE); } printf ("I: Connecting downstream to %s\n", backend); if (zmq_connect (backend_socket, backend)) { printf ("E: cannot connect to '%s': %s\n", backend, zmq_strerror (errno)); exit (EXIT_FAILURE); } } zmq_device (device, frontend_socket, backend_socket); }
// Starts a device as configured by a full ZDCF config file // static void s_start_configured_device (char *filename) { FILE *file; // JSON config file stream if (streq (filename, "-")) file = stdin; // "-" means read from stdin else { file = fopen (filename, "rb"); if (!file) { printf ("E: '%s' doesn't exist or can't be read\n", filename); exit (EXIT_FAILURE); } } // Load config data into blob zfl_blob_t *blob = zfl_blob_new (); assert (blob); zfl_blob_load (blob, file); fclose (file); // Create a new config from the blob data zfl_config_t *config = zfl_config_new (zfl_blob_data (blob)); assert (config); // Find first device char *device = zfl_config_device (config, 0); if (!*device) { printf ("E: No device specified, please read http://rfc.zeromq.org/spec:3\n"); exit (EXIT_FAILURE); } // Process device type char *type = zfl_config_device_type (config, device); int device_type; // 0MQ defined type int frontend_type; // Socket types depending int backend_type; // on the device type s_parse_device_type (type, &device_type, &frontend_type, &backend_type); // Create and configure sockets void *frontend = zfl_config_socket (config, device, "frontend", frontend_type); assert (frontend); void *backend = zfl_config_socket (config, device, "backend", backend_type); assert (backend); // Start the device now if (zfl_config_verbose (config)) printf ("I: Starting device...\n"); // Will not actually ever return zmq_device (device_type, frontend, backend); zfl_config_destroy (&config); }
int main () { // Prepare our context and sockets zmq::context_t context (1); zmq::socket_t servers (context, ZMQ_DEALER); servers.bind ("tcp://*:5555"); zmq::socket_t clients (context, ZMQ_ROUTER); clients.bind ("tcp://*:5556"); zmq_device (ZMQ_FORWARDER, (void *)clients, (void *)servers); return 0; }
int main (int argc, char *argv[]) { zmq::context_t context(1); zmq::socket_t frontend ( context, ZMQ_ROUTER ); frontend.bind( "tcp://127.0.0.1:5000"); zmq::socket_t backend ( context, ZMQ_DEALER ); backend.bind( "tcp://127.0.0.1:5001"); zmq_device( ZMQ_QUEUE, frontend, backend ); return 0; }
int main ( int argc, char *argv[] ) { void *context = zmq_init (1); assert(context); // Create frontend and backend sockets void *frontend = zmq_socket (context, ZMQ_XREP); assert (frontend); void *backend = zmq_socket (context, ZMQ_XREQ); assert (backend); // Bind both sockets to TCP ports assert (zmq_bind (frontend, argv[1]) == 0); assert (zmq_bind (backend, argv[2]) == 0); /* assert (zmq_bind (frontend, "tcp://\*:4446") == 0); */ /* assert (zmq_bind (backend, "tcp://\*:13000") == 0); */ zmq_device (ZMQ_QUEUE, frontend, backend); }
int main(int argc, char **argv){ char addr[32]; init_options(); parse_options(argc, argv); if (GlobalArgs.helpflag || GlobalArgs.dbname == NULL){ metadatadb_usage(); return 0; } /* init daemon */ init_process(); /* init zeromq */ void *ctx = zmq_init(1); if (ctx == NULL){ syslog(LOG_CRIT,"MAIN ERROR: unable to init zmq context"); exit(1); } snprintf(addr, 32, "tcp://*:%d", GlobalArgs.port); void *clients_skt = socket_bind(ctx, ZMQ_XREP, addr); if (clients_skt == NULL){ syslog(LOG_CRIT,"MAIN ERROR: unable to bind skt to %s", addr); exit(1); } void *workers_skt = socket_bind(ctx, ZMQ_XREQ, "inproc://pipe"); if (workers_skt == NULL){ syslog(LOG_CRIT,"MAIN ERROR: unable to bind workers skt"); exit(1); } unsigned int i; pthread_t worker_thr; for (i=0;i < GlobalArgs.nbthreads;i++){ assert(pthread_create(&worker_thr, NULL, dowork, ctx) == 0); } assert(zmq_device(ZMQ_QUEUE, clients_skt, workers_skt) == 0); return 0; }
int main (int argc, char *argv[]) { zmq::context_t context(1); // Socket facing clients zmq::socket_t frontend (context, ZMQ_ROUTER); frontend.bind("tcp://*:5559"); // Socket facing services zmq::socket_t backend (context, ZMQ_DEALER); zmq_bind (static_cast<void *>(backend), "tcp://*:5560"); // Start built-in device zmq_device ( ZMQ_QUEUE, static_cast<void *>(frontend), static_cast<void *>(backend) ); return 0; }
int main (void) { void *context = zmq_init (1); // Socket facing clients void *frontend = zmq_socket (context, ZMQ_XREP); zmq_bind (frontend, "tcp://*:5559"); // Socket facing services void *backend = zmq_socket (context, ZMQ_XREQ); zmq_bind (backend, "tcp://*:5560"); // Start built-in device zmq_device (ZMQ_QUEUE, frontend, backend); // We never get here... zmq_close (frontend); zmq_close (backend); zmq_term (context); return 0; }
// Starts a device as configured by a JSON or text config file // static void s_start_configured_device (char *filename) { // Create and configure a zfl_device object zfl_device_t *device = zfl_device_new (filename); if (!device) { printf ("E: '%s' can't be read, or has invalid syntax\n", filename); exit (EXIT_FAILURE); } // Find first device char *main_device = zfl_device_locate (device, 0); if (!*main_device) { printf ("E: No device specified, please read http://rfc.zeromq.org/spec:5\n"); exit (EXIT_FAILURE); } // Process device type char *type = zfl_device_property (device, main_device, "type"); int device_type; // 0MQ defined type int frontend_type; // Socket types depending int backend_type; // on the device type s_parse_device_type (type, &device_type, &frontend_type, &backend_type); // Create and configure sockets void *frontend = zfl_device_socket (device, main_device, "frontend", frontend_type); assert (frontend); void *backend = zfl_device_socket (device, main_device, "backend", backend_type); assert (backend); // Start the device now if (zfl_device_verbose (device)) printf ("I: Starting device...\n"); // Will not actually ever return zmq_device (device_type, frontend, backend); zfl_device_destroy (&device); }
ZMQ_EXPORT int WINAPI mql4zmq_device (int device, void * insocket, void* outsocket) { return zmq_device(device, insocket, outsocket); }
inline void device (int device_, void * insocket_, void* outsocket_) { int rc = zmq_device (device_, insocket_, outsocket_); if (rc != 0) throw error_t (); }
int main(int argc, char** argv) { s_catch_signals(); std::string configPath(CONFIG); if(argc > 1) { configPath = std::string(argv[1]); } std::ifstream file; std::stringstream ss; file.open(configPath.c_str()); if (file.is_open()) { ss << file.rdbuf(); file.close(); } else { exit(1); } json::JSONObject config(ss); leveldb::Options options; options.create_if_missing = true; leveldb::DB::Open(options, config.get("StorageDirectory"), &db); context = zmq_init(1); void *frontend = zmq_socket(context, ZMQ_ROUTER), *backend = zmq_socket(context, ZMQ_DEALER); zmq_bind(frontend, config.get("StorageAdd").c_str()); zmq_bind(backend, "inproc://storageWorkers"); MessageFactory *msgFactory = new MessageFactory(); ActionFactory *actionsFactory = new ActionFactory(db); nExecutors = config.getInt("StorageNWorkers"); executors = new Executor*[nExecutors]; for(size_t i = 0; i < nExecutors; i++) { executors[i] = new Executor(msgFactory, actionsFactory, context); executors[i]->start(); } zmq_device(ZMQ_QUEUE, frontend, backend); for(size_t i = 0; i < nExecutors; i++) { delete executors[i]; } delete actionsFactory; delete msgFactory; delete executors; zmq_close(frontend); zmq_close(backend); zmq_term(context); return 0; }
Z K3(device){TC2(x,-KI,-KJ); IC(x); PC(y); PC(z); R kj(zmq_device(xi, VSK(y), VSK(z)));}
int main(int argc, char **argv) { if (argc < 2) { printf("\n"); printf("rs_test LIO_COMMON_OPTIONS\n"); lio_print_options(stdout); return(1); } int thread_nbr; lio_init(&argc, &argv); thread_nbr = lio_parallel_task_count; //*** Parses the args char *svr_proto, *svr_addr, *svr_port, *zmq_svr; //** Retrieves remote zmq server name, transport protocol, and lisenting port svr_proto = inip_get_string(lio_gc->ifd, "zmq_server", "protocol", RS_ZMQ_DFT_PROTO); svr_addr = inip_get_string(lio_gc->ifd, "zmq_server", "server", NULL); svr_port = inip_get_string(lio_gc->ifd, "zmq_server", "port", RS_ZMQ_DFT_PORT); asprintf(&zmq_svr, "%s://%s:%s", string_trim(svr_proto), string_trim(svr_addr), string_trim(svr_port)); //** Creates zmq context void *context = zmq_ctx_new(); assert(context != NULL); //** Creates zmq router and binds it to tcp://*:5555 //** It talks to rs client void *router = zmq_socket(context, ZMQ_ROUTER); assert(router != NULL); int rc = zmq_bind(router, zmq_svr); assert(rc != -1); printf("ZMQ router socket created.\n"); // Creates and binds DEALER socket to inproc://worker // It talks to workers void *dealer = zmq_socket(context, ZMQ_DEALER); assert(dealer != NULL); rc = zmq_bind(dealer, "inproc://worker"); assert(rc != -1); printf("ZMQ dealer socket created.\n"); //** Blocks the SIGINT, SIGTERM signals sigemptyset(&signal_mask); sigaddset(&signal_mask, SIGINT); sigaddset(&signal_mask, SIGTERM); rc = pthread_sigmask(SIG_BLOCK, &signal_mask, NULL); assert(rc == 0); //** Launches thread pool printf("Launching threads...\n"); int thread_count; rs_zmq_thread_arg_t **arg; type_malloc_clear(arg, rs_zmq_thread_arg_t *, thread_nbr); pthread_t *workers; type_malloc_clear(workers, pthread_t, thread_nbr); for (thread_count = 0; thread_count < thread_nbr; thread_count++) { type_malloc_clear(arg[thread_count], rs_zmq_thread_arg_t, 1); arg[thread_count]->zmq_context = context; arg[thread_count]->rs = lio_gc->rs; arg[thread_count]->da = ds_attr_create(lio_gc->ds); arg[thread_count]->ds = lio_gc->ds; arg[thread_count]->timeout = lio_gc->timeout; pthread_create(&workers[thread_count], NULL, rs_zmq_worker_routine, (void *)arg[thread_count]); } printf("Launched all %d threads.\n", thread_nbr); //** Unblocks the SIGINT, SIGTERM signals rc = pthread_sigmask(SIG_UNBLOCK, &signal_mask, NULL); assert(rc == 0); //** Catches the SIGNIT, SIGTERM signals catch_signals(); //** Uses a QUEUE device to connect router and dealer zmq_device(ZMQ_QUEUE, router, dealer); if (s_interrupted == 1) printf("Interrupt received, killing server...\n"); //** Shutdown zmq should go before cleaning thread resources zmq_close(router); zmq_close(dealer); printf("Destroied ZMQ router and dealer\n"); zmq_ctx_destroy(context); //** This "trigers" the exit of all threads, because it makes all blocking operations on sockets return printf("Destroied ZMQ context\n"); fflush(stdout); //** Waits for all threads to exit for (thread_count = 0; thread_count < thread_nbr; thread_count++) { pthread_join(workers[thread_count], NULL); } //** Destroys allocations for threads for (thread_count = 0; thread_count < thread_nbr; thread_count++) { ds_attr_destroy(lio_gc->ds, arg[thread_count]->da); free(arg[thread_count]); } free(arg); free(workers); free(svr_proto); free(svr_addr); free(svr_port); free(zmq_svr); lio_shutdown(); return(0); }