Fxing dead-locking problem.

This commit is contained in:
marcins78@gmail.com
2012-04-18 18:30:33 +00:00
parent 9c3fc3b9fc
commit 4dd971be52
6 changed files with 48 additions and 156 deletions

View File

@@ -5,14 +5,14 @@ public abstract class AbstractServer {
private boolean _started;
public final synchronized void startServer() {
public void startServer() {
if (!_started) {
_cleaningTask.addServer(this);
_started = true;
}
}
public final synchronized void stopServer() {
public void stopServer() {
if (_started) {
_cleaningTask.removeServer(this);
_started = false;

View File

@@ -9,6 +9,7 @@ public class ServerCleaner {
private CleaningThread _thr;
public synchronized void addServer(AbstractServer server) {
System.out.println("Adding server: " + server.getClass());
_servers.add(server);
if (_thr == null) {
_thr = new CleaningThread();
@@ -38,8 +39,8 @@ public class ServerCleaner {
// We can't do much about it
}
}
Thread.sleep(1000);
}
Thread.sleep(1000);
}
} catch (InterruptedException exp) {
// Thread interrupted - get lost

View File

@@ -51,31 +51,32 @@ public class LotroServer extends AbstractServer {
final int[] cardCounts = new int[]{129, 365, 122, 122, 365, 128, 128, 365, 122, 52, 122, 266, 203, 203, 15, 207, 6, 157, 149, 40};
Thread thr = new Thread(
new Runnable() {
public void run() {
for (int i = 0; i <= 19; i++) {
for (int j = 1; j <= cardCounts[i]; j++) {
String blueprintId = i + "_" + j;
try {
if (_lotroCardBlueprintLibrary.getBaseBlueprintId(blueprintId).equals(blueprintId)) {
LotroCardBlueprint cardBlueprint = _lotroCardBlueprintLibrary.getLotroCardBlueprint(blueprintId);
CardType cardType = cardBlueprint.getCardType();
if (cardType == CardType.SITE || cardType == CardType.THE_ONE_RING)
_defaultCollection.addItem(blueprintId, 1);
else
_defaultCollection.addItem(blueprintId, 4);
}
} catch (IllegalArgumentException exp) {
}
}
}
_collectionReadyLatch.countDown();
// Thread thr = new Thread(
// new Runnable() {
// public void run() {
for (int i = 0; i <= 19; i++) {
System.out.println("Loading set " + i);
for (int j = 1; j <= cardCounts[i]; j++) {
String blueprintId = i + "_" + j;
try {
if (_lotroCardBlueprintLibrary.getBaseBlueprintId(blueprintId).equals(blueprintId)) {
LotroCardBlueprint cardBlueprint = _lotroCardBlueprintLibrary.getLotroCardBlueprint(blueprintId);
CardType cardType = cardBlueprint.getCardType();
if (cardType == CardType.SITE || cardType == CardType.THE_ONE_RING)
_defaultCollection.addItem(blueprintId, 1);
else
_defaultCollection.addItem(blueprintId, 4);
}
} catch (IllegalArgumentException exp) {
}
);
thr.start();
}
}
_collectionReadyLatch.countDown();
// }
// }
// );
// thr.start();
_gameRecorder = new GameRecorder(_gameHistoryDao);
}

View File

@@ -196,8 +196,8 @@ public class ServerResource extends AbstractResource {
} else {
sb.append("You are not logged in, log in below or <button id='clickToRegister'>register</button>.");
sb.append("<div class='status'>Tables count: ").append(_hallServer.getTablesCount()).append(", players in hall: ").append(_chatServer.getChatRoom("Game Hall").getUsersInRoom().size())
.append(", games played in last 24 hours: ").append(_gameHistoryDao.getGamesPlayedCountInLastMs(1000 * 60 * 60 * 24))
.append(",<br/> active players in last week: ").append(_gameHistoryDao.getActivePlayersInLastMs(1000 * 60 * 60 * 24 * 7))
.append(", games played in last 24 hours: unknown")//.append(_gameHistoryDao.getGamesPlayedCountInLastMs(1000 * 60 * 60 * 24))
.append(",<br/> active players in last week: unknown")//.append(_gameHistoryDao.getActivePlayersInLastMs(1000 * 60 * 60 * 24 * 7))
.append("</div>");
sb.append(getLoginHTML());
}

View File

@@ -1,110 +0,0 @@
package com.gempukku.lotro.server;
import com.gempukku.lotro.game.CardCollection;
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
import com.gempukku.lotro.game.Player;
import com.gempukku.lotro.trade.TradeServer;
import com.gempukku.lotro.trade.TradeState;
import com.gempukku.lotro.trade.TradeStateVisitor;
import com.sun.jersey.spi.resource.Singleton;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.util.Map;
@Singleton
@Path("/trade")
public class TradeResource extends AbstractResource {
@Context
private TradeServer _tradeServer;
@Context
private LotroCardBlueprintLibrary _library;
@GET
@Produces(MediaType.APPLICATION_XML)
public Document getOngoingTrade(@QueryParam("participantId") String participantId,
@Context HttpServletRequest request,
@Context HttpServletResponse response) throws ParserConfigurationException {
Player resourceOwner = getResourceOwnerSafely(request, participantId);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.newDocument();
Element trade = doc.createElement("trade");
final TradeState ongoingTrade = _tradeServer.getOngoingTrade(resourceOwner);
if (ongoingTrade != null)
ongoingTrade.processTradeStateVisitor(resourceOwner.getName(), new OngoingTradeVisitor(doc, trade));
doc.appendChild(trade);
processDeliveryServiceNotification(request, response);
return doc;
}
private class OngoingTradeVisitor implements TradeStateVisitor {
private Document _doc;
private Element _root;
private OngoingTradeVisitor(Document doc, Element root) {
_doc = doc;
_root = root;
}
@Override
public void processTradeState(String otherParty, CardCollection offering, CardCollection getting, boolean selfAccepted, boolean otherAccepted, int tradeState, boolean selfConfirmed, boolean otherConfirmed) {
_root.setAttribute("otherParty", otherParty);
if (selfAccepted)
_root.setAttribute("selfAccepted", "true");
if (otherAccepted)
_root.setAttribute("otherAccepted", "true");
if (selfAccepted && otherAccepted)
_root.setAttribute("tradeState", String.valueOf(tradeState));
if (selfConfirmed)
_root.setAttribute("selfConfirmed", "true");
if (otherConfirmed)
_root.setAttribute("otherConfirmed", "true");
final Element offer = _doc.createElement("offer");
appendItems(_doc, offer, offering);
_root.appendChild(offer);
Element get = _doc.createElement("get");
appendItems(_doc, get, getting);
_root.appendChild(get);
}
private void appendItems(Document doc, Element elem, CardCollection collection) {
final Map<String, Integer> items = collection.getAll();
for (Map.Entry<String, Integer> itemCount : items.entrySet()) {
String blueprintId = itemCount.getKey();
if (blueprintId.contains("_")) {
Element card = doc.createElement("card");
card.setAttribute("count", String.valueOf(itemCount.getValue()));
card.setAttribute("blueprintId", blueprintId);
elem.appendChild(card);
} else {
Element pack = doc.createElement("pack");
pack.setAttribute("count", String.valueOf(itemCount.getValue()));
pack.setAttribute("blueprintId", blueprintId);
elem.appendChild(pack);
}
}
}
}
}

View File

@@ -10,7 +10,6 @@ import com.gempukku.lotro.game.formats.LotroFormatLibrary;
import com.gempukku.lotro.hall.HallServer;
import com.gempukku.lotro.league.LeagueService;
import com.gempukku.lotro.merchant.MerchantService;
import com.gempukku.lotro.trade.TradeServer;
import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.core.spi.component.ComponentScope;
import com.sun.jersey.spi.inject.Injectable;
@@ -28,7 +27,7 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
private Injectable<MerchantService> _merchantServiceInjectable;
private Injectable<HallServer> _hallServerInjectable;
private Injectable<LotroServer> _lotroServerInjectable;
private Injectable<TradeServer> _tradeServerInjectable;
// private Injectable<TradeServer> _tradeServerInjectable;
private Injectable<CollectionsManager> _collectionsManagerInjectable;
private Injectable<DeliveryService> _deliveryServiceInjectable;
private Injectable<LotroFormatLibrary> _lotroFormatLibraryInjectable;
@@ -66,8 +65,8 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
return getLeagueServiceInjectable();
if (type.equals(CollectionsManager.class))
return getCollectionsManagerInjectable();
if (type.equals(TradeServer.class))
return getTradeServerInjectable();
// if (type.equals(TradeServer.class))
// return getTradeServerInjectable();
if (type.equals(DeliveryService.class))
return getDeliveryServiceInjectable();
if (type.equals(LotroFormatLibrary.class))
@@ -80,7 +79,7 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
@PreDestroy
public void destroyResources() {
getHallServerInjectable().getValue().stopServer();
getTradeServerInjectable().getValue().stopServer();
// getTradeServerInjectable().getValue().stopServer();
getLotroServerInjectable().getValue().stopServer();
getChatServerInjectable().getValue().stopServer();
}
@@ -152,20 +151,21 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
return _hallServerInjectable;
}
private synchronized Injectable<TradeServer> getTradeServerInjectable() {
if (_tradeServerInjectable == null) {
final TradeServer tradeServer = new TradeServer(getChatServerInjectable().getValue());
tradeServer.startServer();
_tradeServerInjectable = new Injectable<TradeServer>() {
@Override
public TradeServer getValue() {
return tradeServer;
}
};
}
return _tradeServerInjectable;
}
// private synchronized Injectable<TradeServer> getTradeServerInjectable() {
// if (_tradeServerInjectable == null) {
// final TradeServer tradeServer = new TradeServer(getChatServerInjectable().getValue());
// tradeServer.startServer();
// _tradeServerInjectable = new Injectable<TradeServer>() {
// @Override
// public TradeServer getValue() {
// return tradeServer;
// }
// };
// }
// return _tradeServerInjectable;
// }
//
private synchronized Injectable<LotroServer> getLotroServerInjectable() {
if (_lotroServerInjectable == null) {
final LotroServer lotroServer = new LotroServer(_deckDao, _gameHistoryDao, _library, getChatServerInjectable().getValue(), false);