package org.jboss.mq.il.oil;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.rmi.RemoteException;
import org.jboss.logging.Logger;
import org.jboss.mq.ReceiveRequest;
import org.jboss.mq.SpyDestination;
import org.jboss.mq.il.ClientIL;
public final class OILClientIL
implements ClientIL,
java.io.Serializable
{
static final long serialVersionUID = 7812173621233374692L;
private final static Logger log = Logger.getLogger(OILClientIL.class);
private InetAddress addr;
private int port;
protected boolean enableTcpNoDelay=false;
private transient ObjectInputStream in;
private transient ObjectOutputStream out;
private transient Socket socket;
OILClientIL(InetAddress addr, int port, boolean enableTcpNoDelay)
{
this.addr = addr;
this.port = port;
this.enableTcpNoDelay = enableTcpNoDelay;
}
public synchronized void close()
throws Exception
{
if ( log.isTraceEnabled())
log.trace("Closing OILClientIL");
checkSocket();
out.writeByte(OILConstants.CLOSE);
waitAnswer();
try
{
socket.close();
in.close();
out.close();
}
catch(Exception e)
{
if(log.isDebugEnabled())
log.debug("Error closing the socket connection", e);
}
}
public synchronized void deleteTemporaryDestination(SpyDestination dest)
throws Exception
{
checkSocket();
out.writeByte(OILConstants.DELETE_TEMPORARY_DESTINATION);
out.writeObject(dest);
waitAnswer();
}
public synchronized void pong(long serverTime)
throws Exception
{
checkSocket();
out.writeByte(OILConstants.PONG);
out.writeLong(serverTime);
waitAnswer();
}
public synchronized void receive(ReceiveRequest messages[])
throws Exception
{
boolean trace = log.isTraceEnabled();
if( trace )
log.trace("Checking socket");
checkSocket();
if( trace )
log.trace("Writing request");
out.writeByte(OILConstants.RECEIVE);
out.writeInt(messages.length);
for (int i = 0; i < messages.length; ++i)
{
messages[i].writeExternal(out);
}
if( trace )
log.trace("Waiting for anwser");
waitAnswer();
if( trace )
log.trace("Done");
}
private void checkSocket()
throws RemoteException
{
if (socket == null)
{
createConnection();
}
}
private void createConnection()
throws RemoteException
{
try
{
if (log.isDebugEnabled()) {
log.debug("ConnectionReceiverOILClient is connecting to: " +
addr.getHostAddress() + ":" + port);
}
socket = new Socket(addr, port);
out = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
out.flush();
in = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
}
catch (Exception e)
{
log.error("Cannot connect to the ConnectionReceiver/Server", e);
throw new RemoteException("Cannot connect to the ConnectionReceiver/Server");
}
}
private void waitAnswer()
throws Exception
{
Exception throwException = null;
try
{
out.reset();
out.flush();
int val = in.readByte();
switch (val)
{
case OILConstants.EXCEPTION:
Exception e = (Exception)in.readObject();
throwException = new RemoteException("", e);
break;
}
}
catch (IOException e)
{
throw new RemoteException("Cannot contact the remote object", e);
}
if (throwException != null)
{
throw throwException;
}
}
}