static ssize_t _thsafe_zmq_client_read_generic (smio_t *self, loff_t offs, uint8_t *data,
        uint32_t size)
{
    assert (self);
    ssize_t ret_size = -1;
    zmsg_t *send_msg = zmsg_new ();
    ASSERT_ALLOC(send_msg, err_msg_alloc);
    uint32_t opcode;

    DBE_DEBUG (DBG_MSG | DBG_LVL_TRACE, "[smio_thsafe_client:zmq] Calling _thsafe_read_generic\n");

    switch (size) {
        case THSAFE_READ_16_DSIZE:
            opcode = THSAFE_READ_16;
        break;

        case THSAFE_READ_32_DSIZE:
            opcode = THSAFE_READ_32;
        break;

        case THSAFE_READ_64_DSIZE:
            opcode = THSAFE_READ_64;
        break;

        default:
            opcode = THSAFE_READ_32;
    }

    /* Message is:
     * frame 0: READ<size> opcode
     * frame 1: offset */
    int zerr = zmsg_addmem (send_msg, &opcode, sizeof (opcode));
    ASSERT_TEST(zerr == 0, "Could not add READ opcode in message",
            err_add_opcode);
    zerr = zmsg_addmem (send_msg, &offs, sizeof (offs));
    ASSERT_TEST(zerr == 0, "Could not add offset in message",
            err_add_offset);

    DBE_DEBUG (DBG_MSG | DBG_LVL_TRACE, "[smio_thsafe_client:zmq] Sending message:\n");
#ifdef LOCAL_MSG_DBG
    zmsg_print (send_msg);
#endif

    zerr = zmsg_send (&send_msg, self->pipe);
    ASSERT_TEST(zerr == 0, "Could not send message", err_send_msg);

    /* Message is:
     * frame 0: reply code
     * frame 1: return code
     * frame 2: data */
    ret_size = _thsafe_zmq_client_recv_read (self, data, size);

err_send_msg:
err_add_offset:
err_add_opcode:
    zmsg_destroy (&send_msg);
err_msg_alloc:
    return ret_size;
}
Exemple #2
0
/**
 * Send a message, but leave the reply on the socket
 * @param uuid
 *   A unique identifier for this send
 * @param command
 *   The string that will be sent
 * @return 
 *   If the send was successful
 */
