KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xsocket > stream > io > impl > ChainableIoHandler


1 // $Id: IoHandlerBase.java 1315 2007-06-10 08:05:00Z 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.io.impl;
23
24 import java.io.IOException JavaDoc;
25 import java.net.InetAddress JavaDoc;
26 import java.nio.ByteBuffer JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.xsocket.stream.io.spi.IIoHandler;
31 import org.xsocket.stream.io.spi.IIoHandlerCallback;
32
33
34
35 /**
36  * internal IoHandler
37  *
38  *
39  * @author grro@xsocket.org
40  */

41 abstract class ChainableIoHandler implements IIoHandler {
42     
43     private ChainableIoHandler successor = null;
44     private ChainableIoHandler previous = null;
45     
46     private IIoHandlerCallback callback = null;
47     
48     
49     /**
50      * constructor
51      *
52      * @param successor the sucessor
53      */

54     public ChainableIoHandler(ChainableIoHandler successor) {
55         setSuccessor(successor);
56     }
57     
58     
59     /**
60      * return the successor
61      *
62      * @return the successor
63      */

64     public final ChainableIoHandler getSuccessor() {
65         return successor;
66     }
67     
68     
69     /**
70      * set the successor
71      *
72      * @param successor the successor
73      */

74     protected final void setSuccessor(ChainableIoHandler successor) {
75         this.successor = successor;
76         if (successor != null) {
77             successor.setPrevious(this);
78         }
79     }
80
81     
82     /**
83      * set the previous IoHandler
84      * @param previous the previous IoHandler
85      */

86     protected final void setPrevious(ChainableIoHandler previous) {
87         this.previous = previous;
88     }
89
90     
91     /**
92      * get the previous IoHandler
93      * @return the previous IoHandler
94      */

95     protected final ChainableIoHandler getPrevious() {
96         return previous;
97     }
98     
99     
100     /**
101      * get the local address of the underlying connection
102      *
103      * @return the local address of the underlying connection
104      */

105     public InetAddress JavaDoc getLocalAddress() {
106         return getSuccessor().getLocalAddress();
107     }
108     
109     
110     /**
111      * {@inheritDoc}
112      */

113     public int getPendingWriteDataSize() {
114         ChainableIoHandler successor = getSuccessor();
115         if (successor != null) {
116             return successor.getPendingWriteDataSize();
117         } else {
118             return 0;
119         }
120     }
121
122     
123     int getPendingReceiveDataSize() {
124         ChainableIoHandler successor = getSuccessor();
125         if (successor != null) {
126             return successor.getPendingReceiveDataSize();
127         } else {
128             return 0;
129         }
130     }
131
132     
133
134     public void suspendRead() throws IOException JavaDoc {
135         ChainableIoHandler successor = getSuccessor();
136         if (successor != null) {
137             successor.suspendRead();
138         }
139     }
140     
141     public void resumeRead() throws IOException JavaDoc {
142         ChainableIoHandler successor = getSuccessor();
143         if (successor != null) {
144             successor.resumeRead();
145         }
146     }
147     
148     
149     public String JavaDoc getId() {
150         return getSuccessor().getId();
151     }
152
153     
154     void setPreviousCallback(IIoHandlerCallback callback) {
155         this.callback = callback;
156     }
157
158     IIoHandlerCallback getPreviousCallback() {
159         return callback;
160     }
161     
162     
163     /**
164      * get the local port of the underlying connection
165      *
166      * @return the local port of the underlying connection
167      */

168     public int getLocalPort() {
169         return getSuccessor().getLocalPort();
170     }
171     
172     
173     
174     /**
175      * get the address of the remote host of the underlying connection
176      *
177      * @return the address of the remote host of the underlying connection
178      */

179     public InetAddress JavaDoc getRemoteAddress() {
180         return getSuccessor().getRemoteAddress();
181     }
182     
183     
184     /**
185      * get the port of the remote host of the underlying connection
186      *
187      * @return the port of the remote host of the underlying connection
188      */

189     public int getRemotePort() {
190         return getSuccessor().getRemotePort();
191     }
192     
193     
194     /**
195      * check, if handler is open
196      *
197      * @return true, if the handler is open
198      */

199     public boolean isOpen() {
200         return getSuccessor().isOpen();
201     }
202     
203     
204     /**
205      * {@inheritDoc}
206      */

207     public void setIdleTimeoutSec(int timeout) {
208         getSuccessor().setIdleTimeoutSec(timeout);
209     }
210     
211     
212     
213     /**
214      * sets the connection timout
215      *
216      * @param timeout the connection timeout
217      */

218     public void setConnectionTimeoutSec(int timeout) {
219         getSuccessor().setConnectionTimeoutSec(timeout);
220     }
221     
222     
223     /**
224      * gets the connection timeout
225      *
226      * @return the connection timeout
227      */

228     public int getConnectionTimeoutSec() {
229         return getSuccessor().getConnectionTimeoutSec();
230     }
231     
232     
233     
234     /**
235      * {@inheritDoc}
236      */

237     public int getIdleTimeoutSec() {
238         return getSuccessor().getIdleTimeoutSec();
239     }
240     
241
242
243
244     /**
245      * {@inheritDoc}
246      */

247     public Object JavaDoc getOption(String JavaDoc name) throws IOException JavaDoc {
248         return getSuccessor().getOption(name);
249     }
250
251     
252     /**
253      * {@inheritDoc}
254      */

255     public Map JavaDoc<String JavaDoc, Class JavaDoc> getOptions() {
256         return getSuccessor().getOptions();
257     }
258     
259     
260     /**
261      * {@inheritDoc}
262      */

263     public void setOption(String JavaDoc name, Object JavaDoc value) throws IOException JavaDoc {
264         getSuccessor().setOption(name, value);
265     }
266     
267     
268     /**
269      * flush the outgoing buffers
270      *
271      * @throws IOException If some other I/O error occurs
272      */

273     public abstract void flushOutgoing() throws IOException JavaDoc;
274     
275     
276     
277     /**
278      * drain the handler's read buffer
279      *
280      * @return the content of the handler's read buffer
281      */

282     public abstract LinkedList JavaDoc<ByteBuffer JavaDoc> drainIncoming();
283     
284     
285     
286     @Override JavaDoc
287     public String JavaDoc toString() {
288         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(this.getClass().getSimpleName() + "#" + hashCode());
289         
290         
291         ChainableIoHandler ioHandler = this;
292         while (ioHandler.getPrevious() != null) {
293             ioHandler = ioHandler.getPrevious();
294         }
295         
296         String JavaDoc forwardChain = "";
297         while (ioHandler != null) {
298             forwardChain = forwardChain + ioHandler.getClass().getSimpleName() + " > ";
299             ioHandler = ioHandler.getSuccessor();
300         }
301         forwardChain = forwardChain.trim();
302         forwardChain = forwardChain.substring(0, forwardChain.length() - 1);
303         sb.append(" (forward chain: " + forwardChain.trim() + ")");
304         
305         return sb.toString();
306     }
307 }
308
Popular Tags