1 22 package org.xsocket.stream.io.impl; 23 24 import java.io.IOException ; 25 import java.net.InetAddress ; 26 import java.nio.ByteBuffer ; 27 import java.util.LinkedList ; 28 import java.util.Map ; 29 30 import org.xsocket.stream.io.spi.IIoHandler; 31 import org.xsocket.stream.io.spi.IIoHandlerCallback; 32 33 34 35 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 54 public ChainableIoHandler(ChainableIoHandler successor) { 55 setSuccessor(successor); 56 } 57 58 59 64 public final ChainableIoHandler getSuccessor() { 65 return successor; 66 } 67 68 69 74 protected final void setSuccessor(ChainableIoHandler successor) { 75 this.successor = successor; 76 if (successor != null) { 77 successor.setPrevious(this); 78 } 79 } 80 81 82 86 protected final void setPrevious(ChainableIoHandler previous) { 87 this.previous = previous; 88 } 89 90 91 95 protected final ChainableIoHandler getPrevious() { 96 return previous; 97 } 98 99 100 105 public InetAddress getLocalAddress() { 106 return getSuccessor().getLocalAddress(); 107 } 108 109 110 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 { 135 ChainableIoHandler successor = getSuccessor(); 136 if (successor != null) { 137 successor.suspendRead(); 138 } 139 } 140 141 public void resumeRead() throws IOException { 142 ChainableIoHandler successor = getSuccessor(); 143 if (successor != null) { 144 successor.resumeRead(); 145 } 146 } 147 148 149 public String 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 168 public int getLocalPort() { 169 return getSuccessor().getLocalPort(); 170 } 171 172 173 174 179 public InetAddress getRemoteAddress() { 180 return getSuccessor().getRemoteAddress(); 181 } 182 183 184 189 public int getRemotePort() { 190 return getSuccessor().getRemotePort(); 191 } 192 193 194 199 public boolean isOpen() { 200 return getSuccessor().isOpen(); 201 } 202 203 204 207 public void setIdleTimeoutSec(int timeout) { 208 getSuccessor().setIdleTimeoutSec(timeout); 209 } 210 211 212 213 218 public void setConnectionTimeoutSec(int timeout) { 219 getSuccessor().setConnectionTimeoutSec(timeout); 220 } 221 222 223 228 public int getConnectionTimeoutSec() { 229 return getSuccessor().getConnectionTimeoutSec(); 230 } 231 232 233 234 237 public int getIdleTimeoutSec() { 238 return getSuccessor().getIdleTimeoutSec(); 239 } 240 241 242 243 244 247 public Object getOption(String name) throws IOException { 248 return getSuccessor().getOption(name); 249 } 250 251 252 255 public Map <String , Class > getOptions() { 256 return getSuccessor().getOptions(); 257 } 258 259 260 263 public void setOption(String name, Object value) throws IOException { 264 getSuccessor().setOption(name, value); 265 } 266 267 268 273 public abstract void flushOutgoing() throws IOException ; 274 275 276 277 282 public abstract LinkedList <ByteBuffer > drainIncoming(); 283 284 285 286 @Override 287 public String toString() { 288 StringBuilder sb = new StringBuilder (this.getClass().getSimpleName() + "#" + hashCode()); 289 290 291 ChainableIoHandler ioHandler = this; 292 while (ioHandler.getPrevious() != null) { 293 ioHandler = ioHandler.getPrevious(); 294 } 295 296 String 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 |