Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generic Connection Manager
#1
I made this class so no more crashers would crash my friends Runescape private server. This could also be implemented into other generic applications as well. To be honest, there are lots of uses for this.
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;
    }*/

}

I probably should have added some sort of action throttle.
Enjoy.
My SMF Modifications:
http://anthony.vibrantvps.com/smf
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)