View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent;
2   
3   import com.google.inject.AbstractModule;
4   import com.google.inject.Injector;
5   import com.google.inject.Provider;
6   import com.google.inject.name.Names;
7   
8   import cz.cuni.amis.pogamut.base.agent.IAgent;
9   import cz.cuni.amis.pogamut.base.communication.connection.IWorldConnection;
10  import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.ISocketConnectionAddress;
11  import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.SocketConnection;
12  import cz.cuni.amis.pogamut.base.communication.messages.InfoMessage;
13  import cz.cuni.amis.pogamut.base.communication.parser.IWorldMessageParser;
14  import cz.cuni.amis.pogamut.base.communication.parser.impl.yylex.IYylex;
15  import cz.cuni.amis.pogamut.base.communication.parser.impl.yylex.IYylexObserver;
16  import cz.cuni.amis.pogamut.base.communication.translator.IWorldMessageTranslator;
17  import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
18  import cz.cuni.amis.pogamut.base.component.controller.ComponentDependencies;
19  import cz.cuni.amis.pogamut.base.component.controller.ComponentDependencyType;
20  import cz.cuni.amis.pogamut.base.factory.guice.GuiceAgentModule;
21  import cz.cuni.amis.pogamut.base.factory.guice.GuiceRemoteAgentModule;
22  import cz.cuni.amis.pogamut.base.utils.guice.AdaptableProvider;
23  import cz.cuni.amis.pogamut.ut2004.agent.params.UT2004AgentParameters;
24  import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType;
25  import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemTypeTranslator;
26  import cz.cuni.amis.pogamut.ut2004.communication.messages.UT2004ItemType;
27  import cz.cuni.amis.pogamut.ut2004.communication.messages.UT2004ItemTypeTranslator;
28  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Yylex;
29  import cz.cuni.amis.pogamut.ut2004.communication.parser.IUT2004Yylex;
30  import cz.cuni.amis.pogamut.ut2004.communication.parser.UT2004Parser;
31  import cz.cuni.amis.pogamut.ut2004.communication.translator.itemdescriptor.ItemTranslator;
32  
33  /**
34   * Module extending {@link RemoteGuiceAgentModule} for the purpose of UT2004 communication specification.
35   * <p><p>
36   * Newly binded classes:
37   * <table>
38   * <tr><th>Mapped class</th>                    <th>  </th> <th>Target</th>                          <th>Description</th></tr>
39   * 
40   * <tr><td>{@link IWorldConnection}</td>        <td>-></td> <td>{@link SocketConnection}</td>        <td>Agent bus synchronizing starting/stopping/etc. events.</td></tr>
41   * <tr><td>{@link SocketConnection} dependencies</td>
42   *                                              <td>-></td> <td>{@link UT2004CommunicationModule#connectionDependenciesProvider}</td></tr>
43   * <tr><td>{@link SocketConnection} address</td><td>-></td> <td>{@link UT2004BotModule#getAddressProvider()</td></tr>                                             
44   * <tr><td>{@link IWorldMessageParser}</td>		<td>-></td> <td>{@link UT2004Parser}</td>            <td>Wrapper for the yylex parser of the messages coming from GameBots2004.</td></tr>
45   * <tr><td>{@link IYylex}</td>                  <td>-></td> <td>{@link IUT2004Yylex}</td>            <td>Specifying yylex further.</td>
46   * <tr><td>{@link IUT2004Yylex}</td>            <td>-></td> <td>{@link Yylex}</td>                   <td>Specifying yylex further.</td>
47   * <tr><td>{@link IYylexObserver}</td>          <td>-></td> <td>{@link IYylexObserver.LogObserver}</td>
48   *                                                                                                   <td>Yylex observer reporting errors.</td>
49   * <tr><td>{@link IUT2004Yylex}</td>            <td>-></td> <td>{@link Yylex}</td>                   <td>Concrete Yylex implementations that parses the messages coming from GameBots2004.</td></tr>
50   * <tr><td>{@link ItemTranslator}</td>			<td>-></td>	<td>{@link ItemTranslator}</td>			 <td>Object handling translation of INV messages.</td></tr>
51   * <tr><td>{@link UT2004AgentParameters}</td>   <td>-></td> <td>{@link UT2004CommunicationModule#getAgentParamsProvider()}</td>
52   *                                                                                                   <td>Agent parameters passed by the factory, contains additional runtime dependencies.</td>
53   * </table>
54   * <p></p>
55   * 
56   * </table>
57   * To have <b>successful module</b> the descendant <b>must specify</b> these <b>missing bindings</b>:
58   * <table>
59   * <tr><th>Mapped class</th>                    <th>Description</th></tr>
60   * <tr><td>{@link IWorldView}</td>				<td>Binds world view as vision world view.</td></tr>
61   * <tr><td>{@link IWorldMessageTranslator}</td>	<td>Protocol-validating translator of {@link InfoMessage}s of GameBots2004.</td></tr>
62   * <tr><td>{@link IAgent}</td>                 
63   * </table>
64   * ... plus all newly introduced dependencies (by various implementors of mentioned interfaces).<p>
65   * ... <b>don't forget to call super.configureModules()</b> in the subclasses ;-)
66   * 
67   * @see GuiceRemoteAgentModule
68   * @see GuiceAgentModule
69   * @author Jimmy
70   */
71  public class UT2004CommunicationModule<PARAMS extends UT2004AgentParameters> extends GuiceRemoteAgentModule<PARAMS>{
72  
73  	protected AdaptableProvider<ComponentDependencies> connectionDependenciesProvider = new AdaptableProvider<ComponentDependencies>(null);
74  	
75  	/**
76  	 * Binds runtime dependencies to the module/{@link Injector}.
77  	 * <p><p>
78  	 * Must be called before the new agent is instantiated with {@link Injector}.
79  	 * @param agentId
80  	 * @param address
81  	 */
82  	@Override
83  	public void prepareNewAgent(PARAMS agentParameters) {
84  		super.prepareNewAgent(agentParameters);
85  		connectionDependenciesProvider.set(new ComponentDependencies(ComponentDependencyType.STARTS_WITH).add(agentParameters.getAgentId()));
86  	}
87  	
88  	@Override
89  	protected void configureModules() {
90  		super.configureModules();
91  		addModule(new AbstractModule() {
92  
93  			@Override
94  			public void configure() {
95  				bind(IWorldConnection.class).to(SocketConnection.class);
96  				bind(ComponentDependencies.class).annotatedWith(Names.named(SocketConnection.CONNECTION_DEPENDENCY)).toProvider(connectionDependenciesProvider);
97                  bind(ISocketConnectionAddress.class).annotatedWith(Names.named(SocketConnection.CONNECTION_ADDRESS_DEPENDENCY)).toProvider((Provider<ISocketConnectionAddress>) getAddressProvider());
98  				bind(IWorldMessageParser.class).to(UT2004Parser.class);
99  				bind(ItemTypeTranslator.class).to(UT2004ItemTypeTranslator.class);
100 				bind(IYylex.class).to(IUT2004Yylex.class);
101 				bind(IUT2004Yylex.class).to(Yylex.class);
102 				bind(IYylexObserver.class).to(IYylexObserver.LogObserver.class);
103 				bind(UT2004AgentParameters.class).toProvider(getAgentParamsProvider());
104 			}
105 			
106 		});
107 	}
108 
109 }