Пример #1
1
zmsg_t *
zmsg_load (zmsg_t *self, FILE *file)
{
    assert (file);
    if (!self)
        self = zmsg_new ();
    if (!self)
        return NULL;

    while (true) {
        size_t frame_size;
        size_t rc = fread (&frame_size, sizeof (frame_size), 1, file);
        if (rc == 1) {
            zframe_t *frame = zframe_new (NULL, frame_size);
            rc = fread (zframe_data (frame), frame_size, 1, file);
            if (frame_size > 0 && rc != 1) {
                zframe_destroy (&frame);
                break;          //  Unable to read properly, quit
            }
            zmsg_append (self, &frame);
        }
        else
            break;              //  Unable to read properly, quit
    }
    if (!zmsg_size (self)) {
        zmsg_destroy (&self);
        self = NULL;
    }
    return self;
}
Пример #2
0
zmsg_t *
zmsg_recv (void *source)
{
    assert (source);
    zmsg_t *self = zmsg_new ();
    if (!self)
        return NULL;

    while (true) {
        zframe_t *frame = zframe_recv (source);
        if (!frame) {
            if (errno == EINTR && zlist_head (self->frames))
                continue;
            else {
                zmsg_destroy (&self);
                break;              //  Interrupted or terminated
            }
        }
        if (zmsg_append (self, &frame)) {
            zmsg_destroy (&self);
            break;
        }
        if (!zsock_rcvmore (source))
            break;              //  Last message frame
    }
    return self;
}
Пример #3
0
void PrimeWorker::SendReply(const proto::Reply& rep, zmsg_t** msg, void* socket) {
	
	size_t fsize = rep.ByteSize();
	zframe_t* frame = zframe_new(0, fsize);
	byte* data = zframe_data(frame);
	rep.SerializeToArray(data, fsize);
	
	zmsg_append(*msg, &frame);
	zmsg_send(msg, socket);
	
}
Пример #4
0
void s_connect_to_broker (mdwrk_t *self)
{
    if (self->worker)
        zmq_close (self->worker);
    self->worker = zmq_socket (self->context, ZMQ_XREQ);
    int linger = 0;
    zmq_setsockopt (self->worker, ZMQ_LINGER, &linger, sizeof (linger));
    zmq_connect (self->worker, self->broker);

    //  Register service with broker
    zmsg_t *msg = zmsg_new ();
    zmsg_append (msg, MDPS_HEADER);
    zmsg_append (msg, MDPS_READY);
    zmsg_append (msg, self->service);
    zmsg_send (&msg, self->worker);

    //  If liveness hits zero, queue is considered disconnected
    self->liveness = HEARTBEAT_LIVENESS;
    self->heartbeat_at = s_clock () + HEARTBEAT_INTERVAL;
}
Пример #5
0
void PrimeWorker::SendData(const proto::Data& dataobj, void* socket) {
	
	size_t fsize = dataobj.ByteSize();
	zframe_t* frame = zframe_new(0, fsize);
	byte* data = zframe_data(frame);
	dataobj.SerializeToArray(data, fsize);
	
	zmsg_t* msg = zmsg_new();
	zmsg_append(msg, &frame);
	zmsg_send(&msg, socket);
	
}
Пример #6
0
static void Send(const C& req, void* socket) {
	
	zmsg_t* msg = zmsg_new();
	size_t fsize = req.ByteSize();
	zframe_t* frame = zframe_new(0, fsize);
	byte* data = zframe_data(frame);
	req.SerializeToArray(data, fsize);
	
	zmsg_append(msg, &frame);
	zmsg_send(&msg, socket);
	
}
Пример #7
0
void PoolServer::SendSignal(proto::Signal& sig, void* socket) {
	
	size_t fsize = sig.ByteSize()+1;
	zframe_t* frame = zframe_new(0, fsize);
	byte* data = zframe_data(frame);
	data[0] = 1;
	sig.SerializeToArray(data+1, fsize-1);
	
	zmsg_t* msg = zmsg_new();
	zmsg_append(msg, &frame);
	zmsg_send(&msg, socket);
	
}
Пример #8
0
zmsg_t *
zmsg_decode (const byte *buffer, size_t buffer_size)
{
    zmsg_t *self = zmsg_new ();
    if (!self)
        return NULL;

    const byte *source = buffer;
    const byte *limit = buffer + buffer_size;
    while (source < limit) {
        size_t frame_size = *source++;
        if (frame_size == 255) {
            if (source > limit - 4) {
                zmsg_destroy (&self);
                break;
            }
            frame_size = (source [0] << 24)
                       + (source [1] << 16)
                       + (source [2] << 8)
                       +  source [3];
            source += 4;
        }
        if (source > limit - frame_size) {
            zmsg_destroy (&self);
            break;
        }
        zframe_t *frame = zframe_new (source, frame_size);
        if (frame) {
            if (zmsg_append (self, &frame)) {
                zmsg_destroy (&self);
                break;
            }
            source += frame_size;
        }
        else {
            zmsg_destroy (&self);
            break;
        }
    }
    return self;
}
Пример #9
0
Файл: zmsg.c Проект: jemc/czmq
zmsg_t *
zmsg_recv (void *source)
{
    assert (source);
    zmsg_t *self = zmsg_new ();
    if (!self)
        return NULL;

    while (true) {
        zframe_t *frame = zframe_recv (source);
        if (!frame) {
            zmsg_destroy (&self);
            break;              //  Interrupted or terminated
        }
        if (zmsg_append (self, &frame)) {
            zmsg_destroy (&self);
            break;
        }
        if (!zsock_rcvmore (source))
            break;              //  Last message frame
    }
    return self;
}
Пример #10
0
static void
s_self_handle_udp (self_t *self)
{
    assert (self);

    char peername [INET_ADDRSTRLEN];
    zframe_t *frame = zsys_udp_recv (self->udpsock, peername, INET_ADDRSTRLEN);

    //  If filter is set, check that beacon matches it
    bool is_valid = false;
    if (self->filter) {
        byte  *filter_data = zframe_data (self->filter);
        size_t filter_size = zframe_size (self->filter);
        if (zframe_size (frame) >= filter_size
        && memcmp (zframe_data (frame), filter_data, filter_size) == 0)
            is_valid = true;
    }
    //  If valid, discard our own broadcasts, which UDP echoes to us
    if (is_valid && self->transmit) {
        byte  *transmit_data = zframe_data (self->transmit);
        size_t transmit_size = zframe_size (self->transmit);
        if (zframe_size (frame) == transmit_size
        && memcmp (zframe_data (frame), transmit_data, transmit_size) == 0)
            is_valid = false;
    }
    //  If still a valid beacon, send on to the API
    if (is_valid) {
        zmsg_t *msg = zmsg_new ();
        assert (msg);
        zmsg_addstr (msg, peername);
        zmsg_append (msg, &frame);
        zmsg_send (&msg, self->pipe);
    }
    else
        zframe_destroy (&frame);
}
Пример #11
0
///
//  Add frame to the end of the message, i.e. after all other frames.      
//  Message takes ownership of frame, will destroy it when message is sent.
//  Returns 0 on success. Deprecates zmsg_add, which did not nullify the   
//  caller's frame reference.                                              
int QZmsg::append (QZframe *frameP)
{
    int rv = zmsg_append (self, &frameP->self);
    return rv;
}
Пример #12
0
zmsg_t *
zre_log_msg_encode (zre_log_msg_t **self_p)
{
    assert (self_p);
    assert (*self_p);
    
    zre_log_msg_t *self = *self_p;
    zmsg_t *msg = zmsg_new ();

    size_t frame_size = 2 + 1;          //  Signature and message ID
    switch (self->id) {
        case ZRE_LOG_MSG_LOG:
            //  level is a 1-byte integer
            frame_size += 1;
            //  event is a 1-byte integer
            frame_size += 1;
            //  node is a 2-byte integer
            frame_size += 2;
            //  peer is a 2-byte integer
            frame_size += 2;
            //  time is a 8-byte integer
            frame_size += 8;
            //  data is a string with 1-byte length
            frame_size++;       //  Size is one octet
            if (self->data)
                frame_size += strlen (self->data);
            break;
            
        default:
            printf ("E: bad message type '%d', not sent\n", self->id);
            //  No recovery, this is a fatal application error
            assert (false);
    }
    //  Now serialize message into the frame
    zframe_t *frame = zframe_new (NULL, frame_size);
    self->needle = zframe_data (frame);
    PUT_NUMBER2 (0xAAA0 | 2);
    PUT_NUMBER1 (self->id);

    switch (self->id) {
        case ZRE_LOG_MSG_LOG:
            PUT_NUMBER1 (self->level);
            PUT_NUMBER1 (self->event);
            PUT_NUMBER2 (self->node);
            PUT_NUMBER2 (self->peer);
            PUT_NUMBER8 (self->time);
            if (self->data) {
                PUT_STRING (self->data);
            }
            else
                PUT_NUMBER1 (0);    //  Empty string
            break;

    }
    //  Now send the data frame
    if (zmsg_append (msg, &frame)) {
        zmsg_destroy (&msg);
        zre_log_msg_destroy (self_p);
        return NULL;
    }
    //  Destroy zre_log_msg object
    zre_log_msg_destroy (self_p);
    return msg;
}
Пример #13
0
void WorkerAgent::actorTask(zsock_t *pipe, void *args)
{
    assert(pipe);
    assert(args);

    const WorkerAgent &agent = *static_cast<WorkerAgent *>(args);

    const auto worker = detail::makeSock(zsock_new_router(agent._localEndpoint.c_str()));
    assert(worker.get());

    const auto poller = detail::makePoller(zpoller_new(pipe, worker.get(), nullptr));
    assert(poller.get());

    int rc = zsock_signal(pipe, 0);
    UNUSED(rc);
    assert(rc == 0);

    bool terminated = false;
    while (!terminated && !zsys_interrupted) {
        zsock_t *sock = static_cast<zsock_t *>(zpoller_wait(poller.get(), -1));

        if (sock == pipe) {
            std::unique_ptr<char> command(zstr_recv(sock));
            if (streq(command.get(), "$TERM")) {
                terminated = true;
            }
        } else if (sock == worker.get()) {
            auto request = detail::makeMsg(zmsg_recv(sock));
            assert(zmsg_size(request.get()) == 3);

            auto identity = detail::makeFrame(zmsg_pop(request.get()));
            auto id = detail::makeFrame(zmsg_pop(request.get()));

            static const auto respond = [](
                                            auto &&identity,
                                            auto &&id,
                                            const char *status,
                                            auto &&data,
            zsock_t *sock) {
                zmsg_t *response = zmsg_new();

                // Identity for ROUTER
                zframe_t *f = identity.release();
                zmsg_append(response, &f);

                // Caller local ID
                f = id.release();
                zmsg_append(response, &f);

                // Status
                f = zframe_new(status, std::strlen(status));
                zmsg_append(response, &f);

                // Result
                f = data.release();
                zmsg_append(response, &f);

                zmsg_send(&response, sock);
            };

            try {
                auto argsFrame = detail::makeFrame(zmsg_pop(request.get()));

                msgpack::unpacked msg;
                msgpack::unpack(
                    &msg,
                    reinterpret_cast<const char *>(zframe_data(argsFrame.get())),
                    zframe_size(argsFrame.get()));

                msgpack::sbuffer sbuf;
                agent._callback(msg, sbuf);

                auto resultFrame =
                    detail::makeFrame(zframe_new(sbuf.data(), sbuf.size()));

                respond(identity, id, "", resultFrame, sock);
            } catch (const msgpack::type_error &e) {
                respond(identity, id, "I", detail::makeFrame(zframe_new_empty()), sock);
            } catch (const InvalidArgument &e) {
                respond(identity, id, "I", detail::makeFrame(zframe_new_empty()), sock);
            } catch (const UndefinedReference &e) {
                respond(identity, id, "U", detail::makeFrame(zframe_new_empty()), sock);
            } catch (const NetworkError &e) {
                respond(identity, id, "N", detail::makeFrame(zframe_new_empty()), sock);
            } catch (...) {
                respond(identity, id, "?", detail::makeFrame(zframe_new_empty()), sock);
            }
        }
    }

    zsys_debug("Cleaned up worker agent.");
}
Пример #14
0
END_TEST


