View Javadoc

1   package nl.tudelft.goal.unreal.messages;
2   
3   import java.net.URI;
4   import java.net.URISyntaxException;
5   import java.util.ArrayList;
6   import java.util.List;
7   import java.util.logging.Level;
8   
9   
10  /**
11   * Describes the configuration for the environment. This configuration is
12   * provided through the init command.
13   * 
14   * @author M.P. Korstanje
15   * 
16   */
17  
18  public class Configuration {
19  
20  	public static final Level DEFAULT_LOG_LEVEL = Level.WARNING;
21  	public static final String DEFAULT_BOT_NAME = "UnrealGOALBot";
22  	public static final String LOCAL_HOST = "127.0.0.1";
23  	public static final int CONTROL_SERVER_PORT = 3001;
24  	public static final int BOT_SERVER_PORT = 3000;
25  	
26  	public static final String CONTROL_SERVER = LOCAL_HOST + ":" + CONTROL_SERVER_PORT;
27  	public static final String BOT_SERVER = LOCAL_HOST + ":" + BOT_SERVER_PORT;
28  
29  	private static final URI VISUALIZER_SERVER = null;
30  	// Environment parameters
31  	private List<BotParameters> bots;
32  	private URI visualizer;
33  	private URI botServer;
34  	private URI controlServer;
35  
36  	private Level logLevel;
37  
38  	public List<BotParameters> getBots() {
39  		return bots == null ? new ArrayList<BotParameters>() : bots;
40  	}
41  
42  	public void setBots(List<BotParameters> bots) {
43  		this.bots = bots;
44  	}
45  
46  	public URI getVisualizer() {
47  		return visualizer;
48  	}
49  
50  	public void setVisualizer(URI visualizer) {
51  		this.visualizer = visualizer;
52  	}
53  
54  	public URI getBotServer() {
55  		return botServer;
56  	}
57  
58  	public void setBotServer(URI botServer) {
59  		this.botServer = botServer;
60  	}
61  
62  	public URI getControlServer() {
63  		return controlServer;
64  	}
65  
66  	public void setControlServer(URI controlServer) {
67  		this.controlServer = controlServer;
68  	}
69  
70  	public Configuration setLogLevel(Level level) {
71  		assert level != null;
72  		this.logLevel = level;
73  		return this;
74  	}
75  
76  	public Level getLogLevel() {
77  		return logLevel;
78  	}
79  
80  	public String getDefaultBotName() {
81  		return DEFAULT_BOT_NAME;
82  	}
83  
84  	/**
85  	 * 
86  	 * @return The port bot server, or -1 if the port is undefined
87  	 */
88  	public int getBotServerPort() {
89  		return botServer == null ? -1 : botServer.getPort();
90  	}
91  
92  	/**
93  	 * 
94  	 * @return The host of the bot server, or null if the server is undefined
95  	 */
96  	public String getBotServerHost() {
97  		return botServer == null ? null : botServer.getHost();
98  	}
99  
100 	public static Configuration getDefaults() {
101 
102 		Configuration configuration = new Configuration();
103 
104 		configuration.setBots(new ArrayList<BotParameters>());
105 		configuration.setVisualizer(VISUALIZER_SERVER);
106 
107 		try {
108 			configuration.setBotServer(new URI(BOT_SERVER));
109 			configuration.setControlServer(new URI(CONTROL_SERVER));
110 		} catch (URISyntaxException e) {
111 			// Ignore. Syntax is correct
112 		}
113 
114 		configuration.setLogLevel(DEFAULT_LOG_LEVEL);
115 
116 		return configuration;
117 	}
118 
119 	public void assignDefaults(Configuration configuration) {
120 
121 		if (bots == null)
122 			bots = configuration.bots;
123 		if (visualizer == null)
124 			visualizer = configuration.visualizer;
125 		if (botServer == null)
126 			botServer = configuration.botServer;
127 		if (controlServer == null)
128 			controlServer = configuration.controlServer;
129 		if (logLevel == null)
130 			logLevel = configuration.logLevel;
131 
132 	}
133 
134 	public int getControlServerPort() {
135 		return controlServer == null ? -1 : controlServer.getPort();
136 	}
137 
138 	public String getControlServerHost() {
139 		return controlServer == null ? null : controlServer.getHost();
140 	}
141 }