Ejemplo n.º 1
0
 /**
  *
  * @param TimerInterface $timer
  *
  * @return MessageInterface
  *
  * @throws TimeoutException
  */
 public function read(TimerInterface $timer = null)
 {
     $timer = $timer ?: $this->defaultTimer;
     $timeout = $timer->timeout();
     if (!$this->socket->hasEvents($timeout)) {
         throw new TimeoutException('Timeout: ' . $timeout . '.');
     }
     // Read the raw message.
     $msg = $this->socket->mrecv();
     // Dispatch the event.
     $event = $this->createEvent($msg);
     $this->dispatch(StreamInterface::MESSAGE, $event);
     // Dispatch the new message.
     return $event->getProtocolMessage();
 }
Ejemplo n.º 2
0
 /**
  *
  * @param Socket $socket
  *
  * @return Service
  * @throws RuntimeException
  */
 public function createStream(Socket $socket)
 {
     if ($socket->getSocketType() != ZMQ::SOCKET_REP) {
         throw new RuntimeException('Invalid socket type.');
     }
     $this->getPoller()->add($socket, ZMQ::POLL_IN);
     $stream = $socket->getStream();
     $callback = array($this, 'onWorkerMessage');
     $stream->addListener(StreamInterface::MESSAGE, function (MessageEvent $event) use($callback) {
         call_user_func($callback, $event->getProtocolMessage());
     });
     $this->stream = $stream;
     return $this;
 }
Ejemplo n.º 3
0
 /**
  *
  * @param string $manager
  *
  * @return Socket
  */
 protected function getSocket($manager)
 {
     $context = new ZMQContext();
     $socket = Socket::create($context, ZMQ::SOCKET_REQ, $this->logger);
     $socket->connect($manager);
     return $socket;
 }
Ejemplo n.º 4
0
 /**
  * Returns a socket for the given server.
  *
  * If the socket does not yet exist, it is created.
  *
  * @param string $server
  *
  * @return Socket
  */
 protected function getSocket($server)
 {
     if (isset($this->socket[$server])) {
         return $this->socket[$server];
     }
     $context = new ZMQContext();
     $socket = Socket::create($context, ZMQ::SOCKET_REQ, $this->logger);
     $socket->connect($server);
     $this->socket[$server] = $socket;
     return $socket;
 }
Ejemplo n.º 5
0
 /**
  * Creates a socket.
  *
  * @param int    $type
  * @param string $mode
  * @param string $dsn
  * @param array  $options
  *
  * @return Socket
  */
 protected function createSocket($type, $mode, $dsn = null, $options = array())
 {
     $context = $this->getContext();
     $socket = new Socket($context, $type);
     $options = $this->options + $options;
     foreach ($options as $key => $value) {
         $socket->setSockOpt($key, $value);
     }
     $this->connect($socket, $mode, $dsn);
     return $socket;
 }
Ejemplo n.º 6
0
 /**
  * Creates a socket to communicate with the service.
  *
  * @param Socket $socket
  *
  * @return null
  */
 protected function createServiceStream(Socket $socket)
 {
     $socket->setId('worker_service');
     $this->getPoller()->add($socket, ZMQ::POLL_IN);
     $that = $this;
     $this->serviceStream = $socket->getStream();
     $this->serviceStream->addListener(StreamInterface::MESSAGE, function (MessageEvent $event) use($that) {
         if (null === $event->getProtocolMessage()) {
             $message = $event->getMessage();
             $that->getLogger()->error('Non-protocol message detected in service stream: ' . $message);
             return;
         }
         $that->onServiceMessage($event->getProtocolMessage());
     });
 }