Inheritance: extends AbstractSession
Example #1
0
 /**
  * @param ClientSession $session
  */
 public function onSessionStart(ClientSession $session)
 {
     try {
         //Temp place for keep-alive ping.  Every 30 seconds ping the far end and wait 5 seconds for a response
         $loop = $this->getClient()->getLoop();
         $loop->addPeriodicTimer(20, function () use($session, $loop) {
             $this->getClient()->getLogger()->info("Sending a Ping\n");
             $session->ping(5);
             $loop->tick();
         });
         $this->getClient()->getLogger()->info("Connection has opened");
         $this->session = $session;
         if ($this->resources && ($enabled = array_intersect_key($this->pluginManager->getDefinitions(), $this->resources))) {
             foreach ($enabled as $key => $resourceInfo) {
                 if ("thruway" === $resourceInfo['provider']) {
                     $resourceInfo['bundles'] = $this->resources[$key];
                     $this->handleResource($resourceInfo);
                 }
             }
         }
     } catch (\Exception $e) {
         //@todo handle exception
         print_r($e->getMessage());
     }
 }
Example #2
0
 /**
  * @param \Thruway\ClientSession $session
  * @param \Thruway\Transport\TransportInterface $transport
  */
 public function onSessionStart($session, $transport)
 {
     // TODO: now that the session has started, setup the stuff
     echo "--------------- Hello from InternalClient ------------\n";
     $session->register('com.example.getphpversion', [$this, 'getPhpVersion']);
     $session->register('com.example.getonline', [$this, 'getOnline']);
     $session->subscribe('wamp.metaevent.session.on_join', [$this, 'onSessionJoin']);
     $session->subscribe('wamp.metaevent.session.on_leave', [$this, 'onSessionLeave']);
 }
Example #3
0
 /**
  * Handle on session start
  *
  * @param \Thruway\ClientSession $session
  * @param \Thruway\Transport\TransportInterface $transport
  */
 public function onSessionStart($session, $transport)
 {
     $this->thePromise->then(function () use($session) {
         $session->call('com.example.thefunction0', [])->then(function ($res) {
             echo "Done.\n";
             exit;
         });
     });
 }
 /**
  * @param \Thruway\ClientSession $session
  * @param \Thruway\Transport\TransportInterface $transport
  * @throws \yii\base\InvalidConfigException
  */
 public function onSessionStart($session, $transport)
 {
     echo "--------------- Hello from InternalClient ------------" . PHP_EOL;
     try {
         $this->session = $session;
         $session->register('app.auth', [$this, 'onAuth']);
         $session->subscribe('wamp.metaevent.session.on_join', [$this, 'onSessionJoin']);
         $session->subscribe('wamp.metaevent.session.on_leave', [$this, 'onSessionLeave']);
         // todo регистрировать только после авторизации
         // инициализация методов
         foreach ($this->controllers as $controllerClass) {
             $controller = \Yii::createObject($controllerClass);
             if ($controller && $controller instanceof WampController) {
                 /** @var $controller WampController */
                 foreach ($controller->registers() as $callUri => $callOptions) {
                     if (is_array($callOptions) === false) {
                         $callOptions = [$callOptions];
                     }
                     if (is_numeric($callUri) || empty($callOptions[0])) {
                         // todo throw exception
                         continue;
                     }
                     $procedureName = $controller->getUri($callUri);
                     $callback = [$controller, '_call_' . $callOptions[0]];
                     unset($callOptions[0]);
                     $options = empty($callOptions) ? $callOptions : [];
                     //                        $this->getCallee()->register(
                     //                            $this->getSession(),
                     $session->register($procedureName, $callback, $options);
                 }
                 foreach ($controller->subscribes() as $subscribeUri => $subscribeOptions) {
                     if (is_array($subscribeOptions) === false) {
                         $subscribeOptions = [$subscribeOptions];
                     }
                     if (is_numeric($subscribeUri) || empty($subscribeOptions[0])) {
                         // todo throw exception
                         continue;
                     }
                     $topicName = $controller->getUri($subscribeUri);
                     $callback = [$controller, '_call_' . $subscribeOptions[0]];
                     unset($subscribeOptions[0]);
                     $options = empty($subscribeOptions) ? $subscribeOptions : [];
                     //                        $this->getSubscriber()->subscribe(
                     //                            $this->getSession(),
                     $session->subscribe($topicName, $callback, $options);
                 }
                 $this->_controllers[$controller->getId()] = $controller;
             }
         }
         foreach ($this->_controllers as $controller) {
             $controller->init();
         }
     } catch (\Exception $e) {
         echo Console::renderColoredString('%rError: %w' . (string) $e . '%n') . PHP_EOL;
     }
 }
