View Javadoc

1   /*
2    * Created on Nov 7, 2003
3    * 
4    */
5   package org.neo.swarm.services.tcp;
6   
7   import java.lang.reflect.UndeclaredThrowableException;
8   
9   import org.neo.swarm.ApplicationContext;
10  import org.neo.swarm.core.aop.silc.comp.Invocation;
11  import org.neo.swarm.util.network.tcp.ListenCallback;
12  import org.neo.swarm.util.serialize.SerializationException;
13  import org.neo.swarm.util.serialize.Serializer;
14  
15  /***
16   * Propogates incoming invocations into the AppContext.
17   * 
18   * @author neil.avery
19   */
20  public class ServerCompRouter implements ListenCallback {
21  
22  	Serializer serializer;
23  	ApplicationContext ac;
24  	boolean syncLocked;
25  
26  	public ServerCompRouter(ApplicationContext ac, Serializer serializer) {
27  		this.ac = ac;
28  		this.serializer = serializer;
29  		this.syncLocked = false;
30  	}
31  
32  	/***
33  	 * Deserialise the data into a WiredInvocation object and pass it onto the
34  	 * appropriate component local interface.
35  	 * 
36  	 * @see org.neo.swarm.util.network.tcp.ListenCallback#messageDataReceived(byte[])
37  	 */
38  	public byte[] messageDataReceived(byte[] data) {
39  
40  		try {
41  			Invocation invocation = (Invocation) serializer.deserialize(data);
42  
43  			// inject the invocation into the local component aspect stack.
44  			Object retData = ac.inject(invocation.getKey(), invocation);
45  			if (syncLocked || invocation.getMethod().getReturnType() != void.class) {
46  				return serializer.serialize(retData);
47  			}
48  			return new byte[0];
49  		} catch (Exception e) {
50  
51  			// Allow the exception to propogate back through the wire
52  			//
53  			// Note: Returning the exception through the wire will only cause problems where
54  			// OneWay, invocations and Blocking Sockets are used. On the server
55  			// this is not the case so will only cause the socket buffer to fill, dropping any
56  			// further data.
57  			// 
58  			Exception cause = e;
59  			if (e instanceof UndeclaredThrowableException) {
60  				cause = (Exception) ((UndeclaredThrowableException) e).getCause();
61  			}
62  			cause.printStackTrace();
63  			try {
64  				return serializer.serialize(cause);
65  			} catch (SerializationException ex) {
66  				ex.printStackTrace();
67  			}
68  		}
69  		return new byte[0];
70  	}
71  }