-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplica.java
More file actions
332 lines (291 loc) · 10.5 KB
/
Copy pathReplica.java
File metadata and controls
332 lines (291 loc) · 10.5 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import java.util.*;
import javax.crypto.SecretKey;
import javax.management.RuntimeErrorException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SealedObject;
import java.security.SecureRandom;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.rmi.RemoteException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import java.io.File; // Import the File class
import java.io.FileInputStream;
import javax.crypto.BadPaddingException;
import java.io.*;
import org.jgroups.*;
import org.jgroups.blocks.*;
import org.jgroups.Message.TransientFlag;
import org.jgroups.util.*;
public class Replica {
private Key key;
private List<Seller> sellers = new ArrayList<>();
private List<Buyer> buyers = new ArrayList<>();
private int lastID = 0;
private int confirmSellerIdentity = -1;
private int confirmBuyerIdentity = -1;
private JChannel channel;
private RpcDispatcher dispatcher;
private RequestOptions opts;
private RspList rspList;
public static void main(String[] args) throws java.rmi.RemoteException, Exception, IOException, NoSuchPaddingException, IllegalBlockSizeException, ClassNotFoundException {
new Replica();
}
public Replica() throws java.rmi.RemoteException, Exception, IOException, NoSuchPaddingException, IllegalBlockSizeException, ClassNotFoundException {
channel = new JChannel();
opts = new RequestOptions(ResponseMode.GET_ALL, 1000L).setTransientFlags(TransientFlag.DONT_LOOPBACK);
dispatcher = new RpcDispatcher(channel,this);
channel.connect("MyCluster");
rspList = dispatcher.callRemoteMethods(null, "updateSellers", new Object[] {}, new Class[] {}, opts);
sellers = majority() == null ? new ArrayList<>() : (List) majority();
rspList = dispatcher.callRemoteMethods(null, "updateBuyers", new Object[] {}, new Class[] {}, opts);
buyers = majority() == null ? new ArrayList<>() : (List) majority();
rspList = dispatcher.callRemoteMethods(null, "updateLastId", new Object[] {}, new Class[] {}, opts);
lastID = majority() == null ? 0 : (int) majority();
}
public List<Seller> updateSellers() {
List<Seller> res = new ArrayList<>();
for (Seller s : sellers) res.add(s);
return res;
}
public List<Buyer> updateBuyers() {
List<Buyer> res = new ArrayList<>();
for (Buyer b : buyers) res.add(b);
return res;
}
public int updateLastId() {
return lastID;
}
/**
* Returns the most common response list.
* @param responseList the response list
* @return the most common response list
*/
private Object majority() throws Exception {
Map<Object, Integer> result = new HashMap<>();
if (rspList.getResults().isEmpty()) return null;
System.out.println("Responses:" + rspList);
for (Object response : rspList.getResults()) {
// System.out.println("Response: " + response);
if (!result.containsKey(response))
result.put(response, 1);
else
result.put(response, result.get(response)+1);
}
return Collections.max(result.entrySet(), Comparator.comparingInt(Map.Entry::getValue)).getKey();
}
/**
* Returns the auction item based on the provided item id. The seller is also required to be able to extract the userID and find the
* associated key.
* @param s the seller to extract the sealed object
* @param itemId the item id of the auction item
* @return sealed object containing the auction item
*/
public AuctionItem getAuctionItemById(int itemId) throws java.rmi.RemoteException, NoSuchAlgorithmException,
InvalidKeyException, IOException, NoSuchPaddingException, IllegalBlockSizeException, ClassNotFoundException {
for (Seller seller : sellers) {
for (int i = 0; i < seller.getAllAuctionItems().size(); i++) {
if (seller.getAllAuctionItems().get(i).getItemId() == itemId) {
return seller.getAllAuctionItems().get(i);
}
}
}
return null;
}
/**
* Retrieves the seller by item id.
* @param itemId
* @return
* @throws RemoteException
*/
public Seller getSellerByItemId(int itemId) throws RemoteException {
for (Seller seller : sellers) {
for (Integer auctionId : seller.getSellings().keySet()) {
if (seller.getSellings().get(auctionId).getItemId() == itemId) {
return seller;
}
}
}
return null;
}
/**
* Displays all the available actions.
* @return string containing all the auctions
* @throws RemoteException
*/
public String enlistAvailableAuctions() throws RemoteException {
StringBuilder sb = new StringBuilder();
for (Seller seller : sellers){
System.out.println(seller.enlistSellings());
sb.append(seller.enlistSellings());
}
return sb.toString();
}
/**
* Adds an auction item onto the seller's list of items.
* @param ai the auction item to be added
* @param seller the seller who will be the owner of the item
* @param auctionId the key for the map of the seller
* @throws RemoteException
*/
public void addAuctionItem(AuctionItem ai, Seller seller, int auctionId) throws RemoteException {
for (Seller s: sellers) {
if (s.getEmail().equals(seller.getEmail())) {
s.addSelling(auctionId, ai);
}
}
lastID++;
}
/**
* Removes an auction item based on the auction id.
* @param ai the uaction item to be removed
* @param auctionId the auction id of the item
* @throws RemoteException
*/
public void removeAuctionItem(AuctionItem ai, int auctionId) throws RemoteException {
Seller s = null;
for (Seller seller : sellers) {
for (int aid : seller.getSellings().keySet()) {
if (seller.getSellings().get(aid).getItemId() == ai.getItemId()) {
s = seller;
}
}
}
s.removeSelling(auctionId);
}
/**
* Gets the last id.
* @return the last id
* @throws RemoteException
*/
public int getLastID() throws RemoteException {
return lastID;
}
/**
* Overwrites the auction item's information based on the buyer's bid.
* @param itemId the id of the item
* @param newBid the new bid value
* @param newBuyer the new buyer who made the bid
* @throws RemoteException
*/
public synchronized void overwriteBid(int itemId, int newBid, Buyer newBuyer) throws RemoteException {
// search for the seller
for (Seller seller : sellers) {
// get the sellings for the seller
for (int auctionId : seller.getSellings().keySet()) {
// overwrite that selling with the new buyer
if (seller.getSellings().get(auctionId).getItemId() == itemId) {
seller.getSellings().get(auctionId).setNewBuyer(newBuyer);
seller.getSellings().get(auctionId).setHighestBid(newBid);
}
}
}
}
/**
* Returns the size of the auction items.
* @return the size of the auction items
* @throws RemoteException
*/
public int size() throws RemoteException {
int size = 0;
for (Seller seller : sellers) {
size += seller.getAllAuctionItems().size();
}
return size;
}
/**
* Adds seller to the server.
* @param seller the seller to be added
* @throws RemoteException
*/
public void addSeller(Seller seller) throws RemoteException {
sellers.add(seller);
}
/**
* Checks if seller exists in the server.
* @param email the email of the seller
* @return true or false based on the existence of the seller
* @throws RemoteException
*/
public boolean sellerExists(String email) throws RemoteException {
for (Seller seller : sellers) {
if (seller.getEmail().equals(email)) {
return true;
}
}
return false;
}
/**
* Returns the seller based on the email.
* @param email the email of the seller
* @return the seller
* @throws RemoteException
*/
public Seller getSeller(String email) throws RemoteException {
for (Seller seller : sellers) {
if (seller.getEmail().equals(email)) {
return seller;
}
}
return null;
}
public Seller getSeller(int index) throws RemoteException {
return sellers.get(index);
}
/**
* Adds a bueyr to the server.
* @param buyer the buyer to be added
* @throws RemoteException
*/
public void addBuyer(Buyer buyer) throws RemoteException {
buyers.add(buyer);
}
/**
* Checks if buyer exists or not.
* @param email the email of the buyer
* @return true or false based on the existence of the bueyr
* @throws RemoteException
*/
public boolean buyerExists(String email) throws RemoteException {
for (Buyer buyer : buyers) {
if (buyer.getEmail().equals(email)) {
return true;
}
}
return false;
}
/**
* Returns the buyer.
* @param email the email of the buyer
* @return the buyer if he exists
* @throws RemoteException
*/
public Buyer getBuyer(String email) throws RemoteException {
for (Buyer buyer : buyers) {
if (buyer.getEmail().equals(email)) {
return buyer;
}
}
return null;
}
/**
* Returns a list of all the sellers that are present in the server.
* @return the list of sellers
* @throws RemoteException
*/
public List<Seller> getSellers() throws RemoteException {
return sellers;
}
public String showAuctionsForSeller(String email) throws RemoteException {
StringBuilder builder = new StringBuilder();
for (AuctionItem ai : getSeller(email).getAllAuctionItems()) {
builder.append(ai);
}
return builder.toString();
}
}