/** * Extracts a new response from a communicator. * * @param Communicator $com The communicator from which to extract * the new response. * @param bool $asStream Whether to populate the argument values * with streams instead of strings. * @param int $sTimeout If a response is not immediatly * available, wait this many seconds. If NULL, wait indefinetly. * @param int $usTimeout Microseconds to add to the waiting time. * @param Registry $reg An optional registry to sync the * response with. * * @see getType() * @see getArgument() */ public function __construct(Communicator $com, $asStream = false, $sTimeout = 0, $usTimeout = null, Registry $reg = null) { if (null === $reg) { if ($com->getTransmitter()->isPersistent()) { $old = $com->getTransmitter()->lock(T\Stream::DIRECTION_RECEIVE); try { $this->_receive($com, $asStream, $sTimeout, $usTimeout); } catch (E $e) { $com->getTransmitter()->lock($old, true); throw $e; } $com->getTransmitter()->lock($old, true); } else { $this->_receive($com, $asStream, $sTimeout, $usTimeout); } } else { while (null === ($response = $reg->getNextResponse())) { $newResponse = new self($com, true, $sTimeout, $usTimeout); $tagInfo = $reg::parseTag($newResponse->getTag()); $newResponse->setTag($tagInfo[1]); if (!$reg->add($newResponse, $tagInfo[0])) { $response = $newResponse; break; } } $this->_type = $response->_type; $this->attributes = $response->attributes; $this->unrecognizedWords = $response->unrecognizedWords; $this->setTag($response->getTag()); if (!$asStream) { foreach ($this->attributes as $name => $value) { $this->setAttribute($name, stream_get_contents($value)); } foreach ($response->unrecognizedWords as $i => $value) { $this->unrecognizedWords[$i] = stream_get_contents($value); } } } }
/** * Sends a request over a communicator. * * @param Communicator $com The communicator to send the request over. * @param Registry $reg An optional registry to sync the request with. * * @return int The number of bytes sent. * @see Client::sendSync() * @see Client::sendAsync() */ public function send(Communicator $com, Registry $reg = null) { if (null !== $reg && (null != $this->getTag() || !$reg->isTaglessModeOwner())) { $originalTag = $this->getTag(); $this->setTag($reg->getOwnershipTag() . $originalTag); $bytes = $this->send($com); $this->setTag($originalTag); return $bytes; } if ($com->getTransmitter()->isPersistent()) { $old = $com->getTransmitter()->lock(T\Stream::DIRECTION_SEND); $bytes = $this->_send($com); $com->getTransmitter()->lock($old, true); return $bytes; } return $this->_send($com); }
public function testTaglessModePassing() { $com1 = new Communicator(\HOSTNAME, PORT, true); Client::login($com1, USERNAME, PASSWORD); $reg1 = new Registry('dummy'); $com2 = new Communicator(\HOSTNAME, PORT, true); $reg2 = new Registry('dummy'); $this->assertNotEquals($reg1->getOwnershipTag(), $reg2->getOwnershipTag()); $pingRequest1 = new Request('/ping address=' . HOSTNAME, null, 'ping'); $pingRequest1->send($com1, $reg1); $response1_1 = new Response($com1, false, null, null, $reg1); $cancelRequest = new Request('/cancel'); $reg1->setTaglessMode(true); $cancelRequest->setArgument('tag', $reg1->getOwnershipTag() . 'ping'); $cancelRequest->send($com1, $reg1); $pingRequest2 = new Request('/ping count=2 address=' . HOSTNAME, null, 'ping'); $pingRequest2->send($com2, $reg2); $response2_1 = new Response($com2, false, null, null, $reg2); $response2_2 = new Response($com2, false, null, null, $reg2); $response2_3 = new Response($com2, false, null, null, $reg2); $reg1->setTaglessMode(false); $com1->close(); $com2->close(); $this->assertEquals(Response::TYPE_DATA, $response2_1->getType()); $this->assertEquals(Response::TYPE_DATA, $response2_2->getType()); $this->assertEquals(Response::TYPE_FINAL, $response2_3->getType()); $response1_2 = new Response($com1, false, null, null, $reg1); $response1_3 = new Response($com1, false, null, null, $reg1); $this->assertEquals(Response::TYPE_DATA, $response1_1->getType()); $this->assertEquals(Response::TYPE_ERROR, $response1_2->getType()); $this->assertEquals(Response::TYPE_FINAL, $response1_3->getType()); $reg1->close(); $this->assertStringStartsWith('-1_', $reg2->getOwnershipTag()); }