All pastes #423955 Raw Edit

Untitled

public text v1 · immutable
·published 2007-04-04 14:34 UTC
rendered paste body
import java.net.*;
import org.python.util.PythonInterpreter;
import org.python.core.*;


/* Project PyEmU
 * @class: ConnectionHandler
 * @author: Development Team
 * @notes: Handles, accepts and rejects connections.
 */



public class ConnectionHandler implements Runnable {
	public static int maxConn = 100; // Zero: Maximum connections (players)
	public static String[] connectingIPS = new String[maxConn]; // Zero: IPs of all players
	private static int Port = 5454; // Zero: Port :-)
	private static stream inStream = null;
	
	public ConnectionHandler() { }
	
	public static void main(String args[]) {
		connects = new ConnectionHandler(); // Zero: Create a new object
		new Thread(connects); // Zero: Create a new thread
		connects.start(); // Zero: Start the thread
		PythonInterpreter pi = new PythonInterpreter();
		
	}
	
	public void run() {
		try {
			cListener = new ServerSocket(Port, 1, null);
			while (true) {
				boolean appro = false;
				Socket s = cListener.accept();
				s.setTcpNoDelay(true);
                String conPlayer = s.getInetAddress().getHostName(); // Zero: Get info from connecting player
                if (conPlayer != "test.somenuller.org" && conPlayer != "null") {
                	for (int i = 0; i < connectingIPS.length; i++) {
                		if (conPlayer != connectingIPS[i]) {
                			appro = true;
                			break;
                		} else {
                			 printTag(1, conPlayer + " Rejected !");
                			 s.close();
                		}		 
                	}
                	if (appro) {
                		printTag(1, conPlayer + " Accepted !");
                		// Execute a python script here? Thief's player_handler? Idk if threads can have args, tho :X
                		PlayerHandler.createPlayer(conPlayer, s);
                	}
                } else {
                	s.close();
                }
			}
		} catch (java.io.IOException ioe) {
			printTag(0, "Unable to start ServerSocket!");
		}
	}
	
	public static void printTag(int typ, String txt) { // Zero: A handy method, feel free to add more cases.
		switch (typ) {
			case 0:
			System.out.println(txt);
			break;
			case 1:
			System.out.println("[ConnectionHandler]: " + txt);
			break;
			case 2:
			System.out.println("[PlayerHandler]: " + txt);
			break;
		}			
	}
 	
}