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