View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.utils;
2   
3   import java.util.List;
4   
5   import cz.cuni.amis.pogamut.base.agent.IAgentId;
6   import cz.cuni.amis.pogamut.base.agent.params.IAgentParameters;
7   import cz.cuni.amis.pogamut.base.agent.utils.runner.impl.AgentRunner;
8   import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.SocketConnectionAddress;
9   import cz.cuni.amis.pogamut.base.factory.IAgentFactory;
10  import cz.cuni.amis.pogamut.base.utils.Pogamut;
11  import cz.cuni.amis.pogamut.base.utils.PogamutPlatform;
12  import cz.cuni.amis.pogamut.ut2004.bot.IUT2004Bot;
13  import cz.cuni.amis.pogamut.ut2004.bot.IUT2004BotController;
14  import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
15  import cz.cuni.amis.pogamut.ut2004.bot.params.UT2004BotParameters;
16  import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004BotFactory;
17  import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004BotModule;
18  import cz.cuni.amis.utils.NullCheck;
19  import cz.cuni.amis.utils.exception.PogamutException;
20  
21  /**
22   * Class used for creating, connecting and starting servers with default settings that are taken from the properties.
23   * <p><p>
24   * The address where the instances will connect are defined either in the constructor
25   * or taken from the properties of the {@link PogamutPlatform}.
26   * <p><p>
27   * For more information about the class see {@link AgentRunner}.
28   * 
29   * @author ik
30   * @author Jimmy
31   *
32   * @param <BOT>
33   * @param <PARAMS>
34   */
35  public class UT2004BotRunner<BOT extends IUT2004Bot, PARAMS extends UT2004BotParameters> extends AgentRunner<BOT, PARAMS> {
36  
37  	/**
38  	 * Default host where the instances are going to be connected as defaults, see {@link IAgentParameters#assignDefaults(IAgentParameters)}.
39  	 */
40      protected String host;
41      
42      /**
43  	 * Default port where the instances are going to be connected as defaults, see {@link IAgentParameters#assignDefaults(IAgentParameters)}.
44  	 */
45      protected int port;
46      
47      /**
48  	 * Default name that will serve as a basis for {@link IAgentId}, see {@link IAgentParameters#assignDefaults(IAgentParameters)}.
49  	 */
50  	protected String name;
51  
52  	/** 
53  	 * Construct the runner + specify all defaults.
54  	 * 
55  	 * @param factory to be used for creating new {@link IUT2004Bot} instances
56  	 * @param name default name that serve as a basis for {@link IAgentId}
57  	 * @param host default host where the instances are going to be connected
58  	 * @param port default port where the instances are going to be connected
59  	 */
60  	public UT2004BotRunner(IAgentFactory<BOT, PARAMS> factory, String name, String host, int port) {
61          super(factory);
62          this.name = name;
63          this.port = port;
64          this.host = host;        
65      }
66  
67  	/**
68  	 * Construct the runner + specify the default name, host:port will be taken from the Pogamut platform properties.
69  	 * 
70  	 * @param factory factory to be used for creating new {@link IUT2004Bot} instances
71  	 * @param log used to log stuff
72  	 * @param name default name that serve as a basis for {@link IAgentId}
73  	 */
74      public UT2004BotRunner(IAgentFactory<BOT, PARAMS> factory, String name) {
75          this(
76          	factory, 
77          	name, 
78          	Pogamut.getPlatform().getProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_HOST.getKey()) == null ? 
79          				"localhost" 
80          			:	Pogamut.getPlatform().getProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_HOST.getKey()), 
81          	Pogamut.getPlatform().getIntProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_PORT.getKey()) == 0 ?
82          				3000
83          			:	Pogamut.getPlatform().getIntProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_PORT.getKey())
84          );
85      }
86      
87      /**
88       * Construct the runner without specifying anything as default. Default name for bots will be "UT2004Bot"
89       * and host:port will be taken from the Pogamut platform properties.
90       * 
91       * @param factory factory to be used for creating new {@link IUT2004Bot} instances
92       */
93      public UT2004BotRunner(IAgentFactory<BOT, PARAMS> factory) {
94          this(factory, "UT2004Bot");
95      }
96      
97      /** 
98  	 * Construct the runner + specify all defaults.
99  	 * 
100 	 * @param module Guice module that is going to be used by the {@link UT2004BotFactory}
101 	 * @param name default name that serve as a basis for {@link IAgentId}
102 	 * @param host default host where the instances are going to be connected
103 	 * @param port default port where the instances are going to be connected
104 	 */
105 	public UT2004BotRunner(UT2004BotModule module, String name, String host, int port) {
106         this(new UT2004BotFactory<BOT, PARAMS>(module), name, host, port);
107     }
108 
109 	/**
110 	 * Construct the runner + specify the default name, host:port will be taken from the Pogamut platform properties.
111 	 * 
112 	 * @param module Guice module that is going to be used by the {@link UT2004BotFactory}
113 	 * @param name default name that serve as a basis for {@link IAgentId}
114 	 */
115     public UT2004BotRunner(UT2004BotModule module, String name) {
116         this(
117         	module, 
118         	name, 
119         	Pogamut.getPlatform().getProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_HOST.getKey()), 
120         	Pogamut.getPlatform().getIntProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_PORT.getKey())
121         );
122     }
123     
124     /**
125      * Construct the runner without specifying anything as default. Default name for bots will be "UT2004Bot"
126      * and host:port will be taken from the Pogamut platform properties.
127      * 
128 	 * @param module Guice module that is going to be used by the {@link UT2004BotFactory}
129 	 */
130     public UT2004BotRunner(UT2004BotModule module) {
131         this(module, "UT2004Bot");
132     }
133     
134     /** 
135 	 * Construct the runner + specify all defaults.
136 	 * 
137 	 * @param botControllerClass controller that will be used to instantiate {@link UT2004BotModule}, i.e., it will control the {@link UT2004Bot} instance
138 	 * @param name default name that serve as a basis for {@link IAgentId}
139 	 * @param host default host where the instances are going to be connected
140 	 * @param port default port where the instances are going to be connected
141 	 */
142 	public UT2004BotRunner(Class<? extends IUT2004BotController> botControllerClass, String name, String host, int port) {
143         this(new UT2004BotModule(botControllerClass), name, host, port);
144     }
145 
146 	/**
147 	 * Construct the runner + specify the default name, host:port will be taken from the Pogamut platform properties.
148 	 * 
149 	 * @param botControllerClass controller that will be used to instantiate {@link UT2004BotModule}, i.e., it will control the {@link UT2004Bot} instance
150 	 * @param name default name that serve as a basis for {@link IAgentId}
151 	 */
152     public UT2004BotRunner(Class<? extends IUT2004BotController> botControllerClass, String name) {
153         this(
154         	new UT2004BotModule(botControllerClass), 
155         	name, 
156         	Pogamut.getPlatform().getProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_HOST.getKey()), 
157         	Pogamut.getPlatform().getIntProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_PORT.getKey())
158         );
159     }
160     
161     /**
162      * Construct the runner without specifying anything as default. Default name for bots will be "UT2004Bot"
163      * and host:port will be taken from the Pogamut platform properties.
164      * 
165 	 * @param botControllerClass controller that will be used to instantiate {@link UT2004BotModule}, i.e., it will control the {@link UT2004Bot} instance
166 	 */
167     public UT2004BotRunner(Class<? extends IUT2004BotController> botControllerClass) {
168         this(new UT2004BotModule(botControllerClass), "UT2004Bot");
169     }
170     
171     @Override
172     public BOT startAgent() throws PogamutException {
173     	return super.startAgent();
174     }
175     
176     @Override
177     public List<BOT> startAgents(int count) throws PogamutException {
178     	return super.startAgents(count);
179     }
180     
181     @Override
182     public List<BOT> startAgents(PARAMS... agentParameters) throws PogamutException {
183     	return super.startAgents(agentParameters);
184     };
185     
186     /**
187      * Returns name that is going to be used to form new {@link IAgentId} of the bots.
188      *     
189      * @return name used for the newly started bots
190      */
191     public String getName() {
192 		return name;
193 	}
194 
195     /**
196      * Sets name that is going to be used to form new {@link IAgentId} of the bots.
197      * <p><p>
198      * If null is passed, generic "UT2004Bot" will be set.
199      *     
200      * @param name name used for the newly started bots
201      * @return this instance
202      */
203 	public UT2004BotRunner<BOT, PARAMS> setName(String name) {
204 		if (name == null) name = "UT2004Bot";
205 		this.name = name;
206 		return this;
207 	}
208 
209 	/**
210      * Returns host, where newly launched bots will be connected to.
211      * 
212      * @return host running GB2004 server
213      */
214     public String getHost() {
215 		return host;
216 	}
217 
218     /**
219      * Sets host, where newly launched bots will be connected to.
220      * 
221      * @param host host running GB2004 server (can't be null)
222      * @return this instance
223      */
224 	public UT2004BotRunner<BOT, PARAMS> setHost(String host) {
225 		this.host = host;
226 		NullCheck.check(this.host, "host");
227 		return this;
228 	}
229 
230 	/**
231      * Returns port, where newly launched bots will be connected to.
232      * 
233      * @return port at the host where GB2004 server is listening for bot connections
234      */
235 	public int getPort() {
236 		return port;
237 	}
238 
239 	/**
240      * Sets port, where newly launched bots will be connected to.
241      * 
242      * @param port at the host where GB2004 server is listening for bot connections
243      * @return this instance
244      */
245 	public UT2004BotRunner<BOT, PARAMS> setPort(int port) {
246 		this.port = port;
247 		return this;
248 	}
249 
250     /**
251      * Provides default parameters that is, {@link IAgentId} using {@link UT2004BotRunner#name} and {@link SocketConnectionAddress}
252      * using {@link UT2004BotRunner#host} and {@link UT2004BotRunner#port}.
253      */
254 	@Override
255 	protected IAgentParameters newDefaultAgentParameters() {
256 		return new UT2004BotParameters().setAgentId(newAgentId(name)).setWorldAddress(new SocketConnectionAddress(host, port));
257 	}
258 	
259 	@Override
260     public UT2004BotRunner<BOT, PARAMS> setMain(boolean state) {
261     	super.setMain(state);
262     	return this;
263     }
264 	
265 	@Override
266 	public UT2004BotRunner<BOT, PARAMS> setConsoleLogging(boolean enabled) {
267 		super.setConsoleLogging(enabled);
268 		return this;
269 	}
270 	
271 }