bool BoomStick::SendAsync(const std::string& uuid, const std::string& command) {
   if (0 == mUtilizedThread) {
      mUtilizedThread = pthread_self();
   } else {
      CHECK(pthread_self() == mUtilizedThread);
   }
   if (nullptr == mCtx || nullptr == mChamber) {
      return false;
   }
   bool success = true;
   if (FindPendingUuid(uuid)) {
      return true;
   }
   zmsg_t* msg = zmsg_new();
   if (zmsg_addmem(msg, uuid.c_str(), uuid.size()) < 0) {
      success = false;
      LOG(WARNING) << "queue error " << zmq_strerror(zmq_errno());
   } else if (zmsg_addmem(msg, command.c_str(), command.size()) < 0) {
      success = false;
      LOG(WARNING) << "queue error " << zmq_strerror(zmq_errno());
   } else {
      zmq_pollitem_t items[1];
      items[0].socket = mChamber;
      items[0].events = ZMQ_POLLOUT;
      int rc = zmq_poll(items, 1, 0);
      if (0 == rc) {
         zmq_poll(items,1,100);
      }
      if (rc < 0) {
         success = false;
         LOG(WARNING) << "Queue error, cannot poll for status";

      } else if (1 == rc) {
         if ((items[0].revents & ZMQ_POLLOUT) != ZMQ_POLLOUT) {
            LOG(WARNING) << "Queue error, cannot send messages the queue is full";
            std::this_thread::sleep_for(std::chrono::milliseconds(100));
            success = false;
         } else if (zmsg_send(&msg, mChamber) == 0) {
            success = true;
            mPendingReplies[uuid] = std::time(NULL);
         } else {
            LOG(WARNING) << "queue error " << zmq_strerror(zmq_errno());
            success = false;
         }
      } else {
         LOG(WARNING) << "Queue error, timeout waiting for queue to be ready";
         success = false;
      }
   }
   if (msg) {

      zmsg_destroy(&msg);
   }

   return success;
}
Exemple #3
0
static void zmqwriter (flux_reactor_t *r, flux_watcher_t *w,
                       int revents, void *arg)
{
    void *sock = flux_zmq_watcher_get_zsock (w);
    static int count = 0;
    if (revents & FLUX_POLLERR) {
        fprintf (stderr, "%s: FLUX_POLLERR is set\n", __FUNCTION__);
        goto error;
    }
    if (revents & FLUX_POLLOUT) {
        uint8_t blob[64];
        zmsg_t *zmsg = zmsg_new ();
        if (!zmsg || zmsg_addmem (zmsg, blob, sizeof (blob)) < 0) {
            fprintf (stderr, "%s: failed to create message: %s\n",
                     __FUNCTION__, strerror (errno));
            goto error;
        }
        if (zmsg_send (&zmsg, sock) < 0) {
            fprintf (stderr, "%s: zmsg_send: %s\n",
                     __FUNCTION__, strerror (errno));
            goto error;
        }
        count++;
        if (count == zmqwriter_msgcount)
            flux_watcher_stop (w);
    }
    return;
error:
    flux_reactor_stop_error (r);
}
Exemple #4
0
zmsg_t *create_data_message(const char *data, uint32_t data_size)
{
    zmsg_t *msg = create_base_message(MSGTYPE_DATA);
    zmsg_addmem(msg, data, data_size);

    return msg;
}
Exemple #5
0
static int
s_zap_request_reply (zap_request_t *self, char *status_code, char *status_text, unsigned char *metadata, size_t metasize)
{
    if (self->verbose)
        zsys_info ("zauth: - ZAP reply status_code=%s status_text=%s",
                   status_code, status_text);

    zmsg_t *msg = zmsg_new ();
    int rc = zmsg_addstr(msg, "1.0");
    assert (rc == 0);
    rc = zmsg_addstr(msg, self->sequence);
    assert (rc == 0);
    rc = zmsg_addstr(msg, status_code);
    assert (rc == 0);
    rc = zmsg_addstr(msg, status_text);
    assert (rc == 0);
    rc = zmsg_addstr(msg, "");
    assert (rc == 0);
    rc = zmsg_addmem(msg, metadata, metasize);
    assert (rc == 0);
    rc = zmsg_send(&msg, self->handler);
    assert (rc == 0);

    return 0;
}
Exemple #6
0
zmsg_t *create_base_message(int16_t msgtype)
{
    zmsg_t *msg = zmsg_new();
    zmsg_addmem(msg, &msgtype, sizeof(int16_t));

    return msg;
}
/**** Read data block from device function pointer, size in bytes ****/
ssize_t thsafe_zmq_client_read_block (smio_t *self, loff_t offs, size_t size, uint32_t *data)
{
    assert (self);
    ssize_t ret_size = -1;
    zmsg_t *send_msg = zmsg_new ();
    ASSERT_ALLOC(send_msg, err_msg_alloc);
    uint32_t opcode = THSAFE_READ_BLOCK;

    DBE_DEBUG (DBG_MSG | DBG_LVL_TRACE, "[smio_thsafe_client:zmq] Calling thsafe_read_block\n");

    /* Message is:
     * frame 0: READ_BLOCK opcode
     * frame 1: offset
     * frame 2: number of bytes to be read */
    int zerr = zmsg_addmem (send_msg, &opcode, sizeof (opcode));
    ASSERT_TEST(zerr == 0, "Could not add READ opcode in message",
            err_add_opcode);
    zerr = zmsg_addmem (send_msg, &offs, sizeof (offs));
    ASSERT_TEST(zerr == 0, "Could not add offset in message",
            err_add_offset);
    zerr = zmsg_addmem (send_msg, &size, sizeof (size));
    ASSERT_TEST(zerr == 0, "Could not add size in message",
            err_add_size);

    DBE_DEBUG (DBG_MSG | DBG_LVL_TRACE, "[smio_thsafe_client:zmq] Sending message:\n");
#ifdef LOCAL_MSG_DBG
    zmsg_print (send_msg);
#endif

    zerr = zmsg_send (&send_msg, self->pipe);
    ASSERT_TEST(zerr == 0, "Could not send message", err_send_msg);

    /* Message is:
     * frame 0: reply code
     * frame 1: return code
     * frame 2: data */
    ret_size = _thsafe_zmq_client_recv_read (self, (uint8_t *) data, size);

err_send_msg:
err_add_size:
err_add_offset:
err_add_opcode:
    zmsg_destroy (&send_msg);
err_msg_alloc:
    return ret_size;
}
/**** Write data block from device function pointer, size in bytes ****/
ssize_t thsafe_zmq_client_write_block (smio_t *self, loff_t offs, size_t size, const uint32_t *data)
{
    assert (self);
    ssize_t ret_size = -1;
    zmsg_t *send_msg = zmsg_new ();
    ASSERT_ALLOC(send_msg, err_msg_alloc);
    uint32_t opcode = THSAFE_WRITE_BLOCK;

    DBE_DEBUG (DBG_MSG | DBG_LVL_TRACE, "[smio_thsafe_client:zmq] Calling thsafe_write_block\n");

    /* Message is:
     * frame 0: WRITE_BLOCK opcode
     * frame 1: offset
     * frame 2: data to be written
     * */
    int zerr = zmsg_addmem (send_msg, &opcode, sizeof (opcode));
    ASSERT_TEST(zerr == 0, "Could not add WRITE opcode in message",
            err_add_opcode);
    zerr = zmsg_addmem (send_msg, &offs, sizeof (offs));
    ASSERT_TEST(zerr == 0, "Could not add offset in message",
            err_add_offset);
    zerr = zmsg_addmem (send_msg, data, size);
    ASSERT_TEST(zerr == 0, "Could not add data in message",
            err_add_data);

    DBE_DEBUG (DBG_MSG | DBG_LVL_TRACE, "[smio_thsafe_client:zmq] Sending message:\n");
#ifdef LOCAL_MSG_DBG
    zmsg_print (send_msg);
#endif

    zerr = zmsg_send (&send_msg, self->pipe);
    ASSERT_TEST(zerr == 0, "Could not send message", err_send_msg);

    /* Message is:
     * frame 0: reply code
     * frame 1: return code */
    ret_size = _thsafe_zmq_client_recv_write (self);

err_send_msg:
err_add_data:
err_add_offset:
err_add_opcode:
    zmsg_destroy (&send_msg);
err_msg_alloc:
    return ret_size;
}
Exemple #9
0
static bpm_client_err_e _bpm_data_acquire (bpm_client_t *self, char *service,
        acq_req_t *acq_req)
{
    assert (self);
    assert (service);
    assert (acq_req);

    bpm_client_err_e err = BPM_CLIENT_SUCCESS;
    ACQ_OPCODE_TYPE operation = ACQ_OPCODE_DATA_ACQUIRE;

    /* Message is:
     * frame 0: operation code
     * frame 1: number of samples
     * frame 2: channel             */
    zmsg_t *request = zmsg_new ();
    zmsg_addmem (request, &operation, sizeof (operation));
    zmsg_addmem (request, &acq_req->num_samples, sizeof (acq_req->num_samples));
    zmsg_addmem (request, &acq_req->chan, sizeof (acq_req->chan));
    mdp_client_send (self->mdp_client, service, &request);

    /* Receive report */
    zmsg_t *report = mdp_client_recv (self->mdp_client, NULL, NULL);
    ASSERT_TEST(report != NULL, "Report received is NULL", err_null_report);
    assert (zmsg_size (report) == 1);

    /* Message is:
     * frame 0: error code      */
    zframe_t *err_code = zmsg_pop(report);
    ASSERT_TEST(err_code != NULL, "Could not receive error code", err_null_code);
    if ( *(ACQ_REPLY_TYPE *) zframe_data(err_code) != ACQ_OK) {
        DBE_DEBUG (DBG_LIB_CLIENT | DBG_LVL_TRACE, "[libclient] bpm_data_acquire: "
                "Data acquire was not required correctly\n");
        err = BPM_CLIENT_ERR_AGAIN;
        goto err_data_acquire;
    }
    DBE_DEBUG (DBG_LIB_CLIENT | DBG_LVL_TRACE, "[libclient] bpm_data_acquire: "
            "Data acquire was successfully required\n");

err_data_acquire:
    zframe_destroy (&err_code);
err_null_code:
    zmsg_destroy (&report);
err_null_report:
    return err;
}
Exemple #10
0
int
zsock_signal (void *self, byte status)
{
    assert (self);
    int64_t signal_value = 0x7766554433221100L + status;
    zmsg_t *msg = zmsg_new ();
    zmsg_addmem (msg, &signal_value, 8);
    return zmsg_send (&msg, self);
}
Exemple #11
0
int
zsock_sendmem (zsock_t *self, const void *data, size_t size)
{
    assert (self);
    assert (data);
    zmsg_t *msg = zmsg_new ();
    zmsg_addmem (msg, data, size);
    return zmsg_send (&msg, self);
}
Exemple #12
0
zmsg_t *
zmsg_new_signal (byte status)
{
    zmsg_t *self = zmsg_new ();
    int64_t signal_value = 0x7766554433221100L + status;
    if (zmsg_addmem (self, &signal_value, 8))
        zmsg_destroy (&self);
    return self;
}
Exemple #13
0
/**************** FMC130M SMIO Functions ****************/
bpm_client_err_e bpm_blink_leds (bpm_client_t *self, char *service, uint32_t leds)
{
    assert (self);
    assert (service);

    bpm_client_err_e err = BPM_CLIENT_SUCCESS;
    FMC130M_4CH_REPLY_TYPE operation = FMC130M_4CH_OPCODE_LEDS;

    zmsg_t *request = zmsg_new ();
    err = (request == NULL) ? BPM_CLIENT_ERR_ALLOC : err;
    ASSERT_ALLOC(request, err_send_msg_alloc);
    zmsg_addmem (request, &operation, sizeof (operation));
    zmsg_addmem (request, &leds, sizeof (leds));
    mdp_client_send (self->mdp_client, service, &request);

err_send_msg_alloc:
    return err;
}
Exemple #14
0
//  ---------------------------------------------------------------------
//  Whisper to peer
void lsd_whisper(lsd_handle_t* self, const char* peer, const uint8_t * msg, size_t len) 
{
	assert(self);
	assert(peer);

	zmsg_t* outgoing = zmsg_new();
	zmsg_addstr (outgoing, peer);
	if(msg && len) {
		zmsg_addmem(outgoing, msg, len);
	}
	zre_node_shout(self->interface, &outgoing);
	zmsg_destroy(&outgoing);
}
int rrsvr_send(rrsvr_t *self, void *buf, size_t len)
{
    zmsg_t *msg = zmsg_new();

    if (self->mode == ASYNC_MODE) {
        rrsvr_construct_envelope(self, msg);
    }

    zmsg_addmem(msg, buf, len);

    int rc = zmsg_send(&msg, self->socket);
    return rc;
}
Exemple #16
0
int __message_send_data(zsock_t *sock, int16_t msgtype, const char *data, uint32_t data_size)
{
    /*zmsg_t *msg = zmsg_new();*/

    /*zmsg_addmem(msg, &msgtype, sizeof(int16_t));*/
    /*zmsg_addmem(msg, data, data_size);*/
    zmsg_t *msg = create_base_message(msgtype);
    zmsg_addmem(msg, data, data_size);
    
    int rc = zmsg_send(&msg, sock);

    return rc;
}
Exemple #17
0
int main(void) {
    zmsg_t *msg;
    zframe_t *frame;
    char *str;
    int i, rc;

    // create push/pull sockets
    zsock_t *push = zsock_new_push("inproc://example");
    zsock_t *pull = zsock_new_pull("inproc://example");

    // send multi-frame message
    msg = zmsg_new();
    zmsg_addmem(msg, "apple", 5);
    zmsg_addmem(msg, "banana", 6);
    zmsg_addmem(msg, "cherry", 6);
    assert(zmsg_size(msg) == 3);
    assert(zmsg_content_size(msg) == 5+6+6);
    rc = zmsg_send(&msg, push);
    assert(msg == NULL);
    assert(rc == 0);

    // receive multi-frame message
    msg = zmsg_recv(pull);
    assert(msg);
    assert(zmsg_size(msg) == 3);
    assert(zmsg_content_size(msg) == 5+6+6);
    for (i = 0; i < 3; i++) {
        str = zmsg_popstr(msg);
        puts(str);
    }
    zmsg_destroy(&msg);

    // disconnect
    zsock_destroy(&push);
    zsock_destroy(&pull);

    return 0;
}
Exemple #18
0
END_TEST


