ExpandCollapse

+ 1 rrbroker.flx

  //
  //  Simple request-reply broker
  //
  //  Prepare our context and sockets
  open ZMQ;
  
  var context = zmq_init 1;
  var frontend = context.mk_socket ZMQ_ROUTER;
  var backend  = context.mk_socket ZMQ_DEALER;
  frontend.bind "tcp://*:5559";
  backend.bind "tcp://*:5560";
  
  //  Initialize poll set
  var items = varray(
    zmq_poll_item (frontend, ZMQ_POLLIN),
    zmq_poll_item (backend, ZMQ_POLLIN));
  
  //  Switch messages between sockets
  while true do
    C_hack::ignore$ zmq_poll (items, -1.0);
    if (items.0.revents \& ZMQ_POLLIN).short != 0s do
      part:while true do 
        //  Process all parts of the message
        var s = frontend.recv_string;
        match frontend.get_opt ZMQ_RCVMORE with
        | zmq_rcvmore more =>
          if more do 
            backend.send_string_part s;
          else 
            backend.send_string s; 
            break part; 
          done
        endmatch;
      done
    done
    if (items.1.revents \& ZMQ_POLLIN).short != 0s do
      part2:while true do 
        //  Process all parts of the message
        s = frontend.recv_string;
        match frontend.get_opt ZMQ_RCVMORE with
        | zmq_rcvmore more =>
          if more do 
            frontend.send_string_part s;
          else 
            frontend.send_string s; 
            break part2; 
          done
        endmatch;
      done
    done
  done