
Untitled
By: a guest on
May 8th, 2012 | syntax:
Java | size: 1.25 KB | hits: 11 | expires: Never
package gamma.threading;
import gamma.main.Environment;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
public class GammaThreadManager
{
private Map<String, GammaThread> Clients;
private Map<GammaThread, Thread> Threads;
public GammaThreadManager()
{
this.Clients = new HashMap<String, GammaThread>();
this.Threads = new HashMap<GammaThread, Thread>();
}
public void AcceptClient(Socket Client)
{
/*
* Check for existing ip address and dispose them
*/
if (Clients.containsKey(this.getIp(Client)) && Threads.containsKey(this.getIp(Client)))
{
/*
* Stop thread.
*/
Clients.get(this.getIp(Client)).Disposed = true;
Threads.get(this.getIp(Client)).interrupt();
/*
* Remove both
*/
Clients.remove(this.getIp(Client));
Threads.remove(this.getIp(Client));
}
/*
* Add client to map.
*/
GammaThread _gThread = new GammaThread(Client);
this.Clients.put(this.getIp(Client), _gThread);
this.Threads.put(_gThread, new Thread(_gThread));
/*
* Start thread.
*/
this.Threads.get(_gThread).start();
}
public String getIp(Socket Socket)
{
return Socket.getLocalAddress().toString().replace("/", "");
}
}