public function send($data) { $msg = new Zmsg($this->socket); $msg->push($data); $msg->wrap(sprintf("%.0f", microtime(1) * 1000)); $msg->send(true); }
} $reroutable = false; $events = $poll->poll($readable, $writeable, 0); if ($events > 0) { foreach ($readable as $socket) { $zmsg = new Zmsg($socket); $zmsg->recv(); if ($local_capacity) { $zmsg->wrap(array_shift($worker_queue), ""); $zmsg->set_socket($localbe)->send(); $local_capacity--; } else { // Route to random broker peer printf("I: route request %s to cloud...%s", $zmsg->body(), PHP_EOL); $zmsg->wrap($_SERVER['argv'][mt_rand(2, $_SERVER['argc'] - 1)]); $zmsg->set_socket($cloudbe)->send(); } } } else { break; // No work, go back to backends } } if ($local_capacity != $previous) { // Broadcast new capacity $zmsg = new Zmsg($statebe); $zmsg->body_set($local_capacity); // We stick our own address onto the envelope $zmsg->wrap($self)->send(); } }
$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 }
/** * 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; }
/** * 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; } } }
/** * Process a request coming from a client * * @param string $sender * @param Zmsg $msg */ public function client_process($sender, $msg) { $service_frame = $msg->pop(); $service = $this->service_require($service_frame); // Set reply return address to client sender $msg->wrap($sender, ""); if (substr($service_frame, 0, 4) == 'mmi.') { $this->service_internal($service_frame, $msg); } else { $this->service_dispatch($service, $msg); } }
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); }
$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); } } else { // Now get next client request, route to next worker $identity = $queue->s_worker_dequeue(); $zmsg->wrap($identity); $zmsg->set_socket($backend)->send(); } } if (microtime(true) > $heartbeat_at) { foreach ($queue as $id => $expiry) { $zmsg = new Zmsg($backend); $zmsg->body_set("HEARTBEAT"); $zmsg->wrap($identity, NULL); $zmsg->send(); } $heartbeat_at = microtime(true) + HEARTBEAT_INTERVAL; } $queue->s_queue_purge(); } }
$poll->add($localfe, ZMQ::POLL_IN); $poll->add($cloudfe, ZMQ::POLL_IN); $reroutable = false; $events = $poll->poll($readable, $writeable, 0); if ($events > 0) { foreach ($readable as $socket) { $zmsg = new Zmsg($socket); $zmsg->recv(); // We'll do peer brokers first, to prevent starvation if ($socket === $cloudfe) { $reroutable = false; } elseif ($socket === $localfe) { $reroutable = true; } // If reroutable, send to cloud 20% of the time // Here we'd normally use cloud status information if ($reroutable && $_SERVER['argc'] > 2 && mt_rand(0, 4) == 0) { $zmsg->wrap($_SERVER['argv'][mt_rand(2, $_SERVER['argc'] - 1)]); $zmsg->set_socket($cloudbe)->send(); } else { $zmsg->wrap(array_shift($worker_queue), ""); $zmsg->set_socket($localbe)->send(); $capacity--; } } } else { break; // No work, go back to backends } } }