View Javadoc

1   /*
2    * Created on May 11, 2004
3    *
4    */
5   package org.neo.swarm.interceptor.tcp;
6   import java.net.InetAddress;
7   
8   import org.aopalliance.intercept.MethodInvocation;
9   import org.neo.swarm.core.aop.silc.comp.MethodInterceptor;
10  import org.neo.swarm.util.network.tcp.client.TcpSender;
11  import org.neo.swarm.util.network.tcp.client.TcpSenderFactoryI;
12  import org.neo.swarm.util.serialize.Serializer;
13  /***
14   * Aspect that provides a tcpClient for sending invocations across the wire using using Tcp along with the given serializer.
15   * Note: The TcpInput/OutputStreams use a custom protocols.
16   * Note: SyncLock determines the syncronous nature of the invocation. When synclock is true the sending thread waits for a response
17   * from the recipient, this allows the propogation of exceptions back to the caller. Whe synclock is false, exception will not be reported
18   * back to the caller.
19   * 
20   * @see org.neo.swarm.util.io.PacketInputStream
21   * @see org.neo.swarm.util.io.PacketOutputStream
22   * @author navery
23   */
24  public class TcpClientInterceptor implements MethodInterceptor {
25  	InetAddress host;
26  	int port;
27  	transient TcpSender client = null;
28  	TcpSenderFactoryI tcpFactory;
29  	transient Serializer serializer;
30  	String serializerClass;
31  	boolean syncLock;
32  	
33  	public TcpClientInterceptor(InetAddress host, int port, TcpSenderFactoryI tcpFactory, Serializer serializer) {
34  		this.host = host;
35  		this.port = port;
36  		this.tcpFactory = tcpFactory;
37  		this.serializer = serializer;
38  		this.serializerClass = serializer.getClass().getName();
39  		this.syncLock = false;
40  	}
41  	/***
42  	 * use the tcp sender to pass off to the recipient
43  	 * 
44  	 * @see org.neo.swarm.interceptor.Interceptor#invoke(java.lang.reflect.Method,
45  	 *            java.lang.Object[])
46  	 */
47  	public Object invoke(MethodInvocation invocation) throws Exception {
48  		try {
49  			if (client == null) {
50  				client = tcpFactory.getTcpSender(4 * 1024, host, port);
51  			}
52  			// TODO: fix hack to allow for a serializer serializer.
53  			if (serializer == null) {
54  				serializer = (Serializer) Class.forName(serializerClass).getDeclaredConstructors()[0].newInstance(new Object[0]);
55  			}
56  			Object retObj = null;
57  			
58  			// read response when synclocked OR we need a reply.
59  			if (syncLock || invocation.getMethod().getReturnType() != void.class) {
60  				retObj = serializer.deserialize(client.sendAndReceiveMessage(serializer.serialize(invocation)));
61  			} else {
62  					client.send(serializer.serialize(invocation)); 
63  			}
64  
65  			// catch any returned exceptions and rethrow them.
66  			if (retObj instanceof Exception) {
67  				throw (Exception) retObj;
68  			}			
69  			return retObj;
70  		} catch (Exception e) {
71  			System.out.println("TCPClient exception:" + host + " port:" + port + " ex:" + e);
72  			throw e;
73  		}
74  	}
75  }