Server.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.util.Collection;
  34. /**
  35. * Represents a host that can send and receive messages to
  36. * a set of remote client connections.
  37. *
  38. * @version $Revision$
  39. * @author Paul Speed
  40. */
  41. public interface Server
  42. {
  43. /**
  44. * Returns the 'game name' for this server. This should match the
  45. * 'game name' set on connecting clients or they will be turned away.
  46. */
  47. public String getGameName();
  48. /**
  49. * Returns the game-specific version of this server used for detecting
  50. * mismatched clients.
  51. */
  52. public int getVersion();
  53. /**
  54. * Sends the specified message to all connected clients.
  55. */
  56. public void broadcast( Message message );
  57. /**
  58. * Sends the specified message to all connected clients that match
  59. * the filter. If no filter is specified then this is the same as
  60. * calling broadcast(message) and the message will be delivered to
  61. * all connections.
  62. * <p>Examples:</p>
  63. * <pre>
  64. * // Broadcast to connections: conn1, conn2, and conn3
  65. * server.broadcast( Filters.in( conn1, conn2, conn3 ), message );
  66. *
  67. * // Broadcast to all connections exception source
  68. * server.broadcast( Filters.notEqualTo( source ), message );
  69. * </pre>
  70. */
  71. public void broadcast( Filter<? super HostedConnection> filter, Message message );
  72. /**
  73. * Sends the specified message over the specified alternate channel to all connected
  74. * clients that match the filter. If no filter is specified then this is the same as
  75. * calling broadcast(message) and the message will be delivered to
  76. * all connections.
  77. * <p>Examples:</p>
  78. * <pre>
  79. * // Broadcast to connections: conn1, conn2, and conn3
  80. * server.broadcast( Filters.in( conn1, conn2, conn3 ), message );
  81. *
  82. * // Broadcast to all connections exception source
  83. * server.broadcast( Filters.notEqualTo( source ), message );
  84. * </pre>
  85. */
  86. public void broadcast( int channel, Filter<? super HostedConnection> filter, Message message );
  87. /**
  88. * Start the server so that it will began accepting new connections
  89. * and processing messages.
  90. */
  91. public void start();
  92. /**
  93. * Adds an alternate channel to the server, using the specified port. This is an
  94. * entirely separate connection where messages are sent and received in parallel
  95. * to the two primary default channels. All channels must be added before the connection
  96. * is started.
  97. * Returns the ID of the created channel for use when specifying the channel in send or
  98. * broadcast calls. The ID is returned entirely out of convenience since the IDs
  99. * are predictably incremented. The first channel is 0, second is 1, and so on.
  100. */
  101. public int addChannel( int port );
  102. /**
  103. * Returns true if the server has been started.
  104. */
  105. public boolean isRunning();
  106. /**
  107. * Closes all client connections, stops and running processing threads, and
  108. * closes the host connection.
  109. */
  110. public void close();
  111. /**
  112. * Retrieves a hosted connection by ID.
  113. */
  114. public HostedConnection getConnection( int id );
  115. /**
  116. * Retrieves a read-only collection of all currently connected connections.
  117. */
  118. public Collection<HostedConnection> getConnections();
  119. /**
  120. * Returns true if the server has active connections at the time of this
  121. * call.
  122. */
  123. public boolean hasConnections();
  124. /**
  125. * Adds a listener that will be notified when new hosted connections
  126. * arrive.
  127. */
  128. public void addConnectionListener( ConnectionListener listener );
  129. /**
  130. * Removes a previously registered connection listener.
  131. */
  132. public void removeConnectionListener( ConnectionListener listener );
  133. /**
  134. * Adds a listener that will be notified when any message or object
  135. * is received from one of the clients.
  136. *
  137. * <p>Note about MessageListener multithreading: on the server, message events may
  138. * be delivered by more than one thread depending on the server
  139. * implementation used. Listener implementations should treat their
  140. * shared data structures accordingly and set them up for multithreaded
  141. * access. The only threading guarantee is that for a single
  142. * HostedConnection, there will only ever be one thread at a time
  143. * and the messages will always be delivered to that connection in the
  144. * order that they were delivered. This is the only restriction placed
  145. * upon server message dispatch pool implementations.</p>
  146. */
  147. public void addMessageListener( MessageListener<? super HostedConnection> listener );
  148. /**
  149. * Adds a listener that will be notified when messages of the specified
  150. * types are received from one of the clients.
  151. */
  152. public void addMessageListener( MessageListener<? super HostedConnection> listener, Class... classes );
  153. /**
  154. * Removes a previously registered wildcard listener. This does
  155. * not remove this listener from any type-specific registrations.
  156. */
  157. public void removeMessageListener( MessageListener<? super HostedConnection> listener );
  158. /**
  159. * Removes a previously registered type-specific listener from
  160. * the specified types.
  161. */
  162. public void removeMessageListener( MessageListener<? super HostedConnection> listener, Class... classes );
  163. }