Extracting direct access to DraftChannelCommunication.

This commit is contained in:
marcins78
2013-01-18 17:23:16 +00:00
parent 1db82b8928
commit 27828408cf
8 changed files with 113 additions and 108 deletions

View File

@@ -3,5 +3,5 @@ package com.gempukku.lotro.async;
public interface LongPollingResource {
public boolean isChanged();
public void process();
public void processIfNotProcessed();
}

View File

@@ -41,7 +41,7 @@ public class LongPollingSystem {
LongPollingResource resource = _waitingActions.poll();
try {
if (resource.isChanged())
resource.process();
resource.processIfNotProcessed();
else
_waitingActions.add(resource);
} catch (Exception exp) {
@@ -67,8 +67,8 @@ public class LongPollingSystem {
}
@Override
public void process() {
_longPollingResource.process();
public void processIfNotProcessed() {
_longPollingResource.processIfNotProcessed();
}
}
@@ -79,7 +79,7 @@ public class LongPollingSystem {
}
@Override
public void process() {
public void processIfNotProcessed() {
pause();
_waitingActions.add(this);
}

View File

@@ -68,7 +68,7 @@ public class ChatRequestHandler extends LotroServerRequestHandler implements Uri
ChatUpdateLongPollingResource polledResource = new ChatUpdateLongPollingResource(chatRoom, room, resourceOwner.getName(), responseWriter);
if (polledResource.isChanged())
polledResource.process();
polledResource.processIfNotProcessed();
else
_longPollingSystem.appendLongPollingResource(polledResource);
}
@@ -78,6 +78,7 @@ public class ChatRequestHandler extends LotroServerRequestHandler implements Uri
private String _room;
private String _playerId;
private ResponseWriter _responseWriter;
private boolean _processed;
private ChatUpdateLongPollingResource(ChatRoomMediator chatRoom, String room, String playerId, ResponseWriter responseWriter) {
_chatRoom = chatRoom;
@@ -96,24 +97,27 @@ public class ChatRequestHandler extends LotroServerRequestHandler implements Uri
}
@Override
public void process() {
try {
List<ChatMessage> chatMessages = _chatRoom.getChatRoomListener(_playerId).consumeMessages();
public synchronized void processIfNotProcessed() {
if (!_processed) {
try {
List<ChatMessage> chatMessages = _chatRoom.getChatRoomListener(_playerId).consumeMessages();
Collection<String> usersInRoom = _chatRoom.getUsersInRoom();
Collection<String> usersInRoom = _chatRoom.getUsersInRoom();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.newDocument();
Document doc = documentBuilder.newDocument();
serializeChatRoomData(_room, chatMessages, usersInRoom, doc);
serializeChatRoomData(_room, chatMessages, usersInRoom, doc);
_responseWriter.writeXmlResponse(doc);
} catch (SubscriptionExpiredException exp) {
_responseWriter.writeError(410);
} catch (Exception exp) {
_responseWriter.writeError(500);
_responseWriter.writeXmlResponse(doc);
} catch (SubscriptionExpiredException exp) {
_responseWriter.writeError(410);
} catch (Exception exp) {
_responseWriter.writeError(500);
}
_processed = true;
}
}
}

View File

@@ -88,7 +88,7 @@ public class GameRequestHandler extends LotroServerRequestHandler implements Uri
GameUpdateLongPollingResource pollingResource = new GameUpdateLongPollingResource(channelNumber, gameMediator, resourceOwner, responseWriter);
if (pollingResource.isChanged())
pollingResource.process();
pollingResource.processIfNotProcessed();
else
_longPollingSystem.appendLongPollingResource(pollingResource);
}
@@ -98,6 +98,7 @@ public class GameRequestHandler extends LotroServerRequestHandler implements Uri
private Player _resourceOwner;
private int _channelNumber;
private ResponseWriter _responseWriter;
private boolean _processed;
private GameUpdateLongPollingResource(int channelNumber, LotroGameMediator gameMediator, Player resourceOwner, ResponseWriter responseWriter) {
_channelNumber = channelNumber;
@@ -120,28 +121,31 @@ public class GameRequestHandler extends LotroServerRequestHandler implements Uri
}
@Override
public void process() {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
public synchronized void processIfNotProcessed() {
if (!_processed) {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.newDocument();
Element update = doc.createElement("update");
Document doc = documentBuilder.newDocument();
Element update = doc.createElement("update");
GatheringParticipantCommunicationChannel communicationChannel = _gameMediator.getCommunicationChannel(_resourceOwner, _channelNumber);
_gameMediator.processVisitor(communicationChannel, _channelNumber, _resourceOwner.getName(), new SerializationVisitor(doc, update));
GatheringParticipantCommunicationChannel communicationChannel = _gameMediator.getCommunicationChannel(_resourceOwner, _channelNumber);
_gameMediator.processVisitor(communicationChannel, _channelNumber, _resourceOwner.getName(), new SerializationVisitor(doc, update));
doc.appendChild(update);
doc.appendChild(update);
_responseWriter.writeXmlResponse(doc);
} catch (SubscriptionConflictException exp) {
_responseWriter.writeError(409);
} catch (PrivateInformationException e) {
_responseWriter.writeError(403);
} catch (SubscriptionExpiredException e) {
_responseWriter.writeError(410);
} catch (Exception e) {
_responseWriter.writeError(500);
_responseWriter.writeXmlResponse(doc);
} catch (SubscriptionConflictException exp) {
_responseWriter.writeError(409);
} catch (PrivateInformationException e) {
_responseWriter.writeError(403);
} catch (SubscriptionExpiredException e) {
_responseWriter.writeError(410);
} catch (Exception e) {
_responseWriter.writeError(500);
}
_processed = true;
}
}

View File

@@ -83,7 +83,7 @@ public class HallRequestHandler extends LotroServerRequestHandler implements Uri
} else if (uri.startsWith("/tournament/") && uri.endsWith("/leave") && request.getMethod() == HttpMethod.POST) {
dropFromTournament(request, uri.substring(12, uri.length() - 6), responseWriter);
} else if (uri.startsWith("/") && uri.endsWith("/leave") && request.getMethod() == HttpMethod.POST) {
leaveTable(request, uri.substring(1, uri.length()-6), responseWriter);
leaveTable(request, uri.substring(1, uri.length() - 6), responseWriter);
} else if (uri.startsWith("/") && request.getMethod() == HttpMethod.POST) {
joinTable(request, uri.substring(1), responseWriter);
} else {
@@ -131,7 +131,7 @@ public class HallRequestHandler extends LotroServerRequestHandler implements Uri
DraftUpdateLongPollingResource polledResource = new DraftUpdateLongPollingResource(tournamentId, resourceOwner, channelNumber, responseWriter);
if (polledResource.isChanged())
polledResource.process();
polledResource.processIfNotProcessed();
else
_longPollingSystem.appendLongPollingResource(polledResource);
}
@@ -141,6 +141,7 @@ public class HallRequestHandler extends LotroServerRequestHandler implements Uri
private int _channelNumber;
private String _tournamentId;
private ResponseWriter _responseWriter;
private boolean _processed;
private DraftUpdateLongPollingResource(String tournamentId, Player player, int channelNumber, ResponseWriter responseWriter) {
_tournamentId = tournamentId;
@@ -161,32 +162,35 @@ public class HallRequestHandler extends LotroServerRequestHandler implements Uri
}
}
public void process() {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.newDocument();
Element draft = doc.createElement("draft");
public synchronized void processIfNotProcessed() {
if (!_processed) {
try {
_hallServer.processChangesInDraft(_tournamentId, _player, _channelNumber, new SerializeDraftVisitor(doc, draft));
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
doc.appendChild(draft);
Document doc = documentBuilder.newDocument();
_responseWriter.writeXmlResponse(doc);
} catch (DraftFinishedException e) {
throw new HttpProcessingException(204);
} catch (SubscriptionConflictException e) {
throw new HttpProcessingException(409);
} catch (SubscriptionExpiredException e) {
throw new HttpProcessingException(410);
Element draft = doc.createElement("draft");
try {
_hallServer.processChangesInDraft(_tournamentId, _player, _channelNumber, new SerializeDraftVisitor(doc, draft));
doc.appendChild(draft);
_responseWriter.writeXmlResponse(doc);
} catch (DraftFinishedException e) {
throw new HttpProcessingException(204);
} catch (SubscriptionConflictException e) {
throw new HttpProcessingException(409);
} catch (SubscriptionExpiredException e) {
throw new HttpProcessingException(410);
}
} catch (HttpProcessingException exp) {
_responseWriter.writeError(exp.getStatus());
} catch (Exception exp) {
_responseWriter.writeError(500);
}
} catch (HttpProcessingException exp) {
_responseWriter.writeError(exp.getStatus());
} catch (Exception exp) {
_responseWriter.writeError(500);
_processed = true;
}
}
}
@@ -424,7 +428,7 @@ public class HallRequestHandler extends LotroServerRequestHandler implements Uri
HallUpdateLongPollingResource polledResource = new HallUpdateLongPollingResource(request, resourceOwner, channelNumber, responseWriter);
if (polledResource.isChanged())
polledResource.process();
polledResource.processIfNotProcessed();
else
_longPollingSystem.appendLongPollingResource(polledResource);
}
@@ -434,6 +438,7 @@ public class HallRequestHandler extends LotroServerRequestHandler implements Uri
private Player _resourceOwner;
private int _channelNumber;
private ResponseWriter _responseWriter;
private boolean _processed;
private HallUpdateLongPollingResource(HttpRequest request, Player resourceOwner, int channelNumber, ResponseWriter responseWriter) {
_request = request;
@@ -454,33 +459,36 @@ public class HallRequestHandler extends LotroServerRequestHandler implements Uri
}
@Override
public void process() {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.newDocument();
Element hall = doc.createElement("hall");
public synchronized void processIfNotProcessed() {
if (!_processed) {
try {
_hallServer.getCommunicationChannel(_resourceOwner, _channelNumber).processCommunicationChannel(_hallServer, _resourceOwner, new SerializeHallInfoVisitor(doc, hall));
} catch (SubscriptionExpiredException exp) {
throw new HttpProcessingException(410);
} catch (SubscriptionConflictException exp) {
throw new HttpProcessingException(409);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.newDocument();
Element hall = doc.createElement("hall");
try {
_hallServer.getCommunicationChannel(_resourceOwner, _channelNumber).processCommunicationChannel(_hallServer, _resourceOwner, new SerializeHallInfoVisitor(doc, hall));
} catch (SubscriptionExpiredException exp) {
throw new HttpProcessingException(410);
} catch (SubscriptionConflictException exp) {
throw new HttpProcessingException(409);
}
hall.setAttribute("currency", String.valueOf(_collectionManager.getPlayerCollection(_resourceOwner, "permanent").getCurrency()));
doc.appendChild(hall);
Map<String, String> headers = new HashMap<String, String>();
processDeliveryServiceNotification(_request, headers);
_responseWriter.writeXmlResponse(doc, headers);
} catch (HttpProcessingException exp) {
_responseWriter.writeError(exp.getStatus());
} catch (Exception exp) {
_responseWriter.writeError(500);
}
hall.setAttribute("currency", String.valueOf(_collectionManager.getPlayerCollection(_resourceOwner, "permanent").getCurrency()));
doc.appendChild(hall);
Map<String, String> headers = new HashMap<String, String>();
processDeliveryServiceNotification(_request, headers);
_responseWriter.writeXmlResponse(doc, headers);
} catch (HttpProcessingException exp) {
_responseWriter.writeError(exp.getStatus());
} catch (Exception exp) {
_responseWriter.writeError(500);
_processed = true;
}
}
}

