some server optimizations

This commit is contained in:
BetaSteward 2011-10-27 11:20:10 -04:00
parent e97d655bd1
commit 7ab5649132
4 changed files with 44 additions and 35 deletions

View file

@ -263,11 +263,7 @@ public class MageServerImpl implements MageServer {
@Override
public List<String> getConnectedPlayers(UUID roomId) throws MageException {
try {
List<String> players = new ArrayList<String>();
for (User user : UserManager.getInstance().getUsers()) {
players.add(user.getName());
}
return players;
return GamesRoomManager.getInstance().getRoom(roomId).getPlayers();
}
catch (Exception ex) {
handleException(ex);

View file

@ -47,6 +47,7 @@ public interface GamesRoom extends Room {
public List<TableView> getTables();
public List<MatchView> getFinished();
public List<String> getPlayers();
public boolean joinTable(UUID userId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList) throws MageException;
public boolean joinTournamentTable(UUID userId, UUID tableId, String name, String playerType, int skill) throws GameException;
public TableView createTable(UUID userId, MatchOptions options);

View file

@ -33,7 +33,6 @@ import mage.server.RoomImpl;
import mage.game.Table;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
@ -48,10 +47,13 @@ import mage.game.GameException;
import mage.game.match.MatchOptions;
import mage.game.tournament.TournamentOptions;
import mage.MageException;
import mage.server.User;
import mage.server.UserManager;
import mage.view.MatchView;
import mage.view.TableView;
import org.apache.log4j.Logger;
/**
*
* @author BetaSteward_at_googlemail.com
@ -63,6 +65,7 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
private static ScheduledExecutorService updateExecutor = Executors.newSingleThreadScheduledExecutor();
private static List<TableView> tableView = new ArrayList<TableView>();
private static List<MatchView> matchView = new ArrayList<MatchView>();
private static List<String> playersView = new ArrayList<String>();
private ConcurrentHashMap<UUID, Table> tables = new ConcurrentHashMap<UUID, Table>();
@ -70,8 +73,7 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
updateExecutor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
updateTables();
updateFinished();
update();
}
}, 2, 2, TimeUnit.SECONDS);
}
@ -81,13 +83,26 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
return tableView;
}
private void updateTables() {
private void update() {
ArrayList<TableView> tableList = new ArrayList<TableView>();
for (Table table: tables.values()) {
if (table.getState() != TableState.FINISHED)
ArrayList<MatchView> matchList = new ArrayList<MatchView>();
List<Table> t = new ArrayList<Table>(tables.values());
Collections.sort(t, new TimestampSorter());
for (Table table: t) {
if (table.getState() != TableState.FINISHED) {
tableList.add(new TableView(table));
}
else if (matchList.size() < 50) {
matchList.add(new MatchView(table.getMatch()));
}
}
tableView = tableList;
matchView = matchList;
List<String> players = new ArrayList<String>();
for (User user : UserManager.getInstance().getUsers()) {
players.add(user.getName());
}
playersView = players;
}
@Override
@ -95,20 +110,6 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
return matchView;
}
private void updateFinished() {
ArrayList<MatchView> matchList = new ArrayList<MatchView>();
List<Table> t = new ArrayList<Table>(tables.values());
Collections.sort(t, new TimestampSorter());
for (Table table: t) {
if (table.getState() == TableState.FINISHED) {
matchList.add(new MatchView(table.getMatch()));
if (matchList.size() >= 50)
break;
}
}
matchView = matchList;
}
@Override
public boolean joinTable(UUID userId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList) throws MageException {
if (tables.containsKey(tableId)) {
@ -170,6 +171,11 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
return TableManager.getInstance().watchTable(userId, tableId);
}
@Override
public List<String> getPlayers() {
return playersView;
}
}
class TimestampSorter implements Comparator<Table> {

View file

@ -29,15 +29,15 @@ package mage.server.util;
import org.apache.log4j.Logger;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
@ -54,6 +54,7 @@ public class ServerMessagesUtil {
private static final Logger log = Logger.getLogger(ServerMessagesUtil.class);
private static final String SERVER_MSG_TXT_FILE = "server.msg.txt";
private static ScheduledExecutorService updateExecutor;
private List<String> messages = new ArrayList<String>();
private ReadWriteLock lock = new ReentrantReadWriteLock();
@ -70,8 +71,13 @@ public class ServerMessagesUtil {
}
public ServerMessagesUtil() {
timer.setInitialDelay(5000);
timer.start();
updateExecutor = Executors.newSingleThreadScheduledExecutor();
updateExecutor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
reloadMessages();
}
}, 5, 5 * 60, TimeUnit.SECONDS);
}
public static ServerMessagesUtil getInstance() {
@ -167,11 +173,11 @@ public class ServerMessagesUtil {
return statistics.toString();
}
private Timer timer = new Timer(1000 * 60, new ActionListener() {
public void actionPerformed(ActionEvent e) {
reloadMessages();
}
});
// private Timer timer = new Timer(1000 * 60, new ActionListener() {
// public void actionPerformed(ActionEvent e) {
// reloadMessages();
// }
// });
public void setStartDate(long milliseconds) {
this.startDate = milliseconds;