olson

mdp client

Sep 21st, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.34 KB | None | 0 0
  1. use strict;
  2. use AnyEvent;
  3. use ZeroMQ ':all';
  4.  
  5. use constant MDP_CLIENT_PROTO => "MDPC02";
  6. use constant MDP_WORKER_PROTO => "MDPW02";
  7.  
  8. use constant MDP_READY => "\x01";
  9. use constant MDP_REQUEST => "\x02";
  10. use constant MDP_PARTIAL => "\x03";
  11. use constant MDP_FINAL => "\x04";
  12. use constant MDP_HEARTBEAT => "\x05";
  13. use constant MDP_DISCONNECT => "\x06";
  14.  
  15. my $service = "test_svc";
  16.  
  17. my $ctx = ZeroMQ::Context->new();
  18. my $sock = $ctx->socket(ZMQ_DEALER);
  19. $sock->connect("tcp://localhost:5110");
  20.  
  21. my $async = 1;
  22. my($cv, $w);
  23.  
  24. if ($async)
  25. {
  26.     my $fd = $sock->getsockopt(ZMQ_FD);
  27.     print "set up wait on fd=$fd\n";
  28.     $cv = AE::cv;
  29.     $w = AE::io $fd, 0, sub { print "Read ready\n"; $cv->send() };
  30. }
  31.  
  32. $sock->send(MDP_CLIENT_PROTO, ZMQ_SNDMORE);
  33. $sock->send(MDP_REQUEST, ZMQ_SNDMORE);
  34. $sock->send($service, ZMQ_SNDMORE);
  35. $sock->send("1", ZMQ_SNDMORE);
  36. $sock->send("hello world");
  37.  
  38. if ($async)
  39. {
  40.     $cv->recv;
  41.     do_receive($sock);
  42. }
  43. else
  44. {
  45.     do_receive($sock);
  46. }
  47.  
  48. sub do_receive
  49. {
  50.     my($sock) = @_;
  51.     my $msg = $sock->recv();
  52.     my $d = $msg->data;
  53.     $d =~ s/([\x00-\x1f])/sprintf("0x%02x", ord($1))/ge;
  54.     print "Rcv: $d\n";
  55.     while ($sock->getsockopt(ZMQ_RCVMORE))
  56.     {
  57.     $msg = $sock->recv();
  58.     my $d = $msg->data;
  59.     $d =~ s/([\x00-\x1f])/sprintf("0x%02x", ord($1))/ge;
  60.     print "Rcv: $d\n";
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment