-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
227 lines (178 loc) · 8.46 KB
/
Server.java
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
import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class Server {
//Listening port for incoming connections
public static int PORT = 5672;
//Maximum size of intermediate read/write
//This will be the most number of bytes we read in one go
public static int SOCKET_BUFFER_SIZE = 1024;
//Delay socket writes, sleep 100ms between each write
//This can be used to test client tollerance for half-filled buffers,
//i.e. to try if clients can handle to only receive parts of frames
public static boolean DELAYED_WRITE = false;
public static void main(String[] args) {
//Prevent the compiler from complaining about possibly uninitialized variables
ServerSocketChannel serverSocketChannel = null;
Selector selector = null;
try {
//Create the NIO socket server and bind it
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(PORT));
//The NIO Selector requires nonblocking sockets
serverSocketChannel.configureBlocking(false);
//Create a new NIO selector
selector = Selector.open();
} catch (IOException e) {
System.err.println("Failed to create or bind socket:");
System.err.println(e.toString());
System.exit(1);
}
//Now we've got a listening socket ready and can receive inbound connections
System.out.println("Listening on port " + PORT);
try {
//Register the listening socket in the selector so we'll get notified on
//incoming connections
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
} catch(ClosedChannelException e) {
System.err.println("Failed to create selector:");
System.err.println(e.toString());
System.exit(1);
}
//Master loop that runs when interesting stuff happens
while(true) {
try {
//If select() returns zero, nothing interesting has happened
//Might be the case if wakeUp() is called from another thread
//We do however request to wake up every second in order to call all
//AMQPConnection objects so they can perform periodical tasks
if (selector.select((long) 1000) < 1) {
//Get all SelectionKeys which are currently being watched
//I.e. those that are connected to the server
Set<SelectionKey> active = selector.keys();
//Loop over all SelektionKeys
for(SelectionKey sk : active) {
//Get the AMQPConnection associated with the client
AMQPConnection conn = (AMQPConnection) sk.attachment();
//conn may stil be null before the connection has been properly
//initialized
if (conn != null) conn.periodical();
//Update the interest set in case the periodical call has put
//some data in the buffer
if (conn != null) {
sk.channel().register(selector, conn.getSelectorRegisterMask(), conn);
}
}
}
//Get an interator over all interesting events
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> keyIterator = selectedKeys.iterator();
//Iterate over all interesting channels
while(keyIterator.hasNext()) {
//Get the current SelectionKey
SelectionKey selectionKey = keyIterator.next();
//Did we receive a new connecting client?
if (selectionKey.isAcceptable()) {
//Accept the incoming connection
SocketChannel sc = ((ServerSocketChannel) selectionKey.channel()).accept();
//NIO Selector requires non-blocking channels
sc.configureBlocking(false);
//Our AMQPConnection which will be responsible for handling this
//connections
AMQPConnection amqpConnection = new AMQPConnection(sc, args);
//Register the channel with the selector to get notified when
//interesting events happen, and attach our object to it
sc.register(selector, amqpConnection.getSelectorRegisterMask(), amqpConnection);
//Print out the connection info
SocketAddress socketAddress = sc.getRemoteAddress();
if (socketAddress != null) {
System.out.println("Accepted connection from " + socketAddress.toString());
}
}
//Did a socket get connected?
if (selectionKey.isConnectable()) {
//Currently not implemented as the project never initiates outgoing connections
}
//Did we receive some data?
if (selectionKey.isReadable()) {
//Get the AMQPConnection associated with this connection
AMQPConnection amqpConnection = (AMQPConnection) selectionKey.attachment();
//Get the SocketChannel which received data
SocketChannel readSocketChannel = (SocketChannel) selectionKey.channel();
//Allocate a ByteBuffer
ByteBuffer readByteBuffer = ByteBuffer.allocate(SOCKET_BUFFER_SIZE);
//Attempt to read data into the byte buffer
//If the read() method returns -1, we have a connection error
if (readSocketChannel.read(readByteBuffer) == -1) {
keyIterator.remove();
readSocketChannel.close();
amqpConnection.clientDisconnect();
continue;
}
//Flip the buffer into reading mode
readByteBuffer.flip();
//Deliver the protocol data to the AMQPConnection buffer
amqpConnection.deliverData(readByteBuffer);
//Notify the AMQPConnection object that we have an updated state
amqpConnection.updateState();
//Update the selector interest set
readSocketChannel.register(selector, amqpConnection.getSelectorRegisterMask(), amqpConnection);
}
//Are we ready to write data?
if (selectionKey.isWritable()) {
//Get the AMQPConnection associated with this connection
AMQPConnection amqpConnection = (AMQPConnection) selectionKey.attachment();
//Check that we actually have data to write
//This check should never fail
if (amqpConnection.queue_outgoing.length() == 0) {
System.err.println("Attemping to write data to channel without any actual data to write");
System.exit(1);
}
//Get the SocketChannel which received data
SocketChannel writeSocketChannel = (SocketChannel) selectionKey.channel();
//Allocate a write ByteBuffer
ByteBuffer write = ByteBuffer.allocate(SOCKET_BUFFER_SIZE);
//Populate the write buffer with pending outgoing protocol data
write.put(amqpConnection.sendData(SOCKET_BUFFER_SIZE));
//Flip the ByteBuffer object to read mode...
write.flip();
//...and attempt to write data to the socket
int writeStatus = writeSocketChannel.write(write);
//Did the send fail?
if (writeStatus == -1) {
System.out.println("Lost client when attempting to send data");
writeSocketChannel.close();
amqpConnection.clientDisconnect();
continue;
}
//Tell the object how much data has been sent via the socket
amqpConnection.confirmSent(writeStatus);
//Sleep a while
if (DELAYED_WRITE) {
try {
Thread.sleep(100);
} catch(InterruptedException e){}
}
//Notify the AMQPConnection object that we have an updated state
amqpConnection.updateState();
//Update the selector interest set
writeSocketChannel.register(selector, amqpConnection.getSelectorRegisterMask(), amqpConnection);
}
//Check if the client should be disconnected
AMQPConnection amqpConnection = (AMQPConnection) selectionKey.attachment();
if (amqpConnection != null && amqpConnection.queue_outgoing.length() == 0 && amqpConnection.status == AMQPConnection.AMQPConnectionState.DISCONNECT) {
selectionKey.channel().close();
System.out.println("Disconnected a client");
continue;
}
keyIterator.remove();
}
} catch(IOException e) {
System.err.println("select() failure:");
System.err.println(e.toString());
}
}
}
}