Exemplo n.º 1
0
 /**
  * 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);
             }
         }
     }
 }