/** * Constructs an instance of this class. * * @param Connection $connection * @param IdentifierGenerator $generator */ public function __construct(Connection $connection, IdentifierGenerator $generator) { $this->connection = $connection; if ($this->connection->getClientID() === '') { $this->connection = $this->connection->withClientID($generator->generateClientID()); } }
/** * Constructs an instance of this class. * * @param Message $message * @param IdentifierGenerator $generator */ public function __construct(Message $message, IdentifierGenerator $generator) { $this->message = $message; if ($this->message->getQosLevel() > 0) { $this->identifier = $generator->generatePacketID(); } }
/** * Connects to a broker. * * @param string $host * @param int $port * @param Connection $connection * @param int $timeout * * @return ExtendedPromiseInterface */ public function connect($host, $port = 1883, Connection $connection = null, $timeout = 5) { if ($this->isConnected || $this->isConnecting) { return new RejectedPromise(new \LogicException('The client is already connected.')); } $this->isConnecting = true; $this->isConnected = false; $this->host = $host; $this->port = $port; if ($connection === null) { $connection = new DefaultConnection(); } if ($connection->getClientID() === '') { $connection = $connection->withClientID($this->identifierGenerator->generateClientID()); } $deferred = new Deferred(); $this->establishConnection($this->host, $this->port, $timeout)->then(function (Stream $stream) use($connection, $deferred, $timeout) { $this->stream = $stream; $this->emit('open', [$connection, $this]); $this->registerClient($connection, $timeout)->then(function (Connection $connection) use($deferred) { $this->isConnecting = false; $this->isConnected = true; $this->connection = $connection; $this->emit('connect', [$connection, $this]); $deferred->resolve($this->connection); })->otherwise(function (\Exception $e) use($deferred, $connection) { $this->isConnecting = false; $this->emitError($e); $deferred->reject($e); $this->stream->close(); $this->emit('close', [$connection, $this]); }); })->otherwise(function (\Exception $e) use($deferred) { $this->isConnecting = false; $this->emitError($e); $deferred->reject($e); }); return $deferred->promise(); }
/** * Constructs an instance of this class. * * @param Subscription[] $subscriptions * @param IdentifierGenerator $generator */ public function __construct(array $subscriptions, IdentifierGenerator $generator) { $this->subscriptions = array_values($subscriptions); $this->identifier = $generator->generatePacketID(); }