/** * Start transport provider * * @param \Thruway\Peer\ClientInterface $client * @param \React\EventLoop\LoopInterface $loop */ public function startTransportProvider(ClientInterface $client, LoopInterface $loop) { Logger::info($this, "Starting Transport"); $this->client = $client; $this->loop = $loop; $this->connector = new Factory($this->loop); $this->connector->__invoke($this->URL, ['wamp.2.json'])->then(function (WebSocket $conn) { Logger::info($this, "Pawl has connected"); $transport = new PawlTransport($conn, $this->loop); $transport->setSerializer(new JsonSerializer()); $this->client->onOpen($transport); $conn->on('message', function ($msg) use($transport) { Logger::debug($this, "Received: {$msg}"); try { $this->client->onMessage($transport, $transport->getSerializer()->deserialize($msg)); } catch (DeserializationException $e) { Logger::warning($this, "Deserialization exception occurred."); } catch (\Exception $e) { Logger::warning($this, "Exception occurred during onMessage: " . $e->getMessage()); } }); $conn->on('close', function ($conn) { Logger::info($this, "Pawl has closed"); $this->client->onClose('close'); }); $conn->on('pong', function ($frame, $ws) use($transport) { $transport->onPong($frame, $ws); }); }, function ($e) { $this->client->onClose('unreachable'); Logger::info($this, "Could not connect: {$e->getMessage()}"); // $this->loop->stop(); }); }
/** * process unregister * * @param \Thruway\ClientSession $session * @param string $Uri * @throws \Exception * @return \React\Promise\Promise|false */ public function unregister(ClientSession $session, $Uri) { // TODO: maybe add an option to wait for pending calls to finish $registration = null; foreach ($this->registrations as $k => $r) { if (isset($r['procedure_name'])) { if ($r['procedure_name'] == $Uri) { $registration =& $this->registrations[$k]; break; } } } if ($registration === null) { Logger::warning($this, "registration not found: " . $Uri); return false; } // we remove the callback from the client here // because we don't want the client to respond to any more calls $registration['callback'] = null; $futureResult = new Deferred(); if (!isset($registration["registration_id"])) { // this would happen if the registration was never acknowledged by the router // we should remove the registration and resolve any pending deferreds Logger::error($this, "Registration ID is not set while attempting to unregister " . $Uri); // reject the pending registration $registration['futureResult']->reject(); // TODO: need to figure out what to do in this off chance // We should still probably return a promise here that just rejects // there is an issue with the pending registration too that // the router may have a "REGISTERED" in transit and may still think that is // good to go - so maybe still send the unregister? } $requestId = Session::getUniqueId(); // save the request id so we can find this in the registration // list to call the deferred and remove it from the list $registration['unregister_request_id'] = $requestId; $registration['unregister_deferred'] = $futureResult; $unregisterMsg = new UnregisterMessage($requestId, $registration['registration_id']); $session->sendMessage($unregisterMsg); return $futureResult->promise(); }
/** * Process All Messages if the session has been authenticated * * @param \Thruway\Session $session * @param \Thruway\Message\Message $msg */ private function processAuthenticated(Session $session, Message $msg) { // authorization stuff here if ($msg instanceof ActionMessageInterface) { if (!$this->getAuthorizationManager()->isAuthorizedTo($session, $msg)) { Logger::alert($this, "Permission denied: " . $msg->getActionName() . " " . $msg->getUri() . " for " . $session->getAuthenticationDetails()->getAuthId()); $session->sendMessage(ErrorMessage::createErrorMessageFromMessage($msg, "wamp.error.not_authorized")); return; } } $handled = false; foreach ($this->roles as $role) { if ($role->handlesMessage($msg)) { $role->onMessage($session, $msg); $handled = true; break; } } if (!$handled) { Logger::warning($this, "Unhandled message sent to \"{$this->getRealmName()}\""); } }
/** * @param Session $session * @param ErrorMessage $msg */ private function processInterruptError(Session $session, ErrorMessage $msg) { $call = isset($this->callInterruptIndex[$msg->getRequestId()]) ? $this->callInterruptIndex[$msg->getRequestId()] : null; if (!$call) { Logger::warning($this, "Interrupt error with no corresponding interrupt index"); return; } $errorMsgToCaller = ErrorMessage::createErrorMessageFromMessage($call->getCancelMessage()); $errorMsgToCaller->setErrorURI($msg->getErrorURI()); $callerSession = $call->getCallerSession(); $callerSession->sendMessage($errorMsgToCaller); $call->getRegistration()->removeCall($call); $this->removeCall($call); }
/** * processCancel processes cancel message from the caller. * Return true if the Call should be removed from active calls * * @param Session $session * @param CancelMessage $msg * @return bool */ public function processCancel(Session $session, CancelMessage $msg) { if ($this->getCallerSession() !== $session) { Logger::warning($this, "session attempted to cancel call they did not own."); return false; } if ($this->getCalleeSession() === null) { // this call has not been sent to a callee yet (it is in a queue) // we can just kill it and say it was canceled $errorMsg = ErrorMessage::createErrorMessageFromMessage($msg, "wamp.error.canceled"); $details = $errorMsg->getDetails() ?: (object) []; $details->_thruway_removed_from_queue = true; $session->sendMessage($errorMsg); return true; } $details = (object) []; if ($this->getCalleeSession()->getHelloMessage() instanceof HelloMessage) { $details = $this->getCalleeSession()->getHelloMessage()->getDetails(); } $calleeSupportsCancel = false; if (isset($details->roles->callee->features->call_canceling) && is_scalar($details->roles->callee->features->call_canceling)) { $calleeSupportsCancel = (bool) $details->roles->callee->features->call_canceling; } if (!$calleeSupportsCancel) { $errorMsg = ErrorMessage::createErrorMessageFromMessage($msg); $errorMsg->setErrorURI('wamp.error.not_supported'); $session->sendMessage($errorMsg); return false; } $this->setCancelMessage($msg); $this->canceling = true; $calleeSession = $this->getCalleeSession(); $interruptMessage = new InterruptMessage($this->getInvocationRequestId(), (object) []); $calleeSession->sendMessage($interruptMessage); $this->setInterruptMessage($interruptMessage); if (isset($msg->getOptions()->mode) && is_scalar($msg->getOptions()->mode) && $msg->getOptions()->mode == "killnowait") { $errorMsg = ErrorMessage::createErrorMessageFromMessage($msg, "wamp.error.canceled"); $session->sendMessage($errorMsg); return true; } return false; }
/** * process call * * @param \Thruway\ClientSession $session * @param string $procedureName * @param mixed $arguments * @param mixed $argumentsKw * @param mixed $options * @return \React\Promise\Promise */ public function call(ClientSession $session, $procedureName, $arguments = null, $argumentsKw = null, $options = null) { //This promise gets resolved in Caller::processResult $futureResult = new Deferred(); $requestId = Session::getUniqueId(); $this->callRequests[$requestId] = ["procedure_name" => $procedureName, "future_result" => $futureResult]; if (!(is_array($options) && Message::isAssoc($options))) { if ($options !== null) { Logger::warning($this, "Options don't appear to be the correct type."); } $options = new \stdClass(); } $callMsg = new CallMessage($requestId, $options, $procedureName, $arguments, $argumentsKw); $session->sendMessage($callMsg); return $futureResult->promise(); }