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

/src/main/java/org/gearman/impl/reactor/Socket.java

https://github.com/innerverse/java-service
Java | 189 lines | 26 code | 20 blank | 143 comment | 0 complexity | d9dc942163fdefb72537c251321b1e26 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * Copyright (c) 2012, Isaiah van der Elst (isaiah.v@comcast.net)
  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 met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package org.gearman.impl.reactor;
  28. import java.io.IOException;
  29. import java.net.InetAddress;
  30. import java.net.SocketAddress;
  31. import java.net.SocketException;
  32. import java.nio.ByteBuffer;
  33. import java.nio.channels.CompletionHandler;
  34. /**
  35. * An asynchronous socket.
  36. *
  37. * @author isaiah.v
  38. */
  39. public interface Socket<X> {
  40. /**
  41. * Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
  42. * @param on
  43. * <code>true</code> to enable TCP_NODELAY, <code>false</code> to disable.
  44. * @throws SocketException
  45. * if there is an error in the underlying protocol, such as a TCP error.
  46. */
  47. public void setTcpNoDelay(boolean on) throws IOException;
  48. /**
  49. * Enable/disable SO_KEEPALIVE.
  50. * @param on
  51. * whether or not to have socket keep alive turned on.
  52. * @throws SocketException
  53. * if there is an error in the underlying protocol, such as a TCP error.
  54. * @throws IOException
  55. */
  56. public void setKeepAlive(boolean on) throws IOException;
  57. /**
  58. * Tests if TCP_NODELAY is enabled.
  59. * @return
  60. * a <code>boolean</code> indicating whether or not TCP_NODELAY is enabled.
  61. * @throws SocketException
  62. * if there is an error in the underlying protocol, such as a TCP error.
  63. */
  64. public boolean getTcpNoDelay() throws IOException;
  65. /**
  66. * Closes this socket.
  67. */
  68. public void close();
  69. /**
  70. * Returns the address to which the socket is connected.
  71. *
  72. * @return the remote IP address to which this socket is connected, or null
  73. * if the socket is not connected.
  74. * @throws IOException
  75. */
  76. public InetAddress getInetAddress();
  77. /**
  78. * Gets the local address to which the socket is bound.
  79. *
  80. * @return the local address to which the socket is bound or
  81. * InetAddress.anyLocalAddress() if the socket is not bound yet.
  82. */
  83. public InetAddress getLocalAddress();
  84. /**
  85. * Returns the local port to which this socket is bound.
  86. *
  87. * @return the local port number to which this socket is bound or -1 if the
  88. * socket is not bound yet.
  89. */
  90. public int getLocalPort();
  91. /**
  92. * Returns the remote port to which this socket is connected.
  93. *
  94. * @return the remote port number to which this socket is connected, or 0 if
  95. * the socket is not connected yet.
  96. */
  97. public int getPort();
  98. /**
  99. * Returns the closed state of the socket.
  100. *
  101. * @return true if the socket has been closed
  102. */
  103. public boolean isClosed();
  104. /**
  105. * Tests if SO_KEEPALIVE is enabled.
  106. *
  107. * @return a <code>boolean</code> indicating whether or not SO_KEEPALIVE is
  108. * enabled.
  109. * @throws SocketException
  110. * if there is an error in the underlying protocol, such as a
  111. * TCP error.
  112. */
  113. public boolean getKeepAlive() throws IOException;
  114. /**
  115. * Returns the address of the endpoint this socket is bound to, or
  116. * <code>null</code> if it is not bound yet.
  117. *
  118. * @return a <code>SocketAddress</code> representing the local endpoint of
  119. * this socket, or <code>null</code> if it is not bound yet.
  120. * @throws IOException
  121. */
  122. public SocketAddress getLocalSocketAddress();
  123. /**
  124. * Returns the address of the endpoint this socket is connected to, or
  125. * <code>null</code> if it is unconnected.
  126. *
  127. * @return
  128. * @throws IOException
  129. */
  130. public SocketAddress getRemoteSocketAddress();
  131. /**
  132. * Writes data to the socket asynchronously.<br>
  133. * <br>
  134. * The write operation may write up to r bytes to the channel, where r is the number of bytes remaining in the buffer, that is, data.remaining() at the time that the write is attempted.<br>
  135. * <br>
  136. * Buffers are not safe for use by multiple concurrent threads so care should be taken to not access the buffer until the operation has completed.
  137. * @param data
  138. * The data to send over the socket
  139. * @param ioeHandler
  140. * Defines what actions to take if an exception occurs while
  141. * sending the data
  142. */
  143. public <A> void write(ByteBuffer data, A att, CompletionHandler<ByteBuffer, A> callback);
  144. /**
  145. * Returns the ByteBuffer for this socket.
  146. *
  147. * @return The ByteBuffer for this socket
  148. */
  149. public ByteBuffer getByteBuffer();
  150. /**
  151. * Sets the ByteBuffer for this
  152. *
  153. * @param buffer
  154. */
  155. public void setByteBuffer(ByteBuffer buffer);
  156. /**
  157. * Gets the socket's attachment
  158. *
  159. * @return The attachment
  160. */
  161. public X getAttachment();
  162. /**
  163. * Sets the socket's attachment
  164. *
  165. * @param att
  166. * The attachment
  167. */
  168. public void setAttachment(X att);
  169. }