View Javadoc

1   package cz.cuni.amis.pogamut.udk.utils;
2   
3   import java.util.List;
4   import java.util.logging.Level;
5   
6   import cz.cuni.amis.pogamut.base.agent.IAgentId;
7   import cz.cuni.amis.pogamut.base.agent.impl.AgentId;
8   import cz.cuni.amis.pogamut.base.agent.params.IAgentParameters;
9   import cz.cuni.amis.pogamut.base.agent.params.impl.RemoteAgentParameters;
10  import cz.cuni.amis.pogamut.base.agent.utils.runner.impl.AgentRunner;
11  import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.SocketConnectionAddress;
12  import cz.cuni.amis.pogamut.base.factory.IAgentFactory;
13  import cz.cuni.amis.pogamut.base.utils.Pogamut;
14  import cz.cuni.amis.pogamut.base.utils.PogamutPlatform;
15  import cz.cuni.amis.pogamut.base.utils.PogamutProperty;
16  import cz.cuni.amis.pogamut.udk.agent.params.UDKAgentParameters;
17  import cz.cuni.amis.pogamut.udk.server.IUDKServer;
18  import cz.cuni.amis.utils.exception.PogamutException;
19  
20  /**
21   * Class used for creating, connecting and starting servers with default settings that are taken from the properties.
22   * <p><p>
23   * The address where the instances will connect are defined either in the constructor
24   * or taken from the properties of the {@link PogamutPlatform}.
25   * <p><p>
26   * For more information about the class see {@link AgentRunner}.
27   * 
28   * @author ik
29   * @author Jimmy
30   */
31  public class UDKServerRunner<SERVER extends IUDKServer, PARAMS extends UDKAgentParameters> extends AgentRunner<SERVER, PARAMS> {
32  
33  	/**
34  	 * Default host where the instances are going to be connected as defaults, see {@link IAgentParameters#assignDefaults(IAgentParameters)}.
35  	 */
36      protected String host;
37      
38      /**
39  	 * Default port where the instances are going to be connected as defaults, see {@link IAgentParameters#assignDefaults(IAgentParameters)}.
40  	 */
41      protected int port;
42      
43      /**
44  	 * Default name that will serve as a basis for {@link IAgentId}, see {@link IAgentParameters#assignDefaults(IAgentParameters)}.
45  	 */
46  	protected String name;
47  
48  	/** 
49  	 * Construct the runner + specify all defaults.
50  	 * 
51  	 * @param factory to be used for creating new {@link IUDKServer} instances
52  	 * @param name default name that serve as a basis for {@link IAgentId}
53  	 * @param host default host where the instances are going to be connected
54  	 * @param port default port where the instances are going to be connected
55  	 */
56  	public UDKServerRunner(IAgentFactory<SERVER, PARAMS> factory, String name, String host, int port) {
57          super(factory);
58          this.name = name;
59          this.port = port;
60          this.host = host;
61      }
62  
63  	/**
64  	 * Construct the runner + specify the default name, host:port will be taken from the Pogamut platform properties.
65  	 * 
66  	 * @param factory factory to be used for creating new {@link IUDKServer} instances
67  	 * @param name default name that serve as a basis for {@link IAgentId}
68  	 */
69      public UDKServerRunner(IAgentFactory<SERVER, PARAMS> factory, String name) {
70          this(
71          	factory, 
72          	name, 
73          	Pogamut.getPlatform().getProperty(PogamutUDKProperty.POGAMUT_UDK_SERVER_HOST.getKey()), 
74          	Pogamut.getPlatform().getIntProperty(PogamutUDKProperty.POGAMUT_UDK_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 IUDKServer} instances
98       */
99      public UDKServerRunner(IAgentFactory<SERVER, PARAMS> factory) {
100         this(factory, "UDKServer");
101     }
102 
103     /**
104      * Provides default parameters that is, {@link IAgentId} using {@link UDKServerRunner#name} and {@link SocketConnectionAddress}
105      * using {@link UDKServerRunner#host} and {@link UDKServerRunner#port}.
106      */
107 	@Override
108 	protected IAgentParameters newDefaultAgentParameters() {
109 		return new UDKAgentParameters().setAgentId(new AgentId(name)).setWorldAddress(new SocketConnectionAddress(host, port));
110 	}
111 	
112 }