Search in sources :

Example 36 with PacketCollector

use of org.jivesoftware.smack.PacketCollector in project ecf by eclipse.

the class LastActivity method getLastActivity.

/**
 * Retrieve the last activity of a particular jid.
 * @param con the current Connection.
 * @param jid the JID of the user.
 * @return the LastActivity packet of the jid.
 * @throws XMPPException thrown if a server error has occured.
 * @deprecated This method only retreives the lapsed time since the last logout of a particular jid.
 * Replaced by {@link  org.jivesoftware.smackx.LastActivityManager#getLastActivity(Connection, String)  getLastActivity}
 */
public static LastActivity getLastActivity(Connection con, String jid) throws XMPPException {
    LastActivity activity = new LastActivity();
    jid = StringUtils.parseBareAddress(jid);
    activity.setTo(jid);
    PacketCollector collector = con.createPacketCollector(new PacketIDFilter(activity.getPacketID()));
    con.sendPacket(activity);
    LastActivity response = (LastActivity) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Cancel the collector.
    collector.cancel();
    if (response == null) {
        throw new XMPPException("No response from server on status set.");
    }
    if (response.getError() != null) {
        throw new XMPPException(response.getError());
    }
    return response;
}
Also used : PacketCollector(org.jivesoftware.smack.PacketCollector) XMPPException(org.jivesoftware.smack.XMPPException) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 37 with PacketCollector

use of org.jivesoftware.smack.PacketCollector in project ecf by eclipse.

the class OfflineMessageManager method getMessages.

/**
 * Returns an Iterator with the offline <tt>Messages</tt> whose stamp matches the specified
 * request. The request will include the list of stamps that uniquely identifies
 * the offline messages to retrieve. The returned offline messages will not be deleted
 * from the server. Use {@link #deleteMessages(java.util.List)} to delete the messages.
 *
 * @param nodes the list of stamps that uniquely identifies offline message.
 * @return an Iterator with the offline <tt>Messages</tt> that were received as part of
 *         this request.
 * @throws XMPPException If the user is not allowed to make this request or the server does
 *                       not support offline message retrieval.
 */
public Iterator<Message> getMessages(final List<String> nodes) throws XMPPException {
    List<Message> messages = new ArrayList<Message>();
    OfflineMessageRequest request = new OfflineMessageRequest();
    for (String node : nodes) {
        OfflineMessageRequest.Item item = new OfflineMessageRequest.Item(node);
        item.setAction("view");
        request.addItem(item);
    }
    // Filter packets looking for an answer from the server.
    PacketFilter responseFilter = new PacketIDFilter(request.getPacketID());
    PacketCollector response = connection.createPacketCollector(responseFilter);
    // Filter offline messages that were requested by this request
    PacketFilter messageFilter = new AndFilter(packetFilter, new PacketFilter() {

        public boolean accept(Packet packet) {
            OfflineMessageInfo info = (OfflineMessageInfo) packet.getExtension("offline", namespace);
            return nodes.contains(info.getNode());
        }
    });
    PacketCollector messageCollector = connection.createPacketCollector(messageFilter);
    // Send the retrieval request to the server.
    connection.sendPacket(request);
    // Wait up to a certain number of seconds for a reply.
    IQ answer = (IQ) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    response.cancel();
    if (answer == null) {
        throw new XMPPException("No response from server.");
    } else if (answer.getError() != null) {
        throw new XMPPException(answer.getError());
    }
    // Collect the received offline messages
    Message message = (Message) messageCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    while (message != null) {
        messages.add(message);
        message = (Message) messageCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    }
    // Stop queuing offline messages
    messageCollector.cancel();
    return messages.iterator();
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) Message(org.jivesoftware.smack.packet.Message) OfflineMessageInfo(org.jivesoftware.smackx.packet.OfflineMessageInfo) ArrayList(java.util.ArrayList) IQ(org.jivesoftware.smack.packet.IQ) OfflineMessageRequest(org.jivesoftware.smackx.packet.OfflineMessageRequest) PacketCollector(org.jivesoftware.smack.PacketCollector) XMPPException(org.jivesoftware.smack.XMPPException)

Example 38 with PacketCollector

use of org.jivesoftware.smack.PacketCollector in project ecf by eclipse.

the class OfflineMessageManager method deleteMessages.

/**
 * Deletes the specified list of offline messages. The request will include the list of
 * stamps that uniquely identifies the offline messages to delete.
 *
 * @param nodes the list of stamps that uniquely identifies offline message.
 * @throws XMPPException If the user is not allowed to make this request or the server does
 *                       not support offline message retrieval.
 */
public void deleteMessages(List<String> nodes) throws XMPPException {
    OfflineMessageRequest request = new OfflineMessageRequest();
    for (String node : nodes) {
        OfflineMessageRequest.Item item = new OfflineMessageRequest.Item(node);
        item.setAction("remove");
        request.addItem(item);
    }
    // Filter packets looking for an answer from the server.
    PacketFilter responseFilter = new PacketIDFilter(request.getPacketID());
    PacketCollector response = connection.createPacketCollector(responseFilter);
    // Send the deletion request to the server.
    connection.sendPacket(request);
    // Wait up to a certain number of seconds for a reply.
    IQ answer = (IQ) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    response.cancel();
    if (answer == null) {
        throw new XMPPException("No response from server.");
    } else if (answer.getError() != null) {
        throw new XMPPException(answer.getError());
    }
}
Also used : OfflineMessageRequest(org.jivesoftware.smackx.packet.OfflineMessageRequest) PacketCollector(org.jivesoftware.smack.PacketCollector) IQ(org.jivesoftware.smack.packet.IQ) XMPPException(org.jivesoftware.smack.XMPPException)

Example 39 with PacketCollector

use of org.jivesoftware.smack.PacketCollector in project ecf by eclipse.

the class OfflineMessageManager method deleteMessages.

/**
 * Deletes all offline messages of the user.
 *
 * @throws XMPPException If the user is not allowed to make this request or the server does
 *                       not support offline message retrieval.
 */
public void deleteMessages() throws XMPPException {
    OfflineMessageRequest request = new OfflineMessageRequest();
    request.setPurge(true);
    // Filter packets looking for an answer from the server.
    PacketFilter responseFilter = new PacketIDFilter(request.getPacketID());
    PacketCollector response = connection.createPacketCollector(responseFilter);
    // Send the deletion request to the server.
    connection.sendPacket(request);
    // Wait up to a certain number of seconds for a reply.
    IQ answer = (IQ) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    response.cancel();
    if (answer == null) {
        throw new XMPPException("No response from server.");
    } else if (answer.getError() != null) {
        throw new XMPPException(answer.getError());
    }
}
Also used : OfflineMessageRequest(org.jivesoftware.smackx.packet.OfflineMessageRequest) PacketCollector(org.jivesoftware.smack.PacketCollector) IQ(org.jivesoftware.smack.packet.IQ) XMPPException(org.jivesoftware.smack.XMPPException)

Example 40 with PacketCollector

use of org.jivesoftware.smack.PacketCollector in project ecf by eclipse.

the class KeepAliveManager method schedulePingServerTask.

/**
 * Cancels any existing periodic ping task if there is one and schedules a new ping task if pingInterval is greater
 * then zero.
 *
 * This is designed so only one executor is used for scheduling all pings on all connections.  This results in only 1 thread used for pinging.
 */
private synchronized void schedulePingServerTask() {
    enableExecutorService();
    stopPingServerTask();
    if (pingInterval > 0) {
        periodicPingTask = periodicPingExecutorService.schedule(new Runnable() {

            public void run() {
                Ping ping = new Ping();
                PacketFilter responseFilter = new PacketIDFilter(ping.getPacketID());
                final PacketCollector response = pingFailedListeners.isEmpty() ? null : connection.createPacketCollector(responseFilter);
                connection.sendPacket(ping);
                if (response != null) {
                    // Schedule a collector for the ping reply, notify listeners if none is received.
                    periodicPingExecutorService.schedule(new Runnable() {

                        public void run() {
                            Packet result = response.nextResult(1);
                            // Stop queuing results
                            response.cancel();
                            // The actual result of the reply can be ignored since we only care if we actually got one.
                            if (result == null) {
                                for (PingFailedListener listener : pingFailedListeners) {
                                    listener.pingFailed();
                                }
                            }
                        }
                    }, SmackConfiguration.getPacketReplyTimeout(), TimeUnit.MILLISECONDS);
                }
            }
        }, getPingInterval(), TimeUnit.MILLISECONDS);
    }
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) PingFailedListener(org.jivesoftware.smack.ping.PingFailedListener) Ping(org.jivesoftware.smack.ping.packet.Ping) PacketCollector(org.jivesoftware.smack.PacketCollector) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Aggregations

PacketCollector (org.jivesoftware.smack.PacketCollector)46 XMPPException (org.jivesoftware.smack.XMPPException)44 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)36 IQ (org.jivesoftware.smack.packet.IQ)24 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)21 Packet (org.jivesoftware.smack.packet.Packet)10 ArrayList (java.util.ArrayList)6 AndFilter (org.jivesoftware.smack.filter.AndFilter)6 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)6 MUCAdmin (org.jivesoftware.smackx.packet.MUCAdmin)6 MUCOwner (org.jivesoftware.smackx.packet.MUCOwner)6 FromMatchesFilter (org.jivesoftware.smack.filter.FromMatchesFilter)4 Message (org.jivesoftware.smack.packet.Message)4 OfflineMessageRequest (org.jivesoftware.smackx.packet.OfflineMessageRequest)4 PacketInterceptor (org.jivesoftware.smack.PacketInterceptor)3 Presence (org.jivesoftware.smack.packet.Presence)3 MUCInitialPresence (org.jivesoftware.smackx.packet.MUCInitialPresence)3 Registration (org.jivesoftware.smack.packet.Registration)2 StreamInitiation (org.jivesoftware.smackx.packet.StreamInitiation)2 AgentInfo (org.jivesoftware.smackx.workgroup.packet.AgentInfo)2