Using long polling to reduce number of calls.
This commit is contained in:
@@ -170,6 +170,10 @@ public class GatheringParticipantCommunicationChannel implements GameStateListen
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean hasGameEvents() {
|
||||||
|
return _events.size()>0;
|
||||||
|
}
|
||||||
|
|
||||||
public Date getLastConsumed() {
|
public Date getLastConsumed() {
|
||||||
return _lastConsumed;
|
return _lastConsumed;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,6 +47,10 @@ public class DefaultUserFeedback implements UserFeedback {
|
|||||||
return _warnings.remove(playerId);
|
return _warnings.remove(playerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean hasWarning(String playerId) {
|
||||||
|
return _warnings.containsKey(playerId);
|
||||||
|
}
|
||||||
|
|
||||||
public Set<String> getUsersPendingDecision() {
|
public Set<String> getUsersPendingDecision() {
|
||||||
return _awaitingDecisionMap.keySet();
|
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 {
|
public void processCommunicationChannel(Player player, int channelNumber, ParticipantCommunicationVisitor visitor) throws PrivateInformationException, SubscriptionConflictException, SubscriptionExpiredException {
|
||||||
String playerName = player.getName();
|
String playerName = player.getName();
|
||||||
if (!player.getType().contains("a") && !_allowSpectators && !_playersPlaying.contains(playerName))
|
if (!player.getType().contains("a") && !_allowSpectators && !_playersPlaying.contains(playerName))
|
||||||
|
|||||||
@@ -83,7 +83,18 @@ public class ChatResource extends AbstractResource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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();
|
Collection<String> usersInRoom = chatRoom.getUsersInRoom();
|
||||||
|
|
||||||
|
|||||||
@@ -172,6 +172,16 @@ public class GameResource extends AbstractResource {
|
|||||||
Document doc = documentBuilder.newDocument();
|
Document doc = documentBuilder.newDocument();
|
||||||
Element update = doc.createElement("update");
|
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));
|
gameMediator.processCommunicationChannel(resourceOwner, channelNumber, new SerializationVisitor(doc, update));
|
||||||
|
|
||||||
doc.appendChild(update);
|
doc.appendChild(update);
|
||||||
|
|||||||
Reference in New Issue
Block a user