View Javadoc

1   
2   package org.neo.swarm.services.binding;
3   
4   import java.io.IOException;
5   import java.net.InetAddress;
6   import java.util.ArrayList;
7   
8   import org.neo.swarm.ApplicationContext;
9   import org.neo.swarm.util.network.multicast.Sender;
10  import org.neo.swarm.util.network.tcp.client.TcpAsyncSender;
11  import org.neo.swarm.util.serialize.Serializer;
12  
13  /***
14   * Maintains a list of addresses and iterates over each entry,
15   * sending the serialized registration aspect to each entry.
16   * 
17   * @see org.neo.swarm.services.binding.DynamicRunnable
18   * @see org.neo.swarm.services.binding.BindingService
19   * @author navery
20   */
21  public class StaticRunnable extends DynamicRunnable {
22  
23  	ArrayList addressList;
24  
25  	public StaticRunnable(ApplicationContext appContext, Sender sender, Serializer serializer, int timeout) {
26  		super(appContext, sender, serializer, timeout);
27  		addressList = new ArrayList();
28  	}
29  
30      /***
31       * Overloads send method with a tcpClient version that uses blocking sockets
32       * to iterate over a list of address/port sets.
33       * TODO: collapse this into dynamicRunnable as the sender API is slightly different.
34       */
35  	public void run() {
36          running = true;
37  		while (running) {
38  			for (int i = 0; i < addressList.size(); i++) {
39  				if (!running)
40  					break;
41  
42  				HostPortSet hpSet = (HostPortSet) addressList.get(i);
43  				TcpAsyncSender sender = new TcpAsyncSender(sendData.length, hpSet.address, hpSet.port, true);
44  				try {
45                      try {
46  						Thread.sleep(this.timeout);
47  					} catch (InterruptedException e1) {
48  					}                    
49  					sender.connect();
50  					sender.send(sendData);
51  				} catch (IOException e) {
52  					e.printStackTrace();
53                      try {
54  						Thread.sleep(this.timeout);
55  						// TODO: repeated failure should invalidate/remove address from the list.
56  					} catch (InterruptedException e1) {
57  					}
58  				}
59  			}
60  		}
61  	}
62  
63      /***
64       * Adds new address location..
65       */
66  	public void addLocation(InetAddress address, int port) {
67  		addressList.add(new HostPortSet(address, port));
68  	}
69  }
70