コード例 #1
9
ファイル: peering3.php プロジェクト: nivertech/zguide
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;
            }
        }
    }
}
コード例 #2
0
ファイル: Client.php プロジェクト: awdn/vigilant-queue
 /**
  * @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());
 }
コード例 #3
0
 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');
 }
コード例 #4
0
 /**
  * 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;
 }
コード例 #5
0
 /**
  * @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;
 }
コード例 #6
0
ファイル: Client.php プロジェクト: awdn/vigilant-queue
 /**
  * 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());
 }
コード例 #7
0
 /**
  * @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);
 }
コード例 #8
0
ファイル: FastBox.php プロジェクト: limitium/zmq
 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);
     }
 }
コード例 #9
0
ファイル: AsynClient.php プロジェクト: mheydt/scalr
 /**
  * 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;
 }
コード例 #10
0
ファイル: ConcentratorTest.php プロジェクト: limitium/zmq
 /**
  * @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);
 }
コード例 #11
0
 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);
         }
     }
 }
コード例 #12
0
ファイル: Madserver.php プロジェクト: zhangjingpu/yaf-lib
 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;
 }
コード例 #13
0
ファイル: Latchet.php プロジェクト: vladski/Latchet
 /**
  * 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;
 }
コード例 #14
0
 /**
  * @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";
         }
     });
 }
コード例 #15
0
ファイル: Queue.php プロジェクト: silktide/queueball-zmq
 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);
     }
 }
コード例 #16
0
ファイル: PublisherTest.php プロジェクト: limitium/zmq
 /**
  * @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);
 }
コード例 #17
0
ファイル: Performer.php プロジェクト: jamset/publisher-pulsar
 /**
  * @return null
  */
 protected function initOrCheckPushConnection()
 {
     if (!$this->pushSocket) {
         $this->pushSocket = $this->context->getSocket(\ZMQ::SOCKET_PUSH);
         $this->pushSocket->connect($this->getSocketsParams()->getPushPulsarSocketAddress());
     }
     return null;
 }
コード例 #18
0
ファイル: ZmqPublish.php プロジェクト: pelim/laravel-zmq
 /**
  * @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;
 }
コード例 #19
0
ファイル: lpclient.php プロジェクト: nivertech/zguide
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;
}
コード例 #20
0
ファイル: lbbroker.php プロジェクト: inactivist/zguide
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);
}
コード例 #21
0
 /**
  * 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;
 }
コード例 #22
0
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));
    }
}
コード例 #23
0
 /**
  * @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;
 }
コード例 #24
0
ファイル: tripping.php プロジェクト: inactivist/zguide
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();
    }
}
コード例 #25
0
ファイル: ZmsgTest.php プロジェクト: mheydt/scalr
 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);
 }
コード例 #26
0
ファイル: Client.php プロジェクト: emilymwang8/ajk-broker
 /**
  * 构造函数
  *
  * @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;
     }
 }
コード例 #27
0
ファイル: peering2.php プロジェクト: inactivist/zguide
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);
    }
}
コード例 #28
0
ファイル: Worker.php プロジェクト: sacredwebsite/scalr
 /**
  * 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;
 }
コード例 #29
0
ファイル: Client.php プロジェクト: picur/php-json-rpc
 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'];
 }
コード例 #30
0
ファイル: ppworker.php プロジェクト: inactivist/zguide
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);
}