SilabGarza - RS Private Server  
Home Forum Play Register Store Market Highscores Mark Forums Read
Go Back   SilabGarza - RS Private Server > SilGar Media > Computers > Programming
Reload this Page Generic Connection Manager


Generic Connection Manager
Old
  (#1 (permalink))
Evolution is Offline
Also known as Anthony`
image
 
Evolution's Avatar
 
Posts: 43
Cookies Given: 0
Cookies Received: 0
Time Online: 2 h 50 m
   
Lightbulb Generic Connection Manager - 06-27-2010, 12:40 PM

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 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;
    }*/
 
}
Here is a snippet of my run() method in the server class (this is taken from a delta server) to illustrate how to configure this,
Code:
java.net.Socket s = clientListener.accept();
s.setTcpNoDelay(true);
String connectingHost = s.getInetAddress().getHostName();
if (true) {
    if (connectingHost.contains("74-129-182-147.dhcp.insightbb.com")) {
        misc.println("Checking Server Status...");
        s.close();
    } else {
        boolean allowed = connectionManager.isConnectionAllowed(s.getLocalAddress(), System.currentTimeMillis());
        if (!allowed)
            s.close();
        System.out.println(connectionManager.getNumberOfConnections());
        System.out.println(connectionManager.connections);
        if (checkHost(connectingHost)) {
            //    misc.println("Connection from "
            //            + connectingHost + ":" + s.getPort());
                            playerHandler.newPlayerClient(s, connectingHost);
        } else {
            //misc.println("ClientHandler: Rejected "+ connectingHost + ":" + s.getPort());
                                s.close();
        }
                        }
} else {
    //misc.println("ClientHandler: Rejected "+ connectingHost + ":" + s.getPort());
    s.close();
}
See if you can figure this out, otherwise just ask for help here.

I probably should have added some sort of action throttle.
Enjoy.
  
Old
  (#2 (permalink))
Asert One is Offline
Senior Member
image
 
Asert One's Avatar
 
Posts: 498
Cookies Given: 0
Cookies Received: 0
Time Online: 5 d 23 h 6 m
   
Default 06-29-2010, 09:46 AM

/facepalm
  
Old
  (#3 (permalink))
Evolution is Offline
Also known as Anthony`
image
 
Evolution's Avatar
 
Posts: 43
Cookies Given: 0
Cookies Received: 0
Time Online: 2 h 50 m
   
Default 06-29-2010, 09:59 AM

Quote:
Originally Posted by Asert One View Post
/facepalm
Whats the facepalm for.
  
Old
  (#4 (permalink))
Asert One is Offline
Senior Member
image
 
Asert One's Avatar
 
Posts: 498
Cookies Given: 0
Cookies Received: 0
Time Online: 5 d 23 h 6 m
   
Default 06-29-2010, 12:03 PM

Using HashMaps for managing connections to your server is what the facepalm is for.
  
Old
  (#5 (permalink))
Evolution is Offline
Also known as Anthony`
image
 
Evolution's Avatar
 
Posts: 43
Cookies Given: 0
Cookies Received: 0
Time Online: 2 h 50 m
   
Default 06-29-2010, 12:22 PM

Quote:
Originally Posted by Asert One View Post
Using HashMaps for managing connections to your server is what the facepalm is for.
Oh yea..? And what do you think is more suitable.
  
Old
  (#6 (permalink))
Asert One is Offline
Senior Member
image
 
Asert One's Avatar
 
Posts: 498
Cookies Given: 0
Cookies Received: 0
Time Online: 5 d 23 h 6 m
   
Default 06-29-2010, 01:34 PM

How about an array list that handles a connection filter?
  
Old
  (#7 (permalink))
Evolution is Offline
Also known as Anthony`
image
 
Evolution's Avatar
 
Posts: 43
Cookies Given: 0
Cookies Received: 0
Time Online: 2 h 50 m
   
Default 06-29-2010, 01:58 PM

Quote:
Originally Posted by Asert One View Post
How about an array list that handles a connection filter?
That completely voides the use of the keys-values then. The only reason I used a HashMap was because it uses keys and values and that the basic operations such as get and put run in constant time O(1). Which is all thats done here.
  
Old
  (#8 (permalink))
Ikr is Offline
Senior Member
image
 
Ikr's Avatar
 
Posts: 2,898
Cookies Given: 96
Cookies Received: 45
Time Online: 34 d 18 h 13 m
Clan: Hibs
   
Default 06-29-2010, 06:37 PM

I have no idea what this is for? Some one explain please?





  
Old
  (#9 (permalink))
Asert One is Offline
Senior Member
image
 
Asert One's Avatar
 
Posts: 498
Cookies Given: 0
Cookies Received: 0
Time Online: 5 d 23 h 6 m
   
Default 06-29-2010, 07:58 PM

Quote:
Originally Posted by Ikr View Post
I have no idea what this is for? Some one explain please?
It's a connection filter that will prevent spam attacks from a third party user. The same function can be done via an array list via process' and such however HashMaps do provide a more efficient connection manager. My bad. Well done however.
  
Old
  (#10 (permalink))
Evolution is Offline
Also known as Anthony`
image
 
Evolution's Avatar
 
Posts: 43
Cookies Given: 0
Cookies Received: 0
Time Online: 2 h 50 m
   
Default 06-30-2010, 11:46 AM

Thanks for your understanding Asert One.
  
adsense code2.3
Closed Thread

Bookmarks

Generic Connection Manager

Programming




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
adsense2.6
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 10:36 AM.





Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.