KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > net > server > Worker


1 package com.quadcap.net.server;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.File JavaDoc;
42 import java.io.FileOutputStream JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.InputStream JavaDoc;
45 import java.io.OutputStream JavaDoc;
46
47 import java.util.Hashtable JavaDoc;
48
49 import java.net.InetAddress JavaDoc;
50 import java.net.Socket JavaDoc;
51
52 import com.quadcap.util.ConfigString;
53 import com.quadcap.util.Debug;
54
55 /**
56  * A worker is a runnable with a context, in/out streams, and (when active)
57  * a socket.
58  *
59  * @author Stan Bailes
60  */

61 public abstract class Worker implements Runnable JavaDoc {
62     Server server;
63     Object JavaDoc lock = new Object JavaDoc();
64     protected Socket JavaDoc socket = null;
65     int sport = -1;
66     boolean terminate = false;
67
68     static int wcnt = 0;
69     int cnt = wcnt++;
70
71     protected Object JavaDoc context;
72     //#ifdef DEBUG
73
protected FileOutputStream JavaDoc log = null;
74     //#endif
75
protected WorkerInputStream win = null;
76     protected WorkerOutputStream wout = null;
77
78     public String JavaDoc toString() { return "Worker " + cnt; }
79     
80     public void init(Server server, Object JavaDoc context) {
81         this.server = server;
82         this.context = context;
83     }
84
85     public void init(Server server, Object JavaDoc context, String JavaDoc name)
86         throws IOException JavaDoc
87     {
88         init(server, context);
89         //#ifdef DEBUG
90
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 JavaDoc("logs").mkdir(); } catch (Throwable JavaDoc t) {}
95             log = new FileOutputStream JavaDoc("logs/" + name + "-" + cnt + ".log");
96         }
97         win = new WorkerInputStream(log);
98         wout = new WorkerOutputStream(log);
99         //#else
100
//- win = new WorkerInputStream(null);
101
//- wout = new WorkerOutputStream(null);
102
//#endif
103
}
104
105     public int getId() { return cnt; }
106
107     void handle(Socket JavaDoc 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         //Jni j = new Jni("work");
117
final WorkerInputStream lwin = win;
118         final WorkerOutputStream lwout = wout;
119         while (!terminate) {
120             //j.reset();
121
boolean interrupted = false;
122             synchronized (lock) {
123                 //j.dump("sync");
124
try {
125                     if (socket == null) lock.wait();
126                     //j.dump("got socket");
127
} catch (InterruptedException JavaDoc e) {
128                 }
129             }
130             if (terminate) return;
131             try {
132                 //j.dump("in try");
133
lwin.reset(socket.getInputStream());
134                 //j.dump("win.reset");
135
lwout.reset(socket.getOutputStream());
136                 //j.dump("wout.reset");
137
doSession();
138                 //j.dump("doSession");
139

140             } catch (Throwable JavaDoc t) {
141                 Debug.print(t);
142             } finally {
143                 try { wout.close(); } catch (Throwable JavaDoc t) {}
144                 try { socket.close(); } catch (Throwable JavaDoc 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 JavaDoc getSocket() {
160         return socket;
161     }
162
163     public final String JavaDoc getHostName() {
164         return socket.getLocalAddress().getHostName();
165     }
166
167     public final int getPort() {
168         return sport;
169     }
170
171     /**
172      * Return the IP address of the agent that sent the request.
173      */

174     public String JavaDoc getRemoteAddr() {
175     InetAddress JavaDoc ia = socket.getInetAddress();
176     return ia.getHostAddress();
177     }
178
179     public String JavaDoc getRemoteHost() {
180     InetAddress JavaDoc ia = socket.getInetAddress();
181     return ia.getHostName();
182     }
183     
184     public abstract void doSession() throws Exception JavaDoc;
185
186     public void stop() {
187         terminate = true;
188         try {
189             synchronized (lock) {
190                 lock.notifyAll();
191             }
192         } catch (Throwable JavaDoc t) {}
193     }
194 }
195
Popular Tags