Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: Java  |  size: 1.25 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package gamma.threading;
  2.  
  3. import gamma.main.Environment;
  4.  
  5. import java.net.Socket;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8.  
  9. public class GammaThreadManager
  10. {
  11.         private Map<String, GammaThread> Clients;
  12.         private Map<GammaThread, Thread> Threads;
  13.        
  14.         public GammaThreadManager()
  15.         {
  16.                 this.Clients = new HashMap<String, GammaThread>();
  17.                 this.Threads = new HashMap<GammaThread, Thread>();
  18.         }
  19.        
  20.         public void AcceptClient(Socket Client)
  21.         {
  22.                 /*
  23.                  * Check for existing ip address and dispose them
  24.                  */
  25.                 if (Clients.containsKey(this.getIp(Client)) && Threads.containsKey(this.getIp(Client)))
  26.                 {
  27.                         /*
  28.                          * Stop thread.
  29.                          */
  30.                         Clients.get(this.getIp(Client)).Disposed = true;
  31.                         Threads.get(this.getIp(Client)).interrupt();
  32.                        
  33.                         /*
  34.                          * Remove both
  35.                          */
  36.                         Clients.remove(this.getIp(Client));
  37.                         Threads.remove(this.getIp(Client));
  38.                 }
  39.                
  40.                 /*
  41.                  * Add client to map.
  42.                  */
  43.                 GammaThread _gThread = new GammaThread(Client);
  44.                
  45.                 this.Clients.put(this.getIp(Client), _gThread);
  46.                 this.Threads.put(_gThread, new Thread(_gThread));
  47.                
  48.                 /*
  49.                  * Start thread.
  50.                  */
  51.                 this.Threads.get(_gThread).start();
  52.         }
  53.        
  54.         public String getIp(Socket Socket)
  55.         {
  56.                 return Socket.getLocalAddress().toString().replace("/", "");
  57.         }
  58. }