예제 #1
0
파일: mdworker.c 프로젝트: ice200117/PTPS
int main (int argc, char *argv [])
{
	zmsg_t *reply = 0;
	mdwrk_t *session = 0;
	zframe_t * lastFrame = 0;
	char* lastMsg = 0;

    int verbose = (argc > 1 && streq (argv [1], "-v"));
	//if (argc = 2 && !streq (argv [1], "-v"))
	//{
	//	session = mdwrk_new ("tcp://localhost:5555", argv[1], verbose);
	//}
	//else if (argc = 3 && !streq (argv [2], ""))
	//{
	//	session = mdwrk_new ("tcp://localhost:5555", argv[2], verbose);
	//}
	//else
	//{
		session = mdwrk_new ("tcp://localhost:5555", "echo", verbose);
	//}
	if (!session) return 1;
	
    while (true) {
        zmsg_t *request = mdwrk_recv (session, &reply);
        if (request == NULL) break;              //  Worker was interrupted

		////////////////////////////////////加入自己的雷达逻辑//////////////////////////////////

		//解析收到的信息
		lastFrame = zmsg_last(request);
		lastMsg = zframe_strdup(lastFrame);

		printf("zmsg_t::msg:%s\n", lastMsg);
		printf("zmsg_t::size:%d\n", zmsg_content_size(request));

		//
		//
		//
		//

		//建立回复信息
		reply = zmsg_new ();
		zmsg_pushstr (reply, "Done!");

		////////////////////////////////////////////////////////////////////////////////////////

		zmsg_destroy (&request);
        //reply = request;        //  Echo is complex... :-)
    }
    mdwrk_destroy (&session);
    return 0;
}
예제 #2
0
파일: mdworker.c 프로젝트: tzuryby/zguide
int main (int argc, char *argv [])
{
    int verbose = (argc > 1 && strcmp (argv [1], "-v") == 0);
    mdwrk_t *session = mdwrk_new ("tcp://localhost:5555", "echo", verbose);

    zmsg_t *reply = NULL;
    while (1) {
        zmsg_t *request = mdwrk_recv (session, &reply);
        if (request == NULL)
            break;              //  Worker was interrupted
        reply = request;        //  Echo is complex... :-)
    }
    mdwrk_destroy (&session);
    return 0;
}
예제 #3
0
파일: worker.c 프로젝트: saidimu/ninjaduino
void generic_worker(void * cvoid, zctx_t * context, void * pipe) {
  workerconfig_t *config = (workerconfig_t*) cvoid;

  rulepackage_t rpkg;
  child_handshake(pipe);


  rpkg.channel = config->channel;
  rpkg.actions = zhash_new();
  rpkg.triggers = zhash_new();
  rpkg.context = context;
  rpkg.base_config = config->base_config;

  // FIX this needs to be initialised differently for each channel
  // normal channels will always have the "value" service,
  // but (for instance) the Camera channel has the "photo" service

  // zhash_t * services = zhash_new();
  
  char * ninja = config->base_config->uuid;
  rpkg.servicename = malloc(strlen(ninja) +
                            strlen(rpkg.channel) + 4);
  sprintf(rpkg.servicename, "n:%s:%s", ninja,rpkg.channel);

  channeldb_t *db = db_init(rpkg.servicename);

  zclock_log("%s worker preparing rules...", rpkg.servicename);

  zclock_log("%s worker reloading rules from db...", rpkg.servicename);
  db_reload_rules(db, &rpkg);

  //  reload_rules_db(context, db, rpkg.servicename, channel, rules, config->base_config);
  zclock_log("%s worker checking rules with server...", rpkg.servicename);
  char * reup = config->base_config->reup_endpoint;
  if(!reup) {
    zclock_log("E: NO REUP CONNECTION DEFINED! Probably dealing with an old server.");
  } else {
    void * reup_sock = zsocket_new(rpkg.context, ZMQ_DEALER);

    zclock_log("connecting to %s", reup);
    zsocket_connect(reup_sock, reup);
    zlist_t * rules = fetch_rules(reup_sock, rpkg.servicename);
    zmsg_t *tmp;
    while(tmp=zlist_pop(rules)) {
      // handle_command(tmp, NULL, &rpkg, db); // FIXME what do we do
                                               // with the reply?
    }
  }

  db_display_rules(db) ;


  zclock_log("%s worker connecting...", rpkg.servicename);
  mdwrk_t *session = mdwrk_new (config->base_config->broker_endpoint, rpkg.servicename, 0);
  zclock_log("%s worker connected!", rpkg.servicename);
  zmsg_t *reply = NULL;
  while (1) {
    zclock_log("%s worker waiting for work.", rpkg.servicename);
    zmsg_t *request = mdwrk_recv (session, &reply);

    if (request == NULL)
      break;              //  Worker was interrupted
    reply = zmsg_new();
    handle_command(request, reply,  &rpkg, db);
  }
  db_destroy(db);
  mdwrk_destroy (&session);
  return;
}
예제 #4
0
void generic_worker(void * cvoid, zctx_t * context, void * pipe) {
  workerconfig_t *config = (workerconfig_t*) cvoid;
  zhash_t * rules = zhash_new();

  child_handshake(pipe);
  zmsg_t *reply = NULL;
  void * rule_pipe = NULL;
  char * ninja = config->base_config->identity;
  char * channel = config->channel;

  char * servicename = malloc(strlen(ninja) +
                              strlen(channel) + 2);
  sprintf(servicename, "%s:%s", ninja,channel);

  // sqlite stuff
  int rc;
  char * tail = NULL;
  sqlite3 *db = init_db(servicename);
  sqlite3_stmt * insert_stmt;
  zclock_log("%s worker preparing rules...", servicename);
  sqlite3_prepare_v2(db, "insert into rules VALUES (@RULEID, @TRIGGER_NAME, @TARGET_WORKER, @AUTH, @ADDINS);", 512, &insert_stmt, NULL);
  zclock_log("%s worker reloading rules...", servicename);
  reload_rules(context, db, servicename, channel, rules);
  zclock_log("%s worker connecting...", servicename);
  mdwrk_t *session = mdwrk_new (config->base_config->broker_endpoint, servicename, 0);
  zclock_log("%s worker connected!", servicename);

  while (1) {
    zclock_log("%s worker waiting for work.", servicename);
    zmsg_t *request = mdwrk_recv (session, &reply);

    if (request == NULL)
      break;              //  Worker was interrupted

    char * command = zmsg_popstr(request);
    char * rule_id = zmsg_popstr(request);

    zclock_log("%s worker servicing request %s for rule %s", servicename,command,rule_id);
    reply = zmsg_new();
    if (strcmp(command, "AddTrigger") == 0) {
      zclock_log("new trigger!");
      if (zhash_lookup(rules, rule_id)) {
        // already have a rule with that id! WTF??
        // FIXME should probably delete this and reinstate
        zclock_log("Received duplicate rule %s, ignoring", rule_id);
        zmsg_destroy(&request);

        zmsg_pushstr(reply, "duplicate");
      } else {
        triggerconfig_t * tconf = malloc(sizeof(triggerconfig_t));
        create_triggerconfig(tconf, request, channel, rule_id);
        char * created = create_trigger(rules, rule_id, context, tconf);
        if(NULL == created) {
          // happy path, so add to db
          sqlite3_bind_text(insert_stmt, 1, tconf->rule_id, -1, SQLITE_TRANSIENT);
          sqlite3_bind_text(insert_stmt, 2, tconf->trigger_name, -1, SQLITE_TRANSIENT);
          sqlite3_bind_text(insert_stmt, 3, tconf->target_worker, -1, SQLITE_TRANSIENT);
          sqlite3_bind_text(insert_stmt, 4, tconf->auth, -1, SQLITE_TRANSIENT);
          sqlite3_bind_text(insert_stmt, 5, tconf->addins, -1, SQLITE_TRANSIENT);
          sqlite3_step(insert_stmt);
          sqlite3_clear_bindings(insert_stmt);
          sqlite3_reset(insert_stmt);

          zmsg_pushstr(reply, "ok");
        } else {
          zclock_log("create_trigger failed: %s", created);
          zmsg_pushstr(reply, created);
        }
        free(created);

      }
    } else if (strcmp(command,"RemoveRule") == 0) {

      if (rule_pipe=zhash_lookup(rules, rule_id)) {
        // found it
        zclock_log("rule %s exists, removing.", rule_id);
        send_sync("Destroy",rule_pipe);
        zclock_log("rule %s waiting for OK from pipe", rule_id);
        recv_sync("ok", rule_pipe);
        zsocket_destroy(context, rule_pipe);
        zhash_delete(rules, rule_id);
        zmsg_pushstr(reply, "ok");
        zclock_log("rule %s completely destroyed", rule_id);
      } else {
        // not there!
        zclock_log("Received delete trigger request for nonexistent rule %s, ignoring", rule_id);
        zmsg_pushstr(reply, "rule not found");
      }
    } else if (strcmp(command, "AddMonitor")==0) {
      // unconditionally fork a monitor for each line
      // they'll die when they get a channel change
      int i;
      for(i=1; i<4; i++) { 
        monitorconfig_t * mconf = malloc(sizeof(monitorconfig_t));
        mconf->line_id = i;
        mconf->source_worker = servicename;
        mconf->out_socket = config->base_config->portwatcher_endpoint;
        mconf->channel = channel;
        void * monitor_pipe = zthread_fork(context, watch_port, (void*)mconf);
        send_sync("ping", monitor_pipe);
        recv_sync("pong", monitor_pipe);
        zsocket_destroy(context, monitor_pipe);
      }
      zmsg_pushstr(reply, "ok");
    } else {
      zclock_log("Can't handle command %s: ignoring", command);
    }
    zmsg_destroy(&request);
  }
  mdwrk_destroy (&session);
  return;
}