Home | History | Annotate | Download | only in packet
      1 /**
      2  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      3  * you may not use this file except in compliance with the License.
      4  * You may obtain a copy of the License at
      5  *
      6  *     http://www.apache.org/licenses/LICENSE-2.0
      7  *
      8  * Unless required by applicable law or agreed to in writing, software
      9  * distributed under the License is distributed on an "AS IS" BASIS,
     10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     11  * See the License for the specific language governing permissions and
     12  * limitations under the License.
     13  */
     14 package org.jivesoftware.smackx.pubsub.packet;
     15 
     16 import org.jivesoftware.smack.PacketCollector;
     17 import org.jivesoftware.smack.SmackConfiguration;
     18 import org.jivesoftware.smack.Connection;
     19 import org.jivesoftware.smack.XMPPException;
     20 import org.jivesoftware.smack.filter.PacketFilter;
     21 import org.jivesoftware.smack.filter.PacketIDFilter;
     22 import org.jivesoftware.smack.packet.Packet;
     23 
     24 /**
     25  * Utility class for doing synchronous calls to the server.  Provides several
     26  * methods for sending a packet to the server and waiting for the reply.
     27  *
     28  * @author Robin Collier
     29  */
     30 final public class SyncPacketSend
     31 {
     32 	private SyncPacketSend()
     33 	{	}
     34 
     35 	static public Packet getReply(Connection connection, Packet packet, long timeout)
     36 		throws XMPPException
     37 	{
     38         PacketFilter responseFilter = new PacketIDFilter(packet.getPacketID());
     39         PacketCollector response = connection.createPacketCollector(responseFilter);
     40 
     41         connection.sendPacket(packet);
     42 
     43         // Wait up to a certain number of seconds for a reply.
     44         Packet result = response.nextResult(timeout);
     45 
     46         // Stop queuing results
     47         response.cancel();
     48 
     49         if (result == null) {
     50             throw new XMPPException("No response from server.");
     51         }
     52         else if (result.getError() != null) {
     53             throw new XMPPException(result.getError());
     54         }
     55         return result;
     56 	}
     57 
     58 	static public Packet getReply(Connection connection, Packet packet)
     59 		throws XMPPException
     60 	{
     61 		return getReply(connection, packet, SmackConfiguration.getPacketReplyTimeout());
     62 	}
     63 }
     64