1 20 package org.apache.mina.example.proxy; 21 22 import java.net.InetSocketAddress ; 23 24 import org.apache.mina.common.ConnectFuture; 25 import org.apache.mina.common.IoConnector; 26 import org.apache.mina.common.IoFuture; 27 import org.apache.mina.common.IoFutureListener; 28 import org.apache.mina.common.IoSession; 29 import org.apache.mina.common.RuntimeIOException; 30 import org.apache.mina.common.TrafficMask; 31 32 39 public class ClientToProxyIoHandler extends AbstractProxyIoHandler { 40 private final ServerToProxyIoHandler connectorHandler; 41 42 private final IoConnector connector; 43 44 private final InetSocketAddress address; 45 46 public ClientToProxyIoHandler(ServerToProxyIoHandler connectorHandler, 47 IoConnector connector, InetSocketAddress address) { 48 this.connectorHandler = connectorHandler; 49 this.connector = connector; 50 this.address = address; 51 } 52 53 public void sessionOpened(final IoSession session) throws Exception { 54 connector.connect(address, connectorHandler).addListener( 55 new IoFutureListener() { 56 public void operationComplete(IoFuture f) { 57 ConnectFuture future = (ConnectFuture) f; 58 try { 59 future.getSession().setAttachment(session); 60 session.setAttachment(future.getSession()); 61 future.getSession().setTrafficMask(TrafficMask.ALL); 62 } catch (RuntimeIOException e) { 63 session.close(); 65 } finally { 66 session.setTrafficMask(TrafficMask.ALL); 67 } 68 } 69 }); 70 } 71 } 72 | Popular Tags |