Home | History | Annotate | Download | only in network
      1 /*
      2  * Copyright (c) 2011 jMonkeyEngine
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * * Redistributions of source code must retain the above copyright
     10  *   notice, this list of conditions and the following disclaimer.
     11  *
     12  * * Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  *
     16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
     17  *   may be used to endorse or promote products derived from this software
     18  *   without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 package com.jme3.network;
     34 
     35 import com.jme3.network.base.DefaultClient;
     36 import com.jme3.network.base.DefaultServer;
     37 import com.jme3.network.base.TcpConnectorFactory;
     38 import com.jme3.network.kernel.tcp.SelectorKernel;
     39 import com.jme3.network.kernel.tcp.SocketConnector;
     40 import com.jme3.network.kernel.udp.UdpConnector;
     41 import com.jme3.network.kernel.udp.UdpKernel;
     42 import java.io.IOException;
     43 import java.net.InetAddress;
     44 
     45 /**
     46  *  The main service provider for conveniently creating
     47  *  server and client instances.
     48  *
     49  *  @version   $Revision: 8979 $
     50  *  @author    Paul Speed
     51  */
     52 public class Network
     53 {
     54     public static final String DEFAULT_GAME_NAME = "Unnamed jME3 Game";
     55     public static final int DEFAULT_VERSION = 42;
     56 
     57     /**
     58      *  Creates a Server that will utilize both reliable and fast
     59      *  transports to communicate with clients.  The specified port
     60      *  will be used for both TCP and UDP communication.
     61      */
     62     public static Server createServer( int port ) throws IOException
     63     {
     64         return createServer( DEFAULT_GAME_NAME, DEFAULT_VERSION, port, port );
     65     }
     66 
     67     /**
     68      *  Creates a Server that will utilize both reliable and fast
     69      *  transports to communicate with clients.  The specified port
     70      *  will be used for both TCP and UDP communication.
     71      */
     72     public static Server createServer( int tcpPort, int udpPort ) throws IOException
     73     {
     74         return createServer( DEFAULT_GAME_NAME, DEFAULT_VERSION, tcpPort, udpPort );
     75     }
     76 
     77     /**
     78      *  Creates a named and versioned Server that will utilize both reliable and fast
     79      *  transports to communicate with clients.  The specified port
     80      *  will be used for both TCP and UDP communication.
     81      *
     82      *  @param gameName This is the name that identifies the game.  Connecting clients
     83      *                  must use this name or be turned away.
     84      *  @param version  This is a game-specific verison that helps detect when out-of-date
     85      *                  clients have connected to an incompatible server.
     86      *  @param tcpPort  The port upon which the TCP hosting will listen for new connections.
     87      *  @param udpPort  The port upon which the UDP hosting will listen for new 'fast' UDP
     88      *                  messages.  Set to -1 if 'fast' traffic should go over TCP.  This will
     89      *                  completely disable UDP traffic for this server.
     90      */
     91     public static Server createServer( String gameName, int version, int tcpPort, int udpPort ) throws IOException
     92     {
     93         UdpKernel fast = udpPort == -1 ? null : new UdpKernel(udpPort);
     94         SelectorKernel reliable = new SelectorKernel(tcpPort);
     95 
     96         return new DefaultServer( gameName, version, reliable, fast );
     97     }
     98 
     99     /**
    100      *  Creates a client that can be connected at a later time.
    101      */
    102     public static NetworkClient createClient()
    103     {
    104         return createClient( DEFAULT_GAME_NAME, DEFAULT_VERSION );
    105     }
    106 
    107     /**
    108      *  Creates a client that can be connected at a later time.  The specified
    109      *  game name and version must match the server or the client will be turned
    110      *  away.
    111      */
    112     public static NetworkClient createClient( String gameName, int version )
    113     {
    114         return new NetworkClientImpl(gameName, version);
    115     }
    116 
    117     /**
    118      *  Creates a Client that communicates with the specified host and port
    119      *  using both reliable and fast transports.
    120      */
    121     public static Client connectToServer( String host, int hostPort ) throws IOException
    122     {
    123         return connectToServer( DEFAULT_GAME_NAME, DEFAULT_VERSION, host, hostPort, hostPort );
    124     }
    125 
    126     /**
    127      *  Creates a Client that communicates with the specified host and separate TCP and UDP ports
    128      *  using both reliable and fast transports.
    129      */
    130     public static Client connectToServer( String host, int hostPort, int remoteUdpPort ) throws IOException
    131     {
    132         return connectToServer( DEFAULT_GAME_NAME, DEFAULT_VERSION, host, hostPort, remoteUdpPort );
    133     }
    134 
    135     /**
    136      *  Creates a Client that communicates with the specified host and port
    137      *  using both reliable and fast transports.
    138      */
    139     public static Client connectToServer( String gameName, int version,
    140                                           String host, int hostPort ) throws IOException
    141     {
    142         return connectToServer( gameName, version, host, hostPort, hostPort );
    143     }
    144 
    145     /**
    146      *  Creates a Client that communicates with the specified host and and separate TCP and UDP ports
    147      *  using both reliable and fast transports.
    148      *
    149      *  @param gameName This is the name that identifies the game.  This must match
    150      *                  the target server's name or this client will be turned away.
    151      *  @param version  This is a game-specific verison that helps detect when out-of-date
    152      *                  clients have connected to an incompatible server.  This must match
    153      *                  the server's version of this client will be turned away.
    154      *  @param hostPort  The remote TCP port on the server to which this client should
    155      *                  send reliable messages.
    156      *  @param remoteUdpPort  The remote UDP port on the server to which this client should
    157      *                  send 'fast'/unreliable messages.   Set to -1 if 'fast' traffic should
    158      *                  go over TCP.  This will completely disable UDP traffic for this
    159      *                  client.
    160      */
    161     public static Client connectToServer( String gameName, int version,
    162                                           String host, int hostPort, int remoteUdpPort ) throws IOException
    163     {
    164         InetAddress remoteAddress = InetAddress.getByName(host);
    165         UdpConnector fast = remoteUdpPort == -1 ? null : new UdpConnector( remoteAddress, remoteUdpPort );
    166         SocketConnector reliable = new SocketConnector( remoteAddress, hostPort );
    167 
    168         return new DefaultClient( gameName, version, reliable, fast, new TcpConnectorFactory(remoteAddress) );
    169     }
    170 
    171 
    172     protected static class NetworkClientImpl extends DefaultClient implements NetworkClient
    173     {
    174         public NetworkClientImpl(String gameName, int version)
    175         {
    176             super( gameName, version );
    177         }
    178 
    179         public void connectToServer( String host, int port, int remoteUdpPort ) throws IOException
    180         {
    181             connectToServer( InetAddress.getByName(host), port, remoteUdpPort );
    182         }
    183 
    184         public void connectToServer( InetAddress address, int port, int remoteUdpPort ) throws IOException
    185         {
    186             UdpConnector fast = new UdpConnector( address, remoteUdpPort );
    187             SocketConnector reliable = new SocketConnector( address, port );
    188 
    189             setPrimaryConnectors( reliable, fast, new TcpConnectorFactory(address) );
    190         }
    191     }
    192 }
    193