Skip to content

Commit 97c7080

Browse files
committed
Whitespace fixes, mostly. Also turned a few variable into private instance variables. Also removed 1 layer of try/catching inside the WebSocketServer's 'run()' loop.
1 parent 18ddfcf commit 97c7080

File tree

2 files changed

+62
-71
lines changed

2 files changed

+62
-71
lines changed

src/net/tootallnate/websocket/WebSocket.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public final class WebSocket {
8585
private BlockingQueue<ByteBuffer> bufferQueue;
8686
/**
8787
* Lock object to ensure that data is sent from the bufferQueue in
88-
* the proper order
88+
* the proper order.
8989
*/
9090
private Object bufferQueueMutex = new Object();
9191

@@ -101,9 +101,7 @@ public final class WebSocket {
101101
* @param listener The {@link WebSocketListener} to notify of events when
102102
* they occur.
103103
*/
104-
WebSocket(SocketChannel socketChannel, BlockingQueue<ByteBuffer> bufferQueue,
105-
WebSocketListener listener)
106-
{
104+
WebSocket(SocketChannel socketChannel, BlockingQueue<ByteBuffer> bufferQueue, WebSocketListener listener) {
107105
this.socketChannel = socketChannel;
108106
this.bufferQueue = bufferQueue;
109107
this.handshakeComplete = false;
@@ -223,8 +221,9 @@ private void recieveFrame() {
223221
String textFrame = null;
224222
// currentFrame will be null if END_OF_FRAME was send directly after
225223
// START_OF_FRAME, thus we will send 'null' as the sent message.
226-
if (this.currentFrame != null)
224+
if (this.currentFrame != null) {
227225
textFrame = new String(this.currentFrame.array(), UTF8_CHARSET);
226+
}
228227
this.wsl.onMessage(this, textFrame);
229228

230229
} else { // Regular frame data, add to current frame buffer
@@ -299,7 +298,6 @@ else if ((h.length>=12 && h[h.length-12] == CR
299298
&& h[h.length-2] == CR
300299
&& h[h.length-1] == LF) && !(new String(this.remoteHandshake.array(), UTF8_CHARSET).contains("Sec")) ||
301300
(h.length==23 && h[h.length-1] == 0) ) {
302-
303301
completeHandshake(null);
304302
}
305303
}

src/net/tootallnate/websocket/WebSocketServer.java

Lines changed: 58 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public abstract class WebSocketServer implements Runnable, WebSocketListener {
4747
* The socket channel for this WebSocket server.
4848
*/
4949
private ServerSocketChannel server;
50+
/**
51+
* The 'Selector' used to get event keys from the underlying socket.
52+
*/
53+
private Selector selector;
5054
/**
5155
* The Draft of the WebSocket protocol the Server is running adhering to.
5256
*/
@@ -169,18 +173,6 @@ public Draft getDraft() {
169173
return draft;
170174
}
171175

172-
/**
173-
* @return A BlockingQueue that should be used by a WebSocket
174-
* to hold data that is waiting to be sent to the client.
175-
* The default implementation returns an unbounded
176-
* LinkedBlockingQueue, but you may choose to override
177-
* this to return a bounded queue to protect against
178-
* running out of memory.
179-
*/
180-
protected BlockingQueue<ByteBuffer> newBufferQueue() {
181-
return new LinkedBlockingQueue<ByteBuffer>();
182-
}
183-
184176

185177
// Runnable IMPLEMENTATION /////////////////////////////////////////////////
186178
public void run() {
@@ -189,71 +181,72 @@ public void run() {
189181
server.configureBlocking(false);
190182
server.socket().bind(new java.net.InetSocketAddress(port));
191183

192-
Selector selector = Selector.open();
184+
selector = Selector.open();
193185
server.register(selector, server.validOps());
186+
} catch (IOException ex) {
187+
ex.printStackTrace();
188+
return;
189+
}
194190

195-
while(true) {
196-
try {
197-
selector.select(100L);
198-
Set<SelectionKey> keys = selector.selectedKeys();
199-
Iterator<SelectionKey> i = keys.iterator();
200-
201-
while(i.hasNext()) {
202-
SelectionKey key = i.next();
203-
204-
// Remove the current key
205-
i.remove();
206-
207-
// if isAcceptable == true
208-
// then a client required a connection
209-
if (key.isAcceptable()) {
210-
SocketChannel client = server.accept();
211-
client.configureBlocking(false);
212-
WebSocket c = new WebSocket(client, newBufferQueue(), this);
213-
client.register(selector, SelectionKey.OP_READ, c);
214-
}
215-
216-
// if isReadable == true
217-
// then the server is ready to read
218-
if (key.isReadable()) {
219-
WebSocket conn = (WebSocket)key.attachment();
220-
conn.handleRead();
221-
}
191+
while(true) {
192+
try {
193+
selector.select();
194+
Set<SelectionKey> keys = selector.selectedKeys();
195+
Iterator<SelectionKey> i = keys.iterator();
196+
197+
while(i.hasNext()) {
198+
SelectionKey key = i.next();
199+
200+
// Remove the current key
201+
i.remove();
202+
203+
// if isAcceptable == true
204+
// then a client required a connection
205+
if (key.isAcceptable()) {
206+
SocketChannel client = server.accept();
207+
client.configureBlocking(false);
208+
WebSocket c = new WebSocket(client, new LinkedBlockingQueue<ByteBuffer>(), this);
209+
client.register(selector, SelectionKey.OP_READ, c);
210+
}
222211

223-
// if isWritable == true
224-
// then we need to send the rest of the data to the client
225-
if (key.isWritable()) {
226-
WebSocket conn = (WebSocket)key.attachment();
227-
if (conn.handleWrite()) {
228-
conn.socketChannel().register(selector,
229-
SelectionKey.OP_READ, conn);
230-
}
231-
}
212+
// if isReadable == true
213+
// then the server is ready to read
214+
if (key.isReadable()) {
215+
WebSocket conn = (WebSocket)key.attachment();
216+
conn.handleRead();
232217
}
233218

234-
for (WebSocket conn : this.connections) {
235-
// We have to do this check here, and not in the thread that
236-
// adds the buffered data to the WebSocket, because the
237-
// Selector is not thread-safe, and can only be accessed
238-
// by this thread.
239-
if (conn.hasBufferedData()) {
219+
// if isWritable == true
220+
// then we need to send the rest of the data to the client
221+
if (key.isWritable()) {
222+
WebSocket conn = (WebSocket)key.attachment();
223+
if (conn.handleWrite()) {
240224
conn.socketChannel().register(selector,
241-
SelectionKey.OP_READ | SelectionKey.OP_WRITE, conn);
225+
SelectionKey.OP_READ, conn);
242226
}
243227
}
228+
}
244229

245-
} catch (IOException e) {
246-
e.printStackTrace();
247-
} catch (RuntimeException e) {
248-
e.printStackTrace();
230+
for (WebSocket conn : this.connections) {
231+
// We have to do this check here, and not in the thread that
232+
// adds the buffered data to the WebSocket, because the
233+
// Selector is not thread-safe, and can only be accessed
234+
// by this thread.
235+
if (conn.hasBufferedData()) {
236+
conn.socketChannel().register(selector,
237+
SelectionKey.OP_READ | SelectionKey.OP_WRITE, conn);
238+
}
249239
}
240+
} catch (IOException ex) {
241+
ex.printStackTrace();
242+
} catch (RuntimeException ex) {
243+
ex.printStackTrace();
244+
} catch (NoSuchAlgorithmException ex) {
245+
ex.printStackTrace();
250246
}
251-
} catch (IOException ex) {
252-
ex.printStackTrace();
253-
} catch (NoSuchAlgorithmException e) {
254-
// TODO Auto-generated catch block
255-
e.printStackTrace();
256247
}
248+
249+
//System.err.println("WebSocketServer thread ended!");
257250
}
258251

259252
/**

0 commit comments

Comments
 (0)