Skip to content

Commit 2b8dd96

Browse files
authored
Merge pull request #1442 from TooTallNate/socket-activation
Socket activation
2 parents 3b29042 + e5253dc commit 2b8dd96

File tree

4 files changed

+161
-2
lines changed

4 files changed

+161
-2
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (c) 2010-2020 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
import java.io.IOException;
27+
import java.nio.ByteBuffer;
28+
import java.nio.channels.ServerSocketChannel;
29+
import java.util.Collections;
30+
import java.util.concurrent.atomic.AtomicInteger;
31+
import org.java_websocket.WebSocket;
32+
import org.java_websocket.drafts.Draft;
33+
import org.java_websocket.drafts.Draft_6455;
34+
import org.java_websocket.handshake.ClientHandshake;
35+
import org.java_websocket.server.WebSocketServer;
36+
37+
/**
38+
* This is a "smart" chat server which will exit when no more clients are left, in order to demonstrate socket activation
39+
*/
40+
public class SocketActivation extends WebSocketServer {
41+
42+
AtomicInteger clients = new AtomicInteger(0);
43+
44+
public SocketActivation(ServerSocketChannel chan) {
45+
super(chan);
46+
}
47+
48+
@Override
49+
public void onOpen(WebSocket conn, ClientHandshake handshake) {
50+
conn.send("Welcome to the server!"); //This method sends a message to the new client
51+
broadcast("new connection: " + handshake.getResourceDescriptor()); //This method sends a message to all clients connected
52+
if(clients.get() == 0) {
53+
broadcast("You are the first client to join");
54+
}
55+
System.out.println(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " entered the room!");
56+
clients.incrementAndGet();
57+
}
58+
59+
@Override
60+
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
61+
broadcast(conn + " has left the room!");
62+
System.out.println(conn + " has left the room!");
63+
if(clients.decrementAndGet() <= 0) {
64+
System.out.println("No more clients left, exiting");
65+
System.exit(0);
66+
}
67+
}
68+
69+
@Override
70+
public void onMessage(WebSocket conn, String message) {
71+
broadcast(message);
72+
System.out.println(conn + ": " + message);
73+
}
74+
75+
@Override
76+
public void onMessage(WebSocket conn, ByteBuffer message) {
77+
broadcast(message.array());
78+
System.out.println(conn + ": " + message);
79+
}
80+
81+
82+
public static void main(String[] args) throws InterruptedException, IOException {
83+
if(System.inheritedChannel() == null) {
84+
System.err.println("System.inheritedChannel() is null, make sure this program is started with file descriptor zero being a listening socket");
85+
System.exit(1);
86+
}
87+
SocketActivation s = new SocketActivation((ServerSocketChannel)System.inheritedChannel());
88+
s.start();
89+
System.out.println(">>>> SocketActivation started on port: " + s.getPort() + " <<<<");
90+
}
91+
92+
@Override
93+
public void onError(WebSocket conn, Exception ex) {
94+
ex.printStackTrace();
95+
}
96+
97+
@Override
98+
public void onStart() {
99+
System.out.println("Server started!");
100+
}
101+
102+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[Unit]
2+
Description=Java-WebSocket systemd activation demo service
3+
After=network.target jws-activation.socket
4+
Requires=jws-activation.socket
5+
6+
[Service]
7+
Type=simple
8+
# Place the command for running SocketActivation.java in file "$HOME"/jws_activation_command:
9+
ExecStart=/bin/sh %h/jws_activation_run
10+
TimeoutStopSec=5
11+
StandardError=journal
12+
StandardOutput=journal
13+
# This is very important - systemd will pass the socket as file descriptor zero, which is what Java expects
14+
StandardInput=socket
15+
16+
[Install]
17+
WantedBy=default.target
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[Unit]
2+
Description=Java-WebSocket systemd activation demo socket
3+
PartOf=jws-activation.service
4+
5+
[Socket]
6+
ListenStream=127.0.0.1:9999
7+
8+
[Install]
9+
WantedBy=sockets.target

src/main/java/org/java_websocket/server/WebSocketServer.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,30 @@ public WebSocketServer(InetSocketAddress address, int decodercount, List<Draft>
181181
this(address, decodercount, drafts, new HashSet<WebSocket>());
182182
}
183183

184+
// Small internal helper function to get around limitations of Java constructors.
185+
private static InetSocketAddress checkAddressOfExistingChannel(ServerSocketChannel existingChannel) {
186+
assert existingChannel.isOpen();
187+
SocketAddress addr;
188+
try {
189+
addr = existingChannel.getLocalAddress();
190+
} catch (IOException e) {
191+
throw new IllegalArgumentException("Could not get address of channel passed to WebSocketServer, make sure it is bound", e);
192+
}
193+
if (addr == null) {
194+
throw new IllegalArgumentException("Could not get address of channel passed to WebSocketServer, make sure it is bound");
195+
}
196+
return (InetSocketAddress)addr;
197+
}
198+
199+
/**
200+
* @param existingChannel An already open and bound server socket channel, which this server will use.
201+
* For example, it can be System.inheritedChannel() to implement socket activation.
202+
*/
203+
public WebSocketServer(ServerSocketChannel existingChannel) {
204+
this(checkAddressOfExistingChannel(existingChannel));
205+
this.server = existingChannel;
206+
}
207+
184208
/**
185209
* Creates a WebSocketServer that will attempt to bind/listen on the given <var>address</var>, and
186210
* comply with <code>Draft</code> version <var>draft</var>.
@@ -575,15 +599,22 @@ private void doWrite(SelectionKey key) throws WrappedIOException {
575599
private boolean doSetupSelectorAndServerThread() {
576600
selectorthread.setName("WebSocketSelector-" + selectorthread.getId());
577601
try {
578-
server = ServerSocketChannel.open();
602+
if (server == null) {
603+
server = ServerSocketChannel.open();
604+
// If 'server' is not null, that means WebSocketServer was created from existing channel.
605+
}
579606
server.configureBlocking(false);
580607
ServerSocket socket = server.socket();
581608
int receiveBufferSize = getReceiveBufferSize();
582609
if (receiveBufferSize > 0) {
583610
socket.setReceiveBufferSize(receiveBufferSize);
584611
}
585612
socket.setReuseAddress(isReuseAddr());
586-
socket.bind(address, getMaxPendingConnections());
613+
// Socket may be already bound, if an existing channel was passed to constructor.
614+
// In this case we cannot modify backlog size from pure Java code, so leave it as is.
615+
if (!socket.isBound()) {
616+
socket.bind(address, getMaxPendingConnections());
617+
}
587618
selector = Selector.open();
588619
server.register(selector, server.validOps());
589620
startConnectionLostTimer();

0 commit comments

Comments
 (0)