Using long polling to reduce number of calls.

This commit is contained in:
marcins78
2013-01-08 11:48:50 +00:00
parent f3fc0d74e8
commit c16161d919
5 changed files with 52 additions and 1 deletions

View File

@@ -170,6 +170,10 @@ public class GatheringParticipantCommunicationChannel implements GameStateListen
return result;
}
public boolean hasGameEvents() {
return _events.size()>0;
}
public Date getLastConsumed() {
return _lastConsumed;
}

View File

@@ -47,6 +47,10 @@ public class DefaultUserFeedback implements UserFeedback {
return _warnings.remove(playerId);
}
public boolean hasWarning(String playerId) {
return _warnings.containsKey(playerId);
}
public Set<String> getUsersPendingDecision() {
return _awaitingDecisionMap.keySet();
}

View File

@@ -333,6 +333,28 @@ public class LotroGameMediator {
}
}
public boolean hasAnyNewMessages(Player player, int channelNumber) throws PrivateInformationException, SubscriptionConflictException, SubscriptionExpiredException {
String playerName = player.getName();
if (!player.getType().contains("a") && !_allowSpectators && !_playersPlaying.contains(playerName))
throw new PrivateInformationException();
_readLock.lock();
try {
GatheringParticipantCommunicationChannel communicationChannel = _communicationChannels.get(playerName);
if (communicationChannel != null) {
if (communicationChannel.getChannelNumber() == channelNumber) {
return communicationChannel.hasGameEvents() || _userFeedback.hasWarning(playerName);
} else {
throw new SubscriptionConflictException();
}
} else {
throw new SubscriptionExpiredException();
}
} finally {
_readLock.unlock();
}
}
public void processCommunicationChannel(Player player, int channelNumber, ParticipantCommunicationVisitor visitor) throws PrivateInformationException, SubscriptionConflictException, SubscriptionExpiredException {
String playerName = player.getName();
if (!player.getType().contains("a") && !_allowSpectators && !_playersPlaying.contains(playerName))

View File

@@ -83,7 +83,18 @@ public class ChatResource extends AbstractResource {
}
try {
List<ChatMessage> chatMessages = chatRoom.getPendingMessages(resourceOwner.getName());
// Use long polling
List<ChatMessage> chatMessages;
int retry = 5;
do {
chatMessages = chatRoom.getPendingMessages(resourceOwner.getName());
retry--;
try {
Thread.sleep(1000);
} catch (InterruptedException exp) {
}
} while (retry>0 && chatMessages.size() == 0);
Collection<String> usersInRoom = chatRoom.getUsersInRoom();

View File

@@ -172,6 +172,16 @@ public class GameResource extends AbstractResource {
Document doc = documentBuilder.newDocument();
Element update = doc.createElement("update");
// Use long polling
int retry = 5;
while (retry>0 && !gameMediator.hasAnyNewMessages(resourceOwner, channelNumber)) {
retry--;
try {
Thread.sleep(1000);
} catch (InterruptedException exp) {
}
}
gameMediator.processCommunicationChannel(resourceOwner, channelNumber, new SerializationVisitor(doc, update));
doc.appendChild(update);