jaroslav@238
|
1 |
/**
|
jaroslav@238
|
2 |
* The MIT License (MIT)
|
jaroslav@238
|
3 |
*
|
jaroslav@238
|
4 |
* Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@238
|
5 |
*
|
jaroslav@238
|
6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
jaroslav@238
|
7 |
* of this software and associated documentation files (the "Software"), to deal
|
jaroslav@238
|
8 |
* in the Software without restriction, including without limitation the rights
|
jaroslav@238
|
9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
jaroslav@238
|
10 |
* copies of the Software, and to permit persons to whom the Software is
|
jaroslav@238
|
11 |
* furnished to do so, subject to the following conditions:
|
jaroslav@238
|
12 |
*
|
jaroslav@238
|
13 |
* The above copyright notice and this permission notice shall be included in
|
jaroslav@238
|
14 |
* all copies or substantial portions of the Software.
|
jaroslav@238
|
15 |
*
|
jaroslav@238
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
jaroslav@238
|
17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
jaroslav@238
|
18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
jaroslav@238
|
19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
jaroslav@238
|
20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
jaroslav@238
|
21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
jaroslav@238
|
22 |
* THE SOFTWARE.
|
jaroslav@238
|
23 |
*/
|
jaroslav@238
|
24 |
package org.apidesign.demo.chat.server;
|
jaroslav@238
|
25 |
|
jaroslav@238
|
26 |
import java.net.Inet4Address;
|
jaroslav@238
|
27 |
import java.net.InterfaceAddress;
|
jaroslav@238
|
28 |
import java.net.NetworkInterface;
|
jaroslav@238
|
29 |
import java.net.URI;
|
jaroslav@238
|
30 |
import java.util.Enumeration;
|
jaroslav@240
|
31 |
import org.glassfish.grizzly.PortRange;
|
jaroslav@238
|
32 |
import org.glassfish.grizzly.http.server.HttpServer;
|
jaroslav@240
|
33 |
import org.glassfish.grizzly.http.server.NetworkListener;
|
jaroslav@240
|
34 |
import org.glassfish.grizzly.websockets.WebSocketAddOn;
|
jaroslav@240
|
35 |
import org.glassfish.grizzly.websockets.WebSocketEngine;
|
jaroslav@238
|
36 |
|
jaroslav@238
|
37 |
/** Starts REST server based on Jersey.
|
jaroslav@238
|
38 |
*/
|
jaroslav@240
|
39 |
final class Main {
|
jaroslav@238
|
40 |
public static void main(String... args) throws Exception {
|
jaroslav@240
|
41 |
HttpServer server = HttpServer.createSimpleServer(null, new PortRange(8080, 8080));
|
jaroslav@240
|
42 |
final WebSocketAddOn addon = new WebSocketAddOn();
|
jaroslav@240
|
43 |
for (NetworkListener listener : server.getListeners()) {
|
jaroslav@240
|
44 |
listener.registerAddOn(addon);
|
jaroslav@240
|
45 |
}
|
jaroslav@240
|
46 |
WebSocketEngine.getEngine().register("", "/chat", new ChatServerResource());
|
jaroslav@240
|
47 |
server.start();
|
jaroslav@238
|
48 |
URI u = new URI("http://0.0.0.0:8080/");
|
jaroslav@238
|
49 |
System.err.println("Server running on following IP addresses:");
|
jaroslav@238
|
50 |
dumpIPs();
|
jaroslav@238
|
51 |
System.err.println("Press Enter to shutdown the server");
|
jaroslav@238
|
52 |
System.in.read();
|
jaroslav@238
|
53 |
server.stop();
|
jaroslav@238
|
54 |
}
|
jaroslav@238
|
55 |
|
jaroslav@238
|
56 |
private static void dumpIPs() throws Exception {
|
jaroslav@238
|
57 |
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
|
jaroslav@238
|
58 |
while (en.hasMoreElements()) {
|
jaroslav@238
|
59 |
NetworkInterface n = en.nextElement();
|
jaroslav@238
|
60 |
if (n.isUp()) {
|
jaroslav@238
|
61 |
for (InterfaceAddress i : n.getInterfaceAddresses()) {
|
jaroslav@238
|
62 |
if (i.getAddress() instanceof Inet4Address) {
|
jaroslav@238
|
63 |
System.err.println(n.getName() + ": " + i.getAddress());
|
jaroslav@238
|
64 |
}
|
jaroslav@238
|
65 |
}
|
jaroslav@238
|
66 |
}
|
jaroslav@238
|
67 |
}
|
jaroslav@238
|
68 |
}
|
jaroslav@238
|
69 |
}
|