/**
  * {@inheritdoc}
  */
 public function listen()
 {
     $this->xmpp->connectAndStartSession($this->timeout);
     // Add the message handler
     $this->xmpp->addXPathHandler('{jabber:client}message/{http://jabber.org/protocol/pubsub#event}event/{http://superfeedr.com/xmpp-pubsub-ext}status', 'handleMessage', $this);
     // Process messages repeatedly
     while (!$this->xmpp->isDisconnected()) {
         $results = $this->xmpp->processUntil('message', $this->timeout);
         if (count($results) === 0) {
             $this->logger->warn(sprintf('Superfeedr listener timed out (no messages for %d seconds).', $this->timeout));
             throw new Exception\TimeoutException(sprintf('No messages for %d seconds.  The connection may have been lost.', $this->timeout));
         }
     }
 }
 /**
  * Handle a response to a subscription request from our connection, and set
  * our success state appropriately.
  * @param \XMPPHP_XMLObj $response The server's response to a subscription
  * request.
  */
 public function handleResponse(\XMPPHP_XMLObj $response)
 {
     if ($response->attrs['type'] == 'result') {
         $this->successful = true;
     } else {
         $this->successful = false;
     }
     $this->xmpp->event('handle_subscription');
 }
    /**
     * Make sure we time out after the appropriate number of iterations with an
     * incomplete buffer that appears disconnected.
     */
    public function testTimeoutOnStuckBuffer()
    {
        // An incomplete buffer
        $xml = <<<XML
<iq from='firehoser.superfeedr.com' to='hearsayer@superfeedr.com/superfeedr' type='result' id='10'>
  <pubsub xmlns='http://jabber.org/protocol/pubsub'>
    <subscription node='http://feeds.feedburner.com/crunchgear' jid='*****@*****.**' subscription='subscribed'>
      <status xmlns='http://superfeedr.com/xmpp-pubsub-ext'>
        <http code='304'>Content not modified</http>
        <next_fetch>2011-06-13T06:42:37+00:00</next_fetch>
        <title>CrunchGear</title>
XML;
        $xmpp = new SuperfeedrXmpp('superfeedr.com', 5222, 'user', 'pass');
        $method = new \ReflectionMethod('Hearsay\\SuperfeedrBundle\\Xmpp\\SuperfeedrXmpp', 'bufferComplete');
        $method->setAccessible(true);
        $xmpp->setMaxIncompleteIterations(2);
        // Run the buffer through a few times without the timeout check
        $this->assertFalse($method->invoke($xmpp, $xml), 'Expected buffer to be incomplete');
        // No incomplete iterations
        $this->assertFalse($method->invoke($xmpp, $xml), 'Expected buffer to be incomplete');
        // First incomplete iteration
        $this->assertFalse($method->invoke($xmpp, $xml), 'Expected buffer to be incomplete');
        // Second incomplete iteration
        $this->assertFalse($method->invoke($xmpp, $xml), 'Expected buffer to be incomplete');
        // Third incomplete iteration, so running count > max allowed; next should trip the timeout check
        $this->setExpectedException('Hearsay\\SuperfeedrBundle\\Exception\\TimeoutException');
        $method->invoke($xmpp, $xml);
    }