Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Following is the workthread routine */
- static void *
- worker_thread_routine(void *priv)
- {
- void *context = zmq_init(2);
- void *command_socket = zmq_socket(context, ZMQ_REP);
- zmq_bind(command_socket, "tcp://127.0.0.1:10000");
- void *subscriber = NULL;
- char *zmq_endpoint = strdup("tcp://initial-server-ip:5555");
- int linger = 0;
- reconnect:
- subscriber = zmq_socket(context,ZMQ_SUB);
- int rc = zmq_connect(subscriber,zeromq_endpoint);
- if(rc)
- LOG_ERROR("zmq_connect error [%d]",rc);
- const char *filter = "";
- zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, filter, strlen(filter));
- zmq_setsockopt(subscriber, ZMQ_LINGER, &linger, sizeof (int));
- while(1){
- zmq_pollitem_t items[2];
- items[0].socket = command_socket;
- items[0].fd = 0;
- items[0].events = ZMQ_POLLIN;
- items[1].socket = subscriber;
- items[1].fd = 0;
- items[1].events = ZMQ_POLLIN;
- int result = zmq_poll(items, 2, -1);
- if(result <= 0){
- sleep(10);
- continue;
- }
- if(items[0].revents > 0){
- const char *zmq_endpoint_new = zstr_recv(command_socket);
- if(zmq_endpoint_new){
- LOG_INFO("going to switch to [%s]",zmq_endpoint_new);
- if(zmq_endpoint_new){
- //zmq_disconnect is expected here?
- if(subscriber){
- zmq_close(subscriber);
- }
- free(zmq_endpoint);
- zmq_endpoint = zmq_endpoint_new;
- goto reconnect;
- }
- }
- }
- if(items[1].revents > 0){
- //business logic
- }
- }
- zmq_close(subscriber);
- zmq_close(command_socket);
- zmq_term(context);
- free(zmq_endpoint);
- NEEDLESS_RETURN(NULL);
- }
Advertisement
Add Comment
Please, Sign In to add comment