jaroslav@238: /** jaroslav@238: * The MIT License (MIT) jaroslav@238: * jaroslav@238: * Copyright (C) 2013 Jaroslav Tulach jaroslav@238: * jaroslav@238: * Permission is hereby granted, free of charge, to any person obtaining a copy jaroslav@238: * of this software and associated documentation files (the "Software"), to deal jaroslav@238: * in the Software without restriction, including without limitation the rights jaroslav@238: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell jaroslav@238: * copies of the Software, and to permit persons to whom the Software is jaroslav@238: * furnished to do so, subject to the following conditions: jaroslav@238: * jaroslav@238: * The above copyright notice and this permission notice shall be included in jaroslav@238: * all copies or substantial portions of the Software. jaroslav@238: * jaroslav@238: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR jaroslav@238: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, jaroslav@238: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE jaroslav@238: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER jaroslav@238: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, jaroslav@238: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN jaroslav@238: * THE SOFTWARE. jaroslav@238: */ jaroslav@238: package org.apidesign.demo.chat.server; jaroslav@238: jaroslav@238: import java.net.Inet4Address; jaroslav@238: import java.net.InterfaceAddress; jaroslav@238: import java.net.NetworkInterface; jaroslav@238: import java.net.URI; jaroslav@238: import java.util.Enumeration; jaroslav@240: import org.glassfish.grizzly.PortRange; jaroslav@238: import org.glassfish.grizzly.http.server.HttpServer; jaroslav@240: import org.glassfish.grizzly.http.server.NetworkListener; jaroslav@240: import org.glassfish.grizzly.websockets.WebSocketAddOn; jaroslav@240: import org.glassfish.grizzly.websockets.WebSocketEngine; jaroslav@238: jaroslav@238: /** Starts REST server based on Jersey. jaroslav@238: */ jaroslav@240: final class Main { jaroslav@238: public static void main(String... args) throws Exception { jaroslav@240: HttpServer server = HttpServer.createSimpleServer(null, new PortRange(8080, 8080)); jaroslav@240: final WebSocketAddOn addon = new WebSocketAddOn(); jaroslav@240: for (NetworkListener listener : server.getListeners()) { jaroslav@240: listener.registerAddOn(addon); jaroslav@240: } jaroslav@240: WebSocketEngine.getEngine().register("", "/chat", new ChatServerResource()); jaroslav@240: server.start(); jaroslav@238: URI u = new URI("http://0.0.0.0:8080/"); jaroslav@238: System.err.println("Server running on following IP addresses:"); jaroslav@238: dumpIPs(); jaroslav@238: System.err.println("Press Enter to shutdown the server"); jaroslav@238: System.in.read(); jaroslav@238: server.stop(); jaroslav@238: } jaroslav@238: jaroslav@238: private static void dumpIPs() throws Exception { jaroslav@238: Enumeration en = NetworkInterface.getNetworkInterfaces(); jaroslav@238: while (en.hasMoreElements()) { jaroslav@238: NetworkInterface n = en.nextElement(); jaroslav@238: if (n.isUp()) { jaroslav@238: for (InterfaceAddress i : n.getInterfaceAddresses()) { jaroslav@238: if (i.getAddress() instanceof Inet4Address) { jaroslav@238: System.err.println(n.getName() + ": " + i.getAddress()); jaroslav@238: } jaroslav@238: } jaroslav@238: } jaroslav@238: } jaroslav@238: } jaroslav@238: }