View Javadoc

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