View Javadoc

1   /**
2    * BaseUnrealEnvironment, an implementation of the environment interface standard that 
3    * facilitates the connection between GOAL and the UT2004 engine. 
4    * 
5    * Copyright (C) 2012 BaseUnrealEnvironment authors.
6    * 
7    * This program is free software: you can redistribute it and/or modify it under
8    * the terms of the GNU General Public License as published by the Free Software
9    * Foundation, either version 3 of the License, or (at your option) any later
10   * version.
11   * 
12   * This program is distributed in the hope that it will be useful, but WITHOUT
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15   * details.
16   * 
17   * You should have received a copy of the GNU General Public License along with
18   * this program. If not, see <http://www.gnu.org/licenses/>.
19   */
20  package nl.tudelft.goal.unreal.messages;
21  
22  import java.net.URI;
23  import java.net.URISyntaxException;
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.logging.Level;
29  
30  import nl.tudelft.goal.EIS2Java.exception.TranslationException;
31  import nl.tudelft.goal.EIS2Java.translation.Translator;
32  import nl.tudelft.goal.unreal.environment.UnrealEnvironmentException;
33  
34  import cz.cuni.amis.pogamut.base.agent.impl.AgentId;
35  import cz.cuni.amis.pogamut.base.agent.params.IAgentParameters;
36  import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.ISocketConnectionAddress;
37  import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.SocketConnectionAddress;
38  import cz.cuni.amis.pogamut.base.component.IComponent;
39  import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
40  import cz.cuni.amis.utils.token.IToken;
41  import cz.cuni.amis.utils.token.Tokens;
42  import eis.iilang.Identifier;
43  import eis.iilang.Parameter;
44  
45  /**
46   * Holds parameters specific for the environment.
47   * 
48   * Parameters stored are:
49   * <ul>
50   * <li>{@link Key#CONTROLSERVER}</li>
51   * <li>{@link Key#LOGLEVEL}</li>
52   * <li>{@link Key#CONTROLSERVERNAME}</li>
53   * <li>{@link Key#TEAMSIZE}</li>
54   * <li>{@link Key#VISUALIZERSERVER}</li>
55   * </ul>
56   * 
57   * Also provides functionality to assign defaults to parameters that have not
58   * been assigned.
59   * 
60   * @author M.P. Korstanje
61   * 
62   */
63  public final class EnvironmentParameters extends Parameters implements IComponent {
64  
65  	// Environment parameters
66  	private Level logLevel;
67  	private List<String> botNames;
68  	private URI visualizerServer;
69  
70  	public EnvironmentParameters(IAgentLogger logger) {
71  		super(logger);
72  	}
73  
74  	public EnvironmentParameters(Map<String, Parameter> parameters, IAgentLogger logger) throws UnrealEnvironmentException {
75  		super(parameters, logger);
76  	}
77  
78  	@Override
79  	public void assignDefaults(IAgentParameters defaults) {
80  		super.assignDefaults(defaults);
81  
82  		if (defaults instanceof EnvironmentParameters) {
83  			EnvironmentParameters parameters = (EnvironmentParameters) defaults;
84  
85  			if (logLevel == null)
86  				logLevel = parameters.getLogLevel();
87  			if (botNames == null)
88  				botNames = new ArrayList<String>(parameters.getBotNames());
89  			if (visualizerServer == null)
90  				visualizerServer = parameters.getVisualizerServer();
91  		}
92  	}
93  
94  	@Override
95  	public IToken getComponentId() {
96  		return Tokens.get(getClass().getSimpleName());
97  	}
98  
99  	public Level getLogLevel() {
100 		return logLevel;
101 	}
102 
103 	public List<String> getBotNames() {
104 		return botNames;
105 	}
106 
107 	public void setVisualizerServer(URI uri) {
108 		this.visualizerServer = uri;
109 	}
110 
111 	public URI getVisualizerServer() {
112 		return visualizerServer;
113 	}
114 
115 	public URI getUTServer() {
116 		SocketConnectionAddress address = (SocketConnectionAddress) getWorldAddress();
117 
118 		if (address.getHost() == null || address.getPort() == -1)
119 			return null;
120 
121 		try {
122 			URI uri = new URI("ut://" + address.getHost() + ":" + address.getPort());
123 			return uri;
124 		} catch (URISyntaxException e) {
125 			e.printStackTrace();
126 		}
127 
128 		return null;
129 	}
130 
131 	/**
132 	 * Returns a map of this parameter object.
133 	 * 
134 	 * 
135 	 * @return
136 	 */
137 	public Map<String, Parameter> map() {
138 
139 		Map<String, Parameter> map = new HashMap<String, Parameter>();
140 
141 		if (logLevel != null) {
142 			map.put(Key.LOGLEVEL.toString(), new Identifier(logLevel.toString()));
143 		}
144 		if (getAgentId() != null) {
145 			map.put(Key.CONTROLSERVERNAME.toString(), new Identifier(getAgentId().getName().getFlag()));
146 		}
147 		if (getWorldAddress() != null) {
148 			assert getWorldAddress() instanceof ISocketConnectionAddress;
149 			ISocketConnectionAddress address = (ISocketConnectionAddress) getWorldAddress();
150 			String adressString = "ut://" + address.getHost() + ":" + address.getPort();
151 			map.put(Key.CONTROLSERVER.toString(), new Identifier(adressString));
152 		}
153 		assert map.size() + 2 == Key.values().length : "Missing values.";
154 
155 		return map;
156 
157 	}
158 
159 	public Parameters setLogLevel(Level level) {
160 		assert level != null;
161 		log.info(String.format("Set %s to %s.", Key.LOGLEVEL, level));
162 		this.logLevel = level;
163 		return this;
164 	}
165 
166 	public Parameters setBotNames(List<String> botNames) {
167 		assert botNames != null;
168 
169 		this.botNames = new ArrayList<String>();
170 		this.botNames.addAll(botNames);
171 
172 		log.info(String.format("Set %s to %s.", Key.BOTNAMES, this.botNames));
173 		return this;
174 	}
175 
176 
177 	@Override
178 	protected void setKey(Key key, Parameter value) throws TranslationException {
179 
180 		switch (key) {
181 		case LOGLEVEL:
182 			setLogLevel(value);
183 			break;
184 		case CONTROLSERVER:
185 			setWorldAddress(value);
186 			break;
187 		case VISUALIZERSERVER:
188 			setVisualizerServer(value);
189 			break;
190 		case CONTROLSERVERNAME:
191 			setServerName(value);
192 			break;
193 		case BOTNAMES:
194 			setBotNames(value);
195 			break;
196 		default:
197 			// Not one of our keys.
198 			break;
199 		}
200 
201 	}
202 
203 	private void setLogLevel(Parameter value) throws TranslationException {
204 		setLogLevel(Translator.getInstance().translate2Java(value, Level.class));
205 	}
206 
207 	private void setServerName(Parameter value) throws TranslationException {
208 		setAgentId(Translator.getInstance().translate2Java(value, AgentId.class));
209 	}
210 
211 	private void setVisualizerServer(Parameter value) throws TranslationException {
212 		URI uri = Translator.getInstance().translate2Java(value, URI.class);
213 		setVisualizerServer(uri);
214 	}
215 
216 	private void setBotNames(Parameter value) throws TranslationException {
217 		setBotNames(Translator.getInstance().translate2Java(value, StringList.class));
218 	}
219 
220 	private void setWorldAddress(Parameter value) throws TranslationException {
221 		URI uri = Translator.getInstance().translate2Java(value, URI.class);
222 		setWorldAddress(new SocketConnectionAddress(uri));
223 	}
224 
225 	public static EnvironmentParameters getDefaults(IAgentLogger log) {
226 
227 		EnvironmentParameters parameters = new EnvironmentParameters(log);
228 
229 		parameters.logLevel = Level.WARNING;
230 		parameters.botNames = new ArrayList<String>();
231 		parameters.visualizerServer = null;
232 		parameters.setAgentIdSilent("UnrealGoalEnvironmentControlServer");
233 		parameters.setWorldAddressSilent(LOCAL_HOST, CONTROL_SERVER_PORT);
234 
235 		return parameters;
236 	}
237 
238 	private void setWorldAddressSilent(String host, int port) {
239 		super.setWorldAddress(new SocketConnectionAddress(host, port));
240 	}
241 
242 	private void setAgentIdSilent(String name) {
243 		super.setAgentId(new AgentId(name));
244 	}
245 
246 }