progress.beginTask("Connection Test with buddy " + plainJID, 68);
try {
Connection connection = sarosNet.getConnection();
if (connection == null || !connection.isConnected())
throw new XMPPException("Connection is not established!");
progress.worked(1);
String id = Packet.nextID();
progress.subTask("Checking if buddy is using Saros");
JID user = discoManager.getSupportingPresence(plainJID,
Saros.NAMESPACE);
if (user == null)
throw new XMPPException("Buddy " + plainJID
+ " is not using Saros");
progress.worked(1);
TransferDescription transferData = TransferDescription
.createTestTransferDescription(user, id, sarosNet.getMyJID());
progress.subTask("Generating Test Data");
byte[] testData = getTestArray(size);
progress.worked(1);
// Create a packet collector to listen for a response.
PacketCollector collector = connection
.createPacketCollector(new PacketIDFilter(id));
StopWatch watch = new StopWatch().start();
try {
try {
progress.subTask("Sending Data");
dataTransferManager.sendData(transferData, testData,
progress.newChild(40));
} catch (IOException e) {
throw new XMPPException("IOException sending data", e);
} catch (SarosCancellationException e) {
throw new XMPPException(
"CancellationException sending data", e);
}
progress.subTask("Waiting for reply");
ConnectionTestResponse response = null;
for (int i = 0; i < 15; i++) {
response = responseProvider.getPayload(collector
.nextResult(1000));
if (response != null)
break;
progress.worked(1);
}
result.transferTime = watch.stop().getTime();
if (response == null)
throw new XMPPException("Timeout after 15s");
if (response.errorMessage != null)
throw new XMPPException("An remote error occurred: "
+ response.errorMessage);
int localDataHash = Arrays.hashCode(testData);
if (response.dataHash != localDataHash)
throw new XMPPException(
"Hash results don't match: Received=="
+ response.dataHash + " expected==" + localDataHash);
result.mode = response.transferMode;