blob: 080129bd03e6eb4ec63a578a390576491696cb3a [file] [log] [blame]
Shuyi Chend7955ce2013-05-22 14:51:55 -07001/**
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 */
14package org.jivesoftware.smackx.pubsub.packet;
15
16import org.jivesoftware.smack.PacketCollector;
17import org.jivesoftware.smack.SmackConfiguration;
18import org.jivesoftware.smack.Connection;
19import org.jivesoftware.smack.XMPPException;
20import org.jivesoftware.smack.filter.PacketFilter;
21import org.jivesoftware.smack.filter.PacketIDFilter;
22import 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 */
30final 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}