//  --------------------------------------------------------------------------
/// Test _size ().
START_TEST(test_msg_size)
{
    sam_selftest_introduce ("test_msg_size");

    zmsg_t *zmsg = zmsg_new ();
    sam_msg_t *msg = sam_msg_new (&zmsg);

    ck_assert_int_eq (sam_msg_size (msg), 0);
    sam_msg_destroy (&msg);

    zmsg = zmsg_new ();
    zmsg_addmem (zmsg, NULL, 0);
    zmsg_addmem (zmsg, NULL, 0);
    msg = sam_msg_new (&zmsg);

    ck_assert_int_eq (sam_msg_size (msg), 2);
    sam_msg_destroy (&msg);
}
Exemple #19
0
/**
 * @brief Send a message through the 0mq socket.
 *
 * This function is blocking.
 *
 * @returns The number of bytes sent.
 *
 * @param buf The data to send.
 * @param len The length of buf.
 */
int mux_0mq_send_msg(void *buf, size_t len)
{
    zmsg_t *msg = zmsg_new();

    mux_printf("Now attempting to send message!");
    zmsg_addstr(msg, display->uuid);
    zmsg_addmem(msg, buf, len);

    if (zmsg_size(msg) != 2) {
        mux_printf_error("Something went wrong building zmsg");
        return -1;
    }

    zmsg_send(&msg, display->zmq.socket); // TODO: figure out how return code works for this

    return len;
}
Exemple #20
0
int
mdp_worker_send (mdp_worker_t **self_p, void *socket)
{
    assert (socket);
    assert (self_p);
    assert (*self_p);
    mdp_worker_t *self = *self_p;

    //  If we're sending to a ROUTER, we send the address first
    zmsg_t *msg = zmsg_new ();
    if (zsockopt_type (socket) == ZMQ_ROUTER) {
        assert (self->address);
        zmsg_add (msg, self->address);
        self->address = NULL;       //  Owned by msg now
    }
    //  Send header fields
    zmsg_addstr (msg, "");
    zmsg_addstr (msg, "MDPW01");
    zmsg_addmem (msg, &self->id, 1);

    switch (self->id) {
        case MDP_WORKER_READY:
            zmsg_addstr (msg, self->service);
            break;
        case MDP_WORKER_REQUEST:
            zmsg_add (msg, self->client);
            self->client = NULL;
            zmsg_add (msg, self->body);
            self->body = NULL;
            break;
        case MDP_WORKER_REPLY:
            zmsg_add (msg, self->client);
            self->client = NULL;
            zmsg_add (msg, self->body);
            self->body = NULL;
            break;
        case MDP_WORKER_HEARBEAT:
            break;
        case MDP_WORKER_DISCONNECT:
            break;
    }
    //  Send the message and destroy mdp_worker object
    int rc = zmsg_send (&msg, socket);
    mdp_worker_destroy (self_p);
    return rc;
}
Exemple #21
0
int
zmsg_addmsg (zmsg_t *self, zmsg_t **msg_p)
{
    assert (self);
    assert (zmsg_is (self));
    assert (msg_p);

    zmsg_t *msg = *msg_p;
    byte *data;
    size_t len = zmsg_encode (msg, &data);
    int r = zmsg_addmem (self, data, len);
    if (r == 0) {
        zmsg_destroy (&msg);
        *msg_p = NULL;
    }
    free (data);
    return r;
}
Exemple #22
0
void *hczmq(void *ptr) {
    struct host_connection hc;
    char port[6], timestamp[16], info[25];
    struct tm *tm;
    int idx;
    int rc;

    struct request req[1];
    zmsg_t *msg;

    printf("message from main thread: %s\n", (char *)ptr);

    // setup zmq server
    zsock_t *sock = zsock_new_rep(HCZMQ_SOCK);

    while(1) {
        // recv
        rc = get_request(sock, req);
        if (rc < 0) {
            // bad request
            continue;
        }
        strftime(timestamp, 16, "%Y%m%d%H%M%S", localtime(&req->timestamp));
        printf("ip: %s, timestamp: %s\n", INET_NTOA(req->ip), timestamp);

        // retrieve host connection by ip and timestamp
        pthread_mutex_lock(&mutex);
        msg = zmsg_new();
        for (idx = 0; idx < 3; idx++) {
            sprintf(port, "%d", ghc->conns[idx].port);
            tm = localtime(&ghc->conns[idx].timestamp);
            strftime(timestamp, 16, "%Y%m%d%H%M%S", tm);
            sprintf(info, "%s:%s", port, timestamp);
            printf("  %s\n", info);
            zmsg_addmem(msg, info, strlen(info));
        }
        pthread_mutex_unlock(&mutex);

        // send
        zmsg_send(&msg, sock);
    }

    pthread_exit((void*)123);
}
Exemple #23
0
zmsg_t *
zmsg_dup (zmsg_t *self)
{
    assert (self);
    assert (zmsg_is (self));

    zmsg_t *copy = zmsg_new ();
    if (!copy)
        return NULL;

    zframe_t *frame = zmsg_first (self);
    while (frame) {
        if (zmsg_addmem (copy, zframe_data (frame), zframe_size (frame))) {
            zmsg_destroy (&copy);
            return NULL;
        }
        frame = zmsg_next (self);
    }
    return copy;
}
Exemple #24
0
zmsg_t *
zmsg_dup (zmsg_t *self)
{
    if (self) {
        assert (zmsg_is (self));
        zmsg_t *copy = zmsg_new ();
        if (copy) {
            zframe_t *frame = zmsg_first (self);
            while (frame) {
                if (zmsg_addmem (copy, zframe_data (frame), zframe_size (frame))) {
                    zmsg_destroy (&copy);
                    break;      //  Abandon attempt to copy message
                }
                frame = zmsg_next (self);
            }
        }
        return copy;
    }
    else
        return NULL;
}
Exemple #25
0
static bpm_client_err_e _bpm_check_data_acquire (bpm_client_t *self, char *service)
{
    assert (self);
    assert (service);

    int err = BPM_CLIENT_SUCCESS;
    ACQ_OPCODE_TYPE operation = ACQ_OPCODE_CHECK_DATA_ACQUIRE;

    /* Message is:
     * frame 0: operation code      */
    zmsg_t *request = zmsg_new ();
    zmsg_addmem (request, &operation, sizeof (operation));
    mdp_client_send (self->mdp_client, service, &request);

    /* Receive report */
    zmsg_t *report = mdp_client_recv (self->mdp_client, NULL, NULL);
    ASSERT_TEST(report != NULL, "Report received is NULL", err_null_report);
    assert (zmsg_size (report) == 1);

    /* Message is:
     * frame 0: error code          */
    zframe_t *err_code = zmsg_pop(report);
    ASSERT_TEST(err_code != NULL, "Could not receive error code", err_null_code);
    if ( *(ACQ_REPLY_TYPE *) zframe_data(err_code) != ACQ_OK) {
        DBE_DEBUG (DBG_LIB_CLIENT | DBG_LVL_TRACE, "[libclient] bpm_check_data_acquire: "
            "Check fail: data acquire was not completed\n");
        err = BPM_CLIENT_ERR_SERVER;
        goto err_check_data_acquire;
    }
    DBE_DEBUG (DBG_LIB_CLIENT | DBG_LVL_TRACE, "[libclient] bpm_check_data_acquire: "
            "Check ok: data acquire was successfully completed\n");

err_check_data_acquire:
    zframe_destroy (&err_code);
err_null_code:
    zmsg_destroy (&report);
err_null_report:
    return err;
}
Exemple #26
0
static void
pipe_send_data (pipe_t *self, zchunk_t **chunk_p)
{
    assert (self);
    assert (self->reader);

    zchunk_t *chunk = *chunk_p;
    assert (chunk);
    
    if (self->reader == REMOTE_NODE) {
        //  Send chunk to remote node reader
        zmsg_t *msg = zmsg_new ();
        zmsg_addstr (msg, "DATA");
        zmsg_addstr (msg, self->name);
        zmsg_addmem (msg, zchunk_data (chunk), zchunk_size (chunk));
        zyre_whisper (self->server->zyre, self->remote, &msg);
        zchunk_destroy (chunk_p);
    }
    else {
        client_store_chunk (self->reader, chunk_p);
        engine_send_event (self->reader, have_data_event);
    }
}
Exemple #27
0
int main(int argc, char const * const *argv)
{
  int socket_count  = argc>1 ? atoi(argv[1]) : 1;
  int message_count = argc>2 ? atoi(argv[2]) : 100000;
  int rc;

  zctx_t *context = zctx_new();
  assert(context);
  zctx_set_sndhwm(context, 1);
  zctx_set_linger(context, 100);

  // zmq 2.2 only supports up to 512 sockets
  assert_x(ZMQ_VERSION <= 20200 && socket_count > 512,
           "zeromq < 3.2 only supports up to 512 sockets");

  void **sockets = zmalloc(socket_count * sizeof(void*));

  int j;
  for (j=0; j<socket_count; j++) {
    void *socket = zsocket_new(context, ZMQ_PUSH);
    if (NULL==socket) {
      printf("Error occurred during %dth call to zsocket_new: %s\n", j, zmq_strerror (errno));
      exit(1);
    }
    // zsocket_set_sndtimeo(socket, 10);
    zsocket_set_sndhwm(socket, 10);
    zsocket_set_linger(socket, 50);
    zsocket_set_reconnect_ivl(socket, 100); // 100 ms
    zsocket_set_reconnect_ivl_max(socket, 10 * 1000); // 10 s
    // printf("hwm %d\n", zsocket_hwm(socket));
    // printf("sndbuf %d\n", zsocket_sndbuf(socket));
    // printf("swap %d\n", zsocket_swap(socket));
    // printf("linger %d\n", zsocket_linger(socket));
    // printf("sndtimeo %d\n", zsocket_sndtimeo(socket));
    // printf("reconnect_ivl %d\n", zsocket_reconnect_ivl(socket));
    // printf("reconnect_ivl_max %d\n", zsocket_reconnect_ivl_max(socket));

    rc = zsocket_connect(socket, "tcp://localhost:12345");
    assert(rc==0);
    sockets[j] = socket;
    zclock_sleep(10); // sleep 10 ms
  }

  char data[MESSAGE_BODY_SIZE];
  memset(data, 'a', MESSAGE_BODY_SIZE);

  char *exchanges[2] = {"zmq-device-1", "zmq-device-2"};
  // char *keys[2] = {"1","2"};

  int i = 0, queued = 0, rejected = 0;
  for (i=0; i<message_count; i++) {
    if (zsys_interrupted)
      break;
    zmsg_t *message = zmsg_new();
    zmsg_addstr(message, exchanges[i%2]);
    zmsg_addstrf(message, "logjam.zmq.test.%d.%d", i%2, i);
    zmsg_addmem(message, data, MESSAGE_BODY_SIZE);
    // zmsg_dump(message);
    rc = zmsg_send_dont_wait(&message, sockets[i%socket_count]);
    assert(message == NULL);
    if (rc == 0) {
      queued++;
    } else {
      rejected++;
    }
    // usleep(20);
    usleep(1000);
  }

  printf("queued:   %8d\n", queued);
  printf("rejected: %8d\n", rejected);

  for (i=0;i<socket_count;i++) {
    zsocket_destroy(context, sockets[i]);
  }
  free(sockets);
  zctx_destroy(&context);

  return 0;
}
Exemple #28
0
void
zmsg_test (bool verbose)
{
    printf (" * zmsg: ");

    int rc = 0;
    //  @selftest
    //  Create two PAIR sockets and connect over inproc
    zsock_t *output = zsock_new_pair ("@inproc://zmsg.test");
    assert (output);
    zsock_t *input = zsock_new_pair (">inproc://zmsg.test");
    assert (input);

    //  Test send and receive of single-frame message
    zmsg_t *msg = zmsg_new ();
    assert (msg);
    zframe_t *frame = zframe_new ("Hello", 5);
    assert (frame);
    zmsg_prepend (msg, &frame);
    assert (zmsg_size (msg) == 1);
    assert (zmsg_content_size (msg) == 5);
    rc = zmsg_send (&msg, output);
    assert (msg == NULL);
    assert (rc == 0);

    msg = zmsg_recv (input);
    assert (msg);
    assert (zmsg_size (msg) == 1);
    assert (zmsg_content_size (msg) == 5);
    zmsg_destroy (&msg);

    //  Test send and receive of multi-frame message
    msg = zmsg_new ();
    assert (msg);
    rc = zmsg_addmem (msg, "Frame0", 6);
    assert (rc == 0);
    rc = zmsg_addmem (msg, "Frame1", 6);
    assert (rc == 0);
    rc = zmsg_addmem (msg, "Frame2", 6);
    assert (rc == 0);
    rc = zmsg_addmem (msg, "Frame3", 6);
    assert (rc == 0);
    rc = zmsg_addmem (msg, "Frame4", 6);
    assert (rc == 0);
    rc = zmsg_addmem (msg, "Frame5", 6);
    assert (rc == 0);
    rc = zmsg_addmem (msg, "Frame6", 6);
    assert (rc == 0);
    rc = zmsg_addmem (msg, "Frame7", 6);
    assert (rc == 0);
    rc = zmsg_addmem (msg, "Frame8", 6);
    assert (rc == 0);
    rc = zmsg_addmem (msg, "Frame9", 6);
    assert (rc == 0);
    zmsg_t *copy = zmsg_dup (msg);
    assert (copy);
    rc = zmsg_send (&copy, output);
    assert (rc == 0);
    rc = zmsg_send (&msg, output);
    assert (rc == 0);

    copy = zmsg_recv (input);
    assert (copy);
    assert (zmsg_size (copy) == 10);
    assert (zmsg_content_size (copy) == 60);
    zmsg_destroy (&copy);

    msg = zmsg_recv (input);
    assert (msg);
    assert (zmsg_size (msg) == 10);
    assert (zmsg_content_size (msg) == 60);

    // create empty file for null test
    FILE *file = fopen ("zmsg.test", "w");
    assert (file);
    fclose (file);

    file = fopen ("zmsg.test", "r");
    zmsg_t *null_msg = zmsg_load (NULL, file);
    assert (null_msg == NULL);
    fclose (file);
    remove ("zmsg.test");

    //  Save to a file, read back
    file = fopen ("zmsg.test", "w");
    assert (file);
    rc = zmsg_save (msg, file);
    assert (rc == 0);
    fclose (file);

    file = fopen ("zmsg.test", "r");
    rc = zmsg_save (msg, file);
    assert (rc == -1);
    fclose (file);
    zmsg_destroy (&msg);

    file = fopen ("zmsg.test", "r");
    msg = zmsg_load (NULL, file);
    assert (msg);
    fclose (file);
    remove ("zmsg.test");
    assert (zmsg_size (msg) == 10);
    assert (zmsg_content_size (msg) == 60);

    //  Remove all frames except first and last
    int frame_nbr;
    for (frame_nbr = 0; frame_nbr < 8; frame_nbr++) {
        zmsg_first (msg);
        frame = zmsg_next (msg);
        zmsg_remove (msg, frame);
        zframe_destroy (&frame);
    }
    //  Test message frame manipulation
    assert (zmsg_size (msg) == 2);
    frame = zmsg_last (msg);
    assert (zframe_streq (frame, "Frame9"));
    assert (zmsg_content_size (msg) == 12);
    frame = zframe_new ("Address", 7);
    assert (frame);
    zmsg_prepend (msg, &frame);
    assert (zmsg_size (msg) == 3);
    rc = zmsg_addstr (msg, "Body");
    assert (rc == 0);
    assert (zmsg_size (msg) == 4);
    frame = zmsg_pop (msg);
    zframe_destroy (&frame);
    assert (zmsg_size (msg) == 3);
    char *body = zmsg_popstr (msg);
    assert (streq (body, "Frame0"));
    free (body);
    zmsg_destroy (&msg);

    //  Test encoding/decoding
    msg = zmsg_new ();
    assert (msg);
    byte *blank = (byte *) zmalloc (100000);
    assert (blank);
    rc = zmsg_addmem (msg, blank, 0);
    assert (rc == 0);
    rc = zmsg_addmem (msg, blank, 1);
    assert (rc == 0);
    rc = zmsg_addmem (msg, blank, 253);
    assert (rc == 0);
    rc = zmsg_addmem (msg, blank, 254);
    assert (rc == 0);
    rc = zmsg_addmem (msg, blank, 255);
    assert (rc == 0);
    rc = zmsg_addmem (msg, blank, 256);
    assert (rc == 0);
    rc = zmsg_addmem (msg, blank, 65535);
    assert (rc == 0);
    rc = zmsg_addmem (msg, blank, 65536);
    assert (rc == 0);
    rc = zmsg_addmem (msg, blank, 65537);
    assert (rc == 0);
    free (blank);
    assert (zmsg_size (msg) == 9);
    byte *buffer;
    size_t buffer_size = zmsg_encode (msg, &buffer);
    zmsg_destroy (&msg);
    msg = zmsg_decode (buffer, buffer_size);
    assert (msg);
    free (buffer);
    zmsg_destroy (&msg);

    //  Test submessages
    msg = zmsg_new ();
    assert (msg);
    zmsg_t *submsg = zmsg_new ();
    zmsg_pushstr (msg, "matr");
    zmsg_pushstr (submsg, "joska");
    rc = zmsg_addmsg (msg, &submsg);
    assert (rc == 0);
    assert (submsg == NULL);
    submsg = zmsg_popmsg (msg);
    assert (submsg == NULL);   // string "matr" is not encoded zmsg_t, so was discarded
    submsg = zmsg_popmsg (msg);
    assert (submsg);
    body = zmsg_popstr (submsg);
    assert (streq (body, "joska"));
    free (body);
    zmsg_destroy (&submsg);
    frame = zmsg_pop (msg);
    assert (frame == NULL);
    zmsg_destroy (&msg);

    //  Test comparison of two messages
    msg = zmsg_new ();
    zmsg_addstr (msg, "One");
    zmsg_addstr (msg, "Two");
    zmsg_addstr (msg, "Three");
    zmsg_t *msg_other = zmsg_new ();
    zmsg_addstr (msg_other, "One");
    zmsg_addstr (msg_other, "Two");
    zmsg_addstr (msg_other, "One-Hundred");
    zmsg_t *msg_dup = zmsg_dup (msg);
    zmsg_t *empty_msg = zmsg_new ();
    zmsg_t *empty_msg_2 = zmsg_new ();
    assert (zmsg_eq (msg, msg_dup));
    assert (!zmsg_eq (msg, msg_other));
    assert (zmsg_eq (empty_msg, empty_msg_2));
    assert (!zmsg_eq (msg, NULL));
    assert (!zmsg_eq (NULL, empty_msg));
    assert (!zmsg_eq (NULL, NULL));
    zmsg_destroy (&msg);
    zmsg_destroy (&msg_other);
    zmsg_destroy (&msg_dup);
    zmsg_destroy (&empty_msg);
    zmsg_destroy (&empty_msg_2);

    //  Test signal messages
    msg = zmsg_new_signal (0);
    assert (zmsg_signal (msg) == 0);
    zmsg_destroy (&msg);
    msg = zmsg_new_signal (-1);
    assert (zmsg_signal (msg) == 255);
    zmsg_destroy (&msg);

    //  Now try methods on an empty message
    msg = zmsg_new ();
    assert (msg);
    assert (zmsg_size (msg) == 0);
    assert (zmsg_unwrap (msg) == NULL);
    assert (zmsg_first (msg) == NULL);
    assert (zmsg_last (msg) == NULL);
    assert (zmsg_next (msg) == NULL);
    assert (zmsg_pop (msg) == NULL);
    //  Sending an empty message is valid and destroys the message
    assert (zmsg_send (&msg, output) == 0);
    assert (!msg);

    zsock_destroy (&input);
    zsock_destroy (&output);

    //  @end
    printf ("OK\n");
}
Exemple #29
0
static bpm_client_err_e _bpm_get_data_block (bpm_client_t *self, char *service,
        acq_trans_t *acq_trans)
{
    assert (self);
    assert (service);
    assert (acq_trans);
    assert (acq_trans->block.data);

    bpm_client_err_e err = BPM_CLIENT_SUCCESS;
    ACQ_OPCODE_TYPE operation = ACQ_OPCODE_GET_DATA_BLOCK;

    /* Message is:
     * frame 0: operation code
     * frame 1: channel
     * frame 2: block required          */
    zmsg_t *request = zmsg_new ();
    zmsg_addmem (request, &operation, sizeof (operation));
    zmsg_addmem (request, &acq_trans->req.chan, sizeof (acq_trans->req.chan));
    zmsg_addmem (request, &acq_trans->block.idx, sizeof (acq_trans->block.idx));
    mdp_client_send (self->mdp_client, service, &request);

    /* Receive report */
    zmsg_t *report = mdp_client_recv (self->mdp_client, NULL, NULL);
    ASSERT_TEST(report != NULL, "Report received is NULL", err_null_report);
    assert (zmsg_size (report) == 3);

    /* Message is:
     * frame 0: error code
     * frame 1: data size
     * frame 2: data block              */
    zframe_t *err_code = zmsg_pop (report);
    ASSERT_TEST(err_code != NULL, "Could not receive error code", err_null_code);
    zframe_t *data_size_frm = zmsg_pop (report);
    ASSERT_TEST(data_size_frm != NULL, "Could not receive data size", err_null_data_size);
    uint32_t data_size = *(uint32_t *) zframe_data(data_size_frm);
    zframe_t *data = zmsg_pop (report);
    ASSERT_TEST(data != NULL, "Could not receive data", err_null_data);

    if ( *(ACQ_REPLY_TYPE *) zframe_data (err_code) != ACQ_OK) {
        DBE_DEBUG (DBG_LIB_CLIENT | DBG_LVL_TRACE, "[libclient] bpm_get_data_block: "
            "Data block was not acquired\n");
        err = BPM_CLIENT_ERR_SERVER;
        goto err_get_data_block;
    }

    /* Data size effectively returned */
    uint32_t read_size = (acq_trans->block.data_size < data_size) ?
        acq_trans->block.data_size : data_size;
    memcpy (acq_trans->block.data, (uint32_t *) zframe_data (data), read_size);

	/* Print some debug messages */
    DBE_DEBUG (DBG_LIB_CLIENT | DBG_LVL_TRACE, "[libclient] bpm_get_data_block: "
            "read_size: %u\n", read_size);
    DBE_DEBUG (DBG_LIB_CLIENT | DBG_LVL_TRACE, "[libclient] bpm_get_data_block: "
            "acq_trans->block.data: %p\n", acq_trans->block.data);

    acq_trans->block.bytes_read = read_size;

err_get_data_block:
    zframe_destroy (&data);
err_null_data:
    zframe_destroy (&data_size_frm);
err_null_data_size:
    zframe_destroy (&err_code);
err_null_code:
    zmsg_destroy (&report);
err_null_report:
    return err;
}
Exemple #30
0
///
//  Add block of memory to the end of the message, as a new frame.
//  Returns 0 on success, -1 on error.                            
int QmlZmsg::addmem (const void *src, size_t size) {
    return zmsg_addmem (self, src, size);
};