Example #5
0
 /**
  * Handle on session start
  *
  * @param \Thruway\ClientSession $session
  * @param \Thruway\Transport\TransportInterface $transport
  */
 public function onSessionStart($session, $transport)
 {
     $session->register('com.example.add2', [$this, 'add2'])->then(function () use($session) {
         echo "Registered RPC\n";
         $session->call('com.example.add2', [2, 3])->then(function ($res) {
             echo "Got result: " . $res[0] . "\n";
             $this->setAttemptRetry(false);
             $this->session->shutdown();
         });
     });
 }
Example #6
0
 /**
  * Handles session started
  *
  * @param \Thruway\ClientSession $session
  * @param \Thruway\Transport\TransportProviderInterface $transport
  */
 public function onSessionStart($session, $transport)
 {
     $promises = [];
     $promises[] = $session->register('add_state_handler', [$this, "addStateHandler"]);
     $promises[] = $session->register('remove_state_handler', [$this, "removeStateHandler"]);
     $pAll = \React\Promise\all($promises);
     $pAll->then(function () {
         $this->setReady(true);
     }, function () {
         $this->setReady(false);
     });
 }
 /**
  * Handles session start
  *
  * @param \Thruway\ClientSession $session
  * @param \Thruway\Transport\TransportProviderInterface $transport
  */
 public function onSessionStart($session, $transport)
 {
     $session->register("thruway.auth.{$this->getMethodName()}.onhello", [$this, 'processHello'], ["replace_orphaned_session" => "yes"])->then(function () use($session) {
         $session->register("thruway.auth.{$this->getMethodName()}.onauthenticate", [$this, 'preProcessAuthenticate'], ["replace_orphaned_session" => "yes"])->then(function () use($session) {
             $registrations = new \stdClass();
             $registrations->onhello = "thruway.auth.{$this->getMethodName()}.onhello";
             $registrations->onauthenticate = "thruway.auth.{$this->getMethodName()}.onauthenticate";
             $session->call('thruway.auth.registermethod', [$this->getMethodName(), $registrations, $this->getAuthRealms()])->then(function ($args) {
                 Logger::debug($this, "Authentication Method Registration Successful: {$this->getMethodName()}");
             });
         });
     });
 }
 /**
  * Gets and published the topics state to this subscription
  *
  * @param Subscription $subscription
  * @return mixed
  */
 public function publishState(Subscription $subscription)
 {
     //Pause all non-state building event messages
     $subscription->pauseForState();
     $sessionId = $subscription->getSession()->getSessionId();
     $this->clientSession->call($this->getProcedureName(), [$subscription->getUri(), $sessionId, $subscription->getOptions(), $subscription->getSession()->getAuthenticationDetails()])->then(function ($res) use($subscription) {
         $pubId = null;
         if (isset($res[0])) {
             $pubId = $res[0];
         }
         $subscription->unPauseForState($pubId);
     }, function ($error) use($subscription) {
         Logger::error($this, "Could not call '{$this->getProcedureName()}' when restoring state");
         $subscription->unPauseForState();
     });
 }
Example #9
0
 /**
  *
  * @param \Thruway\ClientSession $session
  * @param \Thruway\Transport\TransportInterface $transport
  */
 public function onSessionStart($session, $transport)
 {
     $session->register('add_command', [$this, 'addCommand']);
     $session->register('status', [$this, 'status']);
     $session->register('start_process', [$this, 'startProcess']);
     $session->register('stop_process', [$this, 'stopProcess']);
     $session->register('restart_process', [$this, 'restartProcess']);
     $session->register('add_instance', [$this, 'addInstance']);
     //Congestion Manager Client.  This needs to be a separate client because it needs to listen on the main realm and not `process_manager`.
     $config = $this->container->getParameter('voryx_thruway');
     $congestionManager = new Client($config['realm'], $session->getLoop());
     $congestionManager->addTransportProvider(new PawlTransportProvider($config['trusted_url']));
     $congestionManager->on('open', function (ClientSession $session) {
         $session->subscribe("thruway.metaevent.procedure.congestion", [$this, "onCongestion"]);
     });
     $congestionManager->start(false);
 }
