Example #1
0
void rmq_process(int rank)
{
	/* init blocking reader */
	rmq_init_reader();
	rmq_send_t * rmqs;

	/* waiting for commands */
	for (;;) {
		rmqs = rmq_receive();
		if (!rmqs || !rmqs->sock) {
			LM_ERR("invalid receive sock info received\n");
			goto end;
		}
		/* check if we should disconnect it */
		if (!rmqs->msg[0]) {
			rmq_destroy(rmqs->sock);
			goto end;
		}

		/* check if we should reconnect */
		if (rmq_reconnect(rmqs->sock) < 0) {
			LM_ERR("cannot reconnect socket\n");
			goto end;
		}

		/* send msg */
		if (rmq_sendmsg(rmqs))
			LM_ERR("cannot send message\n");
end:
		if (rmqs)
			shm_free(rmqs);
	}
}
Example #2
0
int main(int argc, char* argv[])
{
  remtrans_item* remtrans;
  unsigned char id[32];
  unsigned char pname[32];
  pwr_tStatus sts;
  int i;
  float time_since_scan = 0.0;

  /* Read arg number 2, should be id for this instance and id is our queue
   * number */

  if (argc >= 2)
    strcpy((char*)id, argv[1]);
  else
    strcpy((char*)id, "0");

  /* Build process name with id */

  sprintf((char*)pname, "rs_remrabbitmq_%s", id);

  /* Init of errh */

  errh_Init((char*)pname, errh_eAnix_remote);
  errh_SetStatus(PWR__SRVSTARTUP);

  /* Init of gdh */
  if (debug)
    printf("Before gdh_init\n");
  sts = gdh_Init((char*)pname);
  if (EVEN(sts)) {
    errh_Fatal("gdh_Init, %m", sts);
    errh_SetStatus(PWR__SRVTERM);
    exit(sts);
  }

  /* Arg number 3 should be my remnodes objid in string representation,
     read it, convert to real objid and store in remnode_item */

  sts = 0;
  if (argc >= 3)
    sts = cdh_StringToObjid(argv[2], &rn.objid);
  if (EVEN(sts)) {
    errh_Fatal("cdh_StringToObjid, %m", sts);
    errh_SetStatus(PWR__SRVTERM);
    exit(sts);
  }

  /* Get pointer to RemnodeRabbitMQ object and store locally */
  sts = gdh_ObjidToPointer(rn.objid, (pwr_tAddress*)&rn_rmq);
  if (EVEN(sts)) {
    errh_Fatal("cdh_ObjidToPointer, %m", sts);
    errh_SetStatus(PWR__SRVTERM);
    exit(sts);
  }

  if (streq(rn_rmq->ReceiveQueue, "")
      && streq(rn_rmq->SendQueue, "")) {
    errh_Fatal(
        "Process terminated, neither send or receive queue configured, %s", id);
    errh_SetStatus(PWR__SRVTERM);
    exit(sts);
  }

  /* Create context */
  ctx = calloc(1, sizeof(*ctx));
  ctx->op = rn_rmq;
  if (!streq(rn_rmq->ReceiveQueue, ""))
    ctx->is_consumer = 1;
  if (!streq(rn_rmq->SendQueue, ""))
    ctx->is_producer = 1;

  /* Initialize some internal data and make standard remtrans init */

  rn.next = NULL;
  rn.local = NULL; // We dont use local structure since we only have one remnode
  rn_rmq->ErrCount = 0;
  if (debug)
    printf("Before remtrans_init\n");

  sts = RemTrans_Init(&rn);

  if (EVEN(sts)) {
    errh_Fatal("RemTrans_Init, %m", sts);
    errh_SetStatus(PWR__SRVTERM);
    exit(sts);
  }

  /* Store remtrans objects objid in remnode_qcom object */
  remtrans = rn.remtrans;
  i = 0;
  while (remtrans) {
    rn_rmq->RemTransObjects[i++] = remtrans->objid;
    if (i >= (int)(sizeof(rn_rmq->RemTransObjects)
                 / sizeof(rn_rmq->RemTransObjects[0])))
      break;
    remtrans = (remtrans_item*)remtrans->next;
  }

  /* Connect to rabbitmq broker */
  sts = rmq_connect();
  if (EVEN(sts)) {
    rmq_close(1);
    errh_Fatal("Process terminated, unable to connect to RabbitMQ, %s", id);
    errh_SetStatus(PWR__SRVTERM);
    exit(sts);
  }

  /* Set running status */

  errh_SetStatus(PWR__SRUN);

  /* Set (re)start time in remnode object */

  time_GetTime(&rn_rmq->RestartTime);

  /* Loop forever */

  while (!doomsday) {
    if (rn_rmq->Disable == 1) {
      errh_Fatal("Disabled, exiting");
      errh_SetStatus(PWR__SRVTERM);
      exit(0);
    }
    aproc_TimeStamp(TIME_INCR, 5);

    if (ctx->is_consumer)
      sts = rmq_receive();
    else
      RemoteSleep(TIME_INCR);

    time_since_scan += TIME_INCR;
    if (time_since_scan >= rn_rmq->ScanTime) {
      if (ctx->is_producer)
        sts = RemTrans_Cyclic(&rn, &rmq_send);
      time_since_scan = 0.0;
    }
  }
}