1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public final class EnvironmentParameters extends Parameters implements IComponent {
64
65
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
133
134
135
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
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 }