1 package rero.ident; 2 3 import java.net.*; 4 import java.io.*; 5 import java.util.*; 6 7 import rero.config.*; 8 9 import java.lang.ref.WeakReference ; 10 11 public class IdentDaemon implements Runnable , ClientStateListener 12 { 13 private static IdentDaemon identd; 14 15 protected LinkedList listeners = new LinkedList(); 16 17 public void addIdentListener(IdentListener l) 18 { 19 listeners.add(new WeakReference (l)); 20 } 21 22 public void fireEvent(String host, String text) 23 { 24 Iterator i = listeners.iterator(); 25 while (i.hasNext()) 26 { 27 WeakReference temp = (WeakReference )i.next(); 28 29 if (temp.get() != null) 30 ((IdentListener)temp.get()).identRequest(host, text); 31 } 32 } 33 34 public static void initialize() 35 { 36 identd = new IdentDaemon(); 37 } 38 39 public static IdentDaemon getIdentDaemon() 40 { 41 return identd; 42 } 43 44 public void propertyChanged(String key, String value) 45 { 46 setup(); 47 } 48 49 public IdentDaemon() 50 { 51 ClientState.getClientState().addClientStateListener("ident.enabled", this); 52 setup(); 53 } 54 55 public void setup() 56 { 57 if (ClientState.getClientState().isOption("ident.enabled", ClientDefaults.ident_enabled) && serverThread == null) 58 { 59 serverThread = new Thread (this); 60 serverThread.setPriority(Thread.MIN_PRIORITY); 61 serverThread.setName("jIRCii Ident Daemon"); 62 serverThread.start(); 63 } 64 else if (ClientState.getClientState().isOption("ident.enabled", ClientDefaults.ident_enabled) == false && serverThread != null) 65 { 66 close(); 67 } 68 } 69 70 public void close() 71 { 72 if (serverThread != null) 73 { 74 if (listener != null && ! listener.isClosed()) 75 try { listener.close(); } catch (Exception ex) { } 76 77 if (activeClient != null && ! activeClient.isClosed()) 78 try { activeClient.close(); } catch (Exception ex) { } 79 80 serverThread.interrupt(); 81 serverThread = null; 82 } 83 } 84 85 private Thread serverThread; 86 87 private Socket activeClient; 88 private ServerSocket listener; 89 90 private BufferedReader socketInput; 91 private PrintWriter socketOutput; 92 93 public void run() 94 { 95 try 96 { 97 listener = new ServerSocket(ClientState.getClientState().getInteger("ident.port", ClientDefaults.ident_port)); 98 } 99 catch (Exception ex2) 100 { 101 ex2.printStackTrace(); 102 close(); 103 return; 104 } 105 106 while (Thread.currentThread() == serverThread) 107 { 108 try 109 { 110 activeClient = listener.accept(); 111 112 if (activeClient != null && activeClient.isConnected()) 113 { 114 socketInput = new BufferedReader(new InputStreamReader(activeClient.getInputStream())); 115 socketOutput = new PrintWriter(activeClient.getOutputStream(),true); 116 117 String text = socketInput.readLine(); 118 fireEvent(activeClient.getInetAddress().getHostAddress(), text); 119 120 socketOutput.print(text + " : USERID : "+ClientState.getClientState().getString("ident.system", ClientDefaults.ident_system)+" : "+ ClientState.getClientState().getString("ident.userid", ClientDefaults.ident_userid)); 121 socketOutput.flush(); 122 123 socketInput.close(); 124 socketOutput.close(); 125 activeClient.close(); 126 } 127 } 128 catch (Exception ex) 129 { 130 ex.printStackTrace(); 131 try 132 { 133 Thread.sleep(10 * 1000); 135 if (listener == null || listener.isClosed()) 136 listener = new ServerSocket(ClientState.getClientState().getInteger("ident.port", ClientDefaults.ident_port)); 137 } 138 catch (Exception ex1) 139 { 140 ex1.printStackTrace(); 141 } 142 } 143 } 144 } 145 } 146 147 | Popular Tags |