[zeromq-dev] Pub-Sub written in same file ..!!

Andrew Hume andrew at research.att.com
Tue Dec 10 11:55:38 CET 2013


oh dear!
there is a lot wrong with this, but let me just answer your question:

your a.out hangs because the subscriber is waiting on a message that hasn’t been sent yet.

it works as two processes because the sender gets to send the messages that the subscriber is waiting for.

On Dec 10, 2013, at 2:43 AM, Rahul Mathur <srivmuk at gmail.com> wrote:

> All,
> 
> Below code is working fine when SSOMSSub () and SSOMSPub () API are written as separate two files (sub.cpp and pub.cpp) but it doesn't execute properly when both API SSOMSSub () & SSOMSPub () are written in same single test1.hpp file as below and this file being called by driver file test.cpp as extreme below -
> 
> ---test1.hpp----
> #include <iostream>
> #include <sstream>
> #include <cstdio>
> #include <cstdlib>
> #include <string>
> #include "zmq.hpp"
> #include "zmq_utils.h"
> 
> using namespace std;
> 
> struct MessageStruct {
>         int     UniqueID;
>         short   ProClientIndicator;
>         char    AccountNumber;
>         short   ORDERTYPE;
>         char    Symbol;
>         int     Volume;
>         int     Price;
>         short   Buy_Sell;
> } ss_oms_call;
> 
> class Test {
> 
> public:
>         int SSOMSSub ();
>         int SSOMSPub ();
> };
> 
> int Test::SSOMSSub () {
>         zmq::context_t context (1);
> 
>         std::cout << "Collecting updates from Strategy Server.\n" << std::endl;
>         zmq::socket_t subscriber (context, ZMQ_SUB);
>         subscriber.connect("tcp://localhost:5556");
> 
>          const char *filter = "";
>         subscriber.setsockopt(ZMQ_SUBSCRIBE, filter, strlen (filter));
> 
>         int update_nbr;
>         for (update_nbr = 0; update_nbr < 100; update_nbr++) {
>                 zmq::message_t update;
>                 subscriber.recv(&update);
>                 std::istringstream iss(static_cast<char*>(update.data()));
>                 iss >> ss_oms_call.UniqueID >> ss_oms_call.ProClientIndicator >> ss_oms_call.AccountNumber >>
>                         ss_oms_call.ORDERTYPE >> ss_oms_call.Symbol >> ss_oms_call.Volume >> ss_oms_call.Price >>
>                         ss_oms_call.Buy_Sell;
> 
>         }
>         std::cout << "The Unique ID is .."<< filter <<ss_oms_call.UniqueID<< std::endl;
>         std::cout << "The Type of Account is .."<< filter <<ss_oms_call.ProClientIndicator<< std::endl;
> 
>         std::cout << "The AccountNumber is .."<< filter <<ss_oms_call.AccountNumber<< std::endl;
> 
>         std::cout << "The ORDERTYPE is .."<< filter <<ss_oms_call.ORDERTYPE<< std::endl;
> 
>         std::cout << "The Symbol is .."<< filter <<ss_oms_call.Symbol<< std::endl;
> 
>         std::cout << "The Volume is .."<< filter <<ss_oms_call.Volume<< std::endl;
> 
>         std::cout << "The Price is .."<< filter <<ss_oms_call.Price<< std::endl;
>         std::cout << "The Buy or Sell is .."<< filter <<ss_oms_call.Buy_Sell<< std::endl;
> 
>         return 0;
> }
> 
> int Test::SSOMSPub () {
>         zmq::context_t context (1);
>         zmq::socket_t pubs (context, ZMQ_PUB);
> 
>         pubs.bind("tcp://*:5556");
>         pubs.bind("ipc://ssoms.ipc");
> 
>         while (1) {
>                 ss_oms_call.UniqueID = 10001;
>                 ss_oms_call.ProClientIndicator = 1;
> 
>                 ss_oms_call.AccountNumber = '4';
> 
>                 ss_oms_call.ORDERTYPE = 0x0000;
> 
>                 ss_oms_call.Symbol = 0x4e;
> 
>                 ss_oms_call.Volume = 50;
> 
>                 ss_oms_call.Price = 625000;
>                 ss_oms_call.Buy_Sell = 2;
> 
>                 zmq::message_t message(1000);
> 
>                 snprintf ((char *) message.data(), 1000 , "%d %hu %x %hu %x %d %d %hu ", ss_oms_call.UniqueID,
>                 ss_oms_call.ProClientIndicator, ss_oms_call.AccountNumber, ss_oms_call.ORDERTYPE, ss_oms_call.Symbol,
>                 ss_oms_call.Volume, ss_oms_call.Price, ss_oms_call.Buy_Sell);
> 
>                 pubs.send(message);
>         }
>         return 0;
> }
> ----
> 
> and driver file (test2.cpp) -
> 
> ---
> #include <iostream>
> 
> #include "test1.hpp"
> 
> int main () {
> 
>         Test tst;
>         tst.SSOMSSub ();
>         tst.SSOMSPub ();
> 
> return 0;
> }
> --
> 
> It's execution hangs as -
> $ ./test2
> Collecting updates from Strategy Server.
> 
> --
> 
> Any clue. 
> 
> Thanks!!
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev


-----------------------
Andrew Hume
949-707-1964 (VO and best)
732-420-0907 (NJ)
andrew at research.att.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20131210/349cc5fb/attachment.htm>


More information about the zeromq-dev mailing list