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