PageRenderTime 19ms CodeModel.GetById 14ms app.highlight 3ms 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
  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
 28package org.gearman.impl.reactor;
 29
 30import java.io.IOException;
 31import java.net.InetAddress;
 32import java.net.SocketAddress;
 33import java.net.SocketException;
 34import java.nio.ByteBuffer;
 35import java.nio.channels.CompletionHandler;
 36
 37/**
 38 * An asynchronous socket.
 39 * 
 40 * @author isaiah.v
 41 */
 42public interface Socket<X> {
 43
 44	/**
 45	 * Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm). 
 46	 * @param on
 47	 * 		<code>true</code> to enable TCP_NODELAY, <code>false</code> to disable. 
 48	 * @throws SocketException
 49	 * 		if there is an error in the underlying protocol, such as a TCP error.
 50	 */
 51	public void setTcpNoDelay(boolean on) throws IOException;
 52	
 53	/**
 54	 * Enable/disable SO_KEEPALIVE. 
 55	 * @param on
 56	 * 		whether or not to have socket keep alive turned on.
 57	 * @throws SocketException
 58	 * 		if there is an error in the underlying protocol, such as a TCP error.
 59	 * @throws IOException 
 60	 */
 61	public void setKeepAlive(boolean on) throws IOException;
 62	
 63	/**
 64	 * Tests if TCP_NODELAY is enabled.
 65	 * @return
 66	 * 		a <code>boolean</code> indicating whether or not TCP_NODELAY is enabled. 
 67	 * @throws SocketException
 68	 * 		if there is an error in the underlying protocol, such as a TCP error.
 69	 */
 70	public boolean getTcpNoDelay() throws IOException;
 71
 72	/**
 73	 * Closes this socket.
 74	 */
 75	public void close();
 76
 77	/**
 78	 * Returns the address to which the socket is connected.
 79	 * 
 80	 * @return the remote IP address to which this socket is connected, or null
 81	 *         if the socket is not connected.
 82	 * @throws IOException 
 83	 */
 84	public InetAddress getInetAddress();
 85
 86	/**
 87	 * Gets the local address to which the socket is bound.
 88	 * 
 89	 * @return the local address to which the socket is bound or
 90	 *         InetAddress.anyLocalAddress() if the socket is not bound yet.
 91	 */
 92	public InetAddress getLocalAddress();
 93
 94	/**
 95	 * Returns the local port to which this socket is bound.
 96	 * 
 97	 * @return the local port number to which this socket is bound or -1 if the
 98	 *         socket is not bound yet.
 99	 */
100	public int getLocalPort();
101
102	/**
103	 * Returns the remote port to which this socket is connected.
104	 * 
105	 * @return the remote port number to which this socket is connected, or 0 if
106	 *         the socket is not connected yet.
107	 */
108	public int getPort();
109
110	/**
111	 * Returns the closed state of the socket.
112	 * 
113	 * @return true if the socket has been closed
114	 */
115	public boolean isClosed();
116
117	/**
118	 * Tests if SO_KEEPALIVE is enabled.
119	 * 
120	 * @return a <code>boolean</code> indicating whether or not SO_KEEPALIVE is
121	 *         enabled.
122	 * @throws SocketException
123	 *             if there is an error in the underlying protocol, such as a
124	 *             TCP error.
125	 */
126	public boolean getKeepAlive() throws IOException;
127
128	/**
129	 * Returns the address of the endpoint this socket is bound to, or
130	 * <code>null</code> if it is not bound yet.
131	 * 
132	 * @return a <code>SocketAddress</code> representing the local endpoint of
133	 *         this socket, or <code>null</code> if it is not bound yet.
134	 * @throws IOException 
135	 */
136	public SocketAddress getLocalSocketAddress();
137
138	/**
139	 * Returns the address of the endpoint this socket is connected to, or
140	 * <code>null</code> if it is unconnected.
141	 * 
142	 * @return
143	 * @throws IOException 
144	 */
145	public SocketAddress getRemoteSocketAddress();
146	
147	/**
148	 * Writes data to the socket asynchronously.<br>
149	 * <br>
150	 * 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>
151	 * <br>
152	 * 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.
153	 * @param data
154	 * 		The data to send over the socket
155	 * @param ioeHandler
156	 * 		Defines what actions to take if an exception occurs while
157	 *		sending the data
158	 */
159	public <A> void write(ByteBuffer data, A att, CompletionHandler<ByteBuffer, A> callback);
160
161	/**
162	 * Returns the ByteBuffer for this socket.
163	 * 
164	 * @return The ByteBuffer for this socket
165	 */
166	public ByteBuffer getByteBuffer();
167
168	/**
169	 * Sets the ByteBuffer for this
170	 * 
171	 * @param buffer
172	 */
173	public void setByteBuffer(ByteBuffer buffer);
174
175	/**
176	 * Gets the socket's attachment
177	 * 
178	 * @return The attachment
179	 */
180	public X getAttachment();
181
182	/**
183	 * Sets the socket's attachment
184	 * 
185	 * @param att
186	 *            The attachment
187	 */
188	public void setAttachment(X att);
189}