KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xsocket > stream > NonBlockingConnection


1 // $Id: NonBlockingConnection.java 1564 2007-07-23 17:12:41Z grro $
2
/*
3  * Copyright (c) xsocket.org, 2006 - 2007. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
20  * The latest copy of this software may be found on http://www.xsocket.org/
21  */

22 package org.xsocket.stream;
23
24 import java.io.IOException JavaDoc;
25 import java.io.UnsupportedEncodingException JavaDoc;
26 import java.net.InetAddress JavaDoc;
27 import java.net.InetSocketAddress JavaDoc;
28 import java.nio.BufferUnderflowException JavaDoc;
29 import java.nio.ByteBuffer JavaDoc;
30 import java.nio.channels.WritableByteChannel JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.LinkedList JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.concurrent.Executor JavaDoc;
35 import java.util.concurrent.Executors JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 import javax.net.ssl.SSLContext;
40
41 import org.xsocket.ClosedConnectionException;
42 import org.xsocket.DataConverter;
43 import org.xsocket.MaxReadSizeExceededException;
44 import org.xsocket.stream.io.spi.IHandlerIoProvider;
45 import org.xsocket.stream.io.spi.IIoHandler;
46 import org.xsocket.stream.io.spi.IIoHandlerContext;
47
48
49
50
51 /**
52  * Implementation of the <code>INonBlockingConnection</code> interface. <br><br>
53  *
54  * A newly created connection is in the open state. Write or rad methods can be called immediately
55  *
56  * The methods of this class are not thread-safe.
57  *
58  * @author grro@xsocket.org
59  */

