Code:
import java.net.InetAddress;
import java.util.*;
class ConnectionManager {
final short MAX_PLAYERS = 50;
Map<InetAddress, ArrayList<Long>> connections = new HashMap<InetAddress, ArrayList<Long>>();
public void addConnection(InetAddress address, long currentTime) {
if (connections.containsKey(address))
connections.get(address).add(currentTime);
else {
ArrayList<Long> connectionTime = new ArrayList<Long>();
connectionTime.add(currentTime);
connections.put(address, connectionTime);
}
}
public boolean isConnectionAllowed(InetAddress connectingPlayer, long currentTime) {
if (connections.size() > MAX_PLAYERS)
return false;
if (connections.containsKey(connectingPlayer))
return false;
// TODO: Check whether player is banned or not, return false if so
addConnection(connectingPlayer, currentTime);
return true;
}
public void removeConnection(InetAddress disconnectingPlayer) {
connections.remove(disconnectingPlayer);
}
public int getNumberOfConnections() {
return connections.size();
}
public String toString(InetAddress address) {
return address.toString();
}
public ArrayList<Long> getTimeByAddress(InetAddress address, long currentTime) {
return connections.get(address);
}
public String[] getAddresses(HashMap<InetAddress, ArrayList<Long>> address) {
return (String[]) connections.keySet().toArray();
}
/*public String[] getAddresses(HashMap<InetAddress, ArrayList<Long>> address) {
int index = 0;
String[] addresses = null;
for (Iterator<InetAddress> iterator = (Iterator) address.keySet(); iterator.hasNext(); ) {
String key = iterator.next().toString();
addresses[index++] = key;
return addresses;
}
return null;
}*/
}