From 24b65c4914f255aebd05691a2a3b854a09a4fb25 Mon Sep 17 00:00:00 2001 From: "PSp..om" Date: Thu, 17 Mar 2011 22:01:48 +0000 Subject: [PATCH] Added a form of client that allows delayed connection using the standard network stack. git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7015 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../networking/com/jme3/network/Network.java | 55 ++++++++++++++++++- .../com/jme3/network/NetworkClient.java | 55 +++++++++++++++++++ .../com/jme3/network/base/DefaultClient.java | 16 +++++- 3 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 engine/src/networking/com/jme3/network/NetworkClient.java diff --git a/engine/src/networking/com/jme3/network/Network.java b/engine/src/networking/com/jme3/network/Network.java index 7e9dc0254..1cf48e876 100644 --- a/engine/src/networking/com/jme3/network/Network.java +++ b/engine/src/networking/com/jme3/network/Network.java @@ -66,6 +66,14 @@ public class Network return new DefaultServer( reliable, fast ); } + /** + * Creates a client that can be connected at a later time. + */ + public static NetworkClient createClient() + { + return new NetworkClientImpl(); + } + /** * Creates a Client that communicates with the specified host and port * using both reliable and fast transports. The localUdpPort specifies the @@ -73,7 +81,18 @@ public class Network */ public static Client connectToServer( String host, int hostPort, int localUdpPort ) throws IOException { - return connectToServer( InetAddress.getByName(host), hostPort, localUdpPort ); + return connectToServer( InetAddress.getByName(host), hostPort, hostPort, localUdpPort ); + } + + /** + * Creates a Client that communicates with the specified host and port + * using both reliable and fast transports. The localUdpPort specifies the + * local port to use for listening for incoming 'fast' UDP messages. + */ + public static Client connectToServer( String host, int hostPort, int remoteUdpPort, + int localUdpPort ) throws IOException + { + return connectToServer( InetAddress.getByName(host), hostPort, remoteUdpPort, localUdpPort ); } /** @@ -82,6 +101,17 @@ public class Network * local port to use for listening for incoming 'fast' messages. */ public static Client connectToServer( InetAddress address, int port, int localUdpPort ) throws IOException + { + return connectToServer( address, port, port, localUdpPort ); + } + + /** + * Creates a Client that communicates with the specified address and port + * using both reliable and fast transports. The localUdpPort specifies the + * local port to use for listening for incoming 'fast' messages. + */ + public static Client connectToServer( InetAddress address, int port, int remoteUdpPort, + int localUdpPort ) throws IOException { InetAddress local = InetAddress.getLocalHost(); UdpConnector fast = new UdpConnector( local, localUdpPort, address, port ); @@ -89,4 +119,27 @@ public class Network return new DefaultClient( reliable, fast ); } + + protected static class NetworkClientImpl extends DefaultClient implements NetworkClient + { + public NetworkClientImpl() + { + } + + public void connectToServer( String host, int port, int remoteUdpPort, + int localUdpPort ) throws IOException + { + connectToServer( InetAddress.getByName(host), port, remoteUdpPort, localUdpPort ); + } + + public void connectToServer( InetAddress address, int port, int remoteUdpPort, + int localUdpPort ) throws IOException + { + InetAddress local = InetAddress.getLocalHost(); + UdpConnector fast = new UdpConnector( local, localUdpPort, address, port ); + SocketConnector reliable = new SocketConnector( address, port ); + + setConnectors( reliable, fast ); + } + } } diff --git a/engine/src/networking/com/jme3/network/NetworkClient.java b/engine/src/networking/com/jme3/network/NetworkClient.java new file mode 100644 index 000000000..4c0e364a8 --- /dev/null +++ b/engine/src/networking/com/jme3/network/NetworkClient.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2011 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package com.jme3.network; + +import java.io.IOException; +import java.net.InetAddress; + +/** + * A Client whose network connection information can + * be provided post-creation. The actual connection stack + * will be setup the same as if Network.connectToServer + * had been called. + * + * @version $Revision$ + * @author Paul Speed + */ +public interface NetworkClient extends Client +{ + public void connectToServer( String host, int port, int remoteUdpPort, + int localUdpPort ) throws IOException; + + public void connectToServer( InetAddress address, int port, int remoteUdpPort, + int localUdpPort ) throws IOException; + +} diff --git a/engine/src/networking/com/jme3/network/base/DefaultClient.java b/engine/src/networking/com/jme3/network/base/DefaultClient.java index 59e3c4ace..3536dffa8 100644 --- a/engine/src/networking/com/jme3/network/base/DefaultClient.java +++ b/engine/src/networking/com/jme3/network/base/DefaultClient.java @@ -65,8 +65,22 @@ public class DefaultClient implements Client private ConnectorAdapter reliableAdapter; private ConnectorAdapter fastAdapter; + public DefaultClient() + { + } + public DefaultClient( Connector reliable, Connector fast ) { + setConnectors( reliable, fast ); + } + + protected void setConnectors( Connector reliable, Connector fast ) + { + if( reliable == null ) + throw new IllegalArgumentException( "The reliable connector cannot be null." ); + if( isRunning ) + throw new IllegalStateException( "Client is already started." ); + this.reliable = reliable; this.fast = fast; if( reliable != null ) { @@ -75,7 +89,7 @@ public class DefaultClient implements Client if( fast != null ) { fastAdapter = new ConnectorAdapter(fast, dispatcher); } - } + } protected void checkRunning() {