/** * Constructor * * @param \Thruway\Session $callerSession * @param \Thruway\Message\CallMessage $callMessage * @param Registration $registration */ public function __construct(Session $callerSession, CallMessage $callMessage, Procedure $procedure) { $this->callMessage = $callMessage; $this->callerSession = $callerSession; $this->procedure = $procedure; $this->callStart = microtime(true); $this->invocationRequestId = Utils::getUniqueId(); }
/** * Constructor * * @param string $uri * @param \Thruway\Session $session * @param mixed $options */ public function __construct($uri, Session $session, $options = null) { $this->uri = $uri; $this->session = $session; $this->id = Utils::getUniqueId(); $this->disclosePublisher = false; $this->pausedForState = false; $this->pauseQueue = new \SplQueue(); $this->setOptions($options); }
public function testMakingCallIncrementsCallCount() { $mockSession = new \Thruway\Session(new \Thruway\Transport\DummyTransport()); $this->assertEquals(0, $this->_registration->getCurrentCallCount()); $callMsg = new \Thruway\Message\CallMessage(\Thruway\Common\Utils::getUniqueId(), new \stdClass(), 'test_procedure'); $procedure = $this->getMockBuilder('\\Thruway\\Procedure')->disableOriginalConstructor()->getMock(); $call = new \Thruway\Call($mockSession, $callMsg, $procedure); $this->_registration->processCall($call); $this->assertEquals(1, $this->_registration->getCurrentCallCount()); }
/** * Constructor * * @param \Thruway\Transport\TransportInterface $transport */ public function __construct(TransportInterface $transport) { $this->transport = $transport; $this->state = static::STATE_PRE_HELLO; $this->sessionId = Utils::getUniqueId(); $this->realm = null; $this->sessionStart = new \DateTime(); $this->authenticationDetails = null; $this->dispatcher = new EventDispatcher(); $this->dispatcher->addRealmSubscriber($this); }
/** * Constructor * * @param \Thruway\Transport\TransportInterface $transport * @param \Thruway\Manager\ManagerInterface $manager */ public function __construct(TransportInterface $transport, ManagerInterface $manager = null) { $this->transport = $transport; $this->state = static::STATE_PRE_HELLO; $this->sessionId = Utils::getUniqueId(); $this->realm = null; $this->messagesSent = 0; $this->sessionStart = new \DateTime(); $this->authenticationDetails = null; $this->pendingCallCount = 0; $this->dispatcher = new EventDispatcher(); $this->dispatcher->addRealmSubscriber($this); if ($manager === null) { $manager = new ManagerDummy(); } $this->setManager($manager); }
/** * Constructor * * @param \Thruway\Session $session * @param string $procedureName */ public function __construct(Session $session, $procedureName) { $this->id = Utils::getUniqueId(); $this->session = $session; $this->procedureName = $procedureName; $this->allowMultipleRegistrations = false; $this->discloseCaller = false; $this->calls = []; $this->registeredAt = new \DateTime(); $this->invocationCount = 0; $this->busyTime = 0; $this->invocationAverageTime = 0; $this->maxSimultaneousCalls = 0; $this->lastCallStartedAt = null; $this->lastIdledAt = $this->registeredAt; $this->busyStart = null; $this->completedCallTimeTotal = 0; }
public function testInvocationError() { $dealer = new \Thruway\Role\Dealer(); $callerTransport = new \Thruway\Transport\DummyTransport(); $callerSession = new Session($callerTransport); $calleeTransport = new \Thruway\Transport\DummyTransport(); $calleeSession = new Session($calleeTransport); // register from callee $registerMsg = new \Thruway\Message\RegisterMessage(1, new stdClass(), 'test_proc_name'); $dealer->handleRegisterMessage(new \Thruway\Event\MessageEvent($calleeSession, $registerMsg)); $this->assertInstanceOf('\\Thruway\\Message\\RegisteredMessage', $calleeTransport->getLastMessageSent()); // call from one session $callRequestId = \Thruway\Common\Utils::getUniqueId(); $callMsg = new \Thruway\Message\CallMessage($callRequestId, new stdClass(), 'test_proc_name'); $dealer->handleCallMessage(new \Thruway\Event\MessageEvent($callerSession, $callMsg)); $this->assertInstanceOf('\\Thruway\\Message\\InvocationMessage', $calleeTransport->getLastMessageSent()); $errorMsg = \Thruway\Message\ErrorMessage::createErrorMessageFromMessage($calleeTransport->getLastMessageSent(), 'the.error.uri'); $dealer->handleErrorMessage(new \Thruway\Event\MessageEvent($calleeSession, $errorMsg)); /** @var \Thruway\Message\ErrorMessage $returnedError */ $returnedError = $callerTransport->getLastMessageSent(); $this->assertInstanceOf('\\Thruway\\Message\\ErrorMessage', $returnedError); $this->assertEquals(Message::MSG_CALL, $returnedError->getErrorMsgCode()); $this->assertEquals($callRequestId, $returnedError->getErrorRequestId()); $this->assertEquals('the.error.uri', $returnedError->getErrorURI()); }
/** * Publish meta * * @param string $topicName * @param mixed $arguments * @param mixed $argumentsKw * @param mixed $options */ public function publishMeta($topicName, $arguments, $argumentsKw = null, $options = null) { if ($this->metaSession === null) { // setup a new metaSession $s = new Session(new DummyTransport()); $this->metaSession = $s; } $messageEvent = new MessageEvent($this->metaSession, new PublishMessage(Utils::getUniqueId(), $options, $topicName, $arguments, $argumentsKw)); $this->getBroker()->handlePublishMessage($messageEvent); }
/** * Issue 53 - publishing inside of subscription event callback * prevents other internal clients from receiving the published event * * @depends testStart * @param \Thruway\Peer\Router $router */ public function testIssue53(\Thruway\Peer\Router $router) { $this->_callCount = 0; $transport1 = $this->getMockBuilder('\\Thruway\\Transport\\TransportInterface')->getMock(); $transport2 = $this->getMockBuilder('\\Thruway\\Transport\\TransportInterface')->getMock(); $transport2->expects($this->exactly(3))->method('sendMessage')->withConsecutive([$this->isInstanceOf('\\Thruway\\Message\\WelcomeMessage')], [$this->isInstanceOf('\\Thruway\\Message\\SubscribedMessage')], [$this->isInstanceOf('\\Thruway\\Message\\EventMessage')]); $transportPublisher = $this->getMockBuilder('\\Thruway\\Transport\\TransportInterface')->getMock(); $session1 = new \Thruway\Session($transport1); $session2 = new \Thruway\Session($transport2); $sessionPublisher = new \Thruway\Session($transportPublisher); $transport1->expects($this->exactly(3))->method('sendMessage')->withConsecutive([$this->isInstanceOf('\\Thruway\\Message\\WelcomeMessage')], [$this->isInstanceOf('\\Thruway\\Message\\SubscribedMessage')], [$this->callback(function ($arg) use($router, $transport1, $session1) { $this->assertInstanceOf('\\Thruway\\Message\\EventMessage', $arg); // publish while in the callback $publishMsg = new \Thruway\Message\PublishMessage(12346, (object) [], 'com.example.nowhere'); $session1->dispatchMessage($publishMsg); $this->_callCount = $this->_callCount + 1; return true; })]); $router->getEventDispatcher()->dispatch("connection_open", new \Thruway\Event\ConnectionOpenEvent($session1)); $router->getEventDispatcher()->dispatch("connection_open", new \Thruway\Event\ConnectionOpenEvent($session2)); $router->getEventDispatcher()->dispatch("connection_open", new \Thruway\Event\ConnectionOpenEvent($sessionPublisher)); // send in a few hellos $helloMsg = new \Thruway\Message\HelloMessage("realm_issue53", (object) []); $session1->dispatchMessage($helloMsg); $session2->dispatchMessage($helloMsg); $sessionPublisher->dispatchMessage($helloMsg); // subscribe $subscribeMsg = new \Thruway\Message\SubscribeMessage(\Thruway\Common\Utils::getUniqueId(), (object) [], "com.example.issue53"); $session1->dispatchMessage($subscribeMsg); $session2->dispatchMessage($subscribeMsg); // publish to the topic from the publishing transport $publishMsg = new \Thruway\Message\PublishMessage(12345, (object) [], 'com.example.issue53'); $sessionPublisher->dispatchMessage($publishMsg); }
public function testInvokeRandomOneRegistration() { $transportCaller = new \Thruway\Transport\DummyTransport(); $sessionCaller = new \Thruway\Session($transportCaller); $sessionsToStart = 1; $sessions = array(); for ($i = 0; $i < $sessionsToStart; $i++) { $sessions[] = new \Thruway\Session(new \Thruway\Transport\DummyTransport()); } $proc = new \Thruway\Procedure('random.procedure'); foreach ($sessions as $session) { $registerMsg = new \Thruway\Message\RegisterMessage(\Thruway\Common\Utils::getUniqueId(), ["invoke" => "random"], 'random.procedure'); $proc->processRegister($session, $registerMsg); } //make sure that the registrations have been successful $this->assertEquals(count($sessions), count($proc->getRegistrations())); $calls = 0; for ($i = 0; $i < 10; $i++) { $callMessage = new \Thruway\Message\CallMessage(\Thruway\Common\Utils::getUniqueId(), [], 'random.procedure'); $call = new \Thruway\Call($sessionCaller, $callMessage, $proc); //lets send the call $proc->processCall($sessionCaller, $call); } foreach ($sessions as $session) { $calls += $session->getPendingCallCount(); } $this->assertEquals(10, $calls); }
/** * process publish * * @param \Thruway\ClientSession $session * @param string $topicName * @param mixed $arguments * @param mixed $argumentsKw * @param mixed $options * @return \React\Promise\Promise */ public function publish(ClientSession $session, $topicName, $arguments, $argumentsKw, $options) { $options = (object) $options; $requestId = Utils::getUniqueId(); if (isset($options->acknowledge) && $options->acknowledge === true) { $futureResult = new Deferred(); $this->publishRequests[$requestId] = ['future_result' => $futureResult]; } $publishMsg = new PublishMessage($requestId, $options, $topicName, $arguments, $argumentsKw); $session->sendMessage($publishMsg); return isset($futureResult) ? $futureResult->promise() : false; }
/** * process subscribe * * @param \Thruway\ClientSession $session * @param string $topicName * @param callable $callback * @param $options * @return Promise */ public function subscribe(ClientSession $session, $topicName, $callback, $options = null) { $requestId = Utils::getUniqueId(); $options = $options ? (object) $options : (object) []; $deferred = new Deferred(); $subscription = ["topic_name" => $topicName, "callback" => $callback, "request_id" => $requestId, "options" => $options, "deferred" => $deferred]; array_push($this->subscriptions, $subscription); $subscribeMsg = new SubscribeMessage($requestId, $options, $topicName); $session->sendMessage($subscribeMsg); return $deferred->promise(); }
/** * Create Invocation message from Call message and registration * * @param \Thruway\Message\CallMessage $msg * @param \Thruway\Registration $registration * @return \Thruway\Message\InvocationMessage */ public static function createMessageFrom(CallMessage $msg, Registration $registration) { $requestId = Utils::getUniqueId(); $details = new \stdClass(); return new static($requestId, $registration->getId(), $details, $msg->getArguments(), $msg->getArgumentsKw()); }
public function testLeave() { $session = $this->getMockBuilder('\\Thruway\\Session')->disableOriginalConstructor()->getMock(); $session->expects($this->once())->method("sendMessage")->with($this->isInstanceOf('\\Thruway\\Message\\RegisteredMessage')); $registerMsg = new \Thruway\Message\RegisterMessage(\Thruway\Common\Utils::getUniqueId(), [], 'test_procedure'); $this->_proc->processRegister($session, $registerMsg); $this->assertEquals(1, count($this->_proc->getRegistrations())); $this->_proc->leave($session); $this->assertEquals(0, count($this->_proc->getRegistrations())); }
/** * 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 = Utils::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(); }
public function xtestUnauthorizedActions() { $this->markTestIncomplete("Authorization cannot be tested here and will be moved to a module"); $session = $this->getMockBuilder('\\Thruway\\Session')->disableOriginalConstructor()->setMethods(["sendMessage"])->getMock(); $authorizationManager = $this->getMockBuilder('\\Thruway\\Authentication\\AuthorizationManagerInterface')->getMock(); $realm = new \Thruway\Realm("some_realm"); $realm->setAuthorizationManager($authorizationManager); $subscribeMsg = new \Thruway\Message\SubscribeMessage(\Thruway\Common\Utils::getUniqueId(), [], "some_topic"); $publishMsg = new \Thruway\Message\PublishMessage(\Thruway\Common\Utils::getUniqueId(), (object) ["acknowledge" => true], "some_topic"); $registerMsg = new \Thruway\Message\RegisterMessage(\Thruway\Common\Utils::getUniqueId(), [], 'some_procedure'); $callMsg = new \Thruway\Message\CallMessage(\Thruway\Common\Utils::getUniqueId(), [], "some_procedure"); $authorizationManager->expects($this->exactly(5))->method("isAuthorizedTo")->withConsecutive([$this->isInstanceOf('\\Thruway\\Session'), $this->isInstanceOf('\\Thruway\\Message\\SubscribeMessage')], [$this->isInstanceOf('\\Thruway\\Session'), $this->isInstanceOf('\\Thruway\\Message\\PublishMessage')], [$this->isInstanceOf('\\Thruway\\Session'), $this->isInstanceOf('\\Thruway\\Message\\RegisterMessage')], [$this->isInstanceOf('\\Thruway\\Session'), $this->isInstanceOf('\\Thruway\\Message\\CallMessage')], [$this->isInstanceOf('\\Thruway\\Session'), $this->isInstanceOf('\\Thruway\\Message\\PublishMessage')])->willReturn(false); $errorCheck = function ($msg) { $this->assertInstanceOf('\\Thruway\\Message\\ErrorMessage', $msg); $this->assertEquals('wamp.error.not_authorized', $msg->getErrorUri()); return true; }; $session->expects($this->exactly(5))->method("sendMessage")->withConsecutive([$this->isInstanceOf('\\Thruway\\Message\\WelcomeMessage')], [$this->callback($errorCheck)], [$this->callback($errorCheck)], [$this->callback($errorCheck)], [$this->callback($errorCheck)]); $helloMsg = new \Thruway\Message\HelloMessage("some_realm", []); $realm->onMessage($session, $helloMsg); $realm->onMessage($session, $subscribeMsg); $realm->onMessage($session, $publishMsg); $realm->onMessage($session, $registerMsg); $realm->onMessage($session, $callMsg); // make sure publish doesn't send error back when ack is false $publishMsg2 = $publishMsg = new \Thruway\Message\PublishMessage(\Thruway\Common\Utils::getUniqueId(), [], "some_topic"); $realm->onMessage($session, $publishMsg2); }
/** * 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 = Utils::getUniqueId(); $this->callRequests[$requestId] = ["procedure_name" => $procedureName, "future_result" => $futureResult]; if (is_array($options)) { $options = (object) $options; } if (!is_object($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(); }
public function testRemoveRegistration() { $transport = $this->getMockBuilder('\\Thruway\\Transport\\TransportInterface')->getMock(); $transport->expects($this->any())->method("getTransportDetails")->will($this->returnValue("")); $session = $this->getMockBuilder('\\Thruway\\Session')->setMethods(["sendMessage"])->setConstructorArgs([$transport])->getMock(); /** @var $session \Thruway\Session */ $session->setRealm(new \Thruway\Realm("testrealm")); $broker = new \Thruway\Role\Broker(); $subscribeMsg = new \Thruway\Message\SubscribeMessage(\Thruway\Common\Utils::getUniqueId(), [], "test.topic"); $messageEvent = new \Thruway\Event\MessageEvent($session, $subscribeMsg); $broker->handleSubscribeMessage($messageEvent); $subscriptions = $broker->getSubscriptions(); $this->assertTrue(count($subscriptions) === 1); $subscriptions = array_values($subscriptions); $messageEvent = new \Thruway\Event\MessageEvent($session, new \Thruway\Message\UnsubscribeMessage(\Thruway\Common\Utils::getUniqueId(), $subscriptions[0]->getId())); $broker->handleUnsubscribeMessage($messageEvent); $this->assertTrue(count($broker->getSubscriptions()) === 0); }
/** * Process publish message * * @param \Thruway\Session $session * @param \Thruway\Message\PublishMessage $msg */ protected function processPublish(Session $session, PublishMessage $msg) { if ($msg->getPublicationId() === null) { $msg->setPublicationId(Utils::getUniqueId()); } /** @var SubscriptionGroup $subscriptionGroup */ foreach ($this->subscriptionGroups as $subscriptionGroup) { $subscriptionGroup->processPublish($session, $msg); } if ($msg->acknowledge()) { $session->sendMessage(new PublishedMessage($msg->getRequestId(), $msg->getPublicationId())); } }