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