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.agent.params.UT2004AgentParameters;
13  import cz.cuni.amis.pogamut.ut2004.server.IUT2004Server;
14  import cz.cuni.amis.utils.exception.PogamutException;
15  
16  /**
17   * Class used for creating, connecting and starting servers with default settings that are taken from the properties.
18   * <p><p>
19   * The address where the instances will connect are defined either in the constructor
20   * or taken from the properties of the {@link PogamutPlatform}.
21   * <p><p>
22   * For more information about the class see {@link AgentRunner}.
23   * 
24   * @author ik
25   * @author Jimmy
26   */
27  public class UT2004ServerRunner<SERVER extends IUT2004Server, PARAMS extends UT2004AgentParameters> extends AgentRunner<SERVER, PARAMS> {
28  
29  	/**
30  	 * Default host where the instances are going to be connected as defaults, see {@link IAgentParameters#assignDefaults(IAgentParameters)}.
31  	 */
32      protected String host;
33      
34      /**
35  	 * Default port where the instances are going to be connected as defaults, see {@link IAgentParameters#assignDefaults(IAgentParameters)}.
36  	 */
37      protected int port;
38      
39      /**
40  	 * Default name that will serve as a basis for {@link IAgentId}, see {@link IAgentParameters#assignDefaults(IAgentParameters)}.
41  	 */
42  	protected String name;
43  
44  	/** 
45  	 * Construct the runner + specify all defaults.
46  	 * 
47  	 * @param factory to be used for creating new {@link IUT2004Server} instances
48  	 * @param name default name that serve as a basis for {@link IAgentId}
49  	 * @param host default host where the instances are going to be connected
50  	 * @param port default port where the instances are going to be connected
51  	 */
52  	public UT2004ServerRunner(IAgentFactory<SERVER, PARAMS> factory, String name, String host, int port) {
53          super(factory);
54          this.name = name;
55          this.port = port;
56          this.host = host;
57      }
58  
59  	/**
60  	 * Construct the runner + specify the default name, host:port will be taken from the Pogamut platform properties.
61  	 * 
62  	 * @param factory factory to be used for creating new {@link IUT2004Server} instances
63  	 * @param name default name that serve as a basis for {@link IAgentId}
64  	 */
65      public UT2004ServerRunner(IAgentFactory<SERVER, PARAMS> factory, String name) {
66          this(
67          	factory, 
68          	name, 
69          	Pogamut.getPlatform().getProperty(PogamutUT2004Property.POGAMUT_UT2004_SERVER_HOST.getKey()) == null ? 
70      				"localhost" 
71      			:	Pogamut.getPlatform().getProperty(PogamutUT2004Property.POGAMUT_UT2004_SERVER_HOST.getKey()), 
72      		Pogamut.getPlatform().getIntProperty(PogamutUT2004Property.POGAMUT_UT2004_SERVER_PORT.getKey()) == 0 ?
73      				3001
74      			:	Pogamut.getPlatform().getIntProperty(PogamutUT2004Property.POGAMUT_UT2004_SERVER_PORT.getKey())
75          );
76      }
77      
78      @Override
79      public SERVER startAgent() throws PogamutException {
80      	return super.startAgent();
81      }
82      
83      @Override
84      public List<SERVER> startAgents(int count) throws PogamutException {
85      	return super.startAgents(count);
86      }
87      
88      @Override
89      public List<SERVER> startAgents(PARAMS... agentParameters) throws PogamutException {
90      	return super.startAgents(agentParameters);
91      };
92      
93      /**
94       * Construct the runner without specifying anything as default. Default name for server agents will be "UTServer Factory"
95       * and host:port will be taken from the Pogamut platform properties.
96       * 
97       * @param factory factory to be used for creating new {@link IUT2004Server} instances
98       */
99      public UT2004ServerRunner(IAgentFactory<SERVER, PARAMS> factory) {
100         this(factory, "UT2004Server");
101     }
102 
103     /**
104      * Provides default parameters that is, {@link IAgentId} using {@link UT2004ServerRunner#name} and {@link SocketConnectionAddress}
105      * using {@link UT2004ServerRunner#host} and {@link UT2004ServerRunner#port}.
106      */
107 	@Override
108 	protected IAgentParameters newDefaultAgentParameters() {
109 		return new UT2004AgentParameters().setAgentId(newAgentId(name)).setWorldAddress(new SocketConnectionAddress(host, port));
110 	}
111 	
112 }