static zsock_t* subscriber_pull_socket_new(zconfig_t* config) { zsock_t *socket = zsock_new(ZMQ_PULL); assert(socket); zsock_set_linger(socket, 0); zsock_set_reconnect_ivl(socket, 100); // 100 ms zsock_set_reconnect_ivl_max(socket, 10 * 1000); // 10 s char *pull_spec = zconfig_resolve(config, "frontend/endpoints/subscriber/pull", "tcp://*"); char *full_spec = augment_zmq_connection_spec(pull_spec, pull_port); if (!quiet) printf("[I] subscriber: binding PULL socket to %s\n", full_spec); int rc = zsock_bind(socket, "%s", full_spec); assert(rc != -1); free(full_spec); const char *inproc_binding = "inproc://subscriber-pull"; if (!quiet) printf("[I] subscriber: binding PULL socket to %s\n", inproc_binding); rc = zsock_bind(socket, "%s", inproc_binding); assert(rc != -1); return socket; }
static subscriber_state_t* subscriber_state_new(zsock_t *pipe, zconfig_t* config, zlist_t *devices) { // figure out devices specs if (devices == NULL) devices = zlist_new(); if (zlist_size(devices) == 0) { zlist_destroy(&devices); devices = extract_devices_from_config(config); } if (zlist_size(devices) == 0) zlist_append(devices, augment_zmq_connection_spec("localhost", sub_port)); //create the state subscriber_state_t *state = zmalloc(sizeof(*state)); state->pipe = pipe; state->devices = devices; state->sub_socket = subscriber_sub_socket_new(config, state->devices); state->tracker = device_tracker_new(devices, state->sub_socket); state->pull_socket = subscriber_pull_socket_new(config); state->router_socket = subscriber_router_socket_new(config); state->push_socket = subscriber_push_socket_new(config); if (PUBLISH_DUPLICATES) state->pub_socket = subscriber_pub_socket_new(config); return state; }
static zsock_t* subscriber_sub_socket_new(subscriber_state_t *state) { zsock_t *socket = zsock_new(ZMQ_SUB); assert(socket); zsock_set_rcvhwm(socket, state->rcv_hwm); // set subscription if (!state->subscriptions || zlist_size(state->subscriptions) == 0) { if (!state->subscriptions) state->subscriptions = zlist_new(); zlist_append(state->subscriptions, zconfig_resolve(state->config, "/logjam/subscription", "")); } char *subscription = zlist_first(state->subscriptions); bool subscribed_to_all = false; while (subscription) { printf("[I] subscriber: subscribing to '%s'\n", subscription); if (streq(subscription, "")) subscribed_to_all = true; zsock_set_subscribe(socket, subscription); subscription = zlist_next(state->subscriptions); } if (!subscribed_to_all) zsock_set_subscribe(socket, "heartbeat"); if (!state->devices || zlist_size(state->devices) == 0) { // convert config file to list of devices if (!state->devices) state->devices = zlist_new(); zconfig_t *endpoints = zconfig_locate(state->config, "/logjam/endpoints"); if (!endpoints) { zlist_append(state->devices, "tcp://localhost:9606"); } else { zconfig_t *endpoint = zconfig_child(endpoints); while (endpoint) { char *spec = zconfig_value(endpoint); char *new_spec = augment_zmq_connection_spec(spec, 9606); zlist_append(state->devices, new_spec); endpoint = zconfig_next(endpoint); } } } char* device = zlist_first(state->devices); while (device) { printf("[I] subscriber: connecting SUB socket to logjam-device via %s\n", device); int rc = zsock_connect(socket, "%s", device); log_zmq_error(rc, __FILE__, __LINE__); assert(rc == 0); device = zlist_next(state->devices); } return socket; }
static zsock_t* subscriber_router_socket_new(zconfig_t* config) { zsock_t *socket = zsock_new(ZMQ_ROUTER); assert(socket); zsock_set_linger(socket, 0); zsock_set_reconnect_ivl(socket, 100); // 100 ms zsock_set_reconnect_ivl_max(socket, 10 * 1000); // 10 s char *router_spec = zconfig_resolve(config, "frontend/endpoints/subscriber/router", "tcp://*"); char *full_spec = augment_zmq_connection_spec(router_spec, router_port); if (!quiet) printf("[I] subscriber: binding ROUTER socket to %s\n", full_spec); int rc = zsock_bind(socket, "%s", full_spec); assert(rc != -1); free(full_spec); return socket; }
static void process_arguments(int argc, char * const *argv) { char c; char *v; int longindex = 0; opterr = 0; static struct option long_options[] = { { "compress", no_argument, 0, 'z' }, { "config", required_argument, 0, 'c' }, { "dryrun", no_argument, 0, 'n' }, { "help", no_argument, 0, 0 }, { "hosts", required_argument, 0, 'h' }, { "interface", required_argument, 0, 'i' }, { "parsers", required_argument, 0, 'p' }, { "quiet", no_argument, 0, 'q' }, { "rcv-hwm", required_argument, 0, 'R' }, { "snd-hwm", required_argument, 0, 'S' }, { "subscribe", required_argument, 0, 'e' }, { "verbose", no_argument, 0, 'v' }, { 0, 0, 0, 0 } }; while ((c = getopt_long(argc, argv, "vqc:np:zh:S:R:e", long_options, &longindex)) != -1) { switch (c) { case 'v': if (verbose) debug= true; else verbose = true; break; case 'q': quiet = true; break; case 'c': config_file_name = optarg; break; case 'n': dryrun = true; break; case 'z': compress_gelf = true; break; case 'p': { unsigned int n = strtoul(optarg, NULL, 0); if (n <= MAX_PARSERS) num_parsers = n; else { fprintf(stderr, "parameter value 'p' can not be greater than %d\n", MAX_PARSERS); exit(1); } break; } case 'h': hosts = split_delimited_string(optarg); if (hosts == NULL || zlist_size(hosts) == 0) { printf("[E] must specify at least one device to connect to\n"); exit(1); } break; case 'i': interface = optarg; break; case 'e': subscriptions = split_delimited_string(optarg); break; case 'R': rcv_hwm = atoi(optarg); break; case 'S': snd_hwm = atoi(optarg); break; case 0: print_usage(argv); exit(0); break; case '?': if (optopt == 'c' || optopt == 'p') fprintf(stderr, "option -%c requires an argument.\n", optopt); else if (isprint (optopt)) fprintf(stderr, "unknown option `-%c'.\n", optopt); else fprintf(stderr, "unknown option character `\\x%x'.\n", optopt); print_usage(argv); exit(1); default: fprintf(stderr, "BUG: can't process option -%c\n", optopt); exit(1); } } if (hosts == NULL) hosts = split_delimited_string(getenv("LOGJAM_DEVICES")); if (hosts) augment_zmq_connection_specs(&hosts, 9606); if (interface == NULL) interface = getenv("LOGJAM_INTERACE"); if (interface) interface = augment_zmq_connection_spec(interface, 9610); if (subscriptions == NULL) subscriptions = split_delimited_string(getenv("LOGJAM_SUBSCRIPTIONS")); if (rcv_hwm == -1 && (v = getenv("LOGJAM_RCV_HWM"))) rcv_hwm = atoi(v); if (snd_hwm == -1 && (v = getenv("LOGJAM_SND_HWM"))) snd_hwm = atoi(v); }