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.http;
022:
023: import java.io.IOException;
024: import java.nio.BufferUnderflowException;
025: import java.nio.ByteBuffer;
026:
027: import org.xsocket.Execution;
028: import org.xsocket.MaxReadSizeExceededException;
029: import org.xsocket.connection.IConnectHandler;
030: import org.xsocket.connection.IDataHandler;
031: import org.xsocket.connection.IDisconnectHandler;
032: import org.xsocket.connection.INonBlockingConnection;
033: import org.xsocket.connection.NonBlockingConnection;
034: import org.xsocket.connection.Server;
035: import org.xsocket.connection.multiplexed.MultiplexedConnection;
036:
037: /**
038: *
039: * @author grro@xsocket.org
040: */
041: public final class TcpConcentratorServer extends Server {
042:
043: public TcpConcentratorServer(int listenport, String forwardHost,
044: int forwardPort) throws IOException {
045: super (listenport, new ForwardHandler(forwardHost, forwardPort));
046: }
047:
048: public static void main(String... args) throws IOException {
049: if (args.length != 3) {
050: System.out
051: .println("usage org.xsocket.connection.http.TcpMultiplexerServer <listenport> <forwardhost> <forwardport>");
052: }
053:
054: new TcpConcentratorServer(Integer.parseInt(args[0]), args[1],
055: Integer.parseInt(args[2])).run();
056: }
057:
058: private static class Handler implements IDataHandler,
059: IDisconnectHandler {
060:
061: @Execution(Execution.NONTHREADED)
062: public final boolean onData(INonBlockingConnection connection)
063: throws IOException, BufferUnderflowException,
064: MaxReadSizeExceededException {
065: INonBlockingConnection forwardPipeline = (INonBlockingConnection) connection
066: .getAttachment();
067:
068: ByteBuffer[] data = connection
069: .readByteBufferByLength(connection.available());
070:
071: ByteBuffer[] copy = new ByteBuffer[data.length];
072: for (int i = 0; i < data.length; i++) {
073: copy[i] = data[i].duplicate();
074: }
075:
076: forwardPipeline.write(data);
077:
078: return true;
079: }
080:
081: @Execution(Execution.NONTHREADED)
082: public final boolean onDisconnect(
083: INonBlockingConnection connection) throws IOException {
084:
085: INonBlockingConnection peerConnection = (INonBlockingConnection) connection
086: .getAttachment();
087: if (peerConnection != null) {
088: peerConnection.close();
089: connection.setAttachment(null);
090: }
091:
092: connection.close();
093: //System.out.println("proxy connection destroyed " + connection.getId() + " -> " + peerConnection.getId());
094:
095: return true;
096: }
097: }
098:
099: private static final class ForwardHandler extends Handler implements
100: IConnectHandler {
101:
102: private static final long IDLE_TIMEOUT_MILLIS = 60 * 1000;
103:
104: private MultiplexedConnection forwardMultiplexedConnection = null;
105:
106: public ForwardHandler(String forwardHost, int forwardPort)
107: throws IOException {
108: forwardMultiplexedConnection = new MultiplexedConnection(
109: new NonBlockingConnection(forwardHost, forwardPort));
110: }
111:
112: @Execution(Execution.MULTITHREADED)
113: public boolean onConnect(INonBlockingConnection connection)
114: throws IOException, BufferUnderflowException,
115: MaxReadSizeExceededException {
116:
117: connection.setIdleTimeoutMillis(IDLE_TIMEOUT_MILLIS);
118:
119: String pipelineId = forwardMultiplexedConnection
120: .createPipeline();
121: INonBlockingConnection forwardPipeline = forwardMultiplexedConnection
122: .getNonBlockingPipeline(pipelineId);
123: forwardPipeline.setHandler(new Handler());
124:
125: forwardPipeline
126: .setIdleTimeoutMillis(IDLE_TIMEOUT_MILLIS * 2);
127:
128: connection.setAttachment(forwardPipeline);
129: forwardPipeline.setAttachment(connection);
130:
131: //System.out.println("proxy connection established " + connection.getId() + " <-> " + pipelineId);
132: return true;
133: }
134: }
135:
136: }
|