Search in sources :

Example 16 with PacketCollector

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

the class StreamNegotiator method initiateIncomingStream.

Packet initiateIncomingStream(Connection connection, StreamInitiation initiation) throws XMPPException {
    StreamInitiation response = createInitiationAccept(initiation, getNamespaces());
    // establish collector to await response
    PacketCollector collector = connection.createPacketCollector(getInitiationPacketFilter(initiation.getFrom(), initiation.getSessionID()));
    connection.sendPacket(response);
    Packet streamMethodInitiation = collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    collector.cancel();
    if (streamMethodInitiation == null) {
        throw new XMPPException("No response from file transfer initiator");
    }
    return streamMethodInitiation;
}
Also used : StreamInitiation(org.jivesoftware.smackx.packet.StreamInitiation) Packet(org.jivesoftware.smack.packet.Packet) PacketCollector(org.jivesoftware.smack.PacketCollector) XMPPException(org.jivesoftware.smack.XMPPException)

Example 17 with PacketCollector

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

the class PrivateDataManager method getPrivateData.

/**
 * Returns the private data specified by the given element name and namespace. Each chunk
 * of private data is uniquely identified by an element name and namespace pair.<p>
 *
 * If a PrivateDataProvider is registered for the specified element name/namespace pair then
 * that provider will determine the specific object type that is returned. If no provider
 * is registered, a {@link DefaultPrivateData} instance will be returned.
 *
 * @param elementName the element name.
 * @param namespace the namespace.
 * @return the private data.
 * @throws XMPPException if an error occurs getting the private data.
 */
public PrivateData getPrivateData(final String elementName, final String namespace) throws XMPPException {
    // Create an IQ packet to get the private data.
    IQ privateDataGet = new IQ() {

        public String getChildElementXML() {
            StringBuilder buf = new StringBuilder();
            buf.append("<query xmlns=\"jabber:iq:private\">");
            buf.append("<").append(elementName).append(" xmlns=\"").append(namespace).append("\"/>");
            buf.append("</query>");
            return buf.toString();
        }
    };
    privateDataGet.setType(IQ.Type.GET);
    // Address the packet to the other account if user has been set.
    if (user != null) {
        privateDataGet.setTo(user);
    }
    // Setup a listener for the reply to the set operation.
    String packetID = privateDataGet.getPacketID();
    PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(packetID));
    // Send the private data.
    connection.sendPacket(privateDataGet);
    // Wait up to five seconds for a response from the server.
    IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    collector.cancel();
    if (response == null) {
        throw new XMPPException("No response from the server.");
    } else // If the server replied with an error, throw an exception.
    if (response.getType() == IQ.Type.ERROR) {
        throw new XMPPException(response.getError());
    }
    return ((PrivateDataResult) response).getPrivateData();
}
Also used : IQ(org.jivesoftware.smack.packet.IQ) PacketCollector(org.jivesoftware.smack.PacketCollector) XMPPException(org.jivesoftware.smack.XMPPException) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 18 with PacketCollector

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

the class PrivateDataManager method setPrivateData.

/**
 * Sets a private data value. Each chunk of private data is uniquely identified by an
 * element name and namespace pair. If private data has already been set with the
 * element name and namespace, then the new private data will overwrite the old value.
 *
 * @param privateData the private data.
 * @throws XMPPException if setting the private data fails.
 */
public void setPrivateData(final PrivateData privateData) throws XMPPException {
    // Create an IQ packet to set the private data.
    IQ privateDataSet = new IQ() {

        public String getChildElementXML() {
            StringBuilder buf = new StringBuilder();
            buf.append("<query xmlns=\"jabber:iq:private\">");
            buf.append(privateData.toXML());
            buf.append("</query>");
            return buf.toString();
        }
    };
    privateDataSet.setType(IQ.Type.SET);
    // Address the packet to the other account if user has been set.
    if (user != null) {
        privateDataSet.setTo(user);
    }
    // Setup a listener for the reply to the set operation.
    String packetID = privateDataSet.getPacketID();
    PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(packetID));
    // Send the private data.
    connection.sendPacket(privateDataSet);
    // Wait up to five seconds for a response from the server.
    IQ response = (IQ) collector.nextResult(5000);
    // Stop queuing results
    collector.cancel();
    if (response == null) {
        throw new XMPPException("No response from the server.");
    } else // If the server replied with an error, throw an exception.
    if (response.getType() == IQ.Type.ERROR) {
        throw new XMPPException(response.getError());
    }
}
Also used : IQ(org.jivesoftware.smack.packet.IQ) PacketCollector(org.jivesoftware.smack.PacketCollector) XMPPException(org.jivesoftware.smack.XMPPException) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 19 with PacketCollector

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

