1 package com.quadcap.net.server; 2 3 40 41 import java.io.File ; 42 import java.io.FileOutputStream ; 43 import java.io.IOException ; 44 import java.io.InputStream ; 45 import java.io.OutputStream ; 46 47 import java.util.Hashtable ; 48 49 import java.net.InetAddress ; 50 import java.net.Socket ; 51 52 import com.quadcap.util.ConfigString; 53 import com.quadcap.util.Debug; 54 55 61 public abstract class Worker implements Runnable { 62 Server server; 63 Object lock = new Object (); 64 protected Socket socket = null; 65 int sport = -1; 66 boolean terminate = false; 67 68 static int wcnt = 0; 69 int cnt = wcnt++; 70 71 protected Object context; 72 protected FileOutputStream log = null; 74 protected WorkerInputStream win = null; 76 protected WorkerOutputStream wout = null; 77 78 public String toString() { return "Worker " + cnt; } 79 80 public void init(Server server, Object context) { 81 this.server = server; 82 this.context = context; 83 } 84 85 public void init(Server server, Object context, String name) 86 throws IOException 87 { 88 init(server, context); 89 this.log = null; 91 int trc = Integer.parseInt( 92 ConfigString.find("com.quadcap.net.server.trace", "0").toString()); 93 if (trc > 0) { 94 try { new File ("logs").mkdir(); } catch (Throwable t) {} 95 log = new FileOutputStream ("logs/" + name + "-" + cnt + ".log"); 96 } 97 win = new WorkerInputStream(log); 98 wout = new WorkerOutputStream(log); 99 } 104 105 public int getId() { return cnt; } 106 107 void handle(Socket socket, int sport) { 108 this.socket = socket; 109 this.sport = sport; 110 synchronized (lock) { 111 lock.notify(); 112 } 113 } 114 115 public void run() { 116 final WorkerInputStream lwin = win; 118 final WorkerOutputStream lwout = wout; 119 while (!terminate) { 120 boolean interrupted = false; 122 synchronized (lock) { 123 try { 125 if (socket == null) lock.wait(); 126 } catch (InterruptedException e) { 128 } 129 } 130 if (terminate) return; 131 try { 132 lwin.reset(socket.getInputStream()); 134 lwout.reset(socket.getOutputStream()); 136 doSession(); 138 140 } catch (Throwable t) { 141 Debug.print(t); 142 } finally { 143 try { wout.close(); } catch (Throwable t) {} 144 try { socket.close(); } catch (Throwable t) {} 145 socket = null; 146 server.returnIdleWorker(this); 147 } 148 } 149 } 150 151 public final WorkerInputStream getInputStream() { 152 return win; 153 } 154 155 public final WorkerOutputStream getOutputStream() { 156 return wout; 157 } 158 159 public final Socket getSocket() { 160 return socket; 161 } 162 163 public final String getHostName() { 164 return socket.getLocalAddress().getHostName(); 165 } 166 167 public final int getPort() { 168 return sport; 169 } 170 171 174 public String getRemoteAddr() { 175 InetAddress ia = socket.getInetAddress(); 176 return ia.getHostAddress(); 177 } 178 179 public String getRemoteHost() { 180 InetAddress ia = socket.getInetAddress(); 181 return ia.getHostName(); 182 } 183 184 public abstract void doSession() throws Exception ; 185 186 public void stop() { 187 terminate = true; 188 try { 189 synchronized (lock) { 190 lock.notifyAll(); 191 } 192 } catch (Throwable t) {} 193 } 194 } 195 | Popular Tags |