-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEchoServer.java
More file actions
114 lines (101 loc) · 3.72 KB
/
EchoServer.java
File metadata and controls
114 lines (101 loc) · 3.72 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
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.Set;
import java.util.Date;
import java.text.SimpleDateFormat;
public class EchoServer extends Thread {
//An EchoServer thread uses a ServerSocketChannel to listen for any new connection request on the local port 5672 for 10 seconds once it gets started
//It uses a non-blocking approach
private SimpleDateFormat formatter;
private ServerSocketChannel serverSocketChannel;
private Selector selector;
public EchoServer() {
formatter = new SimpleDateFormat("[hh:mm:ss]");
try {
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
serverSocketChannel.socket().bind(new InetSocketAddress(5672));
selector = Selector.open();
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT, null);
}
catch (IOException e){
e.printStackTrace();
}
}
public void run() {
long startTime = System.currentTimeMillis();
while (System.currentTimeMillis() - startTime < 10000) {
try {
selector.select();
}
catch (IOException e){
e.printStackTrace();
}
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectedKeys.iterator();
while (iterator.hasNext()) {
SelectionKey nextKey = iterator.next();
if(!nextKey.isValid())
continue;
if (nextKey.isAcceptable()) {
accept();
System.out.println(formatter.format(new Date()) + "[SERVER] New connection request accepted.");
}
if(nextKey.isReadable()) {
System.out.println(formatter.format(new Date()) + "[SERVER] New message received.");
readRequest(nextKey);
}
if(nextKey.isWritable()) {
writeRequest(nextKey);
}
iterator.remove();
}
}
close();
}
private void close() {
try{
selector.close();
serverSocketChannel.close();
}
catch(IOException e){
e.printStackTrace();
}
}
private void writeRequest(SelectionKey newWriteRequest){
SocketChannel client = (SocketChannel) newWriteRequest.channel();
EchoByteBuffer attachment = (EchoByteBuffer) newWriteRequest.attachment();
try {
client.write(attachment.getByteBuffer());
}
catch (Exception e){
e.printStackTrace();
}
}
private void readRequest(SelectionKey newReadRequest){
SocketChannel client = (SocketChannel) newReadRequest.channel();
EchoByteBuffer attachment = (EchoByteBuffer) newReadRequest.attachment();
try {
attachment.getByteBuffer().clear();
client.read(attachment.getByteBuffer());
}
catch (IOException e) {
e.printStackTrace();
}
if(attachment.updateOnRead()){
newReadRequest.interestOps(SelectionKey.OP_WRITE);
}
}
private void accept(){
try {
SocketChannel client = serverSocketChannel.accept();
client.configureBlocking(false);
client.register(selector, SelectionKey.OP_READ, new EchoByteBuffer());
}
catch(Exception e) {
e.printStackTrace();
}
}
}