//  --------------------------------------------------------------------------
/// Try to _get () a combination of data.
START_TEST(test_msg_get)
{
    sam_selftest_introduce ("test_msg_get");

    zmsg_t *zmsg = zmsg_new ();
    char *str = "str 1", *nbr = "1";

    char c = 'a';
    zframe_t *frame = zframe_new (&c, sizeof (c));

    void *ptr = (void *) 0xbadc0de;
    zframe_t *ptr_f = zframe_new (&ptr, sizeof (ptr));


    // compose zmsg
    zmsg_addstr (zmsg, str);
    zmsg_addstr (zmsg, nbr);
    zmsg_addstr (zmsg, "skipped");

    zframe_t *frame_dup = zframe_dup (frame);
    zmsg_append (zmsg, &frame_dup);

    frame_dup = zframe_dup (ptr_f);
    zmsg_append (zmsg, &frame_dup);


    // create sam_msg
    sam_msg_t *msg = sam_msg_new (&zmsg);
    ck_assert_int_eq (sam_msg_size (msg), 5);


    // test idempotent _get ()
    int round_c;
    for (round_c = 0; round_c < 2; round_c++) {

        char *pic_str = NULL;
        int pic_nbr = -1;
        zframe_t *pic_frame = NULL;
        void *pic_ptr = NULL;

        int rc = sam_msg_get (
            msg, "si?fp", &pic_str, &pic_nbr, &pic_frame, &pic_ptr);
        ck_assert_int_eq (rc, 0);
        ck_assert_int_eq (sam_msg_size (msg), 5);

        ck_assert_str_eq (pic_str, str);
        ck_assert_int_eq (pic_nbr, atoi (nbr));
        ck_assert (zframe_eq (pic_frame, frame));
        ck_assert (pic_ptr == ptr);

        free (pic_str);
        zframe_destroy (&pic_frame);
    }

    zframe_destroy (&frame);
    zframe_destroy (&ptr_f);
    sam_msg_destroy (&msg);
}
Пример #15
0
Z K2(zmsgappend){PC(x); PC(y); zframe_t*f=(zframe_t*)(intptr_t)yj;  zmsg_append(VSK(x), &f); RZ;}
Пример #16
0
static
int read_router_request_forward(zloop_t *loop, zsock_t *socket, void *callback_data)
{
    subscriber_state_t *state = callback_data;
    zmsg_t *msg = zmsg_recv(socket);
    assert(msg);
    bool ok = true;
    bool is_ping = false;
    state->message_count++;

    // pop the sender id added by the router socket
    zframe_t *sender_id = zmsg_pop(msg);
    zframe_t *empty = zmsg_first(msg);
    zmsg_t *reply = NULL;

    // if the second frame is not empty, we don't need to send a reply
    if (zframe_size(empty) > 0)
        zframe_destroy(&sender_id);
    else {
        // prepare reply
        reply = zmsg_new();
        zmsg_append(reply, &sender_id);
        // pop the empty frame
        empty = zmsg_pop(msg);
        zmsg_append(reply, &empty);
    }

    int n = zmsg_size(msg);
    if (n < 3 || n > 4) {
        fprintf(stderr, "[E] subscriber: (%s:%d): dropped invalid message of size %d\n", __FILE__, __LINE__, n);
        my_zmsg_fprint(msg, "[E] FRAME= ", stderr);
        ok = false;
        goto answer;
    }
    if (n == 4) {
        int is_heartbeat = process_meta_information_and_handle_heartbeat(state, msg);
        if (is_heartbeat) {
            zmsg_destroy(&msg);
            goto answer;
        }
        is_ping = zframe_streq(zmsg_first(msg), "ping");
        if (is_ping)
            goto answer;
    }

    if (PUBLISH_DUPLICATES)
        subscriber_publish_duplicate(msg, state->pub_socket);

    if (!output_socket_ready(state->push_socket, 0) && !state->message_blocks++)
        fprintf(stderr, "[W] subscriber: push socket not ready. blocking!\n");

    int rc = zmsg_send_and_destroy(&msg, state->push_socket);
    if (rc) {
        if (!state->message_drops++)
            fprintf(stderr, "[E] subscriber: dropped message on push socket (%d: %s)\n", errno, zmq_strerror(errno));
    }
 answer:
    if (reply) {
        if (is_ping) {
            if (ok) {
                zmsg_addstr(reply, "200 Pong");
                zmsg_addstr(reply, my_fqdn());
            } else {
                zmsg_addstr(reply, "400 Bad Request");
            }
        } else
            zmsg_addstr(reply, ok ? "202 Accepted" : "400 Bad Request");
        int rc = zmsg_send_and_destroy(&reply, socket);
        if (rc)
            fprintf(stderr, "[E] subscriber: could not send response (%d: %s)\n", errno, zmq_strerror(errno));
    }
    return 0;
}
Пример #17
0
///
//  Add frame to the end of the message, i.e. after all other frames.      
//  Message takes ownership of frame, will destroy it when message is sent.
//  Returns 0 on success. Deprecates zmsg_add, which did not nullify the   
//  caller's frame reference.                                              
int QmlZmsg::append (QmlZframe *frameP) {
    return zmsg_append (self, &frameP->self);
};
Пример #18
0
zmsg_t *
zre_msg_encode (zre_msg_t *self, int socket_type)
{
    assert (self);
    zmsg_t *msg = zmsg_new ();

    //  If we're sending to a ROUTER, send the routing_id first
    if (socket_type == ZMQ_ROUTER)
        zmsg_prepend (msg, &self->routing_id);
        
    size_t frame_size = 2 + 1;          //  Signature and message ID
    switch (self->id) {
        case ZRE_MSG_HELLO:
            //  sequence is a 2-byte integer
            frame_size += 2;
            //  ipaddress is a string with 1-byte length
            frame_size++;       //  Size is one octet
            if (self->ipaddress)
                frame_size += strlen (self->ipaddress);
            //  mailbox is a 2-byte integer
            frame_size += 2;
            //  groups is an array of strings
            frame_size += 4;    //  Size is 4 octets
            if (self->groups) {
                //  Add up size of list contents
                char *groups = (char *) zlist_first (self->groups);
                while (groups) {
                    frame_size += 4 + strlen (groups);
                    groups = (char *) zlist_next (self->groups);
                }
            }
            //  status is a 1-byte integer
            frame_size += 1;
            //  headers is an array of key=value strings
            frame_size += 4;    //  Size is 4 octets
            if (self->headers) {
                self->headers_bytes = 0;
                //  Add up size of dictionary contents
                zhash_foreach (self->headers, s_headers_count, self);
            }
            frame_size += self->headers_bytes;
            break;
            
        case ZRE_MSG_WHISPER:
            //  sequence is a 2-byte integer
            frame_size += 2;
            break;
            
        case ZRE_MSG_SHOUT:
            //  sequence is a 2-byte integer
            frame_size += 2;
            //  group is a string with 1-byte length
            frame_size++;       //  Size is one octet
            if (self->group)
                frame_size += strlen (self->group);
            break;
            
        case ZRE_MSG_JOIN:
            //  sequence is a 2-byte integer
            frame_size += 2;
            //  group is a string with 1-byte length
            frame_size++;       //  Size is one octet
            if (self->group)
                frame_size += strlen (self->group);
            //  status is a 1-byte integer
            frame_size += 1;
            break;
            
        case ZRE_MSG_LEAVE:
            //  sequence is a 2-byte integer
            frame_size += 2;
            //  group is a string with 1-byte length
            frame_size++;       //  Size is one octet
            if (self->group)
                frame_size += strlen (self->group);
            //  status is a 1-byte integer
            frame_size += 1;
            break;
            
        case ZRE_MSG_PING:
            //  sequence is a 2-byte integer
            frame_size += 2;
            break;
            
        case ZRE_MSG_PING_OK:
            //  sequence is a 2-byte integer
            frame_size += 2;
            break;
            
        default:
            printf ("E: bad message type '%d', not sent\n", self->id);
            //  No recovery, this is a fatal application error
            assert (false);
    }
    //  Now serialize message into the frame
    zframe_t *frame = zframe_new (NULL, frame_size);
    self->needle = zframe_data (frame);
    PUT_NUMBER2 (0xAAA0 | 1);
    PUT_NUMBER1 (self->id);

    switch (self->id) {
        case ZRE_MSG_HELLO:
            PUT_NUMBER2 (self->sequence);
            if (self->ipaddress) {
                PUT_STRING (self->ipaddress);
            }
            else
                PUT_NUMBER1 (0);    //  Empty string
            PUT_NUMBER2 (self->mailbox);
            if (self->groups) {
                PUT_NUMBER4 (zlist_size (self->groups));
                char *groups = (char *) zlist_first (self->groups);
                while (groups) {
                    PUT_LONGSTR (groups);
                    groups = (char *) zlist_next (self->groups);
                }
            }
            else
                PUT_NUMBER4 (0);    //  Empty string array
            PUT_NUMBER1 (self->status);
            if (self->headers) {
                PUT_NUMBER4 (zhash_size (self->headers));
                zhash_foreach (self->headers, s_headers_write, self);
            }
            else
                PUT_NUMBER4 (0);    //  Empty dictionary
            break;

        case ZRE_MSG_WHISPER:
            PUT_NUMBER2 (self->sequence);
            break;

        case ZRE_MSG_SHOUT:
            PUT_NUMBER2 (self->sequence);
            if (self->group) {
                PUT_STRING (self->group);
            }
            else
                PUT_NUMBER1 (0);    //  Empty string
            break;

        case ZRE_MSG_JOIN:
            PUT_NUMBER2 (self->sequence);
            if (self->group) {
                PUT_STRING (self->group);
            }
            else
                PUT_NUMBER1 (0);    //  Empty string
            PUT_NUMBER1 (self->status);
            break;

        case ZRE_MSG_LEAVE:
            PUT_NUMBER2 (self->sequence);
            if (self->group) {
                PUT_STRING (self->group);
            }
            else
                PUT_NUMBER1 (0);    //  Empty string
            PUT_NUMBER1 (self->status);
            break;

        case ZRE_MSG_PING:
            PUT_NUMBER2 (self->sequence);
            break;

        case ZRE_MSG_PING_OK:
            PUT_NUMBER2 (self->sequence);
            break;

    }
    //  Now send the data frame
    if (zmsg_append (msg, &frame)) {
        zmsg_destroy (&msg);
        zre_msg_destroy (&self);
        return NULL;
    }
    //  Now send the content field if set
    if (self->id == ZRE_MSG_WHISPER) {
        zframe_t *content_part = zmsg_pop (self->content);
        while (content_part) {
            zmsg_append (msg, &content_part);
            content_part = zmsg_pop (self->content);
        }
    }
    //  Now send the content field if set
    if (self->id == ZRE_MSG_SHOUT) {
        zframe_t *content_part = zmsg_pop (self->content);
        while (content_part) {
            zmsg_append (msg, &content_part);
            content_part = zmsg_pop (self->content);
        }
    }
    //  Destroy zre_msg object
    zre_msg_destroy (&self);
    return msg;

}
Пример #19
0
void thread_test_inv(void* args, zctx_t *ctx, void *pipe) {
	
	int param = (uint64)args;
	
	void* server = zsocket_new(ctx, ZMQ_DEALER);
	
	printf("thread_test_inv: port = %d\n", param);
	int err = zsocket_connect(server, "tcp://localhost:%d", param);
	assert(!err);
	
	static const unsigned maxsize = 1024;
	unsigned char buffer[maxsize+10];
	memset(buffer, 0, sizeof(buffer));
	
	uint64 invcount = 0;
	proto::Request req;
	while(true){
		
		zmsg_t* msg = zmsg_new();
		size_t fsize = rand() % maxsize;
		
		for(size_t i = 0; i < fsize; ++i)
			buffer[i] = rand();
		
		zframe_t* frame = zframe_new(buffer, fsize);
		
		zmsg_append(msg, &frame);
		zmsg_send(&msg, server);
		
		/*if(invcount % 2 == 0){
			
			req.set_type(proto::Request::SHARE);
			req.set_reqid(1);
			GetNewReqNonce(req);
			req.set_version(10);
			req.set_height(1337);
			
			proto::Share* share = req.mutable_share();
			setRandStr(share->mutable_addr(), rand()%32);
			setRandStr(share->mutable_name(), rand()%32);
			setRandStr(share->mutable_hash(), rand()%32);
			setRandStr(share->mutable_merkle(), rand()%32);
			setRandStr(share->mutable_multi(), rand()%50);
			setRandStr(share->mutable_blockhash(), rand()%40);
			share->set_clientid(1313);
			share->set_bits(3444);
			share->set_length(10);
			share->set_chaintype(1);
			share->set_height(2334);
			share->set_isblock(true);
			share->set_nonce(333434);
			
		}else{
			
			req.set_type(proto::Request::STATS);
			req.set_reqid(1);
			GetNewReqNonce(req);
			req.set_version(10);
			req.set_height(1337);
			
			proto::ClientStats* stats = req.mutable_stats();
			setRandStr(stats->mutable_addr(), rand()%32);
			setRandStr(stats->mutable_name(), rand()%32);
			stats->set_clientid(345345);
			stats->set_instanceid(34344555);
			stats->set_version(10);
			stats->set_latency(445);
			stats->set_ngpus(4);
			stats->set_height(433435);
			
		}
		
		Send(req, server);*/
		
		if(zctx_interrupted)
			break;
		
		invcount++;
		if(invcount % 102400 == 0)
			printf("thread_test_inv: invcount = %d/%d\n", (unsigned)(invcount >> 32), (unsigned)invcount);
		
	}
	
	zsocket_destroy(ctx, server);
	
}
Пример #20
0
zmsg_t *
zgossip_msg_encode (zgossip_msg_t **self_p)
{
    assert (self_p);
    assert (*self_p);
    
    zgossip_msg_t *self = *self_p;
    zmsg_t *msg = zmsg_new ();

    size_t frame_size = 2 + 1;          //  Signature and message ID
    switch (self->id) {
        case ZGOSSIP_MSG_HELLO:
            //  version is a 1-byte integer
            frame_size += 1;
            break;
            
        case ZGOSSIP_MSG_PUBLISH:
            //  version is a 1-byte integer
            frame_size += 1;
            //  key is a string with 1-byte length
            frame_size++;       //  Size is one octet
            if (self->key)
                frame_size += strlen (self->key);
            //  value is a string with 1-byte length
            frame_size++;       //  Size is one octet
            if (self->value)
                frame_size += strlen (self->value);
            break;
            
        case ZGOSSIP_MSG_PING:
            //  version is a 1-byte integer
            frame_size += 1;
            break;
            
        case ZGOSSIP_MSG_PONG:
            //  version is a 1-byte integer
            frame_size += 1;
            break;
            
        case ZGOSSIP_MSG_INVALID:
            //  version is a 1-byte integer
            frame_size += 1;
            break;
            
        default:
            zsys_error ("bad message type '%d', not sent\n", self->id);
            //  No recovery, this is a fatal application error
            assert (false);
    }
    //  Now serialize message into the frame
    zframe_t *frame = zframe_new (NULL, frame_size);
    self->needle = zframe_data (frame);
    PUT_NUMBER2 (0xAAA0 | 0);
    PUT_NUMBER1 (self->id);

    switch (self->id) {
        case ZGOSSIP_MSG_HELLO:
            PUT_NUMBER1 (1);
            break;

        case ZGOSSIP_MSG_PUBLISH:
            PUT_NUMBER1 (1);
            if (self->key) {
                PUT_STRING (self->key);
            }
            else
                PUT_NUMBER1 (0);    //  Empty string
            if (self->value) {
                PUT_STRING (self->value);
            }
            else
                PUT_NUMBER1 (0);    //  Empty string
            break;

        case ZGOSSIP_MSG_PING:
            PUT_NUMBER1 (1);
            break;

        case ZGOSSIP_MSG_PONG:
            PUT_NUMBER1 (1);
            break;

        case ZGOSSIP_MSG_INVALID:
            PUT_NUMBER1 (1);
            break;

    }
    //  Now send the data frame
    if (zmsg_append (msg, &frame)) {
        zmsg_destroy (&msg);
        zgossip_msg_destroy (self_p);
        return NULL;
    }
    //  Destroy zgossip_msg object
    zgossip_msg_destroy (self_p);
    return msg;
}
Пример #21
0
zmsg_t *
zgossip_msg_encode (zgossip_msg_t **self_p)
{
    assert (self_p);
    assert (*self_p);
    
    zgossip_msg_t *self = *self_p;
    zmsg_t *msg = zmsg_new ();

    size_t frame_size = 2 + 1;          //  Signature and message ID
    switch (self->id) {
        case ZGOSSIP_MSG_HELLO:
            break;
            
        case ZGOSSIP_MSG_ANNOUNCE:
            //  endpoint is a string with 1-byte length
            frame_size++;       //  Size is one octet
            if (self->endpoint)
                frame_size += strlen (self->endpoint);
            //  service is a string with 1-byte length
            frame_size++;       //  Size is one octet
            if (self->service)
                frame_size += strlen (self->service);
            break;
            
        case ZGOSSIP_MSG_PING:
            break;
            
        case ZGOSSIP_MSG_PONG:
            break;
            
        case ZGOSSIP_MSG_INVALID:
            break;
            
        default:
            printf ("E: bad message type '%d', not sent\n", self->id);
            //  No recovery, this is a fatal application error
            assert (false);
    }
    //  Now serialize message into the frame
    zframe_t *frame = zframe_new (NULL, frame_size);
    self->needle = zframe_data (frame);
    PUT_NUMBER2 (0xAAA0 | 0);
    PUT_NUMBER1 (self->id);

    switch (self->id) {
        case ZGOSSIP_MSG_HELLO:
            break;

        case ZGOSSIP_MSG_ANNOUNCE:
            if (self->endpoint) {
                PUT_STRING (self->endpoint);
            }
            else
                PUT_NUMBER1 (0);    //  Empty string
            if (self->service) {
                PUT_STRING (self->service);
            }
            else
                PUT_NUMBER1 (0);    //  Empty string
            break;

        case ZGOSSIP_MSG_PING:
            break;

        case ZGOSSIP_MSG_PONG:
            break;

        case ZGOSSIP_MSG_INVALID:
            break;

    }
    //  Now send the data frame
    if (zmsg_append (msg, &frame)) {
        zmsg_destroy (&msg);
        zgossip_msg_destroy (self_p);
        return NULL;
    }
    //  Destroy zgossip_msg object
    zgossip_msg_destroy (self_p);
    return msg;
}