View File

@@ -82,11 +82,11 @@ public class DefaultDraft implements Draft {
draftCommunicationChannel.processCommunicationChannel(getCardChoice(playerName), draftChannelVisitor);
}
public boolean hasChanges(String playerName, int channelNumber) throws SubscriptionExpiredException, SubscriptionConflictException {
public DraftCommunicationChannel getCommunicationChannel(String playerName, int channelNumber) throws SubscriptionExpiredException, SubscriptionConflictException {
DraftCommunicationChannel communicationChannel = _playerDraftCommunications.get(playerName);
if (communicationChannel != null) {
if (communicationChannel.getChannelNumber() == channelNumber) {
return communicationChannel.hasChangesInCommunicationChannel(getCardChoice(playerName));
return communicationChannel;
} else {
throw new SubscriptionConflictException();
}
@@ -95,20 +95,7 @@ public class DefaultDraft implements Draft {
}
}
public void processDraft(String playerName, int channelNumber, DraftChannelVisitor draftChannelVisitor) throws SubscriptionExpiredException, SubscriptionConflictException {
DraftCommunicationChannel communicationChannel = _playerDraftCommunications.get(playerName);
if (communicationChannel != null) {
if (communicationChannel.getChannelNumber() == channelNumber) {
communicationChannel.processCommunicationChannel(getCardChoice(playerName), draftChannelVisitor);
} else {
throw new SubscriptionConflictException();
}
} else {
throw new SubscriptionExpiredException();
}
}
private DraftCardChoice getCardChoice(String playerName) {
public DraftCardChoice getCardChoice(String playerName) {
MutableCardCollection cardChoice = _cardChoice.get(playerName);
return new DefaultDraftCardChoice(cardChoice, _lastPickStart + PICK_TIME);

View File

@@ -10,8 +10,10 @@ public interface Draft {
public void playerChosenCard(String playerName, String cardId);
public void signUpForDraft(String playerName, DraftChannelVisitor draftChannelVisitor);
public boolean hasChanges(String playerName, int channelNumber) throws SubscriptionExpiredException, SubscriptionConflictException;
public void processDraft(String playerName, int channelNumber, DraftChannelVisitor draftChannelVisitor) throws SubscriptionExpiredException, SubscriptionConflictException;
public DraftCommunicationChannel getCommunicationChannel(String playerName, int channelNumber) throws SubscriptionExpiredException, SubscriptionConflictException;
public DraftCardChoice getCardChoice(String playerName);
public boolean isFinished();
}

View File

@@ -372,7 +372,7 @@ public class HallServer extends AbstractServer {
Draft draft = tournament.getDraft();
if (draft == null)
throw new DraftFinishedException();
return draft.hasChanges(player.getName(), channelNumber);
return draft.getCommunicationChannel(player.getName(), channelNumber).hasChangesInCommunicationChannel(draft.getCardChoice(player.getName()));
} finally {
_hallDataAccessLock.readLock().unlock();
}
@@ -388,7 +388,7 @@ public class HallServer extends AbstractServer {
Draft draft = tournament.getDraft();
if (draft == null)
throw new DraftFinishedException();
draft.processDraft(player.getName(), channelNumber, draftChannelVisitor);
draft.getCommunicationChannel(player.getName(), channelNumber).processCommunicationChannel(draft.getCardChoice(player.getName()), draftChannelVisitor);
CardCollection cardCollection = _collectionsManager.getPlayerCollection(player, tournament.getCollectionType().getCode());
draftChannelVisitor.chosenCards(cardCollection);
} finally {