View Javadoc

1   
2   package org.neo.swarm.services.multicast;
3   
4   import java.io.IOException;
5   
6   import org.neo.swarm.services.Service;
7   import org.neo.swarm.util.network.multicast.Receiver;
8   import org.neo.swarm.util.network.tcp.ListenCallback;
9   import org.neo.swarm.util.threads.ThreadPool;
10  
11  /***
12   * Feeds incoming multicast invocations into the Container
13   * @see org.neo.swarm.core.aop.silc.comp.AspectInvocation
14   * @author navery
15   */
16  public class MulticastServer implements Service {
17  
18  	boolean running = false;
19  	Runnable servicingThread;
20  	ListenCallback router;
21  	ThreadPool threadPool;
22  	
23  	public MulticastServer(Receiver receiver, ListenCallback router, ThreadPool threadPool) {
24  		this.router = router;
25  		this.threadPool = threadPool;
26  		servicingThread = new ServicingThread(receiver, router);
27  	}
28  	public String getName() {
29  		return "MutlicastService";
30  	}
31  
32  	public void start() {
33  		running = true;
34  		try {
35  			threadPool.execute(servicingThread);
36  		} catch (InterruptedException e) {
37  			e.printStackTrace();
38  		}		
39  	}
40  
41  	public void stop() {
42  		this.running = false;
43  	}
44  	private class ServicingThread implements Runnable {
45  		Receiver receiver;
46  		ListenCallback router;
47  		public ServicingThread(Receiver receiver, ListenCallback router) {
48  			this.receiver = receiver;
49  			this.router = router;
50  		}
51  		public void run() {
52  			while (running) {
53  				try {
54  					router.messageDataReceived(receiver.read());
55  				} catch (IOException ex){
56  				}
57  			}
58  		}
59  	}
60  }