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;
}
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();
}
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());
}
}
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);
}
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();
}
}
Aggregations