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
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
59
60
61
62
63
64 public ObservableSet<EnvironmentService> getEnvironments() {
65 return environments;
66 }
67
68 public void connect() {
69 System.out.println("RMI server started");
70
71
72
73
74
75
76
77
78
79
80 try {
81 LocateRegistry.createRegistry(1099);
82 System.out.println("Java RMI registry created.");
83 } catch (RemoteException e) {
84
85 System.out.println("Java RMI registry already exists.");
86 }
87
88 try {
89
90 handler = new RegistrationHandler();
91
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 }