View Javadoc

1   /*
2    * Copyright (C) 2010 Unreal Visualizer Authors
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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   * s all the gathering of data for the basic functionality of the visualizer. It stores for instance information on each player in {@link GameData}. It is also
32   * the access for the application to get access to subscribe to {@link AbstractData} and retrieve the {@link UTServer} if that is necessary.
33   * 
34   * @author Lennard de Rijk
35   * @author M.P. Korstanje
36   * 
37   */
38  public class ServerController {
39  
40  	/**
41  	 * The {@link UTServer} this {@link ServerController} controls.
42  	 */
43  	private final ServerDefinition<IUT2004Server> serverDefinition;
44  
45  	/**
46  	 * The collection of data we have on the players in the server.
47  	 */
48  	private final GameData gameData;
49  
50  	/**
51  	 * The {@link AbstractData} that contains the latest {@link GameStatus}.
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  		// Hook up the listeners to the server
68  		addServerListener();
69  
70  		// Init data modules.
71  		init();
72  	}
73  
74  	/**
75  	 * Listener for the server status.
76  	 * 
77  	 * Synchronized as events happen in another thread.
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  						// Notify data components that server has changed.
86  						gameData.serverChanged(server);
87  						gameStatus.serverChanged(server);
88  					}
89  				});
90  	}
91  
92  	/**
93  	 * Initializes the ServerController with data that is already present in the UTServer.
94  	 */
95  	private void init() {
96  		// Notify data components that server is already in place.
97  		IUT2004Server server = serverDefinition.getServerFlag().getFlag();
98  		if (server != null) {
99  			gameData.serverChanged(server);
100 			gameStatus.serverChanged(server);
101 		}
102 	}
103 
104 	/**
105 	 * @return The collection of data we have on all the players
106 	 */
107 	public GameData getGameData() {
108 		return gameData;
109 	}
110 
111 	/**
112 	 * @return the {@link GameStatusData} instance that keeps track of the GameStatus
113 	 */
114 	public GameStatusData getGameStatus() {
115 		return gameStatus;
116 	}
117 
118 	/**
119 	 * @return the {@link WorldServerDefinition} this controllers holds.
120 	 */
121 	public ServerDefinition<IUT2004Server> getServerDefinition() {
122 		return serverDefinition;
123 	}
124 
125 	/**
126 	 * Returns the {@link IUT2004Server} managed by this controller {@link WorldServerDefinition}.
127 	 * 
128 	 * Or null if no server is running.
129 	 * 
130 	 * @return null or the server.
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 	 * Disposes the {@link ServerController} instance.
144 	 */
145 	private void dispose() {
146 		this.serverDefinition.stopServer();
147 	}
148 
149 	/**
150 	 * The singleton instance of the ServerController
151 	 */
152 	private static ServerController singleton;
153 
154 	/**
155 	 * Creates an new {@link ServerController}, disposes the old one iff exists.
156 	 * 
157 	 * @param server
158 	 *            The UTServer used by the {@link ServerController}
159 	 */
160 	public static void createNewController() {
161 		disposeController();
162 		singleton = new ServerController();
163 	}
164 
165 	/**
166 	 * Disposes the current {@link ServerController} if it exists.
167 	 */
168 	public static void disposeController() {
169 		if (singleton != null) {
170 			singleton.dispose();
171 			singleton = null;
172 		}
173 	}
174 
175 	/**
176 	 * 
177 	 * @return the singleton instance of the {@link ServerController}
178 	 */
179 	public static ServerController getInstance() {
180 		return singleton;
181 	}
182 
183 }