View Javadoc

1   package cz.cuni.amis.pogamut.unreal.bot.impl;
2   
3   import java.io.File;
4   import java.util.Collections;
5   import java.util.HashMap;
6   import java.util.Map;
7   import java.util.logging.Handler;
8   import java.util.logging.Level;
9   
10  import javax.management.MBeanServer;
11  import javax.management.ObjectName;
12  
13  import cz.cuni.amis.introspection.Folder;
14  import cz.cuni.amis.introspection.IntrospectionException;
15  import cz.cuni.amis.introspection.Property;
16  import cz.cuni.amis.pogamut.base.agent.IAgentId;
17  import cz.cuni.amis.pogamut.base.agent.exceptions.AgentException;
18  import cz.cuni.amis.pogamut.base.agent.exceptions.CantStartJMXException;
19  import cz.cuni.amis.pogamut.base.agent.exceptions.JMXAlreadyEnabledException;
20  import cz.cuni.amis.pogamut.base.agent.impl.AgentId;
21  import cz.cuni.amis.pogamut.base.agent.state.impl.AgentStateStarted;
22  import cz.cuni.amis.pogamut.base.agent.state.level0.IAgentState;
23  import cz.cuni.amis.pogamut.base.communication.command.IAct;
24  import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
25  import cz.cuni.amis.pogamut.base.component.IComponent;
26  import cz.cuni.amis.pogamut.base.component.bus.IComponentBus;
27  import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
28  import cz.cuni.amis.pogamut.base.utils.logging.ILogCategories;
29  import cz.cuni.amis.pogamut.base.utils.logging.ILogPublisher;
30  import cz.cuni.amis.pogamut.base.utils.logging.LogCategory;
31  import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
32  import cz.cuni.amis.pogamut.base3d.worldview.object.Rotation;
33  import cz.cuni.amis.pogamut.base3d.worldview.object.Velocity;
34  import cz.cuni.amis.pogamut.unreal.bot.IUnrealBot;
35  import cz.cuni.amis.pogamut.unreal.communication.messages.gbinfomessages.IPlayer;
36  import cz.cuni.amis.pogamut.unreal.server.IUnrealServer;
37  import cz.cuni.amis.utils.exception.PogamutException;
38  import cz.cuni.amis.utils.flag.Flag;
39  import cz.cuni.amis.utils.flag.ImmutableFlag;
40  
41  /**
42   * IUnrealAgent adapter for native players. Not all operations are supported.
43   * @author ik
44   */
45  public abstract class NativeUnrealBotAdapter implements IUnrealBot {
46  
47  	private IAgentLogger logger = new IAgentLogger() {
48          private LogCategory single = new LogCategory("default");
49  
50          private ILogCategories categories = new ILogCategories() {
51  
52          	@Override
53              public boolean hasCategory(String name) {
54          		if (name == null) return false;
55          		if (name.equalsIgnoreCase("default")) return true;
56                  return false;
57              }
58  
59          	@Override
60              public Map<String, LogCategory> getCategories() {
61          		HashMap<String, LogCategory> categories = new HashMap<String, LogCategory>();
62          		categories.put("default", single);
63          		return Collections.unmodifiableMap(categories);
64              }
65  
66          	@Override
67              public String[] getCategoryNames() {
68                  return new String[]{"default"};
69              }
70  
71          	@Override
72              public String[] getCategoryNamesSorted() {
73                  return new String[]{"default"};
74              }
75  
76          	@Override
77              public LogCategory getCategory(String name) {
78          		if (name == null) throw new UnsupportedOperationException("Not supported for the native bot.");
79          		if (name.equals("default")) return single;
80                  throw new UnsupportedOperationException("Not supported for the native bot.");
81              }
82  
83          	@Override
84              public void setLevel(Level newLevel) {
85          		single.setLevel(newLevel);
86              }
87  
88  			@Override
89  			public void addLogCategory(String name, LogCategory category) {
90  				throw new UnsupportedOperationException("Not supported for the native bot.");
91  			}
92          };
93  
94  		@Override
95  		public void addDefaultConsoleHandler() {
96  			throw new UnsupportedOperationException("Not supported for the native bot.");
97  		}
98  
99  		@Override
100 		public Handler addDefaultFileHandler(File file) {
101 			throw new UnsupportedOperationException("Not supported for the native bot.");
102 		}
103 
104 		@Override
105 		public void addDefaultHandler(Handler handler) {
106 			throw new UnsupportedOperationException("Not supported for the native bot.");
107 		}
108 
109 		@Override
110 		public Handler addDefaultPublisher(ILogPublisher publisher) {
111 			throw new UnsupportedOperationException("Not supported for the native bot.");
112 		}
113 
114 		@Override
115 		public void addToAllCategories(ILogPublisher logPublisher) {
116 			single.addHandler(logPublisher);
117 		}
118 
119 		@Override
120 		public void addToAllCategories(Handler handler) {
121 			single.addHandler(handler);
122 			
123 		}
124 
125 		@Override
126 		public IAgentId getAgentId() {
127 			return agentId;
128 		}
129 
130 		@Override
131 		public Map<String, LogCategory> getCategories() {
132 			return categories.getCategories();
133 		}
134 
135 		@Override
136 		public LogCategory getCategory(IComponent component) {
137 			return categories.getCategory(component.getComponentId().getToken());
138 		}
139 
140 		@Override
141 		public LogCategory getCategory(String name) {
142 			return categories.getCategory(name);
143 		}
144 
145 		@Override
146 		public void setLevel(Level newLevel) {
147 			categories.setLevel(newLevel);
148 		}
149 
150 		@Override
151 		public void enableJMX(MBeanServer mBeanServer, ObjectName parent)
152 				throws JMXAlreadyEnabledException, CantStartJMXException {
153 			throw new UnsupportedOperationException("Not supported for the native bot.");
154 		}
155 
156 		@Override
157 		public Integer getNetworkLoggerPort() {
158 			throw new UnsupportedOperationException("Not supported for the native bot.");
159 		}
160 
161                 @Override
162 		public String getNetworkLoggerHost() {
163 			throw new UnsupportedOperationException("Not supported for the native bot.");
164 		}
165 
166 		@Override
167 		public void addDefaultNetworkHandler() {
168 			throw new UnsupportedOperationException("Not supported for the native bot.");
169 		}
170 
171 		@Override
172 		public boolean isDefaultConsoleHandler() {
173 			throw new UnsupportedOperationException("Not supported for the native bot.");
174 		}
175 
176 		@Override
177 		public boolean isDefaultNetworkHandler() {
178 			throw new UnsupportedOperationException("Not supported for the native bot.");
179 		}
180 
181 		@Override
182 		public void removeDefaultConsoleHandler() {
183 			throw new UnsupportedOperationException("Not supported for the native bot.");
184 		}
185 
186 		@Override
187 		public void removeDefaultHandler(Handler handler) {
188 			throw new UnsupportedOperationException("Not supported for the native bot.");
189 		}
190 
191 		@Override
192 		public void removeDefaultNetworkHandler() {
193 			throw new UnsupportedOperationException("Not supported for the native bot.");
194 		}
195 
196 		@Override
197 		public void removeFromAllCategories(Handler handler) {
198 			throw new UnsupportedOperationException("Not supported for the native bot.");
199 		}
200 
201 		@Override
202 		public Handler getDefaultConsoleHandler() {
203 			// TODO Auto-generated method stub
204 			return null;
205 		}
206 
207 		@Override
208 		public Handler getDefaultNetworkHandler() {
209 			// TODO Auto-generated method stub
210 			return null;
211 		}
212      
213     };
214 	
215     protected IPlayer player = null;
216     protected IUnrealServer server = null;
217     protected IAct act;
218     protected IWorldView worldView = null;
219     /**
220      * Configuration of this bot.
221      */
222    // protected ConfigChange config = null;
223     private Flag<IAgentState> state = new Flag<IAgentState>(new AgentStateStarted("Native bot is running."));
224     private IAgentId agentId; 
225 
226     public NativeUnrealBotAdapter(IPlayer player, IUnrealServer server, IAct act, IWorldView worldView) {
227         this.player = player;
228         this.agentId = new AgentId(player.getName());
229         this.server = server;
230         this.act = act;
231         this.worldView = worldView;
232     }
233 
234     @Override
235     public IAct getAct() {
236         throw new UnsupportedOperationException("Native bots cannot be controled from Java.");
237     }
238     
239     @Override
240     public IWorldView getWorldView() {
241     	return worldView;
242     }
243     
244     @Override
245     public IAgentId getComponentId() {
246     	return agentId;
247     }
248 
249     @Override
250     public String getName() {
251         return agentId.getName().getFlag();
252     }
253 
254     @Override
255     public IAgentLogger getLogger() {
256         return this.logger;
257     }
258     
259     @Override
260 	public IComponentBus getEventBus() {
261 		throw new UnsupportedOperationException("Not supported for the native bot.");
262 	}
263 
264     @Override
265     public ImmutableFlag<IAgentState> getState() {
266         return state.getImmutable();
267     }
268 
269     @Override
270     public void start() throws AgentException {
271         throw new UnsupportedOperationException("Native bot is already started.");
272     }
273     
274     @Override
275     public void startPaused() throws AgentException {
276         throw new UnsupportedOperationException("Native bot is already started.");
277     }
278 
279     @Override
280     public void pause() throws AgentException {
281         throw new UnsupportedOperationException("Not supported for the native bot.");
282     }
283 
284 
285     @Override
286     public void resume() throws AgentException {
287         throw new UnsupportedOperationException("Not supported for the native bot.");
288     }
289 
290     @Override
291     public void kill() {
292         stop();
293     }
294 
295     private Folder folder = new Folder("empty") {
296         private Folder[]  subfolders = new Folder[]{};
297         private Property[]  properties = new Property[]{};
298 
299         @Override
300         public Folder[] getFolders() throws IntrospectionException {
301             return subfolders;
302         }
303 
304         @Override
305         public Property[] getProperties() throws IntrospectionException {
306             return properties;
307         }
308     };
309     
310     @Override
311     public Folder getIntrospection() {
312        return folder;
313     }
314 
315     @Override
316     public Location getLocation() {
317         return player.getLocation();
318     }
319 
320     @Override
321     public Velocity getVelocity() {
322         return player.getVelocity();
323     }
324 
325     @Override
326     public Rotation getRotation() {
327         return player.getRotation();
328     }
329 
330 /* TODO
331     @Override
332     public void setBoolConfigure(BoolBotParam param, boolean value) {
333         try {
334             Configuration configuration = new Configuration();
335             configuration.setId(player.getId());
336             ConfigChange confCh = getConfig();
337             if (confCh != null) {
338                 configuration.copy(confCh);
339                 param.setField(confCh, value);
340             }
341             param.set(configuration, value);
342             act.act(configuration);
343         } catch (Exception ex) {
344             ex.printStackTrace();
345         }
346     }
347 
348     @Override
349     public boolean getBoolConfigure(BoolBotParam param) {
350         try {
351             ConfigChange confCh = getConfig();
352             if (confCh != null) {
353                 return param.get(getConfig());
354             } else {
355                 return false;
356             }
357         } catch (Exception ex) {
358             ex.printStackTrace();
359             return false; // TODO
360         }
361     }
362 
363     protected ConfigChange getConfig() {
364         if (config == null) {
365             for (ConfigChange c : worldView.getAll(ConfigChange.class).values()) {
366                 if (c.getBotId().equals(player.getId())) {
367                     config = c;
368                     break;
369                 }
370             }
371         }
372         return config;
373     }
374 */
375     @Override
376     public boolean equals(Object other) {
377         if (other == null) return false;
378         if (!(other instanceof NativeUnrealBotAdapter)) return false;
379         NativeUnrealBotAdapter otherAdapter = (NativeUnrealBotAdapter) other;
380         return this.agentId.equals(otherAdapter.getComponentId());
381     }
382     
383     @Override
384 	public int hashCode() {
385     	return this.agentId.hashCode();
386     }
387 
388 }