function client_thread($self) { $context = new ZMQContext(); $client = new ZMQSocket($context, ZMQ::SOCKET_REQ); $endpoint = sprintf("ipc://%s-localfe.ipc", $self); $client->connect($endpoint); $monitor = new ZMQSocket($context, ZMQ::SOCKET_PUSH); $endpoint = sprintf("ipc://%s-monitor.ipc", $self); $monitor->connect($endpoint); $readable = $writeable = array(); while (true) { sleep(mt_rand(0, 4)); $burst = mt_rand(1, 14); while ($burst--) { // Send request with random hex ID $task_id = sprintf("%04X", mt_rand(0, 10000)); $client->send($task_id); // Wait max ten seconds for a reply, then complain $poll = new ZMQPoll(); $poll->add($client, ZMQ::POLL_IN); $events = $poll->poll($readable, $writeable, 10 * 1000000); if ($events > 0) { foreach ($readable as $socket) { $zmsg = new Zmsg($socket); $zmsg->recv(); // Worker is supposed to answer us with our task id assert($zmsg->body() == $task_id); } } else { $monitor->send(sprintf("E: CLIENT EXIT - lost task %s", $task_id)); exit; } } } }
/** * @return void */ public function connect() { $this->context = new \ZMQContext(1); $this->socket = new \ZMQSocket($this->context, \ZMQ::SOCKET_PUSH); $this->logger->info("Connecting to server inbound queue on ZMQ '{$this->getZmq()}'."); $this->socket->connect($this->getZmq()); }
public function __construct(CoffeeService $service) { $this->service = $service; $context = new ZMQContext(); $this->socket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'pusher'); $this->socket->connect('tcp://127.0.0.1:4444'); }
/** * Connect to ZMQ and set socket * * @return ZMQ $socket instance */ protected function connectToZMQ() { $context = \App::make('ZMQContext'); $this->socket = $context->getSocket(\ZMQ::SOCKET_PUSH, \Config::get('larapush::persistent_socket_name')); $this->socket->connect($this->getPusherConnect()); return $this->socket; }
/** * @return \ZMQSocket */ private function getSocket() { if (null == $this->pushSocket) { $this->pushSocket = $this->context->getSocket(\ZMQ::SOCKET_PUSH, 'my pusher'); $this->pushSocket->connect("tcp://localhost:5555"); } return $this->pushSocket; }
/** * Connects to the given endpoint */ public function connect() { $this->context = new \ZMQContext(); $this->socket = $this->context->getSocket(\ZMQ::SOCKET_PULL); if ($this->isDebug()) { ConsoleLog::log("Connecting to zmq at '{$this->getZmq()}'"); } $this->socket->connect($this->getZmq()); }
/** * @param string|array $message * @param int $mode */ public function send($message, $mode = 0) { if (false === $this->connected) { $connectedTo = $this->socket->getEndpoints(); if (!in_array($this->dsn, $connectedTo)) { $this->socket->connect($this->dsn); } $this->connected = true; } $this->socket->send($message, $mode); }
private function connect($publisherEndpoint) { if ($this->frontedSocket) { $this->poll->remove($this->frontedSocket); unset($this->frontedSocket); } $this->frontedSocket = $this->createSocket(\ZMQ::SOCKET_SUB, [\ZMQ::SOCKOPT_LINGER => 0, \ZMQ::SOCKOPT_SUBSCRIBE => ""], true); $this->frontedSocket->connect($publisherEndpoint); $this->poll->add($this->frontedSocket, \ZMQ::POLL_IN); if ($this->verbose) { printf("I: Connecting to publisher at %s... %s", $publisherEndpoint, PHP_EOL); } }
/** * Connects or reconnect to broker * * @return AsynClient */ public function connect() { if ($this->client) { unset($this->client); } $this->client = new ZMQSocket($this->context, ZMQ::SOCKET_DEALER); $this->client->setSockOpt(ZMQ::SOCKOPT_LINGER, 0); $this->client->connect($this->broker); if ($this->verbose) { $this->log("ZMQDEBUG", "connecting to broker at %s...", $this->broker); } return $this; }
/** * @param $context * @param $endpoint * @return Zmsg */ private function createPublisher($context, $endpoint) { $publisher = new \ZMQSocket($context, \ZMQ::SOCKET_PUB); $publisher->setSockOpt(\ZMQ::SOCKOPT_SNDHWM, 1); $publisher->connect($endpoint); return new Zmsg($publisher); }
public function run(OutputInterface $output) { $context = new \ZMQContext(); $tasksQueue = new \ZMQSocket($context, \ZMQ::SOCKET_PULL); $tasksQueue->connect(Spider::ZMQ_TASKS_QUEUE_DSN); $resultsQueue = new \ZMQSocket($context, \ZMQ::SOCKET_PUSH); $resultsQueue->connect(Spider::ZMQ_RESULTS_QUEUE_DSN); $output->writeln('HTTP Worker is waiting for tasks'); while (true) { $string = $tasksQueue->recv(); if ($string === Spider::ZMQ_COMMAND_BATCH_START) { $resultsQueue->send($string); } elseif (stripos($string, Spider::ZMQ_COMMAND_BATCH_END) !== false) { // send info for result collector how many results it should expect $resultsQueue->send($string); } elseif ($string === Spider::ZMQ_COMMAND_WORKER_QUIT) { $output->writeln('No more work. Worker stops now.'); break; // no more work } else { $output->writeln('Fetching data from URI: ' . $string); $userData = file_get_contents($string); // TODO: use Guzzle $output->writeln('Sending result for URI: ' . $string); $resultsQueue->send($userData); } } }
public function mainAction() { echo "Madserver\n"; $context = new ZMQContext(); $sub_socket = new ZMQSocket($context, ZMQ::SOCKET_SUB); $sub_socket->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, ""); $sub_socket->setSockOpt(ZMQ::SOCKOPT_TCP_KEEPALIVE, 0); $sub_socket->setSockOpt(ZMQ::SOCKOPT_TCP_KEEPALIVE_IDLE, 10); $sub_socket->setSockOpt(ZMQ::SOCKOPT_TCP_KEEPALIVE_CNT, 2); $sub_socket->setSockOpt(ZMQ::SOCKOPT_TCP_KEEPALIVE_INTVL, 5); $sub_socket->connect("tcp://127.0.0.1:5557"); $snapshot = new ZMQSocket($context, ZMQ::SOCKET_DEALER); $snapshot->connect("tcp://127.0.0.1:5556"); $sequence = 1; $snapshot->sendmulti(array("ICANHAZ?", self::SUBTREE), ZMQ::MODE_SNDMORE); while (1) { $kvmsg = new Zmq_Kvmsg($sequence); $kvmsg->recv($snapshot); if ($kvmsg->key() == "KTHXBAI") { echo "I: received snapshot=" . $kvmsg->sequence() . "\n"; break; } } while (1) { $kvmsg = new Zmq_Kvmsg($sequence); $kvmsg->recv($sub_socket); echo "I: start receive sub=" . $kvmsg->sequence() . "\n"; $kvmsg->dump(); } return false; }
/** * Connect to socket * * @return ZMQSocket instance */ protected function connectZmq() { $context = new \ZMQContext(); $this->socket = $context->getSocket(\ZMQ::SOCKET_PUSH, \Config::get('latchet::socketPushId', sprintf('latchet.push.%s', \App::environment()))); $this->socket->connect("tcp://localhost:" . \Config::get('latchet::zmqPort')); return $this->socket; }
/** * @param LoopInterface $loop */ public function initZmq(LoopInterface $loop) { /** @var \ZMQContext $context */ $context = new \React\ZMQ\Context($loop); $this->listener = $context->getSocket(\ZMQ::SOCKET_PUSH); $this->listener->connect('tcp://127.0.0.1:5559'); $this->receiver = $context->getSocket(\ZMQ::SOCKET_PULL); $this->receiver->connect('tcp://127.0.0.1:5557'); $this->receiver->on('message', function ($msg) { $message = json_decode($msg, true); if (is_array($message) && isset($message['outputs']) && is_array($message['outputs'])) { $cumulative = array(); foreach ($message['outputs'] as $output) { $script = pack("H*", $output['script']); if (!isset($cumulative[$script])) { $cumulative[$script] = $output['value']; } else { $cumulative[$script] += $output['value']; } } $message['requirements'] = $cumulative; $this->contracts[$message['slug']] = $message; echo "New contract: " . $msg . "\n"; } }); }
protected function setupPushSocket($queueId) { $connect = true; if (!empty($this->push)) { $endpoints = $this->push->getendpoints(); if (!empty($endpoints["connect"][0]) && $endpoints["connect"][0] != $queueId) { $this->push->disconnect($endpoints["connect"][0]); } else { $connect = false; } } else { $this->push = $this->socketFactory->createPushSocket(); } if ($connect) { $this->push->connect($queueId); } }
/** * @param $context * @param $endpoint * @return Zmsg */ private function createSubscriber($context, $endpoint) { $receiver = new \ZMQSocket($context, \ZMQ::SOCKET_SUB); $receiver->setSockOpt(\ZMQ::SOCKOPT_LINGER, 0); $receiver->setSockOpt(\ZMQ::SOCKOPT_SUBSCRIBE, ""); $receiver->connect($endpoint); return new Zmsg($receiver); }
/** * @return null */ protected function initOrCheckPushConnection() { if (!$this->pushSocket) { $this->pushSocket = $this->context->getSocket(\ZMQ::SOCKET_PUSH); $this->pushSocket->connect($this->getSocketsParams()->getPushPulsarSocketAddress()); } return null; }
/** * @return \ZMQSocket */ public function connect() { $context = new \ZMQContext(); $socket = new \ZMQSocket($context, \ZMQ::SOCKET_PUB); $socket->connect($this->dsn()); // @need some sleep at :-( usleep(500); return $socket; }
function client_socket(ZMQContext $context) { echo "I: connecting to server...", PHP_EOL; $client = new ZMQSocket($context, ZMQ::SOCKET_REQ); $client->connect("tcp://localhost:5555"); // Configure socket to not wait at close time $client->setSockOpt(ZMQ::SOCKOPT_LINGER, 0); return $client; }
function client_thread() { $context = new ZMQContext(); $client = new ZMQSocket($context, ZMQ::SOCKET_REQ); $client->connect("ipc://frontend.ipc"); // Send request, get reply $client->send("HELLO"); $reply = $client->recv(); printf("Client: %s%s", $reply, PHP_EOL); }
/** * Returns client with newly created ZeroMQ Socket * * @param array $servers list of addresses of servers * @param integer $timeout * @return ZeroMQClientTransport */ public static function create($servers, $timeout = 1) { $socket = new \ZMQSocket(new \ZMQContext(), \ZMQ::SOCKET_REQ); $socket->setSockOpt(\ZMQ::SOCKOPT_LINGER, 0); foreach ((array) $servers as $server) { $socket->connect($server); } $client = new self($socket); $client->setTimeout($timeout); return $client; }
function url_hash_worker() { // Socket to talk to dispatcher $context = new ZMQContext(); $receiver = new ZMQSocket($context, ZMQ::SOCKET_REP); $receiver->connect("ipc://urlhash.ipc"); while (true) { $hashedUrl = $receiver->recv(); $receiver->send(hashDecode($hashedUrl)); } }
/** * @return null */ public function startCommunication() { $this->initLoop(); $this->context = new \ZMQContext(); $this->pulsarRequestSocket = $this->context->getSocket(\ZMQ::SOCKET_REQ); $this->performersReplySocket = $this->context->getSocket(\ZMQ::SOCKET_REP); $this->initStreams(); $replyStackErrorDtoAlreadySent = false; /** * Receive sockets params from Pulsar and start cyclical communication */ $this->readStream->on(EventsConstants::DATA, function ($data) use($replyStackErrorDtoAlreadySent) { $replyStackDto = null; $replyStackDto = @unserialize($data); if ($replyStackDto !== false && $replyStackDto instanceof ReplyStackDto) { $this->pulsarRequestSocket->connect($replyStackDto->getReplyStackVsPulsarSocketAddress()); $this->performersReplySocket->bind($replyStackDto->getReplyStackVsPerformersSocketAddress()); $this->moduleDto = $replyStackDto; $initDto = new InitStartMethodDto(); $initDto->setShutDownArg('warning'); $this->initStartMethods($initDto); //TODO: make resolver of ways of ReplyStack logging //$this->logger->debug("ReplyStack receive initDto from Pulsar."); $this->loop->nextTick([$this, 'startStackWork']); } else { if ($replyStackErrorDtoAlreadySent === false) { $replyStackErrorDtoAlreadySent = true; $replyStackError = new ReplyStackErrorDto(); $replyStackError->setErrorLevel(ErrorsConstants::CRITICAL); $replyStackError->setErrorReason(PulsarErrorConstants::REPLY_STACK_RECEIVE_NOT_CORRECT_DTO); //write to Pulsar's allotted STDIN about critical error $this->writeStream->write(serialize($replyStackError)); $this->loop->nextTick(function () { $this->loop->stop(); }); } } }); $this->loop->run(); return null; }
function worker_task() { $context = new ZMQContext(); $worker = new ZMQSocket($context, ZMQ::SOCKET_DEALER); $worker->setSockOpt(ZMQ::SOCKOPT_IDENTITY, "W"); $worker->connect("tcp://localhost:5556"); while (true) { $zmsg = new Zmsg($worker); $zmsg->recv(); $zmsg->send(); } }
public function testComplex() { $context = new \ZMQContext(); $output = new \ZMQSocket($context, \ZMQ::SOCKET_DEALER); $output->bind("inproc://zmsg_selftest"); $input = new \ZMQSocket($context, \ZMQ::SOCKET_ROUTER); $input->connect("inproc://zmsg_selftest"); // Test send and receive of single-part message $zmsgo = new Zmsg($output); $zmsgo->setLast("Hello"); $this->assertTrue($zmsgo->getLast() == "Hello"); $zmsgo->send(); $zmsgi = new Zmsg($input); $zmsgi->recv(); $this->assertTrue($zmsgi->parts() == 2); $this->assertTrue($zmsgi->getLast() == "Hello"); // Test send and receive of multi-part message $zmsgo = new Zmsg($output); $zmsgo->setLast("Hello"); $zmsgo->wrap("address1", ""); $zmsgo->wrap("address2"); $this->assertTrue($zmsgo->parts() == 4); $zmsgo->send(); $zmsgi = new Zmsg($input); $zmsgi->recv(); $this->assertTrue($zmsgi->parts() == 5); $zmsgi->unwrap(); $this->assertTrue($zmsgi->unwrap() == "address2"); $zmsgi->setLast(sprintf("%s%s", 'W', "orld")); $this->assertTrue($zmsgi->getLast() == "World"); // Pull off address 1, check that empty part was dropped $zmsgi->unwrap(); $this->assertTrue($zmsgi->parts() == 1); // Check that message body was correctly modified $part = $zmsgi->pop(); $this->assertTrue($part == "World"); $this->assertTrue($zmsgi->parts() == 0); // Test load and save $zmsg = new Zmsg(); $zmsg->setLast("Hello"); $zmsg->wrap("address1", ""); $zmsg->wrap("address2"); $this->assertTrue($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->getLast() == $zmsg->getLast()); fclose($fh); $this->assertTrue($zmsg2->parts() == 4); }
/** * 构造函数 * * @params $context ZMQContext * @params $endpoints 套接字链接到的端点 */ public function __construct($context, $endpoints) { if (APS::get_instance()->get_zmq_enabled()) { $socket = new ZMQSocket($context, ZMQ::SOCKET_XREQ); $socket->setsockopt(ZMQ::SOCKOPT_LINGER, 0); $socket->setsockopt(ZMQ::SOCKOPT_HWM, 1000); foreach ($endpoints as $endpoint) { $socket->connect($endpoint); } self::$sockets[] = $socket; $this->socket = $socket; } }
function client_thread($self) { $context = new ZMQContext(); $client = new ZMQSocket($context, ZMQ::SOCKET_REQ); $endpoint = sprintf("ipc://%s-localfe.ipc", $self); $client->connect($endpoint); while (true) { // Send request, get reply $client->send("HELLO"); $reply = $client->recv(); printf("I: client status: %s%s", $reply, PHP_EOL); } }
/** * Connects or reconnect to broker * * @return Worker */ public function connect() { $this->worker = new ZMQSocket($this->ctx, ZMQ::SOCKET_DEALER); $this->worker->connect($this->broker); if ($this->verbose) { $this->log("ZMQDEBUG", "connecting to broker at %s...", $this->broker); } // Register service with broker $this->send(Mdp::WORKER_READY, $this->service, null); // If liveness hits zero, queue is considered disconnected $this->liveness = self::HEARTBEAT_LIVENESS; $this->updateHeartbeatExpiry(); return $this; }
function __call($name, $arguments) { $context = new \ZMQContext(); $zmq = new \ZMQSocket($context, \ZMQ::SOCKET_REQ); $zmq->connect($this->socket); $message = array('jsonrpc' => '2.0', 'method' => $name, 'params' => $arguments, 'id' => round(microtime(true) * 100000)); /** @var \ZMQSocket $resultSocket */ $zmq->send(json_encode($message)); $result = json_decode($zmq->recv(), true); if (isset($result['error'])) { throw new RpcException($result['exception']['message'], $result['exception']['code']); } return $result['result']; }
function s_worker_socket($context) { $worker = new ZMQSocket($context, ZMQ::SOCKET_DEALER); // Set random identity to make tracing easier $identity = sprintf("%04X-%04X", rand(0, 0x10000), rand(0, 0x10000)); $worker->setSockOpt(ZMQ::SOCKOPT_IDENTITY, $identity); $worker->connect("tcp://localhost:5556"); // Configure socket to not wait at close time $worker->setSockOpt(ZMQ::SOCKOPT_LINGER, 0); // Tell queue we're ready for work printf("I: (%s) worker ready%s", $identity, PHP_EOL); $worker->send("READY"); return array($worker, $identity); }