From 0cd2d53496b3819d5f8cccb38400481e2faeda03 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Mon, 12 Apr 2010 20:09:32 +0000 Subject: [PATCH] moved socketproxy to https://wamblee.org/svn/public/socketproxy --- pom.xml | 1 - socketproxy/pom.xml | 26 ---- .../java/org/wamblee/socketproxy/Barrier.java | 46 ------- .../wamblee/socketproxy/ForwarderThread.java | 103 --------------- .../org/wamblee/socketproxy/SocketProxy.java | 118 ------------------ 5 files changed, 294 deletions(-) delete mode 100644 socketproxy/pom.xml delete mode 100644 socketproxy/src/main/java/org/wamblee/socketproxy/Barrier.java delete mode 100644 socketproxy/src/main/java/org/wamblee/socketproxy/ForwarderThread.java delete mode 100644 socketproxy/src/main/java/org/wamblee/socketproxy/SocketProxy.java diff --git a/pom.xml b/pom.xml index b4270ec1..0a74bef9 100644 --- a/pom.xml +++ b/pom.xml @@ -510,7 +510,6 @@ - socketproxy mythtv diff --git a/socketproxy/pom.xml b/socketproxy/pom.xml deleted file mode 100644 index efb2d06d..00000000 --- a/socketproxy/pom.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - org.wamblee - wamblee-utils - 0.2-SNAPSHOT - - - 4.0.0 - org.wamblee - wamblee-socketproxy - jar - - /socketproxy - http://wamblee.org - - - org.wamblee - wamblee-support-general - 0.2-SNAPSHOT - - - - diff --git a/socketproxy/src/main/java/org/wamblee/socketproxy/Barrier.java b/socketproxy/src/main/java/org/wamblee/socketproxy/Barrier.java deleted file mode 100644 index b703f48e..00000000 --- a/socketproxy/src/main/java/org/wamblee/socketproxy/Barrier.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2006 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.wamblee.socketproxy; - -public class Barrier { - - private int _countLeft; - - public Barrier( int aCount ) { - _countLeft = aCount; - } - - public synchronized void block( ) { - _countLeft--; - if ( _countLeft < 0 ) { - throw new IllegalStateException( - "Barrier count became negative, programming error" ); - } - notifyAll( ); - while ( _countLeft > 0 ) { - waitUninterruptable( ); - } - } - - private void waitUninterruptable( ) { - try { - wait( ); - } catch ( InterruptedException e ) { - // ignore. - } - } - -} diff --git a/socketproxy/src/main/java/org/wamblee/socketproxy/ForwarderThread.java b/socketproxy/src/main/java/org/wamblee/socketproxy/ForwarderThread.java deleted file mode 100644 index 22008c94..00000000 --- a/socketproxy/src/main/java/org/wamblee/socketproxy/ForwarderThread.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright 2007 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.wamblee.socketproxy; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -/** - * Forwarder thread which handles forwarding of an input stream to - * an output stream. - * - * @author Erik Brakkee - */ - -public class ForwarderThread extends Thread { - - private String _prefix; - - private Barrier _barrier; - - private InputStream _is; - - private OutputStream _os; - - /** - * Constructs the forwarder thread. - * @param aPrefix Prefix to use in the output. - * @param aBarrier Barrier to block on before actually closing the stream. - * This is done to make sure that connections are only closed in th e - * proxy when the forwarders in both directions are ready to close. - * @param aIs Input stream to read from. - * @param aOs Output stream to forward to. - */ - public ForwarderThread( String aPrefix, Barrier aBarrier, - InputStream aIs, OutputStream aOs ) { - _prefix = aPrefix; - _is = aIs; - _os = aOs; - _barrier = aBarrier; - } - - public void run( ) { - boolean firstChar = true; - try { - int c = _is.read( ); - while ( c > 0 ) { - try { - _os.write( c ); - _os.flush( ); - if ( firstChar ) { - System.out.print( _prefix ); - firstChar = false; - } - System.out.print( (char) c ); - if ( c == '\n' ) { - firstChar = true; - } - } catch ( IOException e ) { - } - - c = _is.read( ); - } - } catch ( IOException e ) { - } - closeStreams(); - } - - /** - * @param is - * @param os - */ - private void closeStreams( ) { - _barrier.block( ); // wait until the other forwarder for the other direction - // is also closed. - try { - _is.close( ); - } catch ( IOException e1 ) { - // Empty. - } - try { - _os.flush( ); - _os.close( ); - } catch ( IOException e1 ) { - // Empty - } - System.out.println(_prefix + " closed"); - } - -} diff --git a/socketproxy/src/main/java/org/wamblee/socketproxy/SocketProxy.java b/socketproxy/src/main/java/org/wamblee/socketproxy/SocketProxy.java deleted file mode 100644 index a4b1deaa..00000000 --- a/socketproxy/src/main/java/org/wamblee/socketproxy/SocketProxy.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright 2007 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.wamblee.socketproxy; - -/* - * Created on Apr 5, 2005 - */ - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.ServerSocket; -import java.net.Socket; - -/** - * @author erik - * - * TODO To change the template for this generated type comment go to Window - - * Preferences - Java - Code Style - Code Templates - * - * @author Erik Brakkee - */ -public class SocketProxy { - - public static void main( final String[] args ) throws IOException { - for ( int i = 0; i < args.length; i++ ) { - // System.out.println(i + " " + args[i]); - String[] fields = args[i].split( ":" ); - final int localPort = Integer.parseInt( fields[0] ); - final String host = fields[1]; - final int remotePort = Integer.parseInt( fields[2] ); - runSocketProxy( localPort, host, remotePort ); - } - } - - /** - * @param localPort - * @param host - * @param remotePort - */ - private static void runSocketProxy( final int localPort, - final String host, final int remotePort ) { - new Thread( new Runnable( ) { - public void run( ) { - try { - new SocketProxy( localPort, host, remotePort ); - } catch ( IOException e ) { - System.out.println( "Problem with socket " + localPort - + ":" + host + ":" + remotePort ); - e.printStackTrace( ); - } - } - } ).start( ); - } - - public SocketProxy( int localPort, String remoteHost, int remotePort ) - throws IOException { - System.out.println( "Listening on port " + localPort ); - ServerSocket server = new ServerSocket( localPort ); - for ( ;; ) { - Socket socket = server.accept( ); - System.out.println( "Got local connection on port " - + localPort ); - InputStream localIs = socket.getInputStream( ); - OutputStream localOs = socket.getOutputStream( ); - Socket clientSocket = new Socket( remoteHost, remotePort ); - final String description = "Port forwarding: " + localPort - + " -> " + remoteHost + ":" + remotePort; - System.out.println( description + " established." ); - InputStream serverIs = clientSocket.getInputStream( ); - OutputStream serverOs = clientSocket.getOutputStream( ); - Barrier barrier = new Barrier(2); - final Thread t1 = runForwarder( barrier, "> ", localIs, serverOs ); - final Thread t2 = runForwarder( barrier, "< ", serverIs, localOs ); - waitForConnectionClose( description, t1, t2 ); - } - } - - /** - * @param description - * @param t1 - * @param t2 - */ - private void waitForConnectionClose( final String description, - final Thread t1, final Thread t2 ) { - new Thread( new Runnable( ) { - public void run( ) { - try { - t1.join( ); - t2.join( ); - } catch ( InterruptedException e ) { - e.printStackTrace( ); - } - System.out.println( description + " closed" ); - } - } ).start( ); - } - - private Thread runForwarder( final Barrier barrier, final String prefix, - final InputStream is, final OutputStream os ) { - Thread t = new ForwarderThread(prefix, barrier, is, os); - t.start( ); - return t; - } -} -- 2.31.1