-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuctioneer.java
More file actions
279 lines (258 loc) · 10.4 KB
/
Copy pathAuctioneer.java
File metadata and controls
279 lines (258 loc) · 10.4 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
//package AuctionHouse;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CyclicBarrier;
import java.io.*;
public class Auctioneer extends Observer implements Runnable{
private ServerSocketChannel serverSocket;
private int port;
private int peerPort;
private Auctioneer auctioneer;
private SocketChannel peerChannel;
private Selector selector;
public HashMap<SocketChannel,String> activeChannels;
public HashMap<String,SocketChannel> revActiveChannels;
public ArrayList<String> interestedUsers = new ArrayList<String>();
public String itemDescription;
public boolean commandFlag;
public boolean bidFlag;
public Subject syncObject;
public String SName;
public int time_interval;//time L for all the actions
CyclicBarrier barrier;
public String dbName;
public Auctioneer(int port,Subject syncObject,String SName,CyclicBarrier barrier) throws IOException
{
this.port = port;
this.syncObject=syncObject;
this.SName=SName;
this.barrier=barrier;
this.syncObject.attach(this);
}
public void run()
{
System.out.println("Auctioneer starting ...");
//initialize variables.
switch (SName){
case "Server1": dbName="auctionDB1";break;
case "Server2": dbName="auctionDB2";break;
case "Server3": dbName="auctionDB3";break;
}
JDBController dbController = new JDBController(dbName);
activeChannels=new HashMap();
revActiveChannels=new HashMap();
time_interval=15000;
commandFlag=false;
bidFlag=false;
itemDescription="no item yet";
ItemController icon=new ItemController(this,barrier);
icon.start();
// Get selector
Selector selector;
try {
selector = Selector.open();
System.out.println("Selector open: " + selector.isOpen());
// Get server socket channel and register with selector
ServerSocketChannel serverSocket = ServerSocketChannel.open();
InetSocketAddress hostAddress = new InetSocketAddress("192.168.1.66", port);
serverSocket.bind(hostAddress);
serverSocket.configureBlocking(false);
int ops = serverSocket.validOps();
SelectionKey selectKy = serverSocket.register(selector, ops, null);
for (;;) {
//System.out.println("Waiting for select...");
int noOfKeys = selector.select();
//System.out.println("Number of selected keys: " + noOfKeys);
Set selectedKeys = selector.selectedKeys();
Iterator iter = selectedKeys.iterator();
while (iter.hasNext()) {
SelectionKey ky = (SelectionKey) iter.next();
if (ky.isAcceptable()) {
// Accept the new client connection
SocketChannel client;
try {
client = serverSocket.accept();
client.configureBlocking(false);
// Add the new connection to the selector
client.register(selector, SelectionKey.OP_READ);
System.out.println("Accepted new connection from client: " + client);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if (ky.isReadable()) {
// Read the data from client
SocketChannel client = (SocketChannel) ky.channel();
ByteBuffer buffer = ByteBuffer.allocate(256);
client.read(buffer);
String output = new String(buffer.array()).trim();
System.out.println("Message read from client: " + output);
String[] parts = output.split(" ");
String part1 = parts[0];
//handleMessages(output);
if (output.equals(Constants.list_high_bid)) {
int itemId = 1; //ItemsArrayList.getCurrentItemId;
String highestBid="";
try {
highestBid = Integer.toString(dbController.getCurrentBid(itemDescription));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
send(client,"Current highest bid:" + highestBid);
}
else if (output.equals(Constants.list_description)){
send(client,"Description of current item:" + itemDescription);
}
else if ((output.equals(Constants.quit))||(output.equals(""))) {
String bidder=activeChannels.get(client);
try {
String bid=activeChannels.get(client);
activeChannels.remove(client);
revActiveChannels.remove(bidder);
boolean flag = false;
flag = dbController.deleteBidder(bid);
if (!flag)
System.out.println("error in deleting bidder!");
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
client.close();
System.out.println("Client messages are complete; close.");
}
else if (part1.equals("bid")){
String name=activeChannels.get(client);
if ((bidFlag)&&(interestedUsers.contains(name))){
int amount=-1;
boolean flag = false;
try{
amount = Integer.parseInt(parts[1]);
int currentBid = dbController.getCurrentBid(itemDescription);
/* Update only on strictly higher bid! */
if (amount > currentBid){
send(client,"Your bid has been accepted.\n");
flag = dbController.updateItemBid(itemDescription, name, amount);
}
else
send(client,"Your bid has been rejected.\n");
}
catch(NumberFormatException e){
send(client,"You haven't entered a number: ");
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (flag){
send(client,"You bidded: " + amount);
syncObject.setState(amount,name,itemDescription);
}
}
else{
send(client,"You cannot bid at this time.");
}
}
else if (part1.equals("connect")){
String name = parts[1];
try {
boolean flag = false;
flag = dbController.isDuplicate(name);
if ((!flag)&&(!name.equals("no_holder"))){
flag = dbController.insertBidder(name, 1);
send(client,"Success on registering!");
}
else{
send(client,Constants.duplicate_name);
client.close();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
activeChannels.put(client,name);
revActiveChannels.put(name, client);
}
if (commandFlag){
if (output.equals(Constants.i_am_interested)){
String name=activeChannels.get(client);
if (!interestedUsers.contains(name)){
interestedUsers.add(name);
}
}
}
} // end if (ky...)
iter.remove();
} // end while loop
} // end for loop
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void send(SocketChannel channel, String message) throws IOException {
byte [] mes = new String(message).getBytes();
ByteBuffer buffer = ByteBuffer.wrap(mes);
channel.write(buffer);
buffer.clear();
}
public void broadcast(String message)
{
CharBuffer buffer;
for (SocketChannel channel : activeChannels.keySet())
{
buffer = CharBuffer.wrap(message);
try {
while (buffer.hasRemaining())
channel.write(Charset.defaultCharset().encode(buffer));
} catch (IOException e) {
//e.printStackTrace();
}
buffer.clear();
}
}
public void broadcastInterested(String message)
{
CharBuffer buffer;
for (String name : interestedUsers)
{
SocketChannel channel=revActiveChannels.get(name);
buffer = CharBuffer.wrap(message);
try {
while (buffer.hasRemaining())
channel.write(Charset.defaultCharset().encode(buffer));
} catch (IOException e) {
}
buffer.clear();
}
}
private void handleMessages(String output) {
// TODO Auto-generated method stub
}
@Override
public void update() {
// TODO Auto-generated method stub
System.out.println(SName+":New high bid");
String message=Constants.new_high_bid+"@"+syncObject.getUsername()+"@"+syncObject.getAmount();
time_interval=15000;
broadcastInterested(message);
}
}