Network.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. InetAddress local = InetAddress.getLocalHost();
  58. UdpKernel fast = new UdpKernel(local, port);
  59. SelectorKernel reliable = new SelectorKernel(local,port);
  60. return new DefaultServer( reliable, fast );
  61. }
  62. /**
  63. * Creates a client that can be connected at a later time.
  64. */
  65. public static NetworkClient createClient()
  66. {
  67. return new NetworkClientImpl();
  68. }
  69. /**
  70. * Creates a Client that communicates with the specified host and port
  71. * using both reliable and fast transports. The localUdpPort specifies the
  72. * local port to use for listening for incoming 'fast' UDP messages.
  73. */
  74. public static Client connectToServer( String host, int hostPort, int localUdpPort ) throws IOException
  75. {
  76. return connectToServer( InetAddress.getByName(host), hostPort, hostPort, localUdpPort );
  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 remoteUdpPort,
  84. int localUdpPort ) throws IOException
  85. {
  86. return connectToServer( InetAddress.getByName(host), hostPort, remoteUdpPort, localUdpPort );
  87. }
  88. /**
  89. * Creates a Client that communicates with the specified address and port
  90. * using both reliable and fast transports. The localUdpPort specifies the
  91. * local port to use for listening for incoming 'fast' messages.
  92. */
  93. public static Client connectToServer( InetAddress address, int port, int localUdpPort ) throws IOException
  94. {
  95. return connectToServer( address, port, port, 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 remoteUdpPort,
  103. int localUdpPort ) throws IOException
  104. {
  105. InetAddress local = InetAddress.getLocalHost();
  106. UdpConnector fast = new UdpConnector( local, localUdpPort, address, port );
  107. SocketConnector reliable = new SocketConnector( address, port );
  108. return new DefaultClient( reliable, fast );
  109. }
  110. protected static class NetworkClientImpl extends DefaultClient implements NetworkClient
  111. {
  112. public NetworkClientImpl()
  113. {
  114. }
  115. public void connectToServer( String host, int port, int remoteUdpPort,
  116. int localUdpPort ) throws IOException
  117. {
  118. connectToServer( InetAddress.getByName(host), port, remoteUdpPort, localUdpPort );
  119. }
  120. public void connectToServer( InetAddress address, int port, int remoteUdpPort,
  121. int localUdpPort ) throws IOException
  122. {
  123. InetAddress local = InetAddress.getLocalHost();
  124. UdpConnector fast = new UdpConnector( local, localUdpPort, address, port );
  125. SocketConnector reliable = new SocketConnector( address, port );
  126. setConnectors( reliable, fast );
  127. }
  128. }
  129. }