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.utils.runner.IAgentDescriptor;
10 import cz.cuni.amis.pogamut.base.agent.utils.runner.impl.MultipleAgentRunner;
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.PogamutProperty;
15 import cz.cuni.amis.pogamut.udk.agent.params.UDKAgentParameters;
16 import cz.cuni.amis.pogamut.udk.bot.IUDKBot;
17 import cz.cuni.amis.pogamut.udk.bot.impl.UDKBot;
18 import cz.cuni.amis.pogamut.udk.factory.guice.remoteagent.UDKBotFactory;
19 import cz.cuni.amis.pogamut.udk.factory.guice.remoteagent.UDKBotModule;
20 import cz.cuni.amis.utils.NullCheck;
21 import cz.cuni.amis.utils.exception.PogamutException;
22
23 /**
24 * This class has only one purpose - execute ONE OR MORE BOTS inside 'main' method. You can't use it for anything else!
25 * It is THE SHORTCUT of all SHORTCUTS to execute multiple bots, wait till they finishe and close the whole Pogamut.
26 * <p><p>
27 * Designed especially for the usage inside NetBeans projects.
28 * <p><p>
29 * NOTE: by default, all bots get paused after they start and they are resumed after all bots are present in UDK. To
30 * change this behaviour pass 'false' through {@link MultipleUDKBotRunner#setPausing(boolean)}.
31 * <p><p>
32 * NOTE: It's not even meant to be instantiated twice for two different batch of bots and consequently executed in two different threads!
33 * Single-purpose class only ;-)
34 * <p><p>
35 * NOTE: It might be very interesting for you to check out the source of method {@link MultipleUDKBotRunner#startAgent()} to
36 * see how the agent should be instantiated via {@link UDKBotFactory} using {@link UDKBotModule}.
37 * <p><p>
38 *
39 *
40 * @author Jimmy
41 */
42 public class MultipleUDKBotRunner<BOT extends UDKBot, PARAMS extends UDKAgentParameters, MODULE extends UDKBotModule> extends MultipleAgentRunner<BOT, PARAMS, MODULE> {
43
44 /**
45 * Default host where the instances are going to be connected as defaults, see {@link IAgentParameters#assignDefaults(IAgentParameters)}.
46 */
47 protected String host;
48
49 /**
50 * Default port where the instances are going to be connected as defaults, see {@link IAgentParameters#assignDefaults(IAgentParameters)}.
51 */
52 protected int port;
53
54 /**
55 * Default name that will serve as a basis for {@link IAgentId}, see {@link IAgentParameters#assignDefaults(IAgentParameters)}.
56 */
57 protected String name;
58
59 /**
60 * Construct the runner + specify all defaults.
61 *
62 * @param name default name that serve as a basis for {@link IAgentId}
63 * @param host default host where the instances are going to be connected
64 * @param port default port where the instances are going to be connected
65 */
66 public MultipleUDKBotRunner(String name, String host, int port) {
67 this.name = name;
68 this.port = port;
69 this.host = host;
70 }
71
72 /**
73 * Construct the runner + specify the default name, host:port will be taken from the Pogamut platform properties.
74 *
75 * @param factory factory to be used for creating new {@link IUDKBot} instances
76 * @param name default name that serve as a basis for {@link IAgentId}
77 */
78 public MultipleUDKBotRunner(String name) {
79 this(
80 name,
81 Pogamut.getPlatform().getProperty(PogamutUDKProperty.POGAMUT_UDK_BOT_HOST.getKey()),
82 Pogamut.getPlatform().getIntProperty(PogamutUDKProperty.POGAMUT_UDK_BOT_PORT.getKey())
83 );
84 }
85
86 /**
87 * Returns name that is going to be used to form new {@link IAgentId} of the bots.
88 *
89 * @return name used for the newly started bots
90 */
91 public String getName() {
92 return name;
93 }
94
95 /**
96 * Sets name that is going to be used to form new {@link IAgentId} of the bots.
97 * <p><p>
98 * If null is passed, generic "UDKBot" will be set.
99 *
100 * @param name name used for the newly started bots
101 * @return this instance
102 */
103 public MultipleUDKBotRunner<BOT, PARAMS, MODULE> setName(String name) {
104 if (name == null) name = "UDKBot";
105 this.name = name;
106 return this;
107 }
108
109 /**
110 * Returns host, where newly launched bots will be connected to.
111 *
112 * @return host running GBUDK server
113 */
114 public String getHost() {
115 return host;
116 }
117
118 /**
119 * Sets host, where newly launched bots will be connected to.
120 *
121 * @param host host running GBUDK server (can't be null)
122 * @return this instance
123 */
124 public MultipleUDKBotRunner<BOT, PARAMS, MODULE> setHost(String host) {
125 this.host = host;
126 NullCheck.check(this.host, "host");
127 return this;
128 }
129
130 /**
131 * Returns port, where newly launched bots will be connected to.
132 *
133 * @return port at the host where GBUDK server is listening for bot connections
134 */
135 public int getPort() {
136 return port;
137 }
138
139 /**
140 * Sets port, where newly launched bots will be connected to.
141 *
142 * @param port at the host where GBUDK server is listening for bot connections
143 * @return this instance
144 */
145 public MultipleUDKBotRunner<BOT, PARAMS, MODULE> setPort(int port) {
146 this.port = port;
147 return this;
148 }
149
150 /**
151 * We're setting the logging level to {@link Level#WARNING} here so the bot won't log much.
152 */
153 @Override
154 protected void preStartHook(BOT agent) throws PogamutException {
155 agent.getLogger().setLevel(Level.WARNING);
156 }
157
158 /**
159 * Provides default parameters that is, {@link IAgentId} using {@link MultipleUDKBotRunner#name} and {@link SocketConnectionAddress}
160 * using {@link MultipleUDKBotRunner#host} and {@link MultipleUDKBotRunner#port}.
161 */
162 @Override
163 protected IAgentParameters newDefaultAgentParameters() {
164 return new UDKAgentParameters().setAgentId(new AgentId(name)).setWorldAddress(new SocketConnectionAddress(host, port));
165 }
166
167 /**
168 * Uses {@link UDKBotFactory} for agent construction.
169 */
170 @Override
171 protected IAgentFactory newAgentFactory(MODULE agentModule) {
172 return new UDKBotFactory<IUDKBot, UDKAgentParameters>(agentModule);
173 }
174
175 public List<BOT> startAgents(IAgentDescriptor<PARAMS,MODULE>... agentDescriptors) {
176 return super.startAgents(agentDescriptors);
177 };
178
179 }