Skip to content

Instantly share code, notes, and snippets.

@purem
Created August 20, 2012 21:33
Show Gist options
  • Save purem/3408114 to your computer and use it in GitHub Desktop.
Save purem/3408114 to your computer and use it in GitHub Desktop.
Pub-sub to Stretched Request-reply
#include "zhelpers.hpp"
using std::string;
using std::cout;
using std::endl;
int main (int argc, char *argv[])
{
cout << "Strategy: Started strategy" << endl;
zmq::context_t context(1);
zmq::socket_t frontend (context, ZMQ_SUB);
zmq::socket_t backend (context, ZMQ_REQ);
s_set_id (frontend); // Makes tracing easier
s_set_id (backend); // Makes tracing easier
frontend.connect("ipc://handler_data_forwarder_backend.ipc");
backend.connect("ipc://order_router_frontend.ipc");
string subscription = "DATA.NASDAQ.AAPL";
frontend.setsockopt(ZMQ_SUBSCRIBE, "", 0);
// Initialize poll set
zmq::pollitem_t items [] = {
{ backend, 0, ZMQ_POLLIN, 0 },
{ frontend, 0, ZMQ_POLLIN, 0 }
};
while(1) {
// Poll to see where we have activity
zmq::poll (&items [0], 2, -1);
if (items [1].revents & ZMQ_POLLIN) {
// Read envelope with topic
std::string topic = s_recv(frontend);
// Read envelope with data
std::string data = s_recv(frontend);
std::cout << "[" << topic << "] " << data << std::endl;
// Send order
string order_string = "NYSE";
cout << "Strategy: Action - ORDER" << endl;
cout << "Strategy: Order - " << order_string << endl;
s_sendmore(backend, "ORDER");
s_send(backend, order_string);
}
if (items [0].revents & ZMQ_POLLIN) {
// Get order response
string action = s_recv (backend);
if(action.compare("ACK") == 0 )
{
string ack = s_recv (backend);
cout << "Strategy: Received" << endl;
cout << "Strategy: Action - " << action << endl;
cout << "Strategy: Acknowledgement - " << ack << endl;
}
}
}
}
// Output
[DATA.NASDAQ.AAPL] MARKET DATA for AAPL on NASDAQ
Strategy: Action - ORDER
Strategy: Order - NYSE
terminate called after throwing an instance of 'zmq::error_t'
what(): Operation cannot be accomplished in current state
[1] 35505 abort ./strategy
@purem
Copy link
Author

purem commented Aug 20, 2012

Strategy: Action - ORDER
Strategy: Order - NYSE
Socket is not connected (tcp_socket.cpp:203)
[1] 39307 abort ./strategy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment