PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/wasync/src/main/java/org/atmosphere/wasync/Socket.java

https://github.com/rage28/wasync
Java | 163 lines | 19 code | 12 blank | 132 comment | 0 complexity | ef140b146b294a90b3cb24fddc709178 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2014 Jeanfrancois Arcand
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package org.atmosphere.wasync;
  17. import java.io.IOException;
  18. import java.util.concurrent.TimeUnit;
  19. /**
  20. * A Socket represents a connection to a remote server. A Socket abstract the transport used and will negotiate
  21. * the best {@link org.atmosphere.wasync.Request.TRANSPORT} to communicate with the Server.
  22. * <p></p>
  23. * Depending on the transport used, one or two connections will be opened. For WebSocket, a single, bi-directional connection
  24. * will be used. For other transport like streaming, server side events and long-polling, a connection will be opened
  25. * to the server and will be suspended (stay opened) until an event happen on the server. A second connection will be opened every time the {@link #fire(Object)}
  26. * method is invoked and cached for further re-use.
  27. * <p></p>
  28. *
  29. * As simple as
  30. * <blockquote><pre>
  31. Client client = AtmosphereClientFactory.getDefault().newClient();
  32. RequestBuilder request = client.newRequestBuilder()
  33. .method(Request.METHOD.GET)
  34. .uri(targetUrl + "/suspend")
  35. .encoder(new Encoder&lt;String, Reader&gt;() { // Stream the request body
  36. &#64;Override
  37. public Reader encode(String s) {
  38. return new StringReader(s);
  39. }
  40. })
  41. .decoder(new Decoder&lt;String, Reader&gt;() {
  42. &#64;Override
  43. public Reader decode(String s) {
  44. return new StringReader(s);
  45. }
  46. })
  47. .transport(Request.TRANSPORT.WEBSOCKET) // Try WebSocket
  48. .transport(Request.TRANSPORT.LONG_POLLING); // Fallback to Long-Polling
  49. Socket socket = client.create();
  50. socket.on("message", new Function&lt;String&gt;() {
  51. &#64;Override
  52. public void on(Reader r) {
  53. // Read the response
  54. }
  55. }).on(new Function&lt;IOException&gt;() {
  56. &#64;Override
  57. public void on(Throwable t) {
  58. // Some IOException occurred
  59. }
  60. }).open(request.build()).fire("echo");
  61. * </pre></blockquote>
  62. *
  63. * @author Jeanfrancois Arcand
  64. */
  65. public interface Socket {
  66. /**
  67. * The current state of the underlying Socket.
  68. */
  69. public enum STATUS {
  70. /**
  71. * The socket is not yet connected
  72. */
  73. INIT,
  74. /**
  75. * The socket is open and ready to send messages
  76. */
  77. OPEN,
  78. /**
  79. * The socket was closed and re-opened
  80. */
  81. REOPENED,
  82. /**
  83. * The socket is close
  84. */
  85. CLOSE,
  86. /**
  87. * The socket is broken
  88. */
  89. ERROR }
  90. /**
  91. * Send data to the remote Server. The object will first be delivered to the set of {@link Encoder}, and then send to the server.
  92. * The server's response will be delivered to the set of defined {@link Function} using the opened {@link Transport}, e.g for
  93. * {@link Request.TRANSPORT#WEBSOCKET}, the same connection will be re-used and, for others transports, the suspended connection.
  94. * @param data object to send
  95. * @return a {@link Future}
  96. * @throws IOException
  97. */
  98. Future fire(Object data) throws IOException;
  99. /**
  100. * Associate a {@link Function} with the Socket. When a response is received, the library will try to associated
  101. * the decoded message (decoded by {@link Decoder}) to the defined type of the {@link Function}
  102. * @param function a {@link Function}
  103. * @return this
  104. */
  105. Socket on(Function<?> function);
  106. /**
  107. * Associate a {@link Function} with the Socket. When a response is received, the library will try to associated
  108. * the decoded message (decoded by {@link Decoder}) to the defined type of the {@link Function}. The default messages
  109. * are defined by {@link org.atmosphere.wasync.Event} but handling of custom message can be done using a {@link FunctionResolver}
  110. * @param function a {@link Function}
  111. * @return this
  112. */
  113. Socket on(String functionMessage, Function<?> function);
  114. /**
  115. * Associate a {@link Function} with an {@link Event}. When the event happen the library will try to associated
  116. * the decoded event (decoded by {@link Decoder}) to the defined type of the {@link Function}. The default event
  117. * are defined by {@link org.atmosphere.wasync.Event} but handling of custom event can be done using a {@link FunctionResolver}
  118. * @param function a {@link Function}
  119. * @return this
  120. */
  121. Socket on(Event event, Function<?> function);
  122. /**
  123. * Connect to the remote Server using the {@link Request}'s information.
  124. * @param request a {@link Request}
  125. * @return this
  126. * @throws IOException in case the connect fails or a network failure occurs.
  127. */
  128. Socket open(Request request) throws IOException;
  129. /**
  130. * Connect to the remote Server using the {@link Request}'s information, will timeout if the connection failed to open
  131. * within a certain time.
  132. * @param request a {@link Request}
  133. * @param timeout the maximum time to wait
  134. * @param unit the time unit of the timeout argument
  135. * @return this
  136. * @throws IOException in case the connect fails or a network failure occurs.
  137. */
  138. Socket open(Request request, long timeout, TimeUnit unit) throws IOException;
  139. /**
  140. * Close this Socket, asynchronously.
  141. */
  142. void close();
  143. /**
  144. * Return the {@link STATUS} of this Socket.
  145. */
  146. STATUS status();
  147. }