001: /*
002: * Copyright (c) xsocket.org, 2006 - 2008. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
019: * The latest copy of this software may be found on http://www.xsocket.org/
020: */
021: package org.xsocket.connection;
022:
023: import java.io.Closeable;
024: import java.io.IOException;
025: import java.net.InetAddress;
026:
027: /**
028: * A connection (session) between two endpoints. It encapsulates the underlying socket channel. <br><br>
029: *
030: *
031: * @author grro@xsocket.org
032: */
033: public interface IConnection extends Closeable {
034:
035: public static final String INITIAL_DEFAULT_ENCODING = "UTF-8";
036:
037: public static final String SO_SNDBUF = "SOL_SOCKET.SO_SNDBUF";
038: public static final String SO_RCVBUF = "SOL_SOCKET.SO_RCVBUF";
039: public static final String SO_REUSEADDR = "SOL_SOCKET.SO_REUSEADDR";
040: public static final String SO_KEEPALIVE = "SOL_SOCKET.SO_KEEPALIVE";
041: public static final String SO_LINGER = "SOL_SOCKET.SO_LINGER";
042: public static final String TCP_NODELAY = "IPPROTO_TCP.TCP_NODELAY";
043: static final String SO_TIMEOUT = "SOL_SOCKET.SO_TIMEOUT";
044:
045: public static final long MAX_TIMEOUT_MILLIS = Long.MAX_VALUE;
046: public static final long DEFAULT_CONNECTION_TIMEOUT_MILLIS = MAX_TIMEOUT_MILLIS;
047: public static final long DEFAULT_IDLE_TIMEOUT_MILLIS = MAX_TIMEOUT_MILLIS;
048:
049: public enum FlushMode {
050: SYNC, ASYNC
051: };
052:
053: public static final FlushMode DEFAULT_FLUSH_MODE = FlushMode.SYNC;
054: public static final boolean DEFAULT_AUTOFLUSH = true;
055:
056: /**
057: * returns the id
058: *
059: * @return id
060: */
061: public String getId();
062:
063: /**
064: * returns, if the connection is open. <br><br>
065: *
066: * Please note, that a connection could be closed, but reading of already
067: * received (and internally buffered) data would not fail. See also
068: * {@link IDataHandler#onData(INonBlockingConnection)}
069: *
070: *
071: * @return true if the connection is open
072: */
073: public boolean isOpen();
074:
075: /**
076: * returns the local port
077: *
078: * @return the local port
079: */
080: public int getLocalPort();
081:
082: /**
083: * returns the local address
084: *
085: * @return the local IP address or InetAddress.anyLocalAddress() if the socket is not bound yet.
086: */
087: public InetAddress getLocalAddress();
088:
089: /**
090: * returns the remote address
091: *
092: * @return the remote address
093: */
094: public InetAddress getRemoteAddress();
095:
096: /**
097: * returns the port of the remote end point
098: *
099: * @return the remote port
100: */
101: public int getRemotePort();
102:
103: /**
104: * returns the idle timeout in millis.
105: *
106: * @return idle timeout in millis
107: */
108: public long getIdleTimeoutMillis();
109:
110: /**
111: * sets the idle timeout in millis
112: *
113: * @param timeoutInSec idle timeout in millis
114: */
115: public void setIdleTimeoutMillis(long timeoutInMillis);
116:
117: /**
118: * gets the connection timeout
119: *
120: * @return connection timeout
121: */
122: public long getConnectionTimeoutMillis();
123:
124: /**
125: * sets the max time for a connections. By
126: * exceeding this time the connection will be
127: * terminated
128: *
129: * @param timeoutSec the connection timeout in millis
130: */
131: public void setConnectionTimeoutMillis(long timeoutMillis);
132:
133: /**
134: * returns the remaining time before a idle timeout occurs
135: *
136: * @return the remaining time
137: */
138: public long getRemainingMillisToIdleTimeout();
139:
140: /**
141: * returns the remaining time before a connection timeout occurs
142: *
143: * @return the remaining time
144: */
145: public long getRemainingMillisToConnectionTimeout();
146:
147: /**
148: * sets the value of a option. <br><br>
149: *
150: * A good article for tuning can be found here {@link http://www.onlamp.com/lpt/a/6324}
151: *
152: * @param name the name of the option
153: * @param value the value of the option
154: * @throws IOException In an I/O error occurs
155: */
156: public void setOption(String name, Object value) throws IOException;
157:
158: /**
159: * returns the value of a option
160: *
161: * @param name the name of the option
162: * @return the value of the option
163: * @throws IOException In an I/O error occurs
164: */
165: public Object getOption(String name) throws IOException;
166:
167: /**
168: * Returns an unmodifiable map of the options supported by this end point.
169: *
170: * The key in the returned map is the name of a option, and its value
171: * is the type of the option value. The returned map will never contain null keys or values.
172: *
173: * @return An unmodifiable map of the options supported by this channel
174: */
175: @SuppressWarnings("unchecked")
176: public java.util.Map<String, Class> getOptions();
177:
178: /**
179: * Attaches the given object to this connection
180: *
181: * @param obj The object to be attached; may be null
182: * @return The previously-attached object, if any, otherwise null
183: */
184: public void setAttachment(Object obj);
185:
186: /**
187: * Retrieves the current attachment.
188: *
189: * @return The object currently attached to this key, or null if there is no attachment
190: */
191: public Object getAttachment();
192:
193: /**
194: * returns if the connection is in secured mode
195: * @return true, if the connection is in secured mode
196: */
197: public boolean isSecure();
198:
199: }
|