Skip to content

Commit 7300e67

Browse files
committed
System Touch
1 parent fd20d6c commit 7300e67

2 files changed

Lines changed: 25 additions & 69 deletions

File tree

source/connections/ConnectionPoller.java

Lines changed: 18 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -99,75 +99,33 @@ private void handleSession(Connection CONNECTION, CurrentConnections CONNECTIONS
9999

100100
if(BUFFER.length() == 0) return;
101101

102-
// 2. Spawn per-session telnet subprocess, forward request, stream response back
103-
ProcessBuilder PER_PB = new ProcessBuilder(WebExpress.TELNET_PROXY_SERVER_ARGS);
104-
Process PER_PROC = PER_PB.start();
105-
106-
try
102+
// 2. Send a sample HTTP GET to tacobell.phd:80 and stream the reply back to the client
103+
try(java.net.Socket proxy = new java.net.Socket())
107104
{
108-
java.io.OutputStream PER_OUT = PER_PROC.getOutputStream();
109-
110-
byte[] REQUEST_BYTES = BUFFER.toString().getBytes();
105+
proxy.connect(new java.net.InetSocketAddress(WebExpress.REMOTE_SITE, Integer.parseInt(WebExpress.REMOTE_PORT)), PROXY_READ_TIMEOUT_MS);
106+
proxy.setSoTimeout(PROXY_READ_TIMEOUT_MS);
111107

112-
PER_OUT.write(REQUEST_BYTES);
113-
PER_OUT.write('\n');
114-
PER_OUT.flush();
108+
java.io.OutputStream proxyOut = proxy.getOutputStream();
109+
String httpRequest = "GET / HTTP/1.0\r\nHost: " + WebExpress.REMOTE_SITE + "\r\nConnection: close\r\n\r\n";
110+
proxyOut.write(httpRequest.getBytes());
111+
proxyOut.flush();
115112

116113
CommonRails.printSystemComponent(this, this.hashCode(),
117-
"WebExpress SessionHandler >> forwarded [" + REQUEST_BYTES.length + " bytes] to telnet backend.");
118-
119-
java.io.InputStream PER_IN = PER_PROC.getInputStream();
120-
java.io.OutputStream CLIENT_OUT = CONNECTION.SOCKET.getOutputStream();
121-
122-
java.util.concurrent.atomic.AtomicLong LAST_READ = new java.util.concurrent.atomic.AtomicLong(-1);
123-
java.util.concurrent.atomic.AtomicBoolean FIRST_BYTE = new java.util.concurrent.atomic.AtomicBoolean(false);
124-
125-
java.util.concurrent.ExecutorService EXEC = java.util.concurrent.Executors.newSingleThreadExecutor();
126-
127-
java.util.concurrent.Future<?> FUTURE = EXEC.submit(() ->
128-
{
129-
byte[] CHUNK = new byte[4096];
130-
int READ;
131-
132-
try
133-
{
134-
while((READ = PER_IN.read(CHUNK)) != -1)
135-
{
136-
CLIENT_OUT.write(CHUNK, 0, READ);
137-
CLIENT_OUT.flush();
138-
FIRST_BYTE.set(true);
139-
LAST_READ.set(System.currentTimeMillis());
140-
141-
CommonRails.printSystemComponent(this, this.hashCode(),
142-
"WebExpress SessionHandler >> proxied [" + READ + " bytes] to client.");
143-
}
144-
}
145-
catch(Exception ignored) {}
146-
});
114+
"WebExpress SessionHandler >> forwarded HTTP GET to " + WebExpress.REMOTE_SITE + ":" + WebExpress.REMOTE_PORT + ".");
147115

148-
long WALL = System.currentTimeMillis() + PROXY_WALL_TIMEOUT_MS;
116+
java.io.OutputStream clientOut = CONNECTION.SOCKET.getOutputStream();
117+
byte[] chunk = new byte[4096];
118+
int read;
119+
long deadline = System.currentTimeMillis() + PROXY_WALL_TIMEOUT_MS;
149120

150-
while(System.currentTimeMillis() < WALL)
121+
while(System.currentTimeMillis() < deadline && (read = proxy.getInputStream().read(chunk)) != -1)
151122
{
152-
if(FUTURE.isDone()) break;
153-
154-
if(FIRST_BYTE.get()
155-
&& System.currentTimeMillis() - LAST_READ.get() > PROXY_READ_TIMEOUT_MS)
156-
break;
123+
clientOut.write(chunk, 0, read);
124+
clientOut.flush();
157125

158-
Thread.sleep(100);
126+
CommonRails.printSystemComponent(this, this.hashCode(),
127+
"WebExpress SessionHandler >> proxied [" + read + " bytes] to client.");
159128
}
160-
161-
FUTURE.cancel(true);
162-
EXEC.shutdownNow();
163-
164-
CommonRails.printSystemComponent(this, this.hashCode(),
165-
"WebExpress SessionHandler >> proxy read window closed for ["
166-
+ CONNECTION.SOCKET.getRemoteSocketAddress() + "].");
167-
}
168-
finally
169-
{
170-
try { PER_PROC.destroyForcibly(); } catch(Exception ignored) {}
171129
}
172130

173131
// Enqueue for MessageQueueSorter audit trail

source/connections/CurrentConnections.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.io.InputStream;
88
import java.io.OutputStream;
99
import java.net.Socket;
10-
import java.util.ArrayList;
10+
import java.util.concurrent.CopyOnWriteArrayList;
1111

1212
public class CurrentConnections
1313
{
@@ -29,7 +29,8 @@ public class CurrentConnections
2929

3030
public ConnectionPoller thread;
3131

32-
public ArrayList<Connection> CURRENT_CONNECTION = new ArrayList<Connection>();
32+
// CopyOnWriteArrayList: safe for concurrent add (BaseServer) + iterate/remove (ConnectionPoller)
33+
public CopyOnWriteArrayList<Connection> CURRENT_CONNECTION = new CopyOnWriteArrayList<Connection>();
3334

3435
public void add(Connection connection)
3536
{
@@ -38,15 +39,12 @@ public void add(Connection connection)
3839

3940
public void remove(Socket socket)
4041
{
41-
for(int i = 0; i<this.CURRENT_CONNECTION.size(); i++)
42+
for(int i = 0; i < this.CURRENT_CONNECTION.size(); i++)
4243
{
43-
Socket _socket = this.CURRENT_CONNECTION.get(i).SOCKET;
44-
45-
if(_socket==socket)
44+
if(this.CURRENT_CONNECTION.get(i).SOCKET == socket)
4645
{
47-
Connection connection = this.CURRENT_CONNECTION.get(i);
48-
49-
this.CURRENT_CONNECTION.remove(connection);
46+
this.CURRENT_CONNECTION.remove(i);
47+
break; // remove only first match
5048
}
5149
}
5250
}

0 commit comments

Comments
 (0)