/** * Checks whether the queue item's target is the HOSTNAME_INVALID constant. * * @param Response $item The item to check. * * @return bool TRUE on success, FALSE on failure. */ function isHostnameInvalid(Response $item) { return $item->getProperty('target') === Test\HOSTNAME_INVALID . '/32'; }
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()); }
/** * Compares two respones. * * Compares two respones, based on criteria defined in * {@link static::$compareBy}. * * @param Response $itemA The response to compare. * @param Response $itemB The response to compare $a against. * * @return int Returns 0 if the two respones are equal according to every * criteria specified, -1 if $a should be placed before $b, and 1 if $b * should be placed before $a. */ protected function compare(Response $itemA, Response $itemB) { foreach ($this->compareBy as $name => $spec) { if (!is_string($name)) { $name = $spec; $spec = null; } $members = array(0 => $itemA->getProperty($name), 1 => $itemB->getProperty($name)); if (is_callable($spec)) { uasort($members, $spec); } elseif ($members[0] === $members[1]) { continue; } else { $flags = SORT_REGULAR; $order = SORT_ASC; if (is_array($spec)) { list($order, $flags) = $spec; } elseif (null !== $spec) { $order = $spec; } if (SORT_ASC === $order) { asort($members, $flags); } else { arsort($members, $flags); } } return key($members) === 0 ? -1 : 1; } return 0; }
/** * Add a response to the registry. * * @param Response $response The response to add. The caller of this * function is responsible for ensuring that the ownership tag and the * original tag are separated, so that only the original one remains in * the response. * @param string $ownershipTag The ownership tag that the response had. * * @return bool TRUE if the request was added to its buffer, FALSE if * this instance owns the response, and therefore doesn't need to add * the response to its buffer. */ public function add(Response $response, $ownershipTag) { if ($this->getOwnershipTag() === $ownershipTag || $this->isTaglessModeOwner() && $response->getType() !== Response::TYPE_FATAL) { return false; } if (null === $ownershipTag) { $this->shm->lock('taglessModeOwner'); if ($this->shm->exists('taglessModeOwner') && $response->getType() !== Response::TYPE_FATAL) { $ownershipTag = $this->shm->get('taglessModeOwner'); $this->shm->unlock('taglessModeOwner'); } else { $this->shm->unlock('taglessModeOwner'); foreach ($this->shm->getIterator('/^(responseBuffer\\_)/', true) as $targetBufferName) { $this->_add($response, $targetBufferName); } return true; } } $this->_add($response, 'responseBuffer_' . $ownershipTag); return true; }
/** * Dispatches the next response in queue. * * Dispatches the next response in queue, i.e. it executes the associated * callback if there is one, or places the response in the response buffer. * * @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. * * @throws SocketException When there's no response within the time limit. * @return Response The dispatched response. */ protected function dispatchNextResponse($sTimeout = 0, $usTimeout = 0) { $response = new Response($this->com, $this->_streamingResponses, $sTimeout, $usTimeout, $this->registry); if ($response->getType() === Response::TYPE_FATAL) { $this->pendingRequestsCount = 0; $this->com->close(); return $response; } $tag = $response->getTag(); $isLastForRequest = $response->getType() === Response::TYPE_FINAL; if ($isLastForRequest) { $this->pendingRequestsCount--; } if ('' != $tag) { if ($this->isRequestActive($tag, self::FILTER_CALLBACK)) { if ($this->callbacks[$tag]($response, $this)) { $this->cancelRequest($tag); } elseif ($isLastForRequest) { unset($this->callbacks[$tag]); } } else { $this->responseBuffer[$tag][] = $response; } } return $response; }