Quadcap Embeddable Server

com/quadcap/net/server/Worker.java

Go to the documentation of this file.
00001 package com.quadcap.net.server; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.File; 00042 import java.io.FileOutputStream; 00043 import java.io.IOException; 00044 import java.io.InputStream; 00045 import java.io.OutputStream; 00046 00047 import java.util.Hashtable; 00048 00049 import java.net.InetAddress; 00050 import java.net.Socket; 00051 00052 import com.quadcap.util.ConfigString; 00053 import com.quadcap.util.Debug; 00054 00055 /** 00056 * A worker is a runnable with a context, in/out streams, and (when active) 00057 * a socket. 00058 * 00059 * @author Stan Bailes 00060 */ 00061 public abstract class Worker implements Runnable { 00062 Server server; 00063 Object lock = new Object(); 00064 protected Socket socket = null; 00065 int sport = -1; 00066 boolean terminate = false; 00067 00068 static int wcnt = 0; 00069 int cnt = wcnt++; 00070 00071 protected Object context; 00072 //#ifdef DEBUG 00073 protected FileOutputStream log = null; 00074 //#endif 00075 protected WorkerInputStream win = null; 00076 protected WorkerOutputStream wout = null; 00077 00078 public String toString() { return "Worker " + cnt; } 00079 00080 public void init(Server server, Object context) { 00081 this.server = server; 00082 this.context = context; 00083 } 00084 00085 public void init(Server server, Object context, String name) 00086 throws IOException 00087 { 00088 init(server, context); 00089 //#ifdef DEBUG 00090 this.log = null; 00091 int trc = Integer.parseInt( 00092 ConfigString.find("com.quadcap.net.server.trace", "0").toString()); 00093 if (trc > 0) { 00094 try { new File("logs").mkdir(); } catch (Throwable t) {} 00095 log = new FileOutputStream("logs/" + name + "-" + cnt + ".log"); 00096 } 00097 win = new WorkerInputStream(log); 00098 wout = new WorkerOutputStream(log); 00099 //#else 00100 //- win = new WorkerInputStream(null); 00101 //- wout = new WorkerOutputStream(null); 00102 //#endif 00103 } 00104 00105 public int getId() { return cnt; } 00106 00107 void handle(Socket socket, int sport) { 00108 this.socket = socket; 00109 this.sport = sport; 00110 synchronized (lock) { 00111 lock.notify(); 00112 } 00113 } 00114 00115 public void run() { 00116 //Jni j = new Jni("work"); 00117 final WorkerInputStream lwin = win; 00118 final WorkerOutputStream lwout = wout; 00119 while (!terminate) { 00120 //j.reset(); 00121 boolean interrupted = false; 00122 synchronized (lock) { 00123 //j.dump("sync"); 00124 try { 00125 if (socket == null) lock.wait(); 00126 //j.dump("got socket"); 00127 } catch (InterruptedException e) { 00128 } 00129 } 00130 if (terminate) return; 00131 try { 00132 //j.dump("in try"); 00133 lwin.reset(socket.getInputStream()); 00134 //j.dump("win.reset"); 00135 lwout.reset(socket.getOutputStream()); 00136 //j.dump("wout.reset"); 00137 doSession(); 00138 //j.dump("doSession"); 00139 00140 } catch (Throwable t) { 00141 Debug.print(t); 00142 } finally { 00143 try { wout.close(); } catch (Throwable t) {} 00144 try { socket.close(); } catch (Throwable t) {} 00145 socket = null; 00146 server.returnIdleWorker(this); 00147 } 00148 } 00149 } 00150 00151 public final WorkerInputStream getInputStream() { 00152 return win; 00153 } 00154 00155 public final WorkerOutputStream getOutputStream() { 00156 return wout; 00157 } 00158 00159 public final Socket getSocket() { 00160 return socket; 00161 } 00162 00163 public final String getHostName() { 00164 return socket.getLocalAddress().getHostName(); 00165 } 00166 00167 public final int getPort() { 00168 return sport; 00169 } 00170 00171 /** 00172 * Return the IP address of the agent that sent the request. 00173 */ 00174 public String getRemoteAddr() { 00175 InetAddress ia = socket.getInetAddress(); 00176 return ia.getHostAddress(); 00177 } 00178 00179 public String getRemoteHost() { 00180 InetAddress ia = socket.getInetAddress(); 00181 return ia.getHostName(); 00182 } 00183 00184 public abstract void doSession() throws Exception; 00185 00186 public void stop() { 00187 terminate = true; 00188 try { 00189 synchronized (lock) { 00190 lock.notifyAll(); 00191 } 00192 } catch (Throwable t) {} 00193 } 00194 }