Network.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. package com.jme3.network;
  33. import java.io.IOException;
  34. import java.net.InetAddress;
  35. import com.jme3.network.base.DefaultClient;
  36. import com.jme3.network.base.DefaultServer;
  37. import com.jme3.network.kernel.tcp.SelectorKernel;
  38. import com.jme3.network.kernel.tcp.SocketConnector;
  39. import com.jme3.network.kernel.udp.UdpConnector;
  40. import com.jme3.network.kernel.udp.UdpKernel;
  41. /**
  42. * The main service provider for conveniently creating
  43. * server and client instances.
  44. *
  45. * @version $Revision$
  46. * @author Paul Speed
  47. */
  48. public class Network
  49. {
  50. /**
  51. * Creates a Server that will utilize both reliable and fast
  52. * transports to communicate with clients. The specified port
  53. * will be used for both TCP and UDP communication.
  54. */
  55. public static Server createServer( int port ) throws IOException
  56. {
  57. return createServer( port, port );
  58. }
  59. /**
  60. * Creates a Server that will utilize both reliable and fast
  61. * transports to communicate with clients. The specified port
  62. * will be used for both TCP and UDP communication.
  63. */
  64. public static Server createServer( int tcpPort, int udpPort ) throws IOException
  65. {
  66. //InetAddress local = InetAddress.getLocalHost();
  67. UdpKernel fast = new UdpKernel(udpPort);
  68. SelectorKernel reliable = new SelectorKernel(tcpPort);
  69. return new DefaultServer( reliable, fast );
  70. }
  71. /**
  72. * Creates a client that can be connected at a later time.
  73. */
  74. public static NetworkClient createClient()
  75. {
  76. return new NetworkClientImpl();
  77. }
  78. /**
  79. * Creates a Client that communicates with the specified host and port
  80. * using both reliable and fast transports. The localUdpPort specifies the
  81. * local port to use for listening for incoming 'fast' UDP messages.
  82. */
  83. public static Client connectToServer( String host, int hostPort, int localUdpPort ) throws IOException
  84. {
  85. return connectToServer( InetAddress.getByName(host), hostPort, hostPort, localUdpPort );
  86. }
  87. /**
  88. * Creates a Client that communicates with the specified host and port
  89. * using both reliable and fast transports. The localUdpPort specifies the
  90. * local port to use for listening for incoming 'fast' UDP messages.
  91. */
  92. public static Client connectToServer( String host, int hostPort, int remoteUdpPort,
  93. int localUdpPort ) throws IOException
  94. {
  95. return connectToServer( InetAddress.getByName(host), hostPort, remoteUdpPort, localUdpPort );
  96. }
  97. /**
  98. * Creates a Client that communicates with the specified address and port
  99. * using both reliable and fast transports. The localUdpPort specifies the
  100. * local port to use for listening for incoming 'fast' messages.
  101. */
  102. public static Client connectToServer( InetAddress address, int port, int localUdpPort ) throws IOException
  103. {
  104. return connectToServer( address, port, port, localUdpPort );
  105. }
  106. /**
  107. * Creates a Client that communicates with the specified address and port
  108. * using both reliable and fast transports. The localUdpPort specifies the
  109. * local port to use for listening for incoming 'fast' messages.
  110. */
  111. public static Client connectToServer( InetAddress address, int port, int remoteUdpPort,
  112. int localUdpPort ) throws IOException
  113. {
  114. InetAddress local = InetAddress.getLocalHost();
  115. UdpConnector fast = new UdpConnector( local, localUdpPort, address, port );
  116. SocketConnector reliable = new SocketConnector( address, port );
  117. return new DefaultClient( reliable, fast );
  118. }
  119. protected static class NetworkClientImpl extends DefaultClient implements NetworkClient
  120. {
  121. public NetworkClientImpl()
  122. {
  123. }
  124. public void connectToServer( String host, int port, int remoteUdpPort,
  125. int localUdpPort ) throws IOException
  126. {
  127. connectToServer( InetAddress.getByName(host), port, remoteUdpPort, localUdpPort );
  128. }
  129. public void connectToServer( InetAddress address, int port, int remoteUdpPort,
  130. int localUdpPort ) throws IOException
  131. {
  132. InetAddress local = InetAddress.getLocalHost();
  133. UdpConnector fast = new UdpConnector( local, localUdpPort, address, port );
  134. SocketConnector reliable = new SocketConnector( address, port );
  135. setConnectors( reliable, fast );
  136. }
  137. }
  138. }