int main (int argc, char *argv []) { int verbose = (argc > 1 && streq (argv [1], "-v")); mdcli_t *session = mdcli_new ("tcp://localhost:5555", verbose); zmsg_t *request; zmsg_t *reply; int count; //for (count = 0; count < 1; count++) { request = zmsg_new (); zmsg_pushstr (request, "KILL"); reply = mdcli_send (session, "echo", &request); if (reply) { zframe_t * lastFrame = zmsg_last(reply); char* msg = zframe_strdup(lastFrame); printf("msg:%s\n", msg); printf("size:%d\n", zmsg_content_size(reply)); //zframe_print(lastFrame,"->"); zmsg_destroy (&reply); } //else // break; // Interrupt or failure //} //printf ("%d requests/replies processed\n", count); mdcli_destroy (&session); return 0; }
int main (void) { mdcli_t *mdcli; int rc; printf (" * mdcli: "); mdcli = mdcli_new ("tcp://127.0.0.1:5055"); mdcli_destroy (&mdcli); return 0; return 0; }
int main (int argc, char *argv []) { int verbose = (argc > 1 && streq (argv [1], "-v")); mdcli_t *session = mdcli_new ("tcp://localhost:5555", verbose); // 1. Send 'echo' request to Titanic zmsg_t *request = zmsg_new (); zmsg_addstr (request, "echo"); zmsg_addstr (request, "Hello world"); zmsg_t *reply = s_service_call ( session, "titanic.request", &request); zframe_t *uuid = NULL; if (reply) { uuid = zmsg_pop (reply); zmsg_destroy (&reply); zframe_print (uuid, "I: request UUID "); } // 2. Wait until we get a reply while (!zctx_interrupted) { zclock_sleep (100); request = zmsg_new (); zmsg_add (request, zframe_dup (uuid)); zmsg_t *reply = s_service_call ( session, "titanic.reply", &request); if (reply) { char *reply_string = zframe_strdup (zmsg_last (reply)); printf ("Reply: %s\n", reply_string); free (reply_string); zmsg_destroy (&reply); // 3. Close request request = zmsg_new (); zmsg_add (request, zframe_dup (uuid)); reply = s_service_call (session, "titanic.close", &request); zmsg_destroy (&reply); break; } else { printf ("I: no reply yet, trying again...\n"); zclock_sleep (5000); // Try again in 5 seconds } } zframe_destroy (&uuid); mdcli_destroy (&session); return 0; }
int main (int argc, char *argv []) { int verbose = (argc > 1 && streq (argv [1], "-v")); mdcli_t *session = mdcli_new ("tcp://localhost:5555", verbose); // 1. Send 'echo' request to Titanic zmsg_t *request = zmsg_new ("Hello world"); zmsg_push (request, "echo"); zmsg_t *reply = s_service_call ( session, "titanic.request", &request); char *uuid = NULL; if (reply) { uuid = zmsg_pop (reply); zmsg_destroy (&reply); printf ("I: request UUID: %s\n", uuid); } // 2. Wait until we get a reply while (!s_interrupted) { s_sleep (100); request = zmsg_new (uuid); zmsg_t *reply = s_service_call ( session, "titanic.reply", &request); if (reply) { printf ("Reply: %s\n", zmsg_body (reply)); zmsg_destroy (&reply); // 3. Close request request = zmsg_new (uuid); reply = s_service_call (session, "titanic.close", &request); zmsg_destroy (&reply); break; } else { printf ("I: no reply yet, trying again...\n"); s_sleep (5000); // Try again in 5 seconds } } mdcli_destroy (&session); return 0; }
void trigger(void *cvoid, zctx_t * context, void * control) { triggerconfig_t * c = (triggerconfig_t*) cvoid; //set up msgpack stuff zclock_log("watch_port started!"); msgpack_zone mempool; msgpack_zone_init(&mempool, 2048); // TODO char * user_id = "17"; // TODO get broker in somehow char * broker = "tcp://au.ninjablocks.com:5773"; mdcli_t * client = mdcli_new(broker, 1); //VERBOSE triggermemory_t trigger_memory; msgpack_object * addins_obj = parse_msgpack(&mempool, c->addins); if(!parse_addins(addins_obj, &trigger_memory)) { //bad message zclock_log("bad trigger definition"); msgpack_object_print(stdout, *addins_obj); send_sync("bad trigger", control); return; } zclock_log("Creating trigger: target %s, rule_id %s, name %s", c->target_worker, c->rule_id, c->trigger_name); dump_trigger(&trigger_memory); triggerfunction trigger_func; if(!(trigger_func = find_trigger(c->channel, c->trigger_name))) { zclock_log("no trigger found for channel %s, trigger %s", c->channel, c->trigger_name); send_sync("no such trigger", control); return; } void * line = zsocket_new(context, ZMQ_SUB); // what line are we on? // this comes in the addins. char * linesocket = to_linesocket(trigger_memory.line_id); zclock_log("trigger is connecting to listen on %s", linesocket); zsocket_connect(line, linesocket); zsockopt_set_unsubscribe(line, ""); zsockopt_set_subscribe(line, "VALUE"); recv_sync("ping", control); send_sync("pong", control); zmq_pollitem_t items [] = { { line, 0, ZMQ_POLLIN, 0 }, { control, 0, ZMQ_POLLIN, 0 } }; while(1) { // listen on control and line zmq_poll (items, 2, -1); if (items[1].revents & ZMQ_POLLIN) { zclock_log("rule %s received message on control pipe", c->rule_id); // control message // really only expecting DESTROY zmsg_t * msg = zmsg_recv(control); char * str = zmsg_popstr(msg); zmsg_destroy(&msg); if (strcmp("Destroy", str) == 0) { zclock_log("rule %s will quit on request", c->rule_id); free(str); send_sync("ok", control); zclock_log("rule %s quitting on request", c->rule_id); break; } else { zclock_log("unexpected command %s for rule %s", str, c->rule_id); free(str); send_sync("ok", control); } } if (items[0].revents & ZMQ_POLLIN) { // serial update zmsg_t * msg = zmsg_recv(line); zframe_t * cmd = zmsg_pop(msg); if(zframe_streq(cmd, "CHANNEL_CHANGE")) { // TODO // must have been dormant to have gotten this char * new_channel = zmsg_popstr(msg); if(strcmp(c->channel, new_channel) == 0) { // oh, happy day! We're relevant again. // reactivate and start looking at reset levels. zclock_log("line %d: changed channel from %s to %s: trigger coming back to life", trigger_memory.line_id, c->channel, new_channel); zsockopt_set_subscribe(line, "VALUE"); zsockopt_set_unsubscribe(line, "CHANNEL_CHANGE"); } free(new_channel); } else if (zframe_streq(cmd, "VALUE")) { zframe_t * vframe = zmsg_pop(msg); int value; memcpy(&value, zframe_data(vframe), sizeof(int)); char * update_channel = zmsg_popstr(msg); if(strcmp(c->channel, update_channel) != 0) { // channel changed, go dormant // this is legit according to my tests at // https://gist.github.com/2042350 zclock_log("line %d: changed channel from %s to %s: trigger going dormant", trigger_memory.line_id, c->channel, update_channel); zsockopt_set_subscribe(line, "CHANNEL_CHANGE"); zsockopt_set_unsubscribe(line, "VALUE"); } else if(trigger_func(&trigger_memory, value)) { send_trigger(client, c->target_worker, c->rule_id, value, user_id); } free(update_channel); } else { // shouldn't ever happen. zclock_log("shouldn't have received command %s\n", zframe_strdup(cmd)); } zmsg_destroy(&msg); zframe_destroy(&cmd); } } msgpack_zone_destroy(&mempool); }