Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <zmq.h>
- #include <memory.h>
- #include <iostream>
- #include <boost/thread/thread.hpp>
- #include <boost/thread/xtime.hpp>
- #define INTERNAL_DATA_SOCKET "inproc://data"
- #define MESSAGE "{\"m_t\": 0, \"data\": {}}"
- void *context;
- void outputWorker();
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[]) {
- context = zmq_init(1);
- boost::thread outputWorkerThread(&outputWorker);
- boost::xtime xtime;
- boost::xtime_get(&xtime, boost::TIME_UTC);
- xtime.sec+=1;
- boost::thread::sleep(xtime);
- void *intDataSocket = zmq_socket(context, ZMQ_PUB);
- zmq_connect(intDataSocket, INTERNAL_DATA_SOCKET);
- int messagesToStore = 1;
- zmq_setsockopt(intDataSocket, ZMQ_HWM, &messagesToStore, strlen(MESSAGE));
- int size = strlen(MESSAGE);
- int rc;
- while(1) {
- zmq_msg_t request;
- zmq_msg_init_size(&request, size);
- memcpy(zmq_msg_data(&request), MESSAGE, size);
- rc = zmq_send(intDataSocket, &request, 0);
- assert(rc == 0);
- zmq_msg_close(&request);
- //Remove comments to remove OUT OF MEMORY problem
- //boost::xtime_get(&xtime, boost::TIME_UTC);
- //xtime.nsec+=1000;
- //boost::thread::sleep(xtime);
- }
- return 0;
- }
- void outputWorker() {
- cout << "Starting worker thread " << endl;
- void *outputSocket = zmq_socket(context, ZMQ_PUB);
- int bindResult = zmq_bind(outputSocket, OUTPUT_SOCKET);
- int messagesToStore = 1;
- zmq_setsockopt(outputSocket, ZMQ_HWM, &messagesToStore, strlen(MESSAGE));
- void *intDataSocket = zmq_socket(context, ZMQ_SUB);
- bindResult = zmq_bind(intDataSocket, INTERNAL_DATA_SOCKET);
- zmq_setsockopt(intDataSocket, ZMQ_SUBSCRIBE, "", 0);
- while(1) {
- zmq_msg_t request;
- zmq_msg_init(&request);
- zmq_recv(intDataSocket, &request, 0);
- size_t size = zmq_msg_size(&request);
- char *buffer = (char*) malloc(sizeof(char) * (size + 1));
- memcpy(buffer, zmq_msg_data(&request), size);
- buffer[size] = 0;
- zmq_msg_close(&request);
- free(buffer);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment