Using long polling to reduce number of calls.
This commit is contained in:
@@ -170,6 +170,10 @@ public class GatheringParticipantCommunicationChannel implements GameStateListen
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean hasGameEvents() {
|
||||
return _events.size()>0;
|
||||
}
|
||||
|
||||
public Date getLastConsumed() {
|
||||
return _lastConsumed;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user