Example #10
0
 /**
  * Handle close session
  *
  * @param mixed $reason
  */
 public function onClose($reason)
 {
     if (isset($this->session)) {
         $this->onSessionEnd($this->session);
         $this->session->onClose();
         $this->session = null;
         $this->emit('close', [$reason]);
     }
     $this->roles = [];
     $this->callee = null;
     $this->caller = null;
     $this->subscriber = null;
     $this->publisher = null;
     $this->retryConnection();
 }
Example #11
0
 /**
  * @param \Thruway\ClientSession $session ClientSession
  * @param \Thruway\Transport\TransportInterface $transport Transport
  */
 public function onSessionStart($session, $transport)
 {
     $session->subscribe('wamp.metaevent.session.on_join', [$this, 'onJoin']);
     $session->subscribe('wamp.metaevent.session.on_leave', [$this, 'onLeave']);
     $session->register('server.get_user_sessions', [$this, 'getUserSession']);
 }
Example #12
0
 /**
  * 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();
 }
Example #13
0
 /**
  * @param \Thruway\ClientSession $session
  * @param \Thruway\Transport\TransportInterface $transport
  */
 public function onSessionStart($session, $transport)
 {
     // TODO: now that the session has started, setup the stuff
     echo "--------------- Hello from InternalClient ------------\n";
     $session->register('com.example.getphpversion', [$this, 'getPhpVersion']);
 }
Example #14
0
 /**
  * Handle on session start
  * 
  * @param \Thruway\ClientSession $session
  * @param \Thruway\Transport\TransportInterface $transport
  */
 public function onSessionStart($session, $transport)
 {
     $session->register('com.example.thefunction' . $this->number, [$this, 'theFunction'])->then(function () {
         $this->registeredDeferred->resolve();
     });
 }
Example #15
0
 /**
  * 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;
 }
Example #16
0
 /**
  * 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();
 }
Example #17
0
 /**
  * @param $procedureName
  * @param $arguments
  * @return \React\Promise\Promise
  */
 public function call(ClientSession $session, $procedureName, $arguments = null, $argumentsKw = null)
 {
     //This promise gets resolved in Caller::processResult
     $futureResult = new Deferred();
     $requestId = Session::getUniqueId();
     $this->callRequests[$requestId] = ["procedure_name" => $procedureName, "future_result" => $futureResult];
     $options = new \stdClass();
     $callMsg = new CallMessage($requestId, $options, $procedureName, $arguments, $argumentsKw);
     $session->sendMessage($callMsg);
     return $futureResult->promise();
 }
Example #18
0
 /**
  * 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();
 }
Example #19
0
 /**
  * @param \Thruway\ClientSession $session
  * @param \Thruway\Transport\TransportInterface $transport
  */
 public function onSessionStart($session, $transport)
 {
     $session->register('com.example.getfreespace', [$this, 'getFreeSpace']);
 }
 /**
  * Handles session started
  *
  * @param \Thruway\ClientSession $session
  * @param \Thruway\Transport\TransportProviderInterface $transport
  */
 public function onSessionStart($session, $transport)
 {
     $session->register('thruway.auth.registermethod', [$this, 'registerAuthMethod'], ['disclose_caller' => true])->then(function () {
         $this->setReady(true);
     }, function () {
         $this->setReady(false);
         Logger::error($this, "registration of registerAuthMethod failed.");
     });
 }
Example #21
0
 /**
  * @param $topicName
  * @param $callback
  */
 public function subscribe(ClientSession $session, $topicName, $callback)
 {
     $requestId = Session::getUniqueId();
     $options = new \stdClass();
     $subscription = ["topic_name" => $topicName, "callback" => $callback, "request_id" => $requestId, "options" => $options];
     array_push($this->subscriptions, $subscription);
     $subscribeMsg = new SubscribeMessage($requestId, $options, $topicName);
     $session->sendMessage($subscribeMsg);
 }