Skip to content

Commit

Permalink
Fix an issue with message timeouts when retrieving the current activi…
Browse files Browse the repository at this point in the history
…ty during a start activity operation
  • Loading branch information
tuck182 committed Aug 27, 2014
1 parent dd405bc commit adc04d3
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/main/java/net/whistlingfish/harmony/HarmonyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

import javax.inject.Inject;

Expand Down Expand Up @@ -69,6 +70,12 @@ public class HarmonyClient {
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private ScheduledFuture<?> heartbeat;

/*
* To prevent timeouts when different threads send a message and expect a response, create a lock that only allows a
* single thread at a time to perform a send/receive action.
*/
private ReentrantLock messageLock = new ReentrantLock();

@Inject
private AuthService authService;

Expand Down Expand Up @@ -189,12 +196,14 @@ private Packet sendOAPacket(XMPPTCPConnection authConnection, OAPacket packet) {
private Packet sendOAPacket(XMPPTCPConnection authConnection, OAPacket packet, long replyTimeout) {
PacketCollector collector = authConnection.createPacketCollector(new EmptyIncrementedIdReplyFilter(packet,
authConnection));
messageLock.lock();
try {
authConnection.sendPacket(packet);
return getNextPacketSkipContinues(collector, replyTimeout);
} catch (SmackException | XMPPErrorException e) {
throw new RuntimeException("Failed communicating with Harmony Hub", e);
} finally {
messageLock.unlock();
collector.cancel();
}
}
Expand All @@ -206,18 +215,20 @@ private <R extends OAPacket> R sendOAPacket(XMPPTCPConnection authConnection, OA
private <R extends OAPacket> R sendOAPacket(XMPPTCPConnection authConnection, OAPacket packet, Class<R> replyClass,
long replyTimeout) {
PacketCollector collector = authConnection.createPacketCollector(new OAReplyFilter(packet, authConnection));
messageLock.lock();
try {
authConnection.sendPacket(packet);
return replyClass.cast(getNextPacketSkipContinues(collector, replyTimeout));
} catch (SmackException | XMPPErrorException e) {
throw new RuntimeException("Failed communicating with Harmony Hub", e);
} finally {
messageLock.unlock();
collector.cancel();
}
}

private Packet getNextPacketSkipContinues(PacketCollector collector, long replyTimeout)
throws NoResponseException, XMPPErrorException {
private Packet getNextPacketSkipContinues(PacketCollector collector, long replyTimeout) throws NoResponseException,
XMPPErrorException {
while (true) {
Packet reply = collector.nextResult(replyTimeout);
if (reply == null) {
Expand Down

0 comments on commit adc04d3

Please sign in to comment.