View Javadoc

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