00001
package com.quadcap.net.server;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
00057
00058
00059
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
00073 protected FileOutputStream
log = null;
00074
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
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
00100
00101
00102
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
00117
final WorkerInputStream lwin =
win;
00118
final WorkerOutputStream lwout =
wout;
00119
while (!
terminate) {
00120
00121
boolean interrupted =
false;
00122
synchronized (
lock) {
00123
00124
try {
00125
if (
socket == null)
lock.wait();
00126
00127 }
catch (InterruptedException e) {
00128 }
00129 }
00130
if (
terminate)
return;
00131
try {
00132
00133 lwin.
reset(
socket.getInputStream());
00134
00135 lwout.
reset(
socket.getOutputStream());
00136
00137
doSession();
00138
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
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 }