-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewer.java
More file actions
218 lines (207 loc) · 6.72 KB
/
viewer.java
File metadata and controls
218 lines (207 loc) · 6.72 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
215
216
217
218
/*
*
* Author : Abhash Jain (ajain28) - CSC591 - HW1
* viewer.java file for wather
*
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
class PlayerInfo{
private String playerName;
private int score;
private boolean isOnline;
private long mTime;
public PlayerInfo(String name,int score,boolean isonline,long time) {
this.playerName = name;
this.score = score;
this.isOnline = isonline;
this.mTime = time;
}
public static Comparator<PlayerInfo> COMPARE_BY_TIME = new Comparator<PlayerInfo>() {
public int compare(PlayerInfo one, PlayerInfo other){
return one.mTime > other.mTime ? -1: one.mTime < other.mTime ?1:0;
}
};
public static Comparator<PlayerInfo> COMPARE_BY_SCORE = new Comparator<PlayerInfo>() {
public int compare(PlayerInfo one,PlayerInfo other){
return one.score > other.score ? -1 : one.score < other.score ?1:0;
}
};
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public boolean isOnline() {
return isOnline;
}
public void setOnline(boolean isOnline) {
this.isOnline = isOnline;
}
public long getmTime() {
return mTime;
}
public void setmTime(long mTime) {
this.mTime = mTime;
}
}
public class viewer {
public static final String BASE_PATH = "/ajain28";
public static final String SCORE_PATH = BASE_PATH + "/scores";
public static final String ONLINE_PATH = BASE_PATH + "/online";
static String IP;
static int port;
static int N;
private ZooKeeper zk;
private CountDownLatch connSignal = new CountDownLatch(0);
static ArrayList<PlayerInfo> recentScores = new ArrayList<PlayerInfo>();
static ArrayList<PlayerInfo> highestScores = new ArrayList<PlayerInfo>();
static ArrayList<PlayerInfo> newList = new ArrayList<PlayerInfo>();
public ZooKeeper connect(String host) throws Exception {
zk = new ZooKeeper(host, 3000, new Watcher() {
@SuppressWarnings("unchecked")
public void process(WatchedEvent event) {
String eventPath = event.getPath();
if (event.getState() == KeeperState.SyncConnected) {
//System.out.println("CLient is sync!");
connSignal.countDown();
} else if(event.getType()==EventType.NodeChildrenChanged){
//System.out.println("Node value is updated");
}
if(event.getType() == Event.EventType.None) {
if(event.getState() == KeeperState.Disconnected) {
//System.out.println("Node is disconnected!");
}
}
//if(eventPath!=null) {
//System.out.println("Watcher has received the event "+eventPath);
try {
List<String> childs = zk.getChildren(SCORE_PATH, false);
newList.clear();
for(String s:childs) {
String t[] = s.split(":");
PlayerInfo pl = new PlayerInfo(t[0], Integer.parseInt(t[1]), false, Integer.parseInt(t[2]));
newList.add(pl);
}
} catch(Exception e) {
System.out.println("Catched in process!");
//e.printStackTrace();
}
recentScores.clear();
highestScores.clear();
recentScores = (ArrayList<PlayerInfo>)newList.clone();
highestScores = (ArrayList<PlayerInfo>)newList.clone();
Collections.sort(recentScores,PlayerInfo.COMPARE_BY_TIME);
Collections.sort(highestScores, PlayerInfo.COMPARE_BY_SCORE);
if(recentScores.size()>N) {
ArrayList<PlayerInfo> r1 = new ArrayList<PlayerInfo>(recentScores.subList(0, N));
recentScores = r1;
}
if(highestScores.size()>N) {
ArrayList<PlayerInfo> h1 = new ArrayList<PlayerInfo>(highestScores.subList(0, N));
highestScores = h1;
}
//Set the flag for online by visiting the node in ONLINE_PATH
List<String> onlineNodes = new ArrayList<String>();
try{
onlineNodes = zk.getChildren(ONLINE_PATH, false);
} catch(Exception e) {
System.out.println("Error: Online Node children fails !");
//e.printStackTrace();
}
for(String s:onlineNodes) {
for(PlayerInfo p:recentScores) {
if(p.getPlayerName().equals(s)) {
p.setOnline(true);
}
}
}
for(String s:onlineNodes) {
for(PlayerInfo p :highestScores) {
if(p.getPlayerName().equals(s)) {
p.setOnline(true);
}
}
}
System.out.println("\nMost recent scores");
System.out.println("------------------");
for(PlayerInfo p :recentScores){
String online = p.isOnline() ? " **" : "";
//System.out.println(p.getPlayerName() + "\t\t\t" + p.getScore() +online);
System.out.printf("%-25s %d %s\n", p.getPlayerName(),p.getScore(),online);
}
System.out.println("\nHighest scores");
System.out.println("--------------");
for(PlayerInfo p : highestScores){
String online = p.isOnline() ? " **":"";
//System.out.println(p.getPlayerName() + "\t\t\t" + p.getScore() + online);
System.out.printf("%-25s %d %s\n", p.getPlayerName(),p.getScore(),online);
}
//}
}
});
connSignal.await();
return zk;
}
public static void main(String[] args) {
if(args.length==2) {
String temp = args[0];
String t[] = temp.split(":");
IP = t[0];
if(t.length == 2){
port = Integer.parseInt(t[1]);
} else {
port = 6000;
}
N = Integer.parseInt(args[1]);
if(N>25){
N=25;
}
String connectionString = IP +":"+port;
viewer view = new viewer();
try {
ZooKeeper zk = view.connect(connectionString);
if(zk==null) {
System.out.println("Error:Connection failed to zookeeper!");
System.exit(0);
}
if(zk.exists(BASE_PATH, false)==null) {
zk.create(BASE_PATH, "Base Node".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
if(zk.exists(ONLINE_PATH, false)==null) {
zk.create(ONLINE_PATH, "IS ONLINE PARENT".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
if(zk.exists(SCORE_PATH, false)==null) {
zk.create(SCORE_PATH, "SCORES".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
while(true) {
List<String> chd = zk.getChildren(SCORE_PATH, true);
List<String> chd1 = zk.getChildren(ONLINE_PATH, true);
}
} catch (Exception e) {
System.out.println("Error: Something went wrong in watcher!");
//e.printStackTrace();
}
}else {
System.out.println("Usage: <exe name> <IP[:port]> N");
System.exit(0);
}//end of If for args check
}
}