public ObjectStore(Server server) { this.server = server; server.addMessageListener(serverEventHandler, RemoteObjectDefMessage.class, RemoteMethodCallMessage.class, RemoteMethodReturnMessage.class); server.addConnectionListener(serverEventHandler); }
public synchronized void close() { if( !isRunning ) { return; } // Gracefully let any connections know that the server is // going down. Without this, their connections will simply // error out. for( HostedConnection conn : server.getConnections() ) { conn.close("Server is shutting down."); } try { Thread.sleep(1000); // wait a couple beats to let the messages go out } catch( InterruptedException e ) { e.printStackTrace(); } server.close(); isRunning = false; notifyAll(); }
protected void runCommand( HostedConnection conn, String user, String command ) { if( "/shutdown".equals(command) ) { server.broadcast(new ChatMessage("server", "Server is shutting down.")); close(); } else if( "/help".equals(command) ) { StringBuilder sb = new StringBuilder(); sb.append("Chat commands:\n"); sb.append("/help - prints this message.\n"); sb.append("/shutdown - shuts down the server."); server.broadcast(new ChatMessage("server", sb.toString())); } }
public static void main(String[] args) throws IOException, InterruptedException{ Logger.getLogger("").getHandlers()[0].setLevel(Level.OFF); Server server = Network.createServer(5110); server.start(); server.addConnectionListener(new TestNetworkStress()); for (int i = 0; i < 1000; i++){ Client client = Network.connectToServer("localhost", 5110); client.start(); Thread.sleep(10); client.close(); } } }
public static void main(String[] args) throws IOException, InterruptedException{ Serializer.registerClass(SomeObject.class); Serializer.registerClass(TestSerializationMessage.class); Server server = Network.createServer( 5110 ); server.start(); Client client = Network.connectToServer( "localhost", 5110 ); client.start(); server.addMessageListener(new TestSerialization(), TestSerializationMessage.class); client.send(new TestSerializationMessage(true)); Thread.sleep(10000); }
/** * Shares a server-wide object associated with the specified name over the specified * channel. All connections with RMI hosting started will have access to this shared * object as soon as they connect and they will all share the same instance. It is up * to the shared object to handle any multithreading that might be required. * All network communcation associated with the shared object will be done over * the specified channel. */ public <T> void shareGlobal( byte channel, String name, T object, Class<? super T> type ) { GlobalShare share = new GlobalShare(channel, object, type); GlobalShare existing = globalShares.put(name, share); if( existing != null ) { // Shouldn't need to do anything actually. } // Go through all of the children for( HostedConnection conn : getServer().getConnections() ) { RmiRegistry child = getRmiRegistry(conn); if( child == null ) { continue; } child.share(channel, name, object, type); } }
public void start() throws IOException { if (server == null) { server = Network.createServer(NetworkConstants.GAME_NAME, NetworkConstants.PROTOCOL_VERSION, port, port); } server.addChannel(port + 1); // Lobby server.addChannel(port + 2); // Chat initialize(); server.addConnectionListener(new ServerConnectionListener(this)); // Adding a delay for the connectionAdded right after the serializer registration // service gets to run let's the client get a small break in the buffer that should // generally prevent the RpcCall messages from coming too quickly and getting processed // before the SerializerRegistrationMessage has had a chance to process. // This "feature" happens with Linux almost all the time server.getServices().addService(new DelayService()); server.getServices().addServices(new RpcHostedService(), new RmiHostedService(), new AccountHostedService(name), new LobbyHostedService(), new ChatHostedService() ); // Add the SimEtheral host that will serve object sync updates to // the clients. EtherealHost ethereal = new EtherealHost(NetworkConstants.OBJECT_PROTOCOL, NetworkConstants.ZONE_GRID, NetworkConstants.ZONE_RADIUS); server.getServices().addService(ethereal); server.start(); start = System.nanoTime(); }
/** * Used internally to setup the message delegator that will * handle HostedConnection specific messages and forward them * to that connection's RpcConnection. */ @Override protected void onInitialize( HostedServiceManager serviceManager ) { Server server = serviceManager.getServer(); // A general listener for forwarding the messages // to the client-specific handler this.delegator = new SessionDataDelegator(RpcConnection.class, ATTRIBUTE_NAME, true); server.addMessageListener(delegator, delegator.getMessageTypes()); if( log.isLoggable(Level.FINEST) ) { log.log(Level.FINEST, "Registered delegator for message types:{0}", Arrays.asList(delegator.getMessageTypes())); } }
/** * Creates a HostedServiceManager for the specified network Server. */ public HostedServiceManager( Server server ) { this.server = server; this.connectionObserver = new ConnectionObserver(); server.addConnectionListener(connectionObserver); }
public void close() { if (server != null && server.isRunning()) { // Close the client connections gracefully for (HostedConnection conn : server.getConnections()) { conn.close("Server closing!"); } server.close(); // FIXME: Really, I'm sure this is not meant to be // https://hub.jmonkeyengine.org/t/solved-for-now-serializer-locked-error-what-does-it-mean-version-jme-3-1/33671 Serializer.setReadOnly(false); } }
/** * Causes this service to stop listening for new connections and * all existing connections will have stopHostingOnConnection() called * for them. */ public void stop() { server.removeConnectionListener(connectionObserver); for( HostedConnection conn : server.getConnections() ) { stopHostingOnConnection(conn); } }
/** * Used internally to remove the message delegator from the * server. */ @Override public void terminate(HostedServiceManager serviceManager) { Server server = serviceManager.getServer(); server.removeMessageListener(delegator, delegator.getMessageTypes()); }
public synchronized void start() { if( isRunning ) { return; } server.start(); isRunning = true; }
public static void main(String[] args) throws IOException, InterruptedException{ Serializer.registerClass(PingMessage.class); Serializer.registerClass(PongMessage.class); Server server = Network.createServer(5110); server.start(); Client client = Network.connectToServer("localhost", 5110); client.start(); server.addMessageListener(new ServerPingResponder(), PingMessage.class); client.addMessageListener(new ClientPingResponder(), PongMessage.class); System.out.println("Client: Sending ping message.."); client.send(new PingMessage()); Object obj = new Object(); synchronized (obj){ obj.wait(); } } }
@Override public void stop() { for( HostedConnection conn : getServer().getConnections() ) { stopHostingOnConnection(conn); } }
/** * Used internally to setup the message delegator that will * handle HostedConnection specific messages and forward them * to that connection's RpcConnection. */ @Override protected void onInitialize( HostedServiceManager serviceManager ) { Server server = serviceManager.getServer(); // A general listener for forwarding the messages // to the client-specific handler this.delegator = new SessionDataDelegator(RpcConnection.class, ATTRIBUTE_NAME, true); server.addMessageListener(delegator, delegator.getMessageTypes()); if( log.isLoggable(Level.FINEST) ) { log.log(Level.FINEST, "Registered delegator for message types:{0}", Arrays.asList(delegator.getMessageTypes())); } }
/** * Creates a HostedServiceManager for the specified network Server. */ public HostedServiceManager( Server server ) { this.server = server; this.connectionObserver = new ConnectionObserver(); server.addConnectionListener(connectionObserver); }
/** * Used internally to remove the message delegator from the * server. */ @Override public void terminate(HostedServiceManager serviceManager) { Server server = serviceManager.getServer(); server.removeMessageListener(delegator, delegator.getMessageTypes()); }
public static void createServer(){ serverApp = new SimpleApplication() { @Override public void simpleInitApp() { } }; serverApp.start(); try { Server server = Network.createServer(5110); server.start(); ObjectStore store = new ObjectStore(server); store.exposeObject("access", new ServerAccessImpl()); } catch (IOException ex) { ex.printStackTrace(); } }
public TestChatServer() throws IOException { // Use this to test the client/server name version check this.server = Network.createServer(NAME, VERSION, PORT, UDP_PORT); // Initialize our own messages only after the server has been created. // It registers some additional messages with the serializer by default // that need to go before custom messages. initializeClasses(); ChatHandler handler = new ChatHandler(); server.addMessageListener(handler, ChatMessage.class); server.addConnectionListener(new ChatConnectionListener()); }