Exemple #1
0
 protected function onPoll($events, $read, $write)
 {
     parent::onPoll($events, $read, $write);
     if ($events > 0) {
         foreach ($read as $socket) {
             //handle publisher
             if ($socket === $this->frontedSocket) {
                 $zmsg = new Zmsg($this->frontedSocket);
                 $zmsg->recv();
                 if ($this->verbose) {
                     echo "I: received message from publisher size: ";
                     echo strlen($zmsg->__toString()), PHP_EOL;
                 }
                 $zmsg->unwrap();
                 //time
                 if ($this->queueLimit > sizeof($this->queue)) {
                     array_unshift($this->queue, $zmsg->pop());
                 }
             }
         }
     }
 }
Exemple #2
0
 /**
  * Start listen for messages in loop
  * @throws \Exception
  */
 public function listen()
 {
     if (!$this->listener) {
         throw new \Exception("Empty listener");
     }
     $this->isListen = true;
     while ($this->isListen) {
         $zmsg = new Zmsg($this->socket);
         $zmsg->recv();
         if ($this->verbose) {
             echo "I: received message from broker:", PHP_EOL;
             echo $zmsg->__toString(), PHP_EOL;
         }
         $time = $zmsg->unwrap();
         if (!$this->normalDelay) {
             $this->normalDelay = microtime(true) * 1000 - (double) $time;
         }
         $delayTime = microtime(true) * 1000 - (double) $time;
         if ($this->misser && $delayTime > $this->normalDelay + $this->maxAllowedDelay) {
             call_user_func($this->misser, $zmsg->pop(), $time, $delayTime);
         }
         call_user_func($this->listener, $zmsg->pop(), $time);
     }
 }
