-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMasterServerImpl.java
More file actions
214 lines (205 loc) · 9.65 KB
/
MasterServerImpl.java
File metadata and controls
214 lines (205 loc) · 9.65 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import java.util.*;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
public class MasterServerImpl implements UtilsServer {
List<UtilsClass> nodeList;
List<String> nodeNames;
Registry registry;
List<String> CassandraNodes = new ArrayList<>();
int counter;
boolean flag;
public MasterServerImpl() {
System.out.printf("Succes in master");
try{
registry = LocateRegistry.getRegistry(null);
// Looking up the registry for the remote object
UtilsClass stub1 = (UtilsClass) registry.lookup("Dream11-node1");
UtilsClass stub2 = (UtilsClass) registry.lookup("Dream11-node2");
UtilsClass stub3 = (UtilsClass) registry.lookup("Dream11-node3");
List<UtilsClass> list = new ArrayList<>();
List<String> names = new ArrayList<>();
list.add(stub1);
names.add("Dream11-node1");
list.add(stub2);
names.add("Dream11-node2");
list.add(stub3);
names.add("Dream11-node3");
nodeList = list;
nodeNames = names;
for(int i=1; i<6; i++) {
CassandraNodes.add("127.0.0."+ i);
}
counter = 0;
}
catch (Exception e) {
System.err.println("Master exception: " + e.toString());
e.printStackTrace();
}
}
public void AddNode(String nodeName) {
int index = nodeNames.indexOf(nodeName);
if (index >= 0) {
System.out.println("Node already exists!");
}
else {
try {
nodeNames.add(nodeName);
UtilsClass stub = (UtilsClass) registry.lookup(nodeName);
nodeList.add(stub);
System.out.println("Node " + nodeName + " added!");
} catch (Exception e) {
System.err.println("Error occured while adding: " + e.toString());
e.printStackTrace();
}
}
}
public void DeleteNode(String nodeName) {
int index = nodeNames.indexOf(nodeName);
try {
if(index >= 0) {
nodeNames.remove(index);
nodeList.remove(index);
}
System.out.println("Node " + nodeName + " removed!");
} catch (Exception e) {
System.err.println("Error occured while deleting: " + e.toString());
e.printStackTrace();
}
}
public String Query(String query) {
try{
System.out.println(query);
String[] splited = query.split(" ");
UtilsClass stub = nodeList.get(0); // here stub will depend on the user id, for now just using the first node
if (splited[0].equals("add_match")) {
try{
String playerNames = splited[2];
for (int i = 3; i < splited.length; i++) {
playerNames = playerNames + " " + splited[i];
}
flag = (counter == 0)? true : false;
int matchID = nodeList.get(0).AddMatch(Integer.parseInt(splited[1]), playerNames, false, CassandraNodes.get(0));
System.out.printf("Match ID %d\n", matchID);
for(int i=1; i<nodeList.size(); i++) {
flag = (counter == i) ? true : false;
System.out.printf("Match ID %d\n", nodeList.get(i).AddMatch(Integer.parseInt(splited[1]), playerNames, flag, CassandraNodes.get(i%CassandraNodes.size())));
}
counter = (counter + 1)%nodeList.size();
return "Match ID " + matchID + " created!";
} catch (Exception ee) {
System.err.println("Client exception: " + ee.toString());
ee.printStackTrace();
return "Invalid syntax: Try: add_match <num players per team> <all player names>";
}
}
else if (splited[0].equals("update_player_score")) {
try {
flag = (counter == 0)? true : false;
for(int i=1; i<nodeList.size(); i++){
flag = (counter == i)? true : false;
nodeList.get(i).UpdatePlayerScore(Integer.parseInt(splited[1]), Integer.parseInt(splited[2]), Integer.parseInt(splited[3]), flag);
}
counter = (counter + 1)%nodeList.size();
return "Player " + splited[2] + " score increased by " + splited[3];
} catch (Exception ee) {
System.err.println("Client exception: " + ee.toString());
ee.printStackTrace();
return "Invalid syntax: Try: update_player_score <match id> <player id> <score>";
}
}
else if (splited[0].equals("create_team")) {
try{
int[] playerIds = new int[splited.length - 2];
for (int i = 0; i < splited.length - 2; i++) {
playerIds[i] = Integer.parseInt(splited[i + 2]);
}
flag = (counter == 0)? true : false;
int userid = nodeList.get(0).UserCreateTeam(Integer.parseInt(splited[1]), playerIds, flag);
// System.out.printf("Your generated userId is %d\n", userid);
for(int i=1; i<nodeList.size(); i++) {
flag = (counter == i)? true : false;
System.out.printf("Your generated userId is %d\n", nodeList.get(i).UserCreateTeam(Integer.parseInt(splited[1]), playerIds, flag));
}
return "Team created! Your userID is " + userid;
} catch (Exception ee) {
System.err.println("Client exception: " + ee.toString());
ee.printStackTrace();
return "Invalid syntax: Try: create_team <match id> <player ids>";
}
}
else if (splited[0].equals("get_score")) {
try{
int userId = Integer.parseInt(splited[2]);
stub = nodeList.get(userId%nodeList.size());
int score = stub.GetScore(Integer.parseInt(splited[1]), userId);
// System.out.printf("Your score with userId %d is %d\n", userId, score);
return "Your score with userId " + userId + " is " + score;
} catch (Exception ee) {
System.err.println("Client exception: " + ee.toString());
ee.printStackTrace();
return "Invalid syntax: Try: get_score <match id> <user id>";
}
}
else if (splited[0].equals("get_rank")) {
try{
int userId = Integer.parseInt(splited[2]);
stub = nodeList.get(userId%nodeList.size());
int rank = stub.GetRank(Integer.parseInt(splited[1]), userId);
// System.out.printf("Your rank with userId %d is %d\n", userId, rank);
return "Your rank with userId " + userId + " is " + rank;
} catch (Exception ee) {
System.err.println("Client exception: " + ee.toString());
ee.printStackTrace();
return "Invalid syntax: Try: get_rank <match id> <user id>";
}
}
else if (splited[0].equals("get_leaderboard")) {
try {
return stub.GetLeaderBoard(Integer.parseInt(splited[1]));
} catch (Exception ee) {
System.err.println("Client exception: " + ee.toString());
ee.printStackTrace();
return "Invalid syntax: Try: get_leaderboard <match id>";
}
}
else if (splited[0].equals("start_match")) {
try{
for(UtilsClass node:nodeList)
node.startMatch(Integer.parseInt(splited[1]));
return "Match " + splited[1] + " started";
} catch (Exception ee) {
System.err.println("Client exception: " + ee.toString());
ee.printStackTrace();
return "Invalid syntax: Try: start_match <match id>";
}
}
else if (splited[0].equals("show_players")) {
try{
return stub.ShowPlayers(Integer.parseInt(splited[1]));
} catch (Exception ee) {
System.err.println("Client exception: " + ee.toString());
ee.printStackTrace();
return "Invalid syntax: Try: show_players <match id>";
}
}
else if (splited[0].equals("exit")) {
try{
nodeList.get(0).DeleteMatch(Integer.parseInt(splited[1]));
return "Match Ended";
} catch (Exception ee) {
System.err.println("Client exception: " + ee.toString());
ee.printStackTrace();
return "Invalid syntax: Try: exit <match id>";
}
}
else {
return "Please provide a valid input";
}
}
catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
return "A server error occured! Please Try again";
}
}
}