Skip to content

Commit 66226d1

Browse files
authored
Merge pull request #3 from vicweeks/milestone3-patch0
Milestone3 patch0
2 parents 061c3b0 + c9fd0f4 commit 66226d1

22 files changed

+257
-28
lines changed

cs455/scaling/client/Client.class

2.62 KB
Binary file not shown.

cs455/scaling/client/Client.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package cs455.scaling.client;
22

33
import cs455.scaling.tasks.ClientTask;
4+
import cs455.scaling.util.ClientLogger;
45
import java.nio.channels.SocketChannel;
56
import java.net.InetSocketAddress;
67
import java.io.IOException;
8+
import java.util.Timer;
79

810
public class Client {
911

1012
private static SocketChannel socketChannel;
13+
private static ClientLogger clientLogger = new ClientLogger();
1114

1215
public static void main(String[] args) {
1316

@@ -27,9 +30,11 @@ public static void main(String[] args) {
2730
if (args.length == 4)
2831
debug = true;
2932

33+
c.printStatus();
34+
3035
try {
3136
c.setUpChannel(serverHost, serverPort);
32-
ClientTask clientTask = new ClientTask(socketChannel, messageRate);
37+
ClientTask clientTask = new ClientTask(socketChannel, messageRate, clientLogger, debug);
3338
clientTask.run();
3439
} catch (IOException ioe) {
3540
System.out.println(ioe.getMessage());
@@ -39,10 +44,18 @@ public static void main(String[] args) {
3944

4045
}
4146

47+
private void printStatus() {
48+
// logs stats to the console evry interval
49+
Timer timer = new Timer();
50+
int interval = 5000;
51+
timer.schedule(clientLogger, interval, interval);
52+
}
53+
4254
private void setUpChannel(String serverHost, int serverPort)
4355
throws IOException, InterruptedException{
4456
System.out.println("Setting up client...");
4557
socketChannel = SocketChannel.open();
58+
socketChannel.configureBlocking(false);
4659
socketChannel.connect(new InetSocketAddress(serverHost, serverPort));
4760
while(!socketChannel.finishConnect())
4861
Thread.sleep(100);

cs455/scaling/server/Server.class

4.43 KB
Binary file not shown.

cs455/scaling/server/Server.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import cs455.scaling.threadpool.ThreadPoolManager;
44
import cs455.scaling.tasks.ServerTask;
5+
import cs455.scaling.util.ServerLogger;
6+
import cs455.scaling.util.ThroughputLogger;
57
import java.nio.channels.Selector;
68
import java.nio.channels.SelectionKey;
79
import java.nio.channels.ServerSocketChannel;
@@ -11,13 +13,16 @@
1113
import java.net.InetSocketAddress;
1214
import java.io.IOException;
1315
import java.util.Set;
16+
import java.util.Timer;
1417

1518
public class Server {
1619

1720
private static boolean debug;
1821
private static ThreadPoolManager tpm;
1922
private static ServerSocketChannel ssChannel;
2023
private static Selector serverSelector;
24+
private static ServerLogger serverLogger = new ServerLogger();
25+
2126
//private final int clientInterestSet = SelectionKey.OP_READ | SelectionKey.OP_WRITE;
2227

2328
public static void main(String[] args) {
@@ -37,6 +42,8 @@ public static void main(String[] args) {
3742
if (args.length == 3)
3843
debug = true;
3944

45+
s.printStatus();
46+
4047
tpm = new ThreadPoolManager(threadPoolSize, debug);
4148

4249
try {
@@ -49,6 +56,13 @@ public static void main(String[] args) {
4956

5057
}
5158

59+
private void printStatus() {
60+
// log stats to the console every interval
61+
Timer timer = new Timer();
62+
int interval = 5000;
63+
timer.schedule(serverLogger, interval, interval);
64+
}
65+
5266
private void setupServerSocket(int portNumber) throws IOException {
5367
System.out.println("Setting up server...");
5468
ssChannel = ServerSocketChannel.open();
@@ -64,7 +78,7 @@ private void executeServerLoop() throws IOException {
6478
if (socketChannel != null)
6579
registerIncomingKey(socketChannel);
6680
// Find read ready channels
67-
if (serverSelector.selectNow() > 0) {
81+
if (serverSelector.select(1000) > 0) {
6882
Set<SelectionKey> selectedKeys = serverSelector.selectedKeys();
6983

7084
for (SelectionKey key : selectedKeys) {
@@ -74,14 +88,13 @@ private void executeServerLoop() throws IOException {
7488
tpm.assignTask();
7589
}
7690
selectedKeys.remove(key);
77-
}
78-
91+
}
7992
}
8093
}
8194
}
82-
95+
8396
private void createTask(SelectionKey key) throws IOException {
84-
ServerTask task = new ServerTask(key);
97+
ServerTask task = new ServerTask(key, serverLogger, debug);
8598
tpm.addTaskToQueue(task);
8699
}
87100

@@ -91,6 +104,8 @@ private void registerIncomingKey(SocketChannel socketChannel) throws IOException
91104
try {
92105
socketChannel.configureBlocking(false);
93106
SelectionKey key = socketChannel.register(serverSelector, SelectionKey.OP_READ);
107+
ThroughputLogger logger = serverLogger.addClient();
108+
key.attach(logger);
94109
} catch (ClosedChannelException cce) {
95110
System.out.println(cce.getMessage());
96111
}
3.38 KB
Binary file not shown.

cs455/scaling/tasks/ClientTask.java

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cs455.scaling.tasks;
22

33
import cs455.scaling.util.HashGenerator;
4+
import cs455.scaling.util.ClientLogger;
45
import java.nio.channels.SocketChannel;
56
import java.nio.ByteBuffer;
67
import java.io.IOException;
@@ -13,26 +14,32 @@ public class ClientTask extends Thread {
1314
private ByteBuffer buf;
1415
private int messageRate;
1516
private LinkedList<String> packetHashcodes;
17+
private ClientLogger logger;
1618
private final HashGenerator hashGen;
19+
private boolean debug;
1720

18-
public ClientTask(SocketChannel socketChannel, int messageRate) {
21+
public ClientTask(SocketChannel socketChannel, int messageRate,
22+
ClientLogger logger, boolean debug) {
1923
this.socketChannel = socketChannel;
2024
buf = ByteBuffer.allocate(8000);
2125
this.messageRate = messageRate;
2226
packetHashcodes = new LinkedList<String>();
2327
hashGen = new HashGenerator();
28+
this.logger = logger;
29+
this.debug = debug;
2430
}
2531

2632
public void run() {
2733
while(!isInterrupted()) {
28-
// test
2934
try {
3035
sendMessage();
31-
Thread.sleep(1000 / messageRate);
36+
receiveMessage();
37+
Thread.sleep(1000 / messageRate);
3238
} catch (IOException ioe) {
3339
System.out.println(ioe.getMessage());
40+
System.exit(0);
3441
} catch (InterruptedException ie) {
35-
System.out.println(ie.getMessage());
42+
System.out.println(ie.getMessage());
3643
}
3744
}
3845
}
@@ -47,11 +54,39 @@ private void sendMessage() throws IOException {
4754
byte[] message = generateRandomBytes();
4855
String messageHash = hashGen.SHA1FromBytes(message);
4956
packetHashcodes.add(messageHash);
50-
buf.clear();
51-
buf.put(message);
52-
buf.flip();
53-
54-
while(buf.hasRemaining())
55-
socketChannel.write(buf);
57+
58+
ByteBuffer buffer = ByteBuffer.wrap(message);
59+
socketChannel.write(buffer);
60+
61+
logger.addSent();
62+
}
63+
64+
private void receiveMessage() {
65+
try {
66+
int bytesRead = socketChannel.read(buf);
67+
if (bytesRead <= 0) { // nothing has been read
68+
if (debug)
69+
System.out.println("Nothing was read. " + bytesRead);
70+
return;
71+
}
72+
byte[] message = new byte[bytesRead];
73+
buf.flip();
74+
buf.get(message);
75+
checkHash(message);
76+
buf.clear();
77+
} catch (IOException ioe) {
78+
System.out.println(ioe.getMessage());
79+
}
80+
}
81+
82+
private void checkHash(byte[] message) {
83+
String messageHash = new String(message);
84+
int hashIndex = packetHashcodes.indexOf(messageHash);
85+
if (hashIndex != -1) { // found hash
86+
packetHashcodes.remove(hashIndex);
87+
logger.addReceived();
88+
if (debug)
89+
System.out.println("Found matching hash: " + messageHash);
90+
}
5691
}
5792
}
2.8 KB
Binary file not shown.
Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,49 @@
11
package cs455.scaling.tasks;
22

33
import cs455.scaling.util.HashGenerator;
4+
import cs455.scaling.util.ThroughputLogger;
5+
import cs455.scaling.util.ServerLogger;
46
import java.nio.channels.SelectionKey;
57
import java.nio.channels.SocketChannel;
68
import java.nio.ByteBuffer;
79
import java.io.IOException;
810

911
public class ServerTask implements Runnable {
1012

13+
private SelectionKey key;
1114
private final HashGenerator hashGen;
1215
private SocketChannel socketChannel;
1316
private ByteBuffer buf;
17+
private ThroughputLogger logger;
18+
private ServerLogger serverLogger;
19+
private boolean debug;
1420

15-
public ServerTask(SelectionKey key) {
21+
public ServerTask(SelectionKey key, ServerLogger serverLogger, boolean debug) {
1622
hashGen = new HashGenerator();
1723
socketChannel = (SocketChannel) key.channel();
1824
buf = ByteBuffer.allocate(8000);
25+
logger = (ThroughputLogger) key.attachment();
26+
this.serverLogger = serverLogger;
27+
this.debug = debug;
28+
this.key = key;
29+
key.interestOps(SelectionKey.OP_WRITE);
1930
}
2031

2132
public void run() {
22-
try {
33+
try {
2334
int bytesRead = socketChannel.read(buf);
35+
if (bytesRead == -1) {
36+
System.out.println("Connection terminated by the client.");
37+
return;
38+
}
39+
2440
byte[] message = new byte[bytesRead];
2541
buf.flip();
2642
buf.get(message);
43+
2744
String messageHash = getHash(message);
28-
System.out.println(messageHash);
29-
buf.clear();
30-
//replyWithHash(messageHash);
45+
buf.clear();
46+
replyWithHash(messageHash);
3147
} catch (IOException ioe) {
3248
System.out.println(ioe.getMessage());
3349
}
@@ -39,11 +55,17 @@ private String getHash(byte[] message) {
3955

4056
private void replyWithHash(String messageHash) throws IOException {
4157
byte[] replyMessage = messageHash.getBytes();
42-
buf.put(replyMessage);
43-
buf.flip();
4458

45-
while(buf.hasRemaining())
46-
socketChannel.write(buf);
59+
ByteBuffer buffer = ByteBuffer.wrap(replyMessage);
60+
socketChannel.write(buffer);
61+
62+
if (debug)
63+
System.out.println("Wrote reply " + messageHash + " to socketChannel");
64+
65+
logger.processMessage();
66+
serverLogger.processMessage();
67+
68+
key.interestOps(SelectionKey.OP_READ);
4769
}
4870

4971
}

cs455/scaling/tasks/TestTask.class

1.48 KB
Binary file not shown.
1.93 KB
Binary file not shown.

0 commit comments

Comments
 (0)