View Javadoc

1   package cz.cuni.amis.pogamut.base.factory.guice;
2   
3   import com.google.inject.AbstractModule;
4   
5   import cz.cuni.amis.pogamut.base.agent.IAgent;
6   import cz.cuni.amis.pogamut.base.agent.params.IRemoteAgentParameters;
7   import cz.cuni.amis.pogamut.base.communication.command.IAct;
8   import cz.cuni.amis.pogamut.base.communication.command.ICommandSerializer;
9   import cz.cuni.amis.pogamut.base.communication.command.impl.Act;
10  import cz.cuni.amis.pogamut.base.communication.command.impl.StringCommandSerializer;
11  import cz.cuni.amis.pogamut.base.communication.connection.IWorldConnection;
12  import cz.cuni.amis.pogamut.base.communication.connection.IWorldConnectionAddress;
13  import cz.cuni.amis.pogamut.base.communication.connection.IWorldReaderProvider;
14  import cz.cuni.amis.pogamut.base.communication.connection.IWorldWriterProvider;
15  import cz.cuni.amis.pogamut.base.communication.messages.InfoMessage;
16  import cz.cuni.amis.pogamut.base.communication.parser.impl.yylex.IYylex;
17  import cz.cuni.amis.pogamut.base.communication.translator.IWorldMessageTranslator;
18  import cz.cuni.amis.pogamut.base.communication.translator.event.IWorldChangeEvent;
19  import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
20  import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEvent;
21  import cz.cuni.amis.pogamut.base.utils.guice.AdaptableProvider;
22  import cz.cuni.amis.utils.NullCheck;
23  
24  /**
25   * Module extending {@link GuiceAgentModule} for the purpose of remote agents (those communicating with the world using 
26   * {@link IWorldConnection}).
27   * <p><p>
28   * Introducing {@link GuiceRemoteAgentModule#getAddressProvider()} that allows you to specify {@link IWorldConnectionAddress} 
29   * at runtime.
30   * <p><p>
31   * To have <b>successful module</b> the descendant <b>must specify</b> these <b>missing bindings</b>:
32   * <table>
33   * <tr><th>Mapped class</th>                    <th>Description</th></tr>
34   * 
35   * <tr><td>{@link IAgent}</td>                  <td>Agent that should be instantiated</td></tr>
36   * <tr><td>{@link IWorldConnection}</td>        <td>Connection to the agent's world.</td></tr>
37   * <tr><td>{@link IYylex}</td>                  <td>World message parser implementation.</td></tr>
38   * <tr><td>{@link IWorldMessageTranslator}</td> <td>World-dependent implementation of {@link InfoMessage}s translator into {@link IWorldChangeEvent} that can be consumed by {@link IWorldView}.</td></tr>
39   * <tr><td>{@link IWorldView</td>               <td>World view processing {@link IWorldChangeEvent}s into {@link IWorldEvent}s that should be consumed by {@link IAgent} implementation.</td></tr>
40   * </table>
41   * ... plus all newly introduced dependencies (by various implementors of mentioned interfaces).<p>
42   * ... <b>don't forget to call super.configure()</b> in the subclasses ;-)
43   * 
44   * <b>NOTE></b> that the module is defining bindings for {@link IWorldReaderProvider} and {@link IWorldWriterProvider} which
45   * might not be suitable for {@link INativeAgentFactory}s - fear not as those bindings might be rebind thanks to Guice v2. 
46   * 
47   * @see GuiceAgentModule
48   * @author Jimmy
49   */
50  public abstract class GuiceRemoteAgentModule<PARAMS extends IRemoteAgentParameters> extends GuiceCommunicationModule<PARAMS> {
51  	
52  	private AdaptableProvider<IWorldConnectionAddress> addressProvider = new AdaptableProvider<IWorldConnectionAddress>(null);
53  	
54  	protected AdaptableProvider getAddressProvider() { // return value can't be strongly typed - NetBeans freaks out in a certain type of use
55  		return addressProvider;
56  	}
57  	
58  	public void prepareNewAgent(PARAMS agentParameters) {
59  		super.prepareNewAgent(agentParameters);
60  		NullCheck.check(agentParameters.getWorldAddress(), "agentParameters.getWorldAddress()");
61  		addressProvider.set(agentParameters.getWorldAddress());
62  	};
63  	
64  	@Override
65  	protected void configureModules() {
66  		super.configureModules();
67  		
68  		addModule(
69  				new AbstractModule() {
70  
71  					@Override
72  					protected void configure() {
73  						bind(ICommandSerializer.class).to(StringCommandSerializer.class);
74  						bind(IAct.class).to(Act.class);
75  						bind(IWorldReaderProvider.class).to(IWorldConnection.class);
76  						bind(IWorldWriterProvider.class).to(IWorldConnection.class);
77  					}				
78  					
79  				}
80  			);		
81  	}
82  
83  }