SHOW:
|
|
- or go back to the newest paste.
| 1 | /* Following is the workthread routine */ | |
| 2 | static void * | |
| 3 | - | subscriber_thread(void *priv) |
| 3 | + | worker_thread_routine(void *priv) |
| 4 | {
| |
| 5 | void *context = zmq_init(2); | |
| 6 | - | THR_SetName("backends-subscriber-thread");
|
| 6 | + | |
| 7 | zmq_bind(command_socket, "tcp://127.0.0.1:10000"); | |
| 8 | ||
| 9 | void *subscriber = NULL; | |
| 10 | char *zmq_endpoint = strdup("tcp://initial-server-ip:5555");
| |
| 11 | int linger = 0; | |
| 12 | reconnect: | |
| 13 | subscriber = zmq_socket(context,ZMQ_SUB); | |
| 14 | int rc = zmq_connect(subscriber,zeromq_endpoint); | |
| 15 | if(rc) | |
| 16 | LOG_ERROR("zmq_connect error [%d]",rc);
| |
| 17 | const char *filter = ""; | |
| 18 | zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, filter, strlen(filter)); | |
| 19 | zmq_setsockopt(subscriber, ZMQ_LINGER, &linger, sizeof (int)); | |
| 20 | ||
| 21 | while(1){
| |
| 22 | ||
| 23 | zmq_pollitem_t items[2]; | |
| 24 | items[0].socket = command_socket; | |
| 25 | items[0].fd = 0; | |
| 26 | items[0].events = ZMQ_POLLIN; | |
| 27 | ||
| 28 | items[1].socket = subscriber; | |
| 29 | items[1].fd = 0; | |
| 30 | items[1].events = ZMQ_POLLIN; | |
| 31 | ||
| 32 | int result = zmq_poll(items, 2, -1); | |
| 33 | ||
| 34 | if(result <= 0){
| |
| 35 | sleep(10); | |
| 36 | continue; | |
| 37 | } | |
| 38 | ||
| 39 | if(items[0].revents > 0){
| |
| 40 | const char *zmq_endpoint_new = zstr_recv(command_socket); | |
| 41 | if(zmq_endpoint_new){
| |
| 42 | LOG_INFO("going to switch to [%s]",zmq_endpoint_new);
| |
| 43 | if(zmq_endpoint_new){
| |
| 44 | //zmq_disconnect is expected here? | |
| 45 | if(subscriber){
| |
| 46 | zmq_close(subscriber); | |
| 47 | } | |
| 48 | free(zmq_endpoint); | |
| 49 | zmq_endpoint = zmq_endpoint_new; | |
| 50 | goto reconnect; | |
| 51 | } | |
| 52 | ||
| 53 | } | |
| 54 | } | |
| 55 | ||
| 56 | if(items[1].revents > 0){
| |
| 57 | //business logic | |
| 58 | } | |
| 59 | } | |
| 60 | ||
| 61 | zmq_close(subscriber); | |
| 62 | zmq_close(command_socket); | |
| 63 | zmq_term(context); | |
| 64 | free(zmq_endpoint); | |
| 65 | NEEDLESS_RETURN(NULL); | |
| 66 | } |