package org.jboss.remoting.transport.socket;
import org.jboss.logging.Logger;
import org.jboss.remoting.marshal.MarshalFactory;
import org.jboss.remoting.marshal.Marshaller;
import org.jboss.remoting.marshal.UnMarshaller;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.LinkedList;
public class ServerThread extends Thread
{
final static private Logger log = Logger.getLogger(ServerThread.class);
protected BufferedInputStream bis;
protected BufferedOutputStream bos;
protected ObjectInputStream in;
protected ObjectOutputStream out;
protected Socket socket;
protected SocketServerInvoker invoker;
protected LRUPool clientpool;
protected LinkedList threadpool;
protected volatile boolean running = true;
protected volatile boolean handlingResponse = true; protected volatile boolean shutdown = false;
protected static int id = 0;
public static synchronized int nextID()
{
int nextID = id++;
return nextID;
}
public ServerThread(Socket socket, SocketServerInvoker invoker, LRUPool clientpool,
LinkedList threadpool, int timeout) throws Exception
{
super("SocketServerInvokerThread-" + socket.getInetAddress().getHostAddress() + "-" + nextID());
this.socket = socket;
this.invoker = invoker;
this.clientpool = clientpool;
this.threadpool = threadpool;
socket.setSoTimeout(timeout);
}
public void shutdown()
{
shutdown = true;
running = false;
if(!handlingResponse)
{
try
{
this.interrupt();
Thread.interrupted(); }
catch(Exception ignored)
{
}
}
}
public void evict()
{
running = false;
if(!handlingResponse)
{
try
{
this.interrupt();
Thread.interrupted(); }
catch(Exception ignored)
{
}
}
}
public synchronized void wakeup(Socket socket, int timeout) throws Exception
{
this.socket = socket;
String name = "SocketServerInvokerThread-" + socket.getInetAddress().getHostAddress() + "-" + nextID();
super.setName(name);
socket.setSoTimeout(timeout);
running = true;
handlingResponse = true;
this.notify();
}
public void run()
{
try
{
while(true)
{
dorun();
if(shutdown)
{
synchronized(clientpool)
{
clientpool.remove(this);
}
return; }
else
{
synchronized(this)
{
synchronized(clientpool)
{
synchronized(threadpool)
{
clientpool.remove(this);
threadpool.add(this);
Thread.interrupted(); clientpool.notify();
}
}
log.debug("begin thread wait");
this.wait();
log.debug("WAKEUP in SERVER THREAD");
}
}
}
}
catch(Exception ignored)
{
log.debug("Exiting run on exception", ignored);
}
}
protected void acknowledge() throws Exception
{
byte ACK = 0;
long startWait = System.currentTimeMillis();
try
{
ACK = in.readByte();
}
catch(EOFException eof)
{
if(log.isTraceEnabled())
{
log.trace("socket timeout is set to : " + socket.getSoTimeout());
log.trace("EOFException waiting on ACK in readByte(). Time waited was " + (System.currentTimeMillis() - startWait));
}
throw eof;
}
catch(IOException e)
{
log.debug("IOException when reading in ACK", e);
throw e;
}
if(log.isDebugEnabled())
{
log.debug("***acknowledge read byte" + Thread.currentThread());
}
handlingResponse = true;
out.writeByte(ACK);
out.flush();
out.reset();
}
protected void processInvocation() throws Exception
{
handlingResponse = true;
UnMarshaller unmarshaller = MarshalFactory.getUnMarshaller(invoker.getLocator(), this.getClass().getClassLoader());
if(unmarshaller == null)
{
unmarshaller = MarshalFactory.getUnMarshaller(invoker.getDataType());
}
Object obj = unmarshaller.read(in, null);
Object resp = null;
try
{
boolean interrupted = Thread.interrupted();
resp = invoker.invoke(obj);
}
catch(Exception ex)
{
resp = ex;
}
Thread.interrupted();
Marshaller marshaller = MarshalFactory.getMarshaller(invoker.getLocator(), this.getClass().getClassLoader());
if(marshaller == null)
{
marshaller = MarshalFactory.getMarshaller(invoker.getDataType());
}
marshaller.write(resp, out);
handlingResponse = false;
}
protected void dorun()
{
log.debug("beginning dorun");
running = true;
handlingResponse = true;
try
{
bos = new BufferedOutputStream(socket.getOutputStream());
out = new ObjectOutputStream(bos);
out.flush();
bis = new BufferedInputStream(socket.getInputStream());
in = new ObjectInputStream(bis);
}
catch(Exception e)
{
log.error("Failed to initialize", e);
}
try
{
processInvocation();
}
catch(Exception ex)
{
log.debug("failed to process invocation.", ex);
running = false;
}
while(running)
{
try
{
acknowledge();
processInvocation();
}
catch(InterruptedIOException e)
{
log.debug("socket timed out", e);
running = false;
}
catch(InterruptedException e)
{
log.debug("interrupted", e);
}
catch(Exception ex)
{
log.debug("failed", ex);
running = false;
}
Thread.interrupted();
}
try
{
if(in != null)
{
in.close();
}
if(out != null)
{
out.close();
}
}
catch(Exception ex)
{
}
try
{
socket.close();
}
catch(Exception ex)
{
log.error("Failed cleanup", ex);
}
socket = null;
in = null;
out = null;
}
}