-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
118 lines (112 loc) · 3.77 KB
/
Copy pathClient.java
File metadata and controls
118 lines (112 loc) · 3.77 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
package zookeeper;
import java.io.IOException;
import java.util.Scanner;
// import zookeeper classes
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.AsyncCallback.StatCallback;
import org.apache.zookeeper.KeeperException.Code;
import org.apache.zookeeper.data.Stat;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.log4j.Logger;
//import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
public class Client{
static boolean wait = false;
static ZooKeeper zoo;
static String myName = "John";
static boolean isConnected = false;
public static void main(String[] args){
org.apache.log4j.BasicConfigurator.configure();
Logger.getRootLogger().setLevel(Level.WARN);
String host = "localhost:2181,localhost:2182,localhost:2183";
try{
zoo = new ZooKeeper(host,5000, new Watcher() {
public void process(WatchedEvent we) {
if (we.getState() == KeeperState.SyncConnected) {
isConnected = true;
}
}
});
while (!isConnected){
Thread.sleep(100);
}
} catch (Exception e){System.out.println(e.getMessage());}
byte[] placeholder = {'0'};
try{
zoo.create("/" + myName, placeholder, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
Watcher clientWatcher = new Watcher() {
public void process(WatchedEvent e) {
try{
System.out.println(new String(zoo.getData("/" + myName, false, null)));
} catch (Exception ex){System.out.println(ex.getMessage());}
}
};
zoo.getData("/" + myName, clientWatcher, null);
} catch (Exception ex){System.out.println(ex.getMessage());}
//assume a client has typed the message
//String message = "purchase John shoes 2";
//String sentMessage = message + " " + myName;
//byte[] sentMessageByte = sentMessage.getBytes();
Scanner sc = new Scanner(System.in);
try{
//zoo.setData("/command", sentMessageByte, -1);
//zoo.setData("/command", sentMessageByte, -1);
System.out.println("commands");
while (sc.hasNextLine()){
final double samples = 10;
String endTime;
String cmd = sc.nextLine();
//String cmd = "purchase robert xbox 1";//"list";//
cmd += " " + myName;
final long startTime = System.nanoTime();
//for(int i = 0; i < samples; i++)
//{
Watcher clientWatcher;
/*if(i == samples - 1)
{
clientWatcher = new Watcher() {
public void process(WatchedEvent e) {
try{
System.out.println(new String(zoo.getData("/" + myName, false, null)));
zoo.getData("/" + myName, false, null);
wait = false;
long elapsedTime = System.nanoTime() - startTime;
double avgTime = elapsedTime/samples/1000000.0;
System.out.println("elapsed time: " + avgTime);
} catch (Exception ex){System.out.println(ex.getMessage());}
}
};
}
else
{*/
clientWatcher = new Watcher() {
public void process(WatchedEvent e) {
try{
System.out.println(new String(zoo.getData("/" + myName, false, null)));
//zoo.getData("/" + myName, false, null);
wait = false;
} catch (Exception ex){System.out.println(ex.getMessage());}
}
};
//}
zoo.getData("/" + myName, clientWatcher, null);
//System.out.println("watch set");
wait = true;
zoo.setData("/command", cmd.getBytes(), -1);
while(wait){Thread.sleep(1000);}
//Thread.sleep(1000);
}
//}
//long elapsedTime = System.nanoTime() - startTime;
//double avgTime = elapsedTime/samples/1000000.0;
//System.out.println("Average time: " + avgTime + " ms");
} catch (Exception e){System.out.println(e.getMessage());}
}
}