forked from yuemingl/SymJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloudConfig.java
More file actions
141 lines (124 loc) · 3.31 KB
/
CloudConfig.java
File metadata and controls
141 lines (124 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package lambdacloud.core;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import lambdacloud.net.CloudClient;
/**
* An instance of CloudConfig contains the information of available servers.
* A global instance of CloudConfig (call getGlobalConfg()) is used in the
* computation if no CloudConfig is provided. The default value of the global
* configure of servers is simulated by multi-thread locally.
*
*/
public class CloudConfig {
protected String configFile;
protected static CloudConfig globalConfig = new CloudConfig();
protected List<CloudClient> clients = new ArrayList<CloudClient>();
protected CloudClient currentClient;
/**
* Construct a CloudConfig object which allows all the computations
* run on local machine and the servers are simulated by multi-thread
*/
public CloudConfig() {
}
/**
* Construct a instance from a configuration file
*
* @param configFile
*/
public CloudConfig(String configFile) {
if(configFile == null) {
System.out.println("Using local config.");
return;
}
this.configFile = configFile;
try {
Path path = Paths.get(System.getProperty("user.dir")+"/conf/"+configFile);
System.out.println("Using config file: "+path);
List<String> hosts = Files.readAllLines(path, Charset.forName("UTF-8"));
for(String host : hosts) {
if(host.trim().length() == 0)
continue;
String[] arr = host.split(":");
if(arr.length == 2) {
CloudClient c = new CloudClient(arr[0], Integer.valueOf(arr[1]));
c.connect();
System.out.println(host);
clients.add(c);
if(currentClient == null) {
currentClient = c;
}
} else {
System.out.println("Can not parse host: "+host);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Set the global configuration by providing a configuration file
*
* @param configFile
*/
public static CloudConfig setGlobalConfig(String configFile) {
globalConfig = new CloudConfig(configFile);
return globalConfig;
}
/**
* Set config as the global configuration
* @param config
* @return
*/
public static CloudConfig setGlobalConfig(CloudConfig config) {
globalConfig = config;
return globalConfig;
}
public static CloudConfig getGlobalConfig() {
if(globalConfig == null) {
throw new RuntimeException("Global CloudConfig is not specified!");
}
return globalConfig;
}
public boolean isLocalConfig() {
return null == configFile;
}
public CloudClient getClientByIndex(int index) {
if(this.isLocalConfig())
return null;
return clients.get(index);
}
public int getNumClients() {
if(this.isLocalConfig())
return 1;
return clients.size();
}
public CloudClient getCurrentClient() {
return currentClient;
}
public void setCurrentClient(CloudClient client) {
if(this.isLocalConfig())
return;
this.currentClient = client;
}
public void setCurrentClient(int index) {
if(this.currentClient == null)
return;
this.currentClient = clients.get(index);
}
public void reconnectAll() {
for(CloudClient client : clients)
try {
client.connect();
} catch (Exception e) {
e.printStackTrace();
}
}
public void disconnectAll() {
for(CloudClient client : clients)
client.shutDown();
}
}