Example #1
0
 /**
  *
  * @param MessageInterface $message
  * @param string|null      $destination
  *
  * @return null
  */
 public function send(MessageInterface $message, $destination = null)
 {
     $msg_to_send = $message->toMessage();
     $msg_to_send->prepend(array(MessageFactory::getLatestProtocolVersion(), $message->getType()));
     if ($destination !== null) {
         $msg_to_send->prependRoutingInformation(array($destination));
     }
     $this->socket->msend($msg_to_send);
 }
Example #2
0
 /**
  * Handles a message send by the worker.
  *
  * @param \AlphaRPC\Common\Protocol\Message\MessageInterface $msg
  *
  * @return null
  * @throws RuntimeException
  */
 public function onWorkerMessage(MessageInterface $msg)
 {
     if ($msg instanceof Protocol\ActionListRequest) {
         $this->getLogger()->debug('[SERVICE] Returning list of actions.');
         $this->stream->send(new Protocol\ActionListResponse($this->getActionList()));
         return;
     }
     if ($msg instanceof Protocol\ExecuteJobRequest) {
         $requestId = $msg->getRequestId();
         $action = $msg->getAction();
         $this->getLogger()->debug('[SERVICE] Job for action: ' . $action . ' with id: ' . $requestId);
         $params = array();
         $serialized = $msg->getParams();
         foreach ($serialized as $param) {
             $params[] = $this->getSerializer()->unserialize($param);
         }
         if (!isset($this->actions[$action])) {
             $this->getLogger()->notice('[SERVICE] Action ' . $action . ' not found.');
             $this->stream->send(new Protocol\ActionNotFound($requestId, $action));
             return;
         }
         $return = $this->execute($action, $params);
         $sendReturn = $this->getSerializer()->serialize($return);
         $this->stream->send(new Protocol\ExecuteJobResponse($requestId, $sendReturn));
         return;
     }
     throw new RuntimeException('Invalid request.');
 }