Ejemplo n.º 1
0
function client_task()
{
    $context = new ZMQContext();
    $client = new ZMQSocket($context, ZMQ::SOCKET_DEALER);
    //  Generate printable identity for the client
    $identity = sprintf("%04X", rand(0, 0x10000));
    $client->setSockOpt(ZMQ::SOCKOPT_IDENTITY, $identity);
    $client->connect("tcp://localhost:5570");
    $read = $write = array();
    $poll = new ZMQPoll();
    $poll->add($client, ZMQ::POLL_IN);
    $request_nbr = 0;
    while (true) {
        //  Tick once per second, pulling in arriving messages
        for ($centitick = 0; $centitick < 100; $centitick++) {
            $events = $poll->poll($read, $write, 1000);
            $zmsg = new Zmsg($client);
            if ($events) {
                $zmsg->recv();
                printf("%s: %s%s", $identity, $zmsg->body(), PHP_EOL);
            }
        }
        $zmsg = new Zmsg($client);
        $zmsg->body_fmt("request #%d", ++$request_nbr)->send();
    }
}
Ejemplo n.º 2
0
function worker_thread($self)
{
    $context = new ZMQContext();
    $worker = $context->getSocket(ZMQ::SOCKET_REQ);
    $endpoint = sprintf("ipc://%s-localbe.ipc", $self);
    $worker->connect($endpoint);
    //  Tell broker we're ready for work
    $worker->send("READY");
    while (true) {
        $zmsg = new Zmsg($worker);
        $zmsg->recv();
        sleep(1);
        $zmsg->body_fmt("OK - %04x", mt_rand(0, 0x10000));
        $zmsg->send();
    }
}
Ejemplo n.º 3
0
 /**
  * Run a self test of the Zmsg class. 
  *
  * @return boolean
  * @todo See if assert returns
  */
 public static function test()
 {
     $result = true;
     $context = new ZMQContext();
     $output = new ZMQSocket($context, ZMQ::SOCKET_XREQ);
     $output->bind("inproc://zmsg_selftest");
     $input = new ZMQSocket($context, ZMQ::SOCKET_XREP);
     $input->connect("inproc://zmsg_selftest");
     //  Test send and receive of single-part message
     $zmsgo = new Zmsg($output);
     $zmsgo->body_set("Hello");
     $result &= assert($zmsgo->body() == "Hello");
     $zmsgo->send();
     $zmsgi = new Zmsg($input);
     $zmsgi->recv();
     $result &= assert($zmsgi->parts() == 2);
     $result &= assert($zmsgi->body() == "Hello");
     echo $zmsgi;
     //  Test send and receive of multi-part message
     $zmsgo = new Zmsg($output);
     $zmsgo->body_set("Hello");
     $zmsgo->wrap("address1", "");
     $zmsgo->wrap("address2");
     $result &= assert($zmsgo->parts() == 4);
     echo $zmsgo;
     $zmsgo->send();
     $zmsgi = new Zmsg($input);
     $zmsgi->recv();
     $result &= assert($zmsgi->parts() == 5);
     $zmsgi->unwrap();
     $result &= assert($zmsgi->unwrap() == "address2");
     $zmsgi->body_fmt("%s%s", 'W', "orld");
     $result &= assert($zmsgi->body() == "World");
     //  Pull off address 1, check that empty part was dropped
     $zmsgi->unwrap();
     $result &= assert($zmsgi->parts() == 1);
     //  Check that message body was correctly modified
     $part = $zmsgi->pop();
     $result &= assert($part == "World");
     $result &= assert($zmsgi->parts() == 0);
     // Test load and save
     $zmsg = new Zmsg();
     $zmsg->body_set("Hello");
     $zmsg->wrap("address1", "");
     $zmsg->wrap("address2");
     $result &= assert($zmsg->parts() == 4);
     $fh = fopen(sys_get_temp_dir() . "/zmsgtest.zmsg", 'w');
     $zmsg->save($fh);
     fclose($fh);
     $fh = fopen(sys_get_temp_dir() . "/zmsgtest.zmsg", 'r');
     $zmsg2 = new Zmsg();
     $zmsg2->load($fh);
     assert($zmsg2->last() == $zmsg->last());
     fclose($fh);
     $result &= assert($zmsg2->parts() == 4);
     echo $result ? "OK" : "FAIL", PHP_EOL;
     return $result;
 }