Exemple #3
0
 $events = 0;
 //  If we have no workers anyhow, wait indefinitely
 try {
     $events = $poll->poll($readable, $writeable, $local_capacity ? 1000000 : -1);
 } catch (ZMQPollException $e) {
     break;
 }
 //  Track if capacity changes during this iteration
 $previous = $local_capacity;
 foreach ($readable as $socket) {
     $zmsg = new Zmsg($socket);
     //  Handle reply from local worker
     if ($socket === $localbe) {
         //  Use worker address for LRU routing
         $zmsg->recv();
         $worker_queue[] = $zmsg->unwrap();
         $local_capacity++;
         if ($zmsg->body() == "READY") {
             $zmsg = null;
             //  Don't route it
         }
     } else {
         if ($socket === $cloudbe) {
             //  We don't use peer broker address for anything
             $zmsg->recv()->unwrap();
         } else {
             if ($socket === $statefe) {
                 $zmsg->recv();
                 $cloud_capacity = $zmsg->body();
                 $zmsg = null;
             } else {
Exemple #4
0
while (true) {
    $poll = new ZMQPoll();
    $poll->add($backend, ZMQ::POLL_IN);
    //  Poll frontend only if we have available workers
    if ($available_workers) {
        $poll->add($frontend, ZMQ::POLL_IN);
    }
    $events = $poll->poll($read, $write);
    foreach ($read as $socket) {
        $zmsg = new Zmsg($socket);
        $zmsg->recv();
        //  Handle worker activity on backend
        if ($socket === $backend) {
            //  Use worker address for LRU routing
            assert($available_workers < MAX_WORKERS);
            array_push($worker_queue, $zmsg->unwrap());
            $available_workers++;
            //  Return reply to client if it's not a READY
            if ($zmsg->address() != "READY") {
                $zmsg->set_socket($frontend)->send();
            }
        } elseif ($socket === $frontend) {
            //  Now get next client request, route to next worker
            //  REQ socket in worker needs an envelope delimiter
            //  Dequeue and drop the next worker address
            $zmsg->wrap(array_shift($worker_queue), "");
            $zmsg->set_socket($backend)->send();
            $available_workers--;
        }
    }
    //  We never exit the main loop
Exemple #5
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;
 }
Exemple #6
0
 /**
  * Send reply, if any, to broker and wait for next request.
  *
  * @param Zmsg $reply 
  * @return Zmsg Returns if there is a request to process
  */
 public function recv($reply = null)
 {
     //  Format and send the reply if we were provided one
     assert($reply || !$this->expect_reply);
     if ($reply) {
         $reply->wrap($this->reply_to);
         $this->send_to_broker(MDPW_REPLY, NULL, $reply);
     }
     $this->expect_reply = true;
     $read = $write = array();
     while (true) {
         $poll = new ZMQPoll();
         $poll->add($this->worker, ZMQ::POLL_IN);
         $events = $poll->poll($read, $write, $this->heartbeat);
         if ($events) {
             $zmsg = new Zmsg($this->worker);
             $zmsg->recv();
             if ($this->verbose) {
                 echo "I: received message from broker:", PHP_EOL;
                 echo $zmsg->__toString();
             }
             $this->liveness = HEARTBEAT_LIVENESS;
             //  Don't try to handle errors, just assert noisily
             assert($zmsg->parts() >= 3);
             $zmsg->pop();
             $header = $zmsg->pop();
             assert($header == MDPW_WORKER);
             $command = $zmsg->pop();
             if ($command == MDPW_REQUEST) {
                 //  We should pop and save as many addresses as there are
                 //  up to a null part, but for now, just save one...
                 $this->reply_to = $zmsg->unwrap();
                 return $zmsg;
                 //  We have a request to process
             } else {
                 if ($command == MDPW_HEARTBEAT) {
                     // Do nothing for heartbeats
                 } else {
                     if ($command == MDPW_DISCONNECT) {
                         $this->connect_to_broker();
                     } else {
                         echo "E: invalid input message", PHP_EOL;
                         echo $zmsg->__toString();
                     }
                 }
             }
         } else {
             if (--$this->liveness == 0) {
                 // poll ended on timeout, $event being false
                 if ($this->verbose) {
                     echo "W: disconnected from broker - retrying...", PHP_EOL;
                 }
                 usleep($this->reconnect * 1000);
                 $this->connect_to_broker();
             }
         }
         // Send HEARTBEAT if it's time
         if (microtime(true) > $this->heartbeat_at) {
             $this->send_to_broker(MDPW_HEARTBEAT, NULL, NULL);
             $this->heartbeat_at = microtime(true) + $this->heartbeat / 1000;
         }
     }
 }
Exemple #7
0
 /**
  * Process message sent to us by a worker
  *
  * @param string $sender
  * @param Zmsg $msg
  */
 public function worker_process($sender, $msg)
 {
     $command = $msg->pop();
     $worker_ready = isset($this->workers[$sender]);
     $worker = $this->worker_require($sender);
     if ($command == MDPW_READY) {
         if ($worker_ready) {
             $this->worker_delete($worker, true);
             //  Not first command in session
         } else {
             if (strlen($sender) >= 4 && substr($sender, 0, 4) == 'mmi.') {
                 $this->worker_delete($worker, true);
             } else {
                 //  Attach worker to service and mark as idle
                 $service_frame = $msg->pop();
                 $worker->service = $this->service_require($service_frame);
                 $worker->service->workers++;
                 $this->worker_waiting($worker);
             }
         }
     } else {
         if ($command == MDPW_REPLY) {
             if ($worker_ready) {
                 //  Remove & save client return envelope and insert the
                 //  protocol header and service name, then rewrap envelope.
                 $client = $msg->unwrap();
                 $msg->push($worker->service->name);
                 $msg->push(MDPC_CLIENT);
                 $msg->wrap($client, "");
                 $msg->set_socket($this->socket)->send();
                 $this->worker_waiting($worker);
             } else {
                 $this->worker_delete($worker, true);
             }
         } else {
             if ($command == MDPW_HEARTBEAT) {
                 if ($worker_ready) {
                     $worker->expiry = microtime(true) + HEARTBEAT_EXPIRY / 1000;
                 } else {
                     $this->worker_delete($worker, true);
                 }
             } else {
                 if ($command == MDPW_DISCONNECT) {
                     $this->worker_delete($worker, true);
                 } else {
                     echo "E: invalid input message", PHP_EOL, $msg->__toString();
                 }
             }
         }
     }
 }
Exemple #8
0
function main()
{
    for ($client_nbr = 0; $client_nbr < NBR_CLIENTS; $client_nbr++) {
        $pid = pcntl_fork();
        if ($pid == 0) {
            client_thread();
            return;
        }
    }
    for ($worker_nbr = 0; $worker_nbr < NBR_WORKERS; $worker_nbr++) {
        $pid = pcntl_fork();
        if ($pid == 0) {
            worker_thread();
            return;
        }
    }
    $context = new ZMQContext();
    $frontend = new ZMQSocket($context, ZMQ::SOCKET_ROUTER);
    $backend = new ZMQSocket($context, ZMQ::SOCKET_ROUTER);
    $frontend->bind("ipc://frontend.ipc");
    $backend->bind("ipc://backend.ipc");
    //  Logic of LRU loop
    //  - Poll backend always, frontend only if 1+ worker ready
    //  - If worker replies, queue worker as ready and forward reply
    //    to client if necessary
    //  - If client requests, pop next worker and send request to it
    //  Queue of available workers
    $available_workers = 0;
    $worker_queue = array();
    $writeable = $readable = array();
    while ($client_nbr > 0) {
        $poll = new ZMQPoll();
        //  Poll front-end only if we have available workers
        if ($available_workers > 0) {
            $poll->add($frontend, ZMQ::POLL_IN);
        }
        //  Always poll for worker activity on backend
        $poll->add($backend, ZMQ::POLL_IN);
        $events = $poll->poll($readable, $writeable);
        if ($events > 0) {
            foreach ($readable as $socket) {
                //  Handle worker activity on backend
                if ($socket === $backend) {
                    //  Queue worker address for LRU routing
                    $zmsg = new Zmsg($socket);
                    $zmsg->recv();
                    assert($available_workers < NBR_WORKERS);
                    $available_workers++;
                    array_push($worker_queue, $zmsg->unwrap());
                    if ($zmsg->body() != "READY") {
                        $zmsg->set_socket($frontend)->send();
                        // exit after all messages relayed
                        $client_nbr--;
                    }
                } else {
                    if ($socket === $frontend) {
                        $zmsg = new Zmsg($socket);
                        $zmsg->recv();
                        $zmsg->wrap(array_shift($worker_queue), "");
                        $zmsg->set_socket($backend)->send();
                        $available_workers--;
                    }
                }
            }
        }
    }
    // Clean up our worker processes
    foreach ($worker_queue as $worker) {
        $zmsg = new Zmsg($backend);
        $zmsg->body_set('END')->wrap($worker, "")->send();
    }
    sleep(1);
}
Exemple #9
0
while (true) {
    $poll = new ZMQPoll();
    $poll->add($backend, ZMQ::POLL_IN);
    //  Poll frontend only if we have available workers
    if ($queue->size()) {
        $poll->add($frontend, ZMQ::POLL_IN);
    }
    $events = $poll->poll($read, $write, HEARTBEAT_INTERVAL * 1000 * 1000);
    // microseconds
    if ($events > 0) {
        foreach ($read as $socket) {
            $zmsg = new Zmsg($socket);
            $zmsg->recv();
            //  Handle worker activity on backend
            if ($socket === $backend) {
                $identity = $zmsg->unwrap();
                //  Return reply to client if it's not a control message
                if ($zmsg->parts() == 1) {
                    if ($zmsg->address() == "READY") {
                        $queue->s_worker_delete($identity);
                        $queue->s_worker_append($identity);
                    } else {
                        if ($zmsg->address() == 'HEARTBEAT') {
                            $queue->s_worker_refresh($identity);
                        } else {
                            printf("E: invalid message from %s%s%s", $identity, PHP_EOL, $zmsg->__toString());
                        }
                    }
                } else {
                    $zmsg->set_socket($frontend)->send();
                    $queue->s_worker_append($identity);