-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandWatcher.java
More file actions
149 lines (135 loc) · 5.56 KB
/
Copy pathCommandWatcher.java
File metadata and controls
149 lines (135 loc) · 5.56 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
package zookeeper;
// 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 java.util.List;
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;
public class CommandWatcher implements Watcher{
static ZooKeeper zoo;
static int commandCount = 0;
public CommandWatcher(ZooKeeper zooParam){
zoo = zooParam;
}
@Override
public void process(WatchedEvent e){
try{
zoo.create("/commandNum/" + commandCount, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
commandCount += 1;
byte[] recievedCommandByte = zoo.getData("/command", false, null);
String recievedCommand = new String(recievedCommandByte);
System.out.println(recievedCommand);
String[] tokens = recievedCommand.split(" ");
if (tokens[0].equals("purchase")){
String name = tokens[1];
String item = tokens[2];
String quantityString = tokens[3];
String clientName = tokens[4];
int purchasedQuantity = Integer.parseInt(quantityString);
//no item or run out
//return null Stat object
if (zoo.exists("/store/" + item, false) == null){
String reply = "`Not Available- We do not sell this product";
byte[] replyByte = reply.getBytes();
zoo.setData("/" + clientName, replyByte, -1);
}
else{
//change store
int quantity = Integer.parseInt(new String(zoo.getData("/store/" + item, false, null)));
if(purchasedQuantity > quantity)
{
String reply = "`Not Available- Not Enough Items";
byte[] replyByte = reply.getBytes();
zoo.setData("/" + clientName, replyByte, -1);
}
else
{
quantity = quantity - purchasedQuantity;
byte[] byteQuantity = String.valueOf(quantity).getBytes();
zoo.setData("/store/" + item, byteQuantity, -1);
//change order
int orderCount = Integer.parseInt(new String(zoo.getData("/orderNum", false, null)));
zoo.create("/orders/" + orderCount, recievedCommandByte, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
//customer exists
byte[] placeholder = {0};
if (zoo.exists("/customers/" + name, null) == null){
zoo.create("/customers/" + name, placeholder, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
zoo.create("/customers/" + name + "/" + orderCount, placeholder, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
orderCount += 1;
zoo.setData("/orderNum", ("" + orderCount).getBytes(), -1);
String reply = "Your order has been placed, ## " + name + " " + item + " " + purchasedQuantity;
byte[] replyByte = reply.getBytes();
zoo.setData("/" + clientName, replyByte, -1);
//for debugging
System.out.println(item + " " + quantity);
}
}
}
else if (tokens[0].equals("cancel")){
int orderNum = Integer.parseInt(tokens[1]);
//no such order
if (zoo.exists("/orders/" + orderNum, false) == null){
String reply = orderNum + " not found, no such order";
byte[] replyByte = reply.getBytes();
zoo.setData("/" + tokens[2], replyByte, -1);
}
else{
String orderType = new String(zoo.getData("/orders/" + orderNum, false, null));
String[] orderInfo = orderType.split(" ");
String userName = orderInfo[1];
String itemType = orderInfo[2];
int purchasedNum = Integer.parseInt(orderInfo[3]);
//edit store
int quantity = Integer.parseInt(new String(zoo.getData("/store/" + itemType, false, null)));
quantity = quantity + purchasedNum;
byte[] byteQuantity = String.valueOf(quantity).getBytes();
zoo.setData("/store/" + itemType, byteQuantity, -1);
//delete order
zoo.delete("/orders/" + orderNum, -1);
//delete customer order
zoo.delete("/customers/" + userName + "/" + orderNum, -1);
//reply
String reply = "Order " + orderNum + " is canceled";
byte[] replyByte = reply.getBytes();
zoo.setData("/" + tokens[2], replyByte, -1);
}
}
else if (tokens[0].equals("search")){
List<String> children = zoo.getChildren("/customers/" + tokens[1], false);
String response = "";
for (String thisChild : children){
String data = new String(zoo.getData("/orders/" + thisChild, false, null));
String[] dataTokens = data.split(" ");
response += (thisChild + " " + dataTokens[2] + " " + dataTokens[3] + "\n");
}
//send response
byte[] replyByte = response.getBytes();
zoo.setData("/" + tokens[2], replyByte, -1);
}
else if (tokens[0].equals("list")){
List<String> children = zoo.getChildren("/store", false);
String response = "";
for (String thisChild : children){
String data = new String(zoo.getData("/store/" + thisChild, false, null));
response += (thisChild + " " + data + "\n");
}
//send response
byte[] replyByte = response.getBytes();
zoo.setData("/" + tokens[1], replyByte, -1);
}
} catch (KeeperException ex){commandCount += 1; System.out.println(ex.getMessage());}
catch (Exception ex){System.out.println(ex.getMessage());}
try{
//set watcher again
zoo.getData("/command", new CommandWatcher(zoo), null);
System.out.println("watch set");
} catch (Exception ex){System.out.println(ex.getMessage());}
}
}