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.spi;
022:
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.net.InetAddress;
026: import java.net.Socket;
027: import java.net.SocketTimeoutException;
028: import java.nio.ByteBuffer;
029: import java.nio.channels.ClosedChannelException;
030: import java.util.Map;
031:
032: import org.xsocket.DataConverter;
033: import org.xsocket.connection.spi.IIoHandler;
034: import org.xsocket.connection.spi.IIoHandlerCallback;
035:
036: /**
037: *
038: * @author grro@xsocket.org
039: */
040: final class ExampleIoHandler implements IIoHandler {
041:
042: private volatile boolean isRunning = true;
043:
044: private Socket socket = null;
045: private Thread readerThread = null;
046:
047: private long conectionTimeoutMillis = 60 * 1000;
048: private long idleTimeoutMillis = 60 * 1000;
049:
050: private IIoHandlerCallback callback = null;
051: private String id = null;
052:
053: public ExampleIoHandler(String id, Socket socket) {
054: this .id = id;
055: this .socket = socket;
056: }
057:
058: public void init(final IIoHandlerCallback callbackHandler)
059: throws IOException, SocketTimeoutException {
060: this .callback = callbackHandler;
061:
062: readerThread = new Thread() {
063: @Override
064: public void run() {
065: setName("readerThread");
066:
067: try {
068: InputStream is = socket.getInputStream();
069: byte[] buffer = new byte[1024];
070: int length = 0;
071:
072: while ((length = is.read(buffer)) > 0) {
073: if (!isRunning) {
074: break;
075: }
076:
077: ByteBuffer bb = ByteBuffer.wrap(buffer, 0,
078: length);
079: callbackHandler.onData(new ByteBuffer[] { bb });
080:
081: buffer = new byte[socket.getReceiveBufferSize()];
082:
083: }
084:
085: } catch (IOException ioe) {
086: ioe.printStackTrace();
087: }
088: }
089: };
090: readerThread.start();
091:
092: callbackHandler.onConnect();
093: }
094:
095: public boolean isOpen() {
096: return !socket.isClosed();
097: }
098:
099: public void resumeRead() {
100:
101: }
102:
103: public void suspendRead() {
104:
105: }
106:
107: public boolean reset() {
108: return true;
109: }
110:
111: public void close(boolean immediate) throws IOException {
112: shutdown();
113:
114: callback.onDisconnect();
115: }
116:
117: public int getPendingWriteDataSize() {
118: return 0;
119: }
120:
121: public boolean hasDataToSend() {
122: return false;
123: }
124:
125: public int getPendingReadDataSize() {
126: return 0;
127: }
128:
129: public void write(ByteBuffer[] buffers)
130: throws ClosedChannelException, IOException {
131: for (ByteBuffer buffer : buffers) {
132: try {
133: byte[] bytes = DataConverter.toBytes(buffer);
134: socket.getOutputStream().write(bytes);
135: socket.getOutputStream().flush();
136:
137: callback.onWritten(buffer);
138: } catch (final IOException ioe) {
139: callback.onWriteException(ioe, buffer);
140: }
141: }
142: }
143:
144: public InetAddress getLocalAddress() {
145: return socket.getLocalAddress();
146: }
147:
148: public int getLocalPort() {
149: return socket.getLocalPort();
150: }
151:
152: public InetAddress getRemoteAddress() {
153: return socket.getInetAddress();
154: }
155:
156: public int getRemotePort() {
157: return socket.getPort();
158: }
159:
160: public long getConnectionTimeoutMillis() {
161: return conectionTimeoutMillis;
162: }
163:
164: public String getId() {
165: return id;
166: }
167:
168: public long getIdleTimeoutMillis() {
169: return idleTimeoutMillis;
170: }
171:
172: public void setConnectionTimeoutMillis(long timeout) {
173: this .conectionTimeoutMillis = timeout;
174:
175: }
176:
177: public void setIdleTimeoutMillis(long timeout) {
178: this .idleTimeoutMillis = timeout;
179: }
180:
181: public long getRemainingMillisToConnectionTimeout() {
182: return 0;
183: }
184:
185: public long getRemainingMillisToIdleTimeout() {
186: return 0;
187: }
188:
189: public void setOption(String name, Object value) throws IOException {
190:
191: }
192:
193: public Map<String, Class> getOptions() {
194: return null;
195: }
196:
197: public Object getOption(String name) throws IOException {
198: return null;
199: }
200:
201: void shutdown() {
202: isRunning = false;
203: readerThread.interrupt();
204: try {
205: socket.close();
206: } catch (Exception ignore) {
207: }
208: }
209: }
|