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.unreal.environment.UnrealEnvironmentException;
31 import cz.cuni.amis.pogamut.base.agent.impl.AgentId;
32 import cz.cuni.amis.pogamut.base.agent.params.IAgentParameters;
33 import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.ISocketConnectionAddress;
34 import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.SocketConnectionAddress;
35 import cz.cuni.amis.pogamut.base.component.IComponent;
36 import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
37 import cz.cuni.amis.utils.token.IToken;
38 import cz.cuni.amis.utils.token.Tokens;
39 import eis.eis2java.exception.TranslationException;
40 import eis.eis2java.translation.Translator;
41 import eis.iilang.Identifier;
42 import eis.iilang.Parameter;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public final class EnvironmentParameters extends Parameters implements IComponent {
62
63
64 private Level logLevel;
65 private List<String> botNames;
66 private URI visualizerServer;
67
68 public EnvironmentParameters(IAgentLogger logger) {
69 super(logger);
70 }
71
72 public EnvironmentParameters(Map<String, Parameter> parameters, IAgentLogger logger) throws UnrealEnvironmentException {
73 super(parameters, logger);
74 }
75
76 @Override
77 public void assignDefaults(IAgentParameters defaults) {
78 super.assignDefaults(defaults);
79
80 if (defaults instanceof EnvironmentParameters) {
81 EnvironmentParameters parameters = (EnvironmentParameters) defaults;
82
83 if (logLevel == null)
84 logLevel = parameters.getLogLevel();
85 if (botNames == null)
86 botNames = new ArrayList<String>(parameters.getBotNames());
87 if (visualizerServer == null)
88 visualizerServer = parameters.getVisualizerServer();
89 }
90 }
91
92 @Override
93 public IToken getComponentId() {
94 return Tokens.get(getClass().getSimpleName());
95 }
96
97 public Level getLogLevel() {
98 return logLevel;
99 }
100
101 public List<String> getBotNames() {
102 return botNames;
103 }
104
105 public void setVisualizerServer(URI uri) {
106 this.visualizerServer = uri;
107 }
108
109 public URI getVisualizerServer() {
110 return visualizerServer;
111 }
112
113 public URI getUTServer() {
114 SocketConnectionAddress address = (SocketConnectionAddress) getWorldAddress();
115
116 if (address.getHost() == null || address.getPort() == -1)
117 return null;
118
119 try {
120 URI uri = new URI("ut://" + address.getHost() + ":" + address.getPort());
121 return uri;
122 } catch (URISyntaxException e) {
123 e.printStackTrace();
124 }
125
126 return null;
127 }
128
129
130
131
132
133
134
135 public Map<String, Parameter> map() {
136
137 Map<String, Parameter> map = new HashMap<String, Parameter>();
138
139 if (logLevel != null) {
140 map.put(Key.LOGLEVEL.toString(), new Identifier(logLevel.toString()));
141 }
142 if (getAgentId() != null) {
143 map.put(Key.CONTROLSERVERNAME.toString(), new Identifier(getAgentId().getName().getFlag()));
144 }
145 if (getWorldAddress() != null) {
146 assert getWorldAddress() instanceof ISocketConnectionAddress;
147 ISocketConnectionAddress address = (ISocketConnectionAddress) getWorldAddress();
148 String adressString = "ut://" + address.getHost() + ":" + address.getPort();
149 map.put(Key.CONTROLSERVER.toString(), new Identifier(adressString));
150 }
151 assert map.size() + 2 == Key.values().length : "Missing values.";
152
153 return map;
154
155 }
156
157 public Parameters setLogLevel(Level level) {
158 assert level != null;
159 log.info(String.format("Set %s to %s.", Key.LOGLEVEL, level));
160 this.logLevel = level;
161 return this;
162 }
163
164 public Parameters setBotNames(List<String> botNames) {
165 assert botNames != null;
166
167 this.botNames = new ArrayList<String>();
168 this.botNames.addAll(botNames);
169
170 log.info(String.format("Set %s to %s.", Key.BOTNAMES, this.botNames));
171 return this;
172 }
173
174
175 @Override
176 protected void setKey(Key key, Parameter value) throws TranslationException {
177
178 switch (key) {
179 case LOGLEVEL:
180 setLogLevel(value);
181 break;
182 case CONTROLSERVER:
183 setWorldAddress(value);
184 break;
185 case VISUALIZERSERVER:
186 setVisualizerServer(value);
187 break;
188 case CONTROLSERVERNAME:
189 setServerName(value);
190 break;
191 case BOTNAMES:
192 setBotNames(value);
193 break;
194 default:
195
196 break;
197 }
198
199 }
200
201 private void setLogLevel(Parameter value) throws TranslationException {
202 setLogLevel(Translator.getInstance().translate2Java(value, Level.class));
203 }
204
205 private void setServerName(Parameter value) throws TranslationException {
206 setAgentId(Translator.getInstance().translate2Java(value, AgentId.class));
207 }
208
209 private void setVisualizerServer(Parameter value) throws TranslationException {
210 URI uri = Translator.getInstance().translate2Java(value, URI.class);
211 setVisualizerServer(uri);
212 }
213
214 private void setBotNames(Parameter value) throws TranslationException {
215 setBotNames(Translator.getInstance().translate2Java(value, StringList.class));
216 }
217
218 private void setWorldAddress(Parameter value) throws TranslationException {
219 URI uri = Translator.getInstance().translate2Java(value, URI.class);
220 setWorldAddress(new SocketConnectionAddress(uri));
221 }
222
223 public static EnvironmentParameters getDefaults(IAgentLogger log) {
224
225 EnvironmentParameters parameters = new EnvironmentParameters(log);
226
227 parameters.logLevel = Level.WARNING;
228 parameters.botNames = new ArrayList<String>();
229 parameters.visualizerServer = null;
230 parameters.setAgentIdSilent("UnrealGoalEnvironmentControlServer");
231 parameters.setWorldAddressSilent(LOCAL_HOST, CONTROL_SERVER_PORT);
232
233 return parameters;
234 }
235
236 private void setWorldAddressSilent(String host, int port) {
237 super.setWorldAddress(new SocketConnectionAddress(host, port));
238 }
239
240 private void setAgentIdSilent(String name) {
241 super.setAgentId(new AgentId(name));
242 }
243
244 }