60 public final class NonBlockingConnection extends Connection implements INonBlockingConnection {
61
62     private static final Logger JavaDoc LOG = Logger.getLogger(BlockingConnection.class.getName());
63
64     private static final Executor JavaDoc DEFAULT_WORKER_POOL = Executors.newCachedThreadPool();
65
66
67
68     private IHandler appHandler = null;
69     private boolean disconnectOccured = false;
70
71
72
73
74     /**
75      * constructor. This constructor will be used to create a non blocking
76      * client-side connection. <br><br>
77      *
78      * For multithreading issues see {@link NonBlockingConnection#NonBlockingConnection(InetAddress, int, IHandler, Executor)}.
79      *
80      * @param hostname the remote host
81      * @param port the port of the remote host to connect
82      * @throws IOException If some other I/O error occurs
83      */

84     public NonBlockingConnection(String JavaDoc hostname, int port) throws IOException JavaDoc {
85         this(InetAddress.getByName(hostname), port);
86     }
87
88
89
90     /**
91      * @deprecated
92      */

93     public NonBlockingConnection(String JavaDoc hostname, int port, StreamSocketConfiguration socketConfiguration) throws IOException JavaDoc {
94         this(InetAddress.getByName(hostname), port, socketConfiguration);
95     }
96
97
98
99     /**
100      * constructor. This constructor will be used to create a non blocking
101      * client-side connection.<br><br>
102      *
103      * For multithreading issues see {@link NonBlockingConnection#NonBlockingConnection(InetAddress, int, IHandler, Executor)}.
104      *
105      * @param address the remote host
106      * @param port the port of the remote host to connect
107      * @throws IOException If some other I/O error occurs
108      */

109     public NonBlockingConnection(InetAddress JavaDoc address, int port) throws IOException JavaDoc {
110         this(new InetSocketAddress JavaDoc(address, port), new HashMap JavaDoc<String JavaDoc, Object JavaDoc>(), null, false, null, null);
111     }
112
113
114
115     /**
116      * @deprecated
117      */

118     public NonBlockingConnection(InetAddress JavaDoc address, int port, StreamSocketConfiguration socketConfiguration) throws IOException JavaDoc {
119         this(new InetSocketAddress JavaDoc(address, port), socketConfiguration.toOptions(), null, false, null, null);
120     }
121     
122     
123     /**
124      * constructor. This constructor will be used to create a non blocking
125      * client-side connection.<br><br>
126      *
127      * For multithreading issues see {@link NonBlockingConnection#NonBlockingConnection(InetAddress, int, IHandler, Executor)}.
128      *
129      * @param address the remote host
130      * @param port the port of the remote host to connect
131      * @param options the socket options
132      * @throws IOException If some other I/O error occurs
133      */

134     public NonBlockingConnection(InetAddress JavaDoc address, int port, Map JavaDoc<String JavaDoc, Object JavaDoc> options) throws IOException JavaDoc {
135         this(new InetSocketAddress JavaDoc(address, port), options, null, false, null, null);
136     }
137
138     
139
140     
141     /**
142      * constructor. This constructor will be used to create a non blocking
143      * client-side connection.<br><br>
144      *
145      * For multithreading issues see {@link NonBlockingConnection#NonBlockingConnection(InetAddress, int, IHandler, Executor)}.
146      *
147      *
148      * @param address the remote address
149      * @param port the remote port
150      * @param appHandler the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler and ITimeoutHandler)
151      * @throws IOException If some other I/O error occurs
152      */

153     public NonBlockingConnection(InetAddress JavaDoc address, int port, IHandler appHandler) throws IOException JavaDoc {
154         this(new InetSocketAddress JavaDoc(address, port), new HashMap JavaDoc<String JavaDoc, Object JavaDoc>(), null, false, appHandler, DEFAULT_WORKER_POOL);
155     }
156
157
158
159     /**
160      * @deprecated
161      */

162     public NonBlockingConnection(InetAddress JavaDoc address, int port, StreamSocketConfiguration socketConfiguration, IHandler appHandler) throws IOException JavaDoc {
163         this(new InetSocketAddress JavaDoc(address, port), socketConfiguration.toOptions(), null, false, appHandler, DEFAULT_WORKER_POOL);
164     }
165
166     
167     /**
168      * @deprecated
169      */

170     public NonBlockingConnection(InetAddress JavaDoc address, int port, StreamSocketConfiguration socketConfiguration, IHandler appHandler, int preallocationMemorySize) throws IOException JavaDoc {
171         this(new InetSocketAddress JavaDoc(address, port), socketConfiguration.toOptions(), null, false, appHandler, DEFAULT_WORKER_POOL);
172         LOG.warning("parameter preallocation memory size is not more supported. use System.property instead (see JavaDoc org.xsocket.stream.io.impl.IoProvider)");
173     }
174     
175
176
177     /**
178      * constructor. This constructor will be used to create a non blocking
179      * client-side connection. <br><br>
180      *
181      * For multithreading issues see {@link NonBlockingConnection#NonBlockingConnection(InetAddress, int, IHandler, Executor)}.
182      *
183      * @param address the remote address
184      * @param port the remote port
185      * @param options the socket options
186      * @param appHandler the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler and ITimeoutHandler)
187      * @throws IOException If some other I/O error occurs
188      */

189     public NonBlockingConnection(InetAddress JavaDoc address, int port, Map JavaDoc<String JavaDoc, Object JavaDoc> options, IHandler appHandler) throws IOException JavaDoc {
190         this(new InetSocketAddress JavaDoc(address, port), options, null, false, appHandler, DEFAULT_WORKER_POOL);
191     }
192
193
194
195     /**
196      * constructor <br><br>
197      *
198      * constructor. This constructor will be used to create a non blocking
199      * client-side connection. <br><br>
200      *
201      * For multithreading issues see {@link NonBlockingConnection#NonBlockingConnection(InetAddress, int, IHandler, Executor)}.
202      *
203      * @param hostname the remote host
204      * @param port the remote port
205      * @param appHandler the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler and ITimeoutHandler)
206      * @throws IOException If some other I/O error occurs
207      */

208     public NonBlockingConnection(String JavaDoc hostname, int port, IHandler appHandler) throws IOException JavaDoc {
209         this(new InetSocketAddress JavaDoc(hostname, port), new HashMap JavaDoc<String JavaDoc, Object JavaDoc>(), null, false, appHandler, DEFAULT_WORKER_POOL);
210     }
211
212
213
214     /**
215      * @depreacted
216      */

217     public NonBlockingConnection(String JavaDoc hostname, int port, StreamSocketConfiguration socketConfiguration, IHandler appHandler) throws IOException JavaDoc {
218         this(new InetSocketAddress JavaDoc(hostname, port), socketConfiguration.toOptions(), null, false, appHandler, DEFAULT_WORKER_POOL);
219     }
220
221
222
223     /**
224      * @depreacted
225      */

226     public NonBlockingConnection(String JavaDoc hostname, int port, StreamSocketConfiguration socketConfiguration, IHandler appHandler, int preallocationMemorySize) throws IOException JavaDoc {
227         this(new InetSocketAddress JavaDoc(hostname, port), socketConfiguration.toOptions(), null, false, appHandler, DEFAULT_WORKER_POOL);
228         LOG.warning("parameter preallocation memory size is not more supported. use System.property instead (see JavaDoc org.xsocket.stream.io.impl.IoProvider)");
229     }
230
231     
232
233     /**
234      * constructor <br><br>
235      *
236      * constructor. This constructor will be used to create a non blocking
237      * client-side connection. <br><br>
238      *
239      * For multithreading issues see {@link NonBlockingConnection#NonBlockingConnection(InetAddress, int, IHandler, Executor)}.
240      *
241      * @param hostname the remote host
242      * @param port the remote port
243      * @param options the socket options
244      * @param appHandler the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler and ITimeoutHandler)
245      * @throws IOException If some other I/O error occurs
246      */

247     public NonBlockingConnection(String JavaDoc hostname, int port, Map JavaDoc<String JavaDoc, Object JavaDoc> options, IHandler appHandler) throws IOException JavaDoc {
248         this(new InetSocketAddress JavaDoc(hostname, port), options, null, false, appHandler, DEFAULT_WORKER_POOL);
249     }
250     
251     /**
252      * constructor. This constructor will be used to create a non blocking
253      * client-side connection.<br><br>
254      *
255      * For multithreading issues see {@link NonBlockingConnection#NonBlockingConnection(InetAddress, int, IHandler, Executor)}.
256      *
257      *
258      * @param address the remote address
259      * @param port the remote port
260      * @param sslContext the ssl context to use
261      * @param sslOn true, activate SSL mode. false, ssl can be activated by user (see {@link IConnection#activateSecuredMode()})
262      * @throws IOException If some other I/O error occurs
263      */

264     public NonBlockingConnection(InetAddress JavaDoc address, int port, SSLContext sslContext, boolean sslOn) throws IOException JavaDoc {
265         this(new InetSocketAddress JavaDoc(address, port), new HashMap JavaDoc<String JavaDoc, Object JavaDoc>(), sslContext, sslOn, null, null);
266     }
267
268
269     /**
270      * @deprecated
271      */

272     public NonBlockingConnection(InetAddress JavaDoc address, int port, StreamSocketConfiguration socketConfiguration, SSLContext sslContext, boolean sslOn) throws IOException JavaDoc {
273         this(new InetSocketAddress JavaDoc(address, port), socketConfiguration.toOptions(), sslContext, sslOn, null, null);
274     }
275
276     
277     /**
278      * constructor. This constructor will be used to create a non blocking
279      * client-side connection.<br><br>
280      *
281      * For multithreading issues see {@link NonBlockingConnection#NonBlockingConnection(InetAddress, int, IHandler, Executor)}.
282      *
283      *
284      * @param address the remote address
285      * @param port the remote port
286      * @param options the socket options
287      * @param sslContext the ssl context to use
288      * @param sslOn true, activate SSL mode. false, ssl can be activated by user (see {@link IConnection#activateSecuredMode()})
289      * @throws IOException If some other I/O error occurs
290      */

291     public NonBlockingConnection(InetAddress JavaDoc address, int port, Map JavaDoc<String JavaDoc, Object JavaDoc> options, SSLContext sslContext, boolean sslOn) throws IOException JavaDoc {
292         this(new InetSocketAddress JavaDoc(address, port), options, sslContext, sslOn, null, null);
293     }
294
295
296
297     /**
298      * constructor. This constructor will be used to create a non blocking
299      * client-side connection.<br><br>
300      *
301      * For multithreading issues see {@link NonBlockingConnection#NonBlockingConnection(InetAddress, int, IHandler, Executor)}.
302      *
303      *
304      * @param hostname the remote host
305      * @param port the remote port
306      * @param sslContext the ssl context to use
307      * @param sslOn true, activate SSL mode. false, ssl can be activated by user (see {@link IConnection#activateSecuredMode()})
308      * @throws IOException If some other I/O error occurs
309      */

310     public NonBlockingConnection(String JavaDoc hostname, int port, SSLContext sslContext, boolean sslOn) throws IOException JavaDoc {
311         this(new InetSocketAddress JavaDoc(hostname, port), new HashMap JavaDoc<String JavaDoc, Object JavaDoc>(), sslContext, sslOn, null, null);
312     }
313
314
315     /**
316      * @deprecated
317      */

318     public NonBlockingConnection(String JavaDoc hostname, int port, StreamSocketConfiguration socketConfiguration, SSLContext sslContext, boolean sslOn) throws IOException JavaDoc {
319         this(new InetSocketAddress JavaDoc(hostname, port), socketConfiguration.toOptions(), sslContext, sslOn, null, null);
320     }
321
322     
323     /**
324      * constructor. This constructor will be used to create a non blocking
325      * client-side connection.<br><br>
326      *
327      * For multithreading issues see {@link NonBlockingConnection#NonBlockingConnection(InetAddress, int, IHandler, Executor)}.
328      *
329      *
330      * @param hostname the remote host
331      * @param port the remote port
332      * @param options the socket options
333      * @param sslContext the ssl context to use
334      * @param sslOn true, activate SSL mode. false, ssl can be activated by user (see {@link IConnection#activateSecuredMode()})
335      * @throws IOException If some other I/O error occurs
336      */

337     public NonBlockingConnection(String JavaDoc hostname, int port, Map JavaDoc<String JavaDoc, Object JavaDoc> options, SSLContext sslContext, boolean sslOn) throws IOException JavaDoc {
338         this(new InetSocketAddress JavaDoc(hostname, port), options, sslContext, sslOn, null, null);
339     }
340
341
342
343     /**
344      * constructor. This constructor will be used to create a non blocking
345      * client-side connection. <br><br>
346      *
347      * <b>Multithreading note</b><br>
348      * The data of the </code>NonBlockingConnection</code> will be read and written by using a central dispatcher (selector) thread.
349      * The handler's call back methods (onData, onConnect, ...) will be call by the worker pool's worker thread.
350      * By using this (client-side) constructor, the workerpool will be set manually. For a construtor which doesn't support the workerpool
351      * parameter, a default (vm singleton) CachedThreadPool {@link Executors#newCachedThreadPool()} will be used. <br>
352      * By setting the workerPool with <code>null</code>, the multithreading is "switched off". This means the call back methods will
353      * be executed by the central dispatcher thread. The workerPool can also be shared with a server, which runs in the same process. E.g.
354      * <pre>
355      * ...
356      * // create a new server instance (a associated WorkerPool will be created automatically)
357      * IMultithreadedServer server = new MultithreadedServer(new TestHandler());
358      * StreamUtils.start(server);
359      * ...
360      *
361      * INonBlockingConnection connection = new NonBlockingConnection(host, port, clientHandler, server.getWorkerpool());
362      * ...
363      *
364      * </pre>
365      *
366      *
367      *
368      * @param address the remote address
369      * @param port the remote port
370      * @param appHandler the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler and ITimeoutHandler)
371      * @param workerPool the worker pool to use or <null> to
372      * @throws IOException If some other I/O error occurs
373      */

374     public NonBlockingConnection(InetAddress JavaDoc address, int port, IHandler appHandler, Executor JavaDoc workerPool) throws IOException JavaDoc {
375         this(new InetSocketAddress JavaDoc(address, port), new HashMap JavaDoc<String JavaDoc, Object JavaDoc>(), null, false, appHandler, workerPool);
376     }
377
378
379     /**
380      * @deprecated
381      */

382     public NonBlockingConnection(InetAddress JavaDoc address, int port, IHandler appHandler, Executor JavaDoc workerPool, int preallocationMemorySize) throws IOException JavaDoc {
383         this(new InetSocketAddress JavaDoc(address, port), new HashMap JavaDoc<String JavaDoc, Object JavaDoc>(), null, false, appHandler, workerPool);
384         LOG.warning("parameter preallocation memory size is not more supported. use System.property instead (see JavaDoc org.xsocket.stream.io.impl.IoProvider)");
385     }
386
387     
388
389
390     /**
391      * client constructor, which uses a specific dispatcher
392      */

393     private NonBlockingConnection(InetSocketAddress JavaDoc remoteAddress, Map JavaDoc<String JavaDoc, Object JavaDoc> options, SSLContext sslContext, boolean sslOn, IHandler appHandler, Executor JavaDoc workerPool) throws IOException JavaDoc {
394         super(new IoHandlerContext(appHandler, workerPool), remoteAddress, options, sslContext, sslOn);
395         this.appHandler = appHandler;
396
397         if (LOG.isLoggable(Level.FINE)) {
398             if ((appHandler instanceof IConnectionScoped)) {
399                 LOG.fine("handler type IConnectionScoped is not supported in the client context");
400             }
401
402             if ((appHandler instanceof org.xsocket.ILifeCycle)) {
403                 LOG.fine("ILifeCycle is not supported in the client context");
404             }
405         }
406
407         init();
408     }
409
410
411     /**
412      * server-side constructor
413      */

414     NonBlockingConnection(IIoHandlerContext ctx, IIoHandler ioHandler, IHandler appHandler, IHandlerIoProvider ioProvider) throws IOException JavaDoc {
415         super(ctx, ioHandler, ioProvider);
416         this.appHandler = appHandler;
417
418         init();
419     }
420
421
422     IHandler getAppHandler() {
423         return appHandler;
424     }
425
426
427
428     @Override JavaDoc
429     void reset() throws IOException JavaDoc {
430         try {
431             setWriteTransferRate(UNLIMITED);
432         } catch (Exception JavaDoc e) {
433             if (LOG.isLoggable(Level.FINE)) {
434                 LOG.fine("error occured by reseting (setWriteTransferRate). Reason: " + e.toString());
435             }
436         }
437         super.reset();
438
439         setFlushmode(INonBlockingConnection.INITIAL_FLUSH_MODE);
440     }
441
442
443
444
445     /**
446      * {@inheritDoc}
447      */

448     public void setWriteTransferRate(int bytesPerSecond) throws ClosedConnectionException, IOException JavaDoc {
449
450         if (bytesPerSecond != UNLIMITED) {
451             if (getFlushmode() != FlushMode.ASYNC) {
452                 LOG.warning("setWriteTransferRate is only supported for FlushMode ASYNC. Ignore update of the transfer rate");
453                 return;
454             }
455         }
456         
457         setIoHandler(getIoProvider().setWriteTransferRate(getIoHandler(), bytesPerSecond));
458     }
459
460
461
462
463
464     /**
465      * {@inheritDoc}
466      */

467     public int getNumberOfAvailableBytes() {
468         return getReadQueue().getSize();
469     }
470
471
472     /**
473      * {@inheritDoc}
474      */

475     public ByteBuffer JavaDoc[] readAvailable() throws IOException JavaDoc, ClosedConnectionException {
476         LinkedList JavaDoc<ByteBuffer JavaDoc> buffers = extractAvailableFromReadQueue();
477         if (buffers != null) {
478             return buffers.toArray(new ByteBuffer JavaDoc[buffers.size()]);
479         } else {
480             return new ByteBuffer JavaDoc[0];
481         }
482     }
483
484
485     /**
486      * {@inheritDoc}
487      */

488     public boolean readAvailableByDelimiter(String JavaDoc delimiter, WritableByteChannel JavaDoc outputChannel) throws IOException JavaDoc, ClosedConnectionException {
489         return readAvailableByDelimiter(delimiter, getDefaultEncoding(), outputChannel);
490     }
491
492     /**
493      * {@inheritDoc}
494      */

495     public boolean readAvailableByDelimiter(String JavaDoc delimiter, String JavaDoc encoding, WritableByteChannel JavaDoc outputChannel) throws IOException JavaDoc, ClosedConnectionException {
496         return extractAvailableFromReadQueue(delimiter.getBytes(encoding), outputChannel);
497     }
498
499
500     /**
501      * {@inheritDoc}
502      */

503     public int read(ByteBuffer JavaDoc buffer) throws IOException JavaDoc {
504         int size = buffer.remaining();
505
506         int available = getNumberOfAvailableBytes();
507         if (available < size) {
508             size = available;
509         }
510
511         ByteBuffer JavaDoc[] bufs = readByteBufferByLength(size);
512         for (ByteBuffer JavaDoc buf : bufs) {
513             while (buf.hasRemaining()) {
514                 buffer.put(buf);
515             }
516         }
517
518         return size;
519     }
520
521
522     /**
523      * {@inheritDoc}
524      */

525     public byte readByte() throws IOException JavaDoc, ClosedConnectionException, BufferUnderflowException JavaDoc {
526         return extractByteFromReadQueue();
527     }
528
529
530     /**
531      * {@inheritDoc}
532      */

533     public ByteBuffer JavaDoc[] readByteBufferByDelimiter(String JavaDoc delimiter) throws IOException JavaDoc, ClosedConnectionException, BufferUnderflowException JavaDoc {
534         return readByteBufferByDelimiter(delimiter, Integer.MAX_VALUE);
535     }
536
537
538     /**
539      * {@inheritDoc}
540      */

541     public ByteBuffer JavaDoc[] readByteBufferByDelimiter(String JavaDoc delimiter, int maxLength) throws IOException JavaDoc, ClosedConnectionException, MaxReadSizeExceededException, BufferUnderflowException JavaDoc {
542         return readByteBufferByDelimiter(delimiter, getDefaultEncoding(), maxLength);
543     }
544
545
546     /**
547      * {@inheritDoc}
548      */

549     public ByteBuffer JavaDoc[] readByteBufferByDelimiter(String JavaDoc delimiter, String JavaDoc encoding, int maxLength) throws IOException JavaDoc, ClosedConnectionException, MaxReadSizeExceededException {
550         LinkedList JavaDoc<ByteBuffer JavaDoc> result = extractBytesByDelimiterFromReadQueue(delimiter.getBytes(encoding), maxLength);
551         return result.toArray(new ByteBuffer JavaDoc[result.size()]);
552     }
553
554     /**
555      * {@inheritDoc}
556      */

557     public ByteBuffer JavaDoc[] readByteBufferByLength(int length) throws IOException JavaDoc, ClosedConnectionException, BufferUnderflowException JavaDoc {
558         LinkedList JavaDoc<ByteBuffer JavaDoc> extracted = extractBytesByLength(length);
559
560         return extracted.toArray(new ByteBuffer JavaDoc[extracted.size()]);
561     }
562
563
564
565     /**
566      * {@inheritDoc}
567      */

568     public byte[] readBytesByDelimiter(String JavaDoc delimiter) throws IOException JavaDoc, ClosedConnectionException, BufferUnderflowException JavaDoc {
569         return readBytesByDelimiter(delimiter, Integer.MAX_VALUE);
570     }
571
572
573     /**
574      * {@inheritDoc}
575      */

576     public byte[] readBytesByDelimiter(String JavaDoc delimiter, int maxLength) throws IOException JavaDoc, ClosedConnectionException, MaxReadSizeExceededException, BufferUnderflowException JavaDoc {
577         return readBytesByDelimiter(delimiter, getDefaultEncoding(), maxLength);
578     }
579
580
581     /**
582      * {@inheritDoc}
583      */

584     public byte[] readBytesByDelimiter(String JavaDoc delimiter, String JavaDoc encoding, int maxLength) throws IOException JavaDoc, ClosedConnectionException, MaxReadSizeExceededException {
585         return DataConverter.toBytes(readByteBufferByDelimiter(delimiter, encoding, maxLength));
586     }
587
588
589     /**
590      * {@inheritDoc}
591      */

592     public byte[] readBytesByLength(int length) throws IOException JavaDoc, ClosedConnectionException, BufferUnderflowException JavaDoc {
593         return DataConverter.toBytes(readByteBufferByLength(length));
594     }
595
596
597     /**
598      * {@inheritDoc}
599      */

600     public double readDouble() throws IOException JavaDoc, ClosedConnectionException, BufferUnderflowException JavaDoc {
601         return extractDoubleFromReadQueue();
602     }
603
604
605     /**
606      * {@inheritDoc}
607      */

608     public int readInt() throws IOException JavaDoc, ClosedConnectionException, BufferUnderflowException JavaDoc {
609         return extractIntFromReadQueue();
610     }
611     
612     /**
613      * {@inheritDoc}
614      */

615     public short readShort() throws IOException JavaDoc, ClosedConnectionException, BufferUnderflowException JavaDoc {
616         return extractShortFromReadQueue();
617     }
618
619
620
621     /**
622      * {@inheritDoc}
623      */

624     public long readLong() throws IOException JavaDoc, ClosedConnectionException, BufferUnderflowException JavaDoc {
625         return extractLongFromReadQueue();
626     }
627
628
629     /**
630      * {@inheritDoc}
631      */

632     public String JavaDoc readStringByDelimiter(String JavaDoc delimiter) throws IOException JavaDoc ,ClosedConnectionException ,BufferUnderflowException JavaDoc ,UnsupportedEncodingException JavaDoc {
633         return readStringByDelimiter(delimiter, Integer.MAX_VALUE);
634     };
635
636
637     /**
638      * {@inheritDoc}
639      */

640     public String JavaDoc readStringByDelimiter(String JavaDoc delimiter, int maxLength) throws IOException JavaDoc ,ClosedConnectionException ,BufferUnderflowException JavaDoc ,UnsupportedEncodingException JavaDoc ,MaxReadSizeExceededException {
641         return readStringByDelimiter(delimiter, getDefaultEncoding(), maxLength);
642     };
643
644
645     /**
646      * {@inheritDoc}
647      */

648     public String JavaDoc readStringByDelimiter(String JavaDoc delimiter, String JavaDoc encoding) throws IOException JavaDoc, ClosedConnectionException, BufferUnderflowException JavaDoc, UnsupportedEncodingException JavaDoc {
649         return readStringByDelimiter(delimiter, encoding, Integer.MAX_VALUE);
650     }
651
652
653     /**
654      * {@inheritDoc}
655      */

656     public String JavaDoc readStringByDelimiter(String JavaDoc delimiter, String JavaDoc encoding, int maxLength) throws IOException JavaDoc, ClosedConnectionException, BufferUnderflowException JavaDoc, UnsupportedEncodingException JavaDoc, MaxReadSizeExceededException {
657         LinkedList JavaDoc<ByteBuffer JavaDoc> extracted = extractBytesByDelimiterFromReadQueue(delimiter.getBytes(encoding), maxLength);
658
659         return DataConverter.toString(extracted, encoding);
660     }
661
662
663     /**
664      * {@inheritDoc}
665      */

666     public String JavaDoc readStringByLength(int length) throws IOException JavaDoc, ClosedConnectionException, BufferUnderflowException JavaDoc, UnsupportedEncodingException JavaDoc {
667         return readStringByLength(length, getDefaultEncoding());
668     }
669
670
671     /**
672      * {@inheritDoc}
673      */

674     public String JavaDoc readStringByLength(int length, String JavaDoc encoding) throws IOException JavaDoc, ClosedConnectionException, BufferUnderflowException JavaDoc, UnsupportedEncodingException JavaDoc {
675         LinkedList JavaDoc<ByteBuffer JavaDoc> extracted = extractBytesByLength(length);
676         return DataConverter.toString(extracted, encoding);
677     }
678
679
680
681
682     /**
683      * {@inheritDoc}
684      */

685     public int getIndexOf(String JavaDoc str) throws IOException JavaDoc, ClosedConnectionException, BufferUnderflowException JavaDoc {
686         return getIndexOf(str, Integer.MAX_VALUE);
687     }
688
689
690     /**
691      * {@inheritDoc}
692      */

693     public int getIndexOf(String JavaDoc str, int maxLength) throws IOException JavaDoc, ClosedConnectionException, BufferUnderflowException JavaDoc, MaxReadSizeExceededException {
694         return getIndexOf(str, getDefaultEncoding(), maxLength);
695     }
696
697
698     /**
699      * {@inheritDoc}
700      */

701     public int getIndexOf(String JavaDoc str, String JavaDoc encoding, int maxLength) throws IOException JavaDoc, ClosedConnectionException, MaxReadSizeExceededException {
702         return readIndexOf(str.getBytes(encoding), maxLength);
703     }
704
705
706     /**
707      * {@inheritDoc}
708      */

709     @Override JavaDoc
710     public INonBlockingConnection setOption(String JavaDoc name, Object JavaDoc value) throws IOException JavaDoc {
711         return (INonBlockingConnection) super.setOption(name, value);
712     }
713
714     
715
716     @Override JavaDoc
717     protected int onDataEvent() {
718
719         int addSize = super.onDataEvent();
720
721         if (addSize > 0) {
722             if (appHandler != null) {
723                 boolean remaingDataToHandle = false;
724                 try {
725                     do {
726                         remaingDataToHandle = false;
727                         int insertVersion = getReadQueue().getInsertVersionVersion();
728                         int sizeBeforeHandle = getReadQueue().getSize();
729
730                         // calling onData method of the handler (return value will be ignored)
731
try {
732
733                             ((IDataHandler) appHandler).onData(NonBlockingConnection.this);
734
735                         } catch (MaxReadSizeExceededException mee) {
736                             try {
737                                 close();
738                             } catch (Exception JavaDoc fe) {
739                                 // ignore
740
}
741
742                             return addSize;
743
744                         } catch (BufferUnderflowException JavaDoc bue) {
745                             // ignore
746
return addSize;
747
748
749                         } catch (Exception JavaDoc e) {
750                             if (LOG.isLoggable(Level.FINE)) {
751                                 LOG.fine("[" + getId() + "] closing connection because an error has been occured by handling data by appHandler. " + appHandler + " Reason: " + e.toString());
752                             }
753                             try {
754                                 close();
755                             } catch (IOException JavaDoc ignore) { }
756                             return addSize;
757                         }
758
759
760                         // check if there is more data in readQueue, to decide if handle should be called again
761
if (!getReadQueue().isEmpty()) {
762                             
763                             // has data be inserted meanwhile?
764
if (insertVersion != getReadQueue().getInsertVersionVersion()) {
765                                 // yes ... re-run loop
766
remaingDataToHandle = true;
767                             
768                             // no, than ...
769
} else {
770                                 // has data size of queue been changed?
771
if (sizeBeforeHandle != getReadQueue().getSize()) {
772                                     // yes ... re-run loop
773
remaingDataToHandle = true;
774                                 }
775                             }
776                         }
777
778                     } while (remaingDataToHandle);
779
780                     
781                 } catch (Exception JavaDoc e) {
782                     if (LOG.isLoggable(Level.FINE)) {
783                         LOG.fine("[" + getId() + "] closing connection because an error has been occured by handling data. Reason: " + e.toString());
784                     }
785                     try {
786                         close();
787                     } catch (IOException JavaDoc ignore) { }
788                 }
789             }
790         }
791
792         return addSize;
793     }
794
795
796
797
798
799
800
801     @Override JavaDoc
802     protected void onConnectEvent() {
803
804         try {
805             if (appHandler != null) {
806                 if (getIoHandlerContext().isAppHandlerListenForConnectEvent()) {
807                     ((IConnectHandler) appHandler).onConnect(NonBlockingConnection.this);
808                 }
809             }
810
811         } catch (MaxReadSizeExceededException mee) {
812             try {
813                 close();
814             } catch (Exception JavaDoc fe) {
815                 // ignore
816
}
817
818         } catch (BufferUnderflowException JavaDoc bue) {
819             // ignore
820

821         } catch (Exception JavaDoc e) {
822             if (LOG.isLoggable(Level.FINE)) {
823                 LOG.fine("[" + getId() + "] closing connection because an error has been occured by on connect data. Reason: " + e.toString());
824             }
825             try {
826                 close();
827             } catch (IOException JavaDoc ignore) { }
828         }
829     }
830
831
832
833     @Override JavaDoc
834     protected void onDisconnectEvent() {
835         if (!disconnectOccured) {
836             disconnectOccured = true;
837             try {
838                 if (appHandler != null) {
839                     if (getIoHandlerContext().isAppHandlerListenforDisconnectEvent()) {
840                         ((IDisconnectHandler) appHandler).onDisconnect(NonBlockingConnection.this);
841                     }
842                 }
843             } catch (Exception JavaDoc e) {
844                 if (LOG.isLoggable(Level.FINE)) {
845                     LOG.fine("[" + getId() + "] error occured by handling connect. Reason: " + e.toString());
846                 }
847             }
848         }
849     }
850
851
852     @Override JavaDoc
853     protected boolean onConnectionTimeoutEvent() {
854         if (getIoHandlerContext().isAppHandlerListenForTimeoutEvent()) {
855             try {
856                 if (appHandler != null) {
857                     boolean isHandled = ((ITimeoutHandler) appHandler).onConnectionTimeout(NonBlockingConnection.this);
858                     return isHandled;
859                 }
860             } catch (MaxReadSizeExceededException mee) {
861                 try {
862                     close();
863                 } catch (Exception JavaDoc fe) {
864                     // ignore
865
}
866
867             } catch (BufferUnderflowException JavaDoc bue) {
868                 // ignore
869

870             } catch (Exception JavaDoc e) {
871                 if (LOG.isLoggable(Level.FINE)) {
872                     LOG.fine("[" + getId() + "] closing connection because an error has been occured by on connect timeout. Reason: " + e.toString());
873                 }
874                 try {
875                     close();
876                 } catch (IOException JavaDoc ignore) { }
877             }
878         }
879
880         return false;
881     }
882
883
884     @Override JavaDoc
885     protected boolean onIdleTimeoutEvent() {
886         if (getIoHandlerContext().isAppHandlerListenForTimeoutEvent()) {
887             try {
888                 if (appHandler != null) {
889                     boolean isHandled = ((ITimeoutHandler) appHandler).onIdleTimeout(NonBlockingConnection.this);
890                     return isHandled;
891                 }
892             } catch (MaxReadSizeExceededException mee) {
893                 try {
894                     close();
895                 } catch (Exception JavaDoc fe) {
896                     // ignore
897
}
898
899             } catch (BufferUnderflowException JavaDoc bue) {
900                 // ignore
901

902             } catch (Exception JavaDoc e) {
903                 if (LOG.isLoggable(Level.FINE)) {
904                     LOG.fine("[" + getId() + "] closing connection because an error has been occured by on idle timeout. Reason: " + e.toString());
905                 }
906                 try {
907                     close();
908                 } catch (IOException JavaDoc ignore) { }
909             }
910         };
911
912         return false;
913     }
914 }
915
Popular Tags