the class RemoteCommand method executeAction.

/**
 * Executes the <code>action</codo> with the <code>form</code>.
 * The action could be any of the available actions. The form must
 * be the anwser of the previous stage. It can be <tt>null</tt> if it is the first stage.
 *
 * @param action the action to execute.
 * @param form the form with the information.
 * @param timeout the amount of time to wait for a reply.
 * @throws XMPPException if there is a problem executing the command.
 */
private void executeAction(Action action, Form form, long timeout) throws XMPPException {
    // TODO: Check that all the required fields of the form were filled, if
    // TODO: not throw the corresponding exeption. This will make a faster response,
    // TODO: since the request is stoped before it's sent.
    AdHocCommandData data = new AdHocCommandData();
    data.setType(IQ.Type.SET);
    data.setTo(getOwnerJID());
    data.setNode(getNode());
    data.setSessionID(sessionID);
    data.setAction(action);
    if (form != null) {
        data.setForm(form.getDataFormToSend());
    }
    PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(data.getPacketID()));
    connection.sendPacket(data);
    Packet response = collector.nextResult(timeout);
    // 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());
    }
    AdHocCommandData responseData = (AdHocCommandData) response;
    this.sessionID = responseData.getSessionID();
    super.setData(responseData);
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) AdHocCommandData(org.jivesoftware.smackx.packet.AdHocCommandData) PacketCollector(org.jivesoftware.smack.PacketCollector) XMPPException(org.jivesoftware.smack.XMPPException) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 20 with PacketCollector

use of org.jivesoftware.smack.PacketCollector in project Openfire by igniterealtime.

the class ThrottleTestWriter method main.

/**
 * Starts the throttle test write client.
 *
 * @param args application arguments.
 */
public static void main(String[] args) {
    if (args.length != 3) {
        System.out.println("Usage: java ThrottleTestWriter [server] [username] [password]");
        System.exit(0);
    }
    String server = args[0];
    String username = args[1];
    String password = args[2];
    try {
        // Connect to the server, without TLS encryption.
        ConnectionConfiguration config = new ConnectionConfiguration(server);
        config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
        final XMPPConnection con = new XMPPConnection(config);
        System.out.print("Connecting to " + server + "... ");
        con.connect();
        con.login(username, password, "writer");
        System.out.print("success.");
        System.out.println("");
        // Get the "real" server address.
        server = con.getServiceName();
        String writerAddress = username + "@" + server + "/writer";
        final String readerAddress = username + "@" + server + "/reader";
        System.out.println("Registered as " + writerAddress);
        // Look for the reader process.
        System.out.print("Looking for " + readerAddress + "...");
        while (true) {
            IQ testIQ = new Time();
            testIQ.setType(IQ.Type.GET);
            testIQ.setTo(readerAddress);
            PacketCollector collector = con.createPacketCollector(new PacketIDFilter(testIQ.getPacketID()));
            con.sendPacket(testIQ);
            // Wait 5 seconds.
            long start = System.currentTimeMillis();
            Packet result = collector.nextResult(5000);
            collector.cancel();
            // If we got a result, continue.
            if (result != null && result.getError() == null) {
                System.out.println(" found reader. Starting packet flood.");
                break;
            }
            System.out.print(".");
            long end = System.currentTimeMillis();
            if (end - start < 5000) {
                try {
                    Thread.sleep(5000 - (end - start));
                } catch (Exception e) {
                // ignore.
                }
            }
        }
        // Create a process to log how many packets we're writing out.
        Runnable statsRunnable = new Runnable() {

            public void run() {
                while (!done) {
                    try {
                        Thread.sleep(5000);
                    } catch (Exception e) {
                    /* ignore */
                    }
                    int count = packetCount.getAndSet(0);
                    System.out.println("Packets per second: " + (count / 5));
                }
            }
        };
        Thread statsThread = new Thread(statsRunnable);
        statsThread.setDaemon(true);
        statsThread.start();
        // Now start flooding packets.
        Message testMessage = new Message(readerAddress);
        testMessage.setBody("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        while (!done) {
            con.sendPacket(testMessage);
            packetCount.getAndIncrement();
        }
    } catch (Exception e) {
        System.out.println("\nError: " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) Message(org.jivesoftware.smack.packet.Message) IQ(org.jivesoftware.smack.packet.IQ) Time(org.jivesoftware.smackx.packet.Time) XMPPConnection(org.jivesoftware.smack.XMPPConnection) ConnectionConfiguration(org.jivesoftware.smack.ConnectionConfiguration) 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