int mdp_client_test (bool verbose) { printf (" * mdp_client: "); // Simple create/destroy test mdp_client_t *self = mdp_client_new (); assert (self); mdp_client_destroy (&self); // Create pair of sockets we can send through zctx_t *ctx = zctx_new (); assert (ctx); void *output = zsocket_new (ctx, ZMQ_DEALER); assert (output); zsocket_bind (output, "inproc://selftest"); void *input = zsocket_new (ctx, ZMQ_ROUTER); assert (input); zsocket_connect (input, "inproc://selftest"); // Encode/send/decode and verify each message type self = mdp_client_new (); mdp_client_service_set (self, "Life is short but Now lasts for ever"); mdp_client_body_set (self, zframe_new ("Captcha Diem", 12)); mdp_client_send (&self, output); self = mdp_client_recv (input); assert (self); assert (streq (mdp_client_service (self), "Life is short but Now lasts for ever")); assert (zframe_streq (mdp_client_body (self), "Captcha Diem")); mdp_client_destroy (&self); zctx_destroy (&ctx); printf ("OK\n"); return 0; }
mdp_client_t * mdp_client_recv (void *socket) { // Read all frames off socket assert (socket); zmsg_t *msg = zmsg_recv (socket); if (!msg) return NULL; // Interrupted // Create and populate new mdp_client instance mdp_client_t *self = mdp_client_new (); // If we're reading from a ROUTER socket, get address if (zsocket_type (socket) == ZMQ_ROUTER) { self->address = zmsg_pop (msg); if (!self->address) goto empty; // Interrupted } // Read and check header zframe_t *empty_frame = zmsg_pop (msg); if (!empty_frame || !zframe_streq (empty_frame, "")) goto malformed; zframe_destroy (&empty_frame); zframe_t *protocol_frame = zmsg_pop (msg); if (!protocol_frame || !zframe_streq (protocol_frame, "MDPC01")) goto malformed; zframe_destroy (&protocol_frame); // All messages have the same structure self->service = zmsg_popstr (msg); if (!self->service) goto malformed; self->body = zmsg_pop (msg); if (!self->body) goto malformed; // Successful return zmsg_destroy (&msg); return self; // Error returns malformed: printf ("E: malformed message '%d'\n", self->id); empty: zmsg_destroy (&msg); mdp_client_destroy (&self); return (NULL); }
int main (int argc, char *argv []) { int verbose = (argc > 1 && streq (argv [1], "-v")); mdp_client_t *client = mdp_client_new ("tcp://localhost:5555", verbose); // Send 100 sell orders int count; for (count = 0; count < 5; count++) { zmsg_t *request = zmsg_new (); zmsg_pushstr (request, "8"); // volume zmsg_pushstr (request, "%d", count + 1000); // price zmsg_pushstr (request, "SELL"); mdp_client_send (client, "NYSE", &request); } // Send 1 buy order. // This order will match all sell orders. zmsg_t *request = zmsg_new (); zmsg_pushstr (request, "800"); // volume zmsg_pushstr (request, "2000"); // price zmsg_pushstr (request, "BUY"); mdp_client_send (client, "NYSE", &request); // Wait for all trading reports while (1) { char *service = NULL; zmsg_t *report = mdp_client_recv (client, &service); if (report == NULL) break; assert (zmsg_size (report) >= 2); zframe_t *report_type = zmsg_pop (report); char *report_type_str = zframe_strdup (report_type); zframe_t *volume = zmsg_pop (report); char *volume_str = zframe_strdup (volume); printf ("%s: %s %s shares\n", service, report_type_str, volume_str); free (service); free (report_type_str); free (volume_str); } mdp_client_destroy (&client); return 0; }
bpm_client_t *bpm_client_new (char *broker_endp, int verbose) { bpm_client_t *self = zmalloc (sizeof *self); ASSERT_ALLOC(self, err_self_alloc); self->mdp_client = mdp_client_new (broker_endp, verbose); ASSERT_TEST(self->mdp_client!=NULL, "Could not create MDP client", err_mdp_client); /* Initialize acquisition table. Defined in hal/smio/modules/ * acq/ddr3_map.h */ self->acq_buf = __acq_buf; return self; err_mdp_client: free (self); err_self_alloc: return NULL; }
int main (int argc, char *argv []) { int verbose = (argc > 1 && streq (argv [1], "-v")); mdp_client_t *session = mdp_client_new ("tcp://localhost:5555", verbose); int count; for (count = 0; count < 100000; count++) { zmsg_t *request = zmsg_new (); zmsg_pushstr (request, "Hello world"); zmsg_t *reply = mdp_client_send (session, "echo", &request); if (reply) zmsg_destroy (&reply); else break; // Interrupt or failure } printf ("%d requests/replies processed\n", count); mdp_client_destroy (&session); return 0; }