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