View Javadoc

1   package nl.tudelft.goal.ut2004.visualizer.data;
2   
3   import java.io.Serializable;
4   import java.rmi.Naming;
5   import java.rmi.RemoteException;
6   import java.rmi.registry.LocateRegistry;
7   import java.rmi.server.UnicastRemoteObject;
8   import java.util.HashSet;
9   
10  import nl.tudelft.goal.ut2004.visualizer.connection.EnvironmentService;
11  import nl.tudelft.goal.ut2004.visualizer.connection.VisualizerService;
12  
13  
14  import cz.cuni.amis.utils.collections.ObservableSet;
15  
16  public class EnvironmentData {
17  
18  	public static final String serviceName = "UnrealVisualizerService";
19  	private final ObservableSet<EnvironmentService> environments;
20  	private RegistrationHandler handler;
21  
22  	public EnvironmentData() {
23  		HashSet<EnvironmentService> set = new HashSet<EnvironmentService>();
24  		environments = new ObservableSet<EnvironmentService>(set);
25  	}
26  
27  	public class RegistrationHandler extends UnicastRemoteObject implements
28  			VisualizerService, Serializable {
29  
30  		protected RegistrationHandler() throws RemoteException {
31  			super();
32  		}
33  
34  		@Override
35  		public void registerEnvironment(EnvironmentService environment)
36  				throws RemoteException {
37  			synchronized (environments) {
38  				// Re add for the benefit of our listeners.
39  				if (environments.contains(environment)) {
40  					environments.remove(environment);
41  				}
42  				environments.add(environment);
43  			}
44  
45  		}
46  
47  		@Override
48  		public void unregisterEnvironment(EnvironmentService environment)
49  				throws RemoteException {
50  			synchronized (environments) {
51  				environments.remove(environment);
52  			}
53  		}
54  	}
55  
56  	/**
57  	 * List of clients currently registered with the visualizer.
58  	 * 
59  	 * NOTE: You must synchronize on this collection if you want to iterate.
60  	 * 
61  	 * @return
62  	 */
63  	public ObservableSet<EnvironmentService> getEnvironments() {
64  		return environments;
65  	}
66  
67  	public void connect() {
68  		System.out.println("RMI server started");
69  
70  		// Create and install a security manager
71  		// TODO: How hoes this work?
72  		// if (System.getSecurityManager() == null) {
73  		// System.setSecurityManager(new RMISecurityManager());
74  		// System.out.println("Security manager installed.");
75  		// } else {
76  		// System.out.println("Security manager already exists.");
77  		// }
78  
79  		try { // special exception handler for registry creation
80  			LocateRegistry.createRegistry(1099);
81  			System.out.println("Java RMI registry created.");
82  		} catch (RemoteException e) {
83  			// do nothing, error means registry already exists
84  			System.out.println("Java RMI registry already exists.");
85  		}
86  
87  		try {
88  			// Instantiate handler
89  			handler = new RegistrationHandler();
90  			// Bind this object instance to the name "RmiServer"
91  			Naming.rebind(serviceName, handler);
92  			System.out.println("UnrealVisualizerService bound in registry");
93  		} catch (Exception e) {
94  			System.err.println("RMI server exception:" + e);
95  			e.printStackTrace();
96  		}
97  	}
98  
99  }