How can we transform cleanly a communication based on HTTP API --> to a message communication using ZMQ library ?
1 Answers
In case you indeed want to do so, one may design a kind of Mediator, using ZeroMQ tools.
ZeroMQ has a set of multi-level abstractions, where AccessPoints, typically have a certain "behaviour" ( a distributed behaviour ) they perform among themselves.
Your indicated target aims at using no such behaviour, but to have some sort of transparent, (almost) wire-level handling of data-flows.
For this very purpose let me direct your kind attention first to the concept:
- ZeroMQ Hierarchy in Less than Five Seconds
and next to a possible tool, feasible to help in the given task:
-ZMQ_STREAM
Scalable Formal Communication Archetype ( for an AccessPoint )
A socket of type
ZMQ_STREAM
is used to send and receive TCP data from a non-ØMQ peer, when using thetcp://
transport. AZMQ_STREAM
socket can act as client and/or server, sending and/or receiving TCP data asynchronously.
When receiving TCP data, aZMQ_STREAM
socket shall prepend a message part containing the identity of the originating peer to the message before passing it to the application. Messages received are fair-queued from among all connected peers.
When sending TCP data, aZMQ_STREAM
socket shall remove the first part of the message and use it to determine the identity of the peer the message shall be routed to, and unroutable messages shall cause anEHOSTUNREACH
orEAGAIN
error.
To open a connection to a server, use thezmq_connect
call, and then fetch the socket identity using theZMQ_IDENTITY zmq_getsockopt
call.
To close a specific connection, send the identity frame followed by a zero-length message (see EXAMPLE section).
When a connection is made, a zero-length message will be received by the application. Similarly, when the peer disconnects (or the connection is lost), a zero-length message will be received by the application.
You must send one identity frame followed by one data frame. TheZMQ_SNDMORE
flag is required for identity frames but is ignored on data frames.
Example:
/* Create Context-Engine */
void *ctx = zmq_ctx_new (); assert (ctx);
/* Create ZMQ_STREAM socket */
void *socket = zmq_socket (ctx, ZMQ_STREAM); assert (socket);
int rc = zmq_bind (socket, "tcp://*:8080"); assert (rc == 0);
/* Data structure to hold the ZMQ_STREAM ID */
uint8_t id [256];
size_t id_size = 256;
/* Data structure to hold the ZMQ_STREAM received data */
uint8_t raw [256];
size_t raw_size = 256;
while (1) {
/* Get HTTP request; ID frame and then request */
id_size = zmq_recv (socket, id, 256, 0); assert (id_size > 0);
do {
raw_size = zmq_recv (socket, raw, 256, 0); assert (raw_size >= 0);
} while (raw_size == 256);
/* Prepares the response */
char http_response [] =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"Hello, World!";
/* Sends the ID frame followed by the response */
zmq_send (socket, id, id_size, ZMQ_SNDMORE);
zmq_send (socket, http_response, strlen (http_response), 0);
/* Closes the connection by sending the ID frame followed by a zero response */
zmq_send (socket, id, id_size, ZMQ_SNDMORE);
zmq_send (socket, 0, 0, 0);
}
zmq_close (socket); zmq_ctx_destroy (ctx); /* Clean Close Sockets / Terminate Context */

- 1
- 6
- 50
- 92