1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package nl.tudelft.goal.ut2004.visualizer.controller;
18
19
20 import nl.tudelft.goal.ut2004.visualizer.data.EnvironmentData;
21 import nl.tudelft.goal.ut2004.visualizer.data.GameData;
22 import nl.tudelft.goal.ut2004.visualizer.data.GameStatusData;
23 import nl.tudelft.pogamut.base.server.ReconnectingServerDefinition;
24 import nl.tudelft.pogamut.base.server.ServerDefinition;
25 import nl.tudelft.pogamut.ut2004.server.UTServerDefinition;
26 import cz.cuni.amis.pogamut.ut2004.server.IUT2004Server;
27 import cz.cuni.amis.utils.flag.FlagListener;
28
29
30
31
32
33
34
35
36
37
38 public class ServerController {
39
40
41
42
43 private final ServerDefinition<IUT2004Server> serverDefinition;
44
45
46
47
48 private final GameData gameData;
49
50
51
52
53 private final GameStatusData gameStatus;
54
55 private final EnvironmentData environmentData;
56
57 private ServerController() {
58
59 UTServerDefinition utServerDefinition = new UTServerDefinition();
60 this.serverDefinition = new ReconnectingServerDefinition<IUT2004Server>(
61 utServerDefinition);
62
63 this.gameData = new GameData();
64 this.gameStatus = new GameStatusData();
65 this.environmentData = new EnvironmentData();
66 this.environmentData.connect();
67
68 addServerListener();
69
70
71 init();
72 }
73
74
75
76
77
78
79
80 private void addServerListener() {
81 this.serverDefinition.getServerFlag().addListener(
82 new FlagListener<IUT2004Server>() {
83 @Override
84 public synchronized void flagChanged(IUT2004Server server) {
85
86 gameData.serverChanged(server);
87 gameStatus.serverChanged(server);
88 }
89 });
90 }
91
92
93
94
95 private void init() {
96
97 IUT2004Server server = serverDefinition.getServerFlag().getFlag();
98 if (server != null) {
99 gameData.serverChanged(server);
100 gameStatus.serverChanged(server);
101 }
102 }
103
104
105
106
107 public GameData getGameData() {
108 return gameData;
109 }
110
111
112
113
114 public GameStatusData getGameStatus() {
115 return gameStatus;
116 }
117
118
119
120
121 public ServerDefinition<IUT2004Server> getServerDefinition() {
122 return serverDefinition;
123 }
124
125
126
127
128
129
130
131
132
133
134 public IUT2004Server getServer() {
135 return serverDefinition.getServerFlag().getFlag();
136 }
137
138 public EnvironmentData getEnvironmentData() {
139 return environmentData;
140 }
141
142
143
144
145 private void dispose() {
146 this.serverDefinition.stopServer();
147 }
148
149
150
151
152 private static ServerController singleton;
153
154
155
156
157
158
159
160 public static void createNewController() {
161 disposeController();
162 singleton = new ServerController();
163 }
164
165
166
167
168 public static void disposeController() {
169 if (singleton != null) {
170 singleton.dispose();
171 singleton = null;
172 }
173 }
174
175
176
177
178
179 public static ServerController getInstance() {
180 return singleton;
181 }
182
183 }