-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgramImpl.java
More file actions
314 lines (278 loc) · 12.2 KB
/
Copy pathProgramImpl.java
File metadata and controls
314 lines (278 loc) · 12.2 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
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.*;
import org.jgroups.util.*;
public class ProgramImpl extends java.rmi.server.UnicastRemoteObject implements Program {
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 RspList rspList;
private RequestOptions opts;
public ProgramImpl() throws java.rmi.RemoteException, IOException, NoSuchPaddingException, IllegalBlockSizeException, ClassNotFoundException, Exception {
super();
opts = new RequestOptions(ResponseMode.GET_ALL, 1000L).setTransientFlags(TransientFlag.DONT_LOOPBACK);
channel = new JChannel();
dispatcher = new RpcDispatcher(channel,this);
channel.connect("MyCluster");
}
/**
* Sends a challenge to the client to verify its identity.
* @return a random number
* @throws RemoteException
*/
public int challenge() throws RemoteException {
return new Random().nextInt(100000);
}
/**
* Authenticates the seller by sending a sealed object with the original key of the seller.
* @param seller the seller to be authenticated
* @return sealed object containing an encrypted number
*/
public SealedObject auth(Seller seller)
throws RemoteException, Exception, ClassNotFoundException, BadPaddingException, NoSuchAlgorithmException, InvalidKeyException, IOException, NoSuchPaddingException, IllegalBlockSizeException {
Key tempKey;
ObjectInputStream oin = new ObjectInputStream(new FileInputStream(seller.getKeyLocation()+".txt"));
try {
tempKey = (Key) oin.readObject();
}
finally {
oin.close();
}
key = tempKey;
Cipher encryptCipher = Cipher.getInstance("AES");
encryptCipher.init(Cipher.ENCRYPT_MODE, tempKey);
confirmSellerIdentity = new Random().nextInt(100000);
SealedObject res = new SealedObject(confirmSellerIdentity, encryptCipher);
return res;
}
/**
* Authenticates the buyer by sending a sealed object with the original key of the buyer.
* @param buyer the buyer to be authenticated
* @return sealed object containing an encrypted number
*/
public SealedObject auth(Buyer buyer)
throws RemoteException, Exception, ClassNotFoundException, BadPaddingException, NoSuchAlgorithmException, InvalidKeyException, IOException, NoSuchPaddingException, IllegalBlockSizeException {
Key tempKey;
ObjectInputStream oin = new ObjectInputStream(new FileInputStream(buyer.getKeyLocation()+".txt"));
try {
tempKey = (Key) oin.readObject();
}
finally {
oin.close();
}
key = tempKey;
Cipher encryptCipher = Cipher.getInstance("AES");
encryptCipher.init(Cipher.ENCRYPT_MODE, tempKey);
confirmBuyerIdentity = new Random().nextInt(100000);
SealedObject res = new SealedObject(confirmBuyerIdentity, encryptCipher);
return res;
}
/**
* Confirms the seller identity by carrying out a check for the received and returned values from the encryption/decryption process.
* @param decryptVal the value provided by the client
* @return true/false if the client provided the right decryption value or not
* @throws RemoteException
*/
public boolean confirmSellerIdentity(int decryptVal) throws RemoteException, Exception {
return decryptVal == confirmSellerIdentity;
}
/**
* Confirms the buyer identity by carrying out a check for the received and returned values from the encryption/decryption process.
* @param decryptVal the value provided by the client
* @return true/false if the client provided the right decryption value or not
* @throws RemoteException
*/
public boolean confirmBuyerIdentity(int decryptVal) throws RemoteException, Exception {
return decryptVal == confirmBuyerIdentity;
}
/**
* 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 cotnaining the auction item
*/
public AuctionItem getAuctionItemById(int itemId) throws java.rmi.RemoteException, Exception, NoSuchAlgorithmException, InvalidKeyException, IOException, NoSuchPaddingException, IllegalBlockSizeException, ClassNotFoundException {
rspList = dispatcher.callRemoteMethods(null, "getAuctionItemById", new Object[]{itemId}, new Class[] {int.class}, opts);
return (AuctionItem) majority();
}
/**
* Retrieves the seller by item id.
* @param itemId
* @return
* @throws RemoteException
*/
public Seller getSellerByItemId(int itemId) throws RemoteException, Exception {
rspList = dispatcher.callRemoteMethods(null, "getSellerByItemId", new Object[]{itemId}, new Class[] {int.class}, opts);
return (Seller) majority();
}
/**
* Displays all the available actions.
* @return string containing all the auctions
* @throws RemoteException
*/
public String enlistAvailableAuctions() throws RemoteException, Exception {
rspList = dispatcher.callRemoteMethods(null, "enlistAvailableAuctions", new Object[]{}, new Class[] {}, opts);
return (String) majority();
}
/**
* 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, Exception {
rspList = dispatcher.callRemoteMethods(null, "addAuctionItem", new Object[]{ai, seller, auctionId}, new Class[] {AuctionItem.class, Seller.class, int.class}, opts);
}
/**
* 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, Exception {
rspList = dispatcher.callRemoteMethods(null, "removeAuctionItem", new Object[]{ai, auctionId}, new Class[] {AuctionItem.class, int.class}, opts);
}
/**
* Gets the last id.
* @return the last id
* @throws RemoteException
*/
public int getLastID() throws RemoteException, Exception {
rspList = dispatcher.callRemoteMethods(null, "getLastID", new Object[]{}, new Class[] {}, opts);
System.out.println("LAST ID : " + lastID);
return (int) majority();
}
/**
* 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, Exception {
rspList = dispatcher.callRemoteMethods(null, "overwriteBid", new Object[]{itemId, newBid, newBuyer}, new Class[] {int.class, int.class, Buyer.class}, opts);
}
/**
* Returns the size of the auction items.
* @return the size of the auction items
* @throws RemoteException
*/
public int size() throws RemoteException, Exception {
rspList = dispatcher.callRemoteMethods(null, "size", new Object[]{}, new Class[] {}, opts);
return (int) majority();
}
/**
* Adds seller to the server.
* @param seller the seller to be added
* @throws RemoteException
*/
public void addSeller(Seller seller) throws RemoteException, Exception {
rspList = dispatcher.callRemoteMethods(null, "addSeller", new Object[]{seller}, new Class[] {Seller.class}, opts);
}
/**
* 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, Exception {
rspList = dispatcher.callRemoteMethods(null, "sellerExists", new Object[]{email}, new Class[] {String.class}, opts);
return (boolean) majority();
}
/**
* 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, Exception {
rspList = dispatcher.callRemoteMethods(null, "getSeller", new Object[]{email}, new Class[] {String.class}, opts);
return (Seller) majority();
}
public Seller getSeller(int index) throws RemoteException, Exception {
rspList = dispatcher.callRemoteMethods(null, "getSeller", new Object[]{index}, new Class[] {int.class}, opts);
return (Seller) majority();
}
/**
* Adds a bueyr to the server.
* @param buyer the buyer to be added
* @throws RemoteException
*/
public void addBuyer(Buyer buyer) throws RemoteException, Exception {
rspList = dispatcher.callRemoteMethods(null, "addBuyer", new Object[]{buyer}, new Class[] {Buyer.class}, opts);
}
/**
* 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, Exception {
rspList = dispatcher.callRemoteMethods(null, "buyerExists", new Object[]{email}, new Class[] {String.class}, opts);
return (boolean) majority();
}
/**
* 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, Exception {
rspList = dispatcher.callRemoteMethods(null, "getBuyer", new Object[]{email}, new Class[] {String.class}, opts);
return (Buyer) majority();
}
/**
* 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, Exception {
rspList = dispatcher.callRemoteMethods(null, "getSellers", new Object[]{}, new Class[] {}, opts);
return (List<Seller>) majority();
}
/**
* 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();
}
}