View Javadoc

1   
2   package org.neo.swarm.services.binding;
3   
4   import java.lang.reflect.UndeclaredThrowableException;
5   import java.net.InetAddress;
6   
7   import org.neo.swarm.ApplicationContext;
8   import org.neo.swarm.core.aop.silc.comp.Invocation;
9   import org.neo.swarm.core.aop.silc.comp.Perspective;
10  import org.neo.swarm.util.network.multicast.Sender;
11  import org.neo.swarm.util.serialize.SerializationException;
12  import org.neo.swarm.util.serialize.Serializer;
13  
14  /***
15   * Takes a multicastsender and multicasts the ACProxy registration Aspect
16   * 
17   * @see org.neo.swarm.services.binding.BindingService 
18   * @author navery
19   */
20  public class DynamicRunnable implements BindingRunnable {
21  
22  	ApplicationContext appContext;
23  	Sender sender;
24  	Serializer serializer;
25  	int timeout;
26  	boolean running = false;
27  	protected byte[] sendData;
28  
29  	public DynamicRunnable(ApplicationContext appContext, Sender sender, Serializer serializer, int timeout) {
30  		this.appContext = appContext;
31          this.sender = sender;
32          this.timeout = timeout;
33          this.serializer = serializer;
34  
35  
36          String localURI = appContext.getURI();
37          ApplicationContext remoteAC = (ApplicationContext) appContext.retrieveComponent(Perspective.REMOTE, localURI);
38  
39          Invocation invocation;
40  		try {
41              RemoteACProxyFactory factory = new RemoteACProxyFactory();            
42              invocation = factory.createRegisterAppContextInvocation("container", localURI, remoteAC);
43              sendData = serializer.serialize(invocation);
44          } catch (SecurityException e1) {
45              e1.printStackTrace();
46          } catch (NoSuchMethodException e1) {
47              e1.printStackTrace();
48          } catch (SerializationException e2) {
49              e2.printStackTrace();
50          }
51  
52  	}
53  
54      public void start() {
55          // running = true is implicitly done through ThreadPool execution
56      }
57      
58  	public void stop() {
59          running = false;
60  	}
61  	/***
62  	 * Pumps out the local AC aspect invocation using the given sender impl
63  	 */
64  	public void run() {
65          running = true;
66  		while (running) {
67  			try {
68  				sender.send(sendData);
69  				Thread.sleep(this.timeout);
70  			} catch (Exception ex) {
71  				try {
72  					ex.printStackTrace();
73  					Thread.sleep(timeout);
74  					// give up on this one and keep going...
75  					if (ex instanceof UndeclaredThrowableException) {
76  						UndeclaredThrowableException udex = (UndeclaredThrowableException) ex;
77  						System.out.println(udex.getCause());
78  					}
79  				} catch (InterruptedException e) {
80  				}
81  			}
82  		}
83  		System.out.println("Bind thread exiting");
84  	}
85      
86  	/***
87  	 *
88  	 */
89  
90  	public String getName() {
91  		return "BindingService";
92  	}
93      
94  	public void addLocation(InetAddress host, int port) {
95  		// ignore for mcaster
96  	}
97  }