1 package org.simantics.db.testing.impl;
2 /*******************************************************************************
3 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v1.0
7 * which accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
11 * VTT Technical Research Centre of Finland - initial API and implementation
12 *******************************************************************************/
15 import java.io.IOException;
16 import java.net.InetSocketAddress;
17 import java.net.ServerSocket;
18 import java.net.Socket;
23 private static int PORT = 6668;
24 private static String SERVER_ADDRESS="localhost:6666";
25 private boolean interrupted = false;
31 ServerSocket service = null;
33 service = new ServerSocket(PORT);
34 } catch (IOException e) {
39 System.out.println("Accepting connections");
41 int connectionNumber = 0;
44 final Socket socket = service.accept();
45 String connectionName = "Connection" + ++connectionNumber;
46 System.out.println(connectionName + ": Client connection accepted.");
47 Runnable runnable = new Runnable() {
53 } catch(IOException e) {
58 Thread t = new Thread(runnable, connectionName);
60 } catch (IOException e) {
69 } catch (IOException ignore) {
74 * Serves socket connection.
79 private void serve(Socket socket) throws IOException {
80 String[] split = SERVER_ADDRESS.split(":");
81 if (split.length != 2)
82 throw new IllegalArgumentException("address does not contain a port, missing ':' character");
84 InetSocketAddress dbAddress = new InetSocketAddress(split[0], Integer.parseInt(split[1]));
85 Socket dbSocket = new Socket();
87 dbSocket.connect(dbAddress);
88 } catch(IOException e) {
90 System.out.println("serve() Couldn't connect to database '" + SERVER_ADDRESS +"'");
93 String t = Thread.currentThread().getName();
94 System.out.println(t + ": Server connection ok.");
95 proxy(dbSocket, socket, "db", "client");
97 System.out.println(t + ": Client connection closed.");
101 * Proxies data from socket a to socket b and vice versa
102 * This method blocks until one of the sockets closes or exception is thrown.
107 private void proxy(final Socket a, final Socket b, final String aName, final String bName) {
109 Runnable ab = new Runnable() {
113 byte buffer[] = new byte[256];
114 while(a.isConnected() && b.isConnected()) {
115 int n = a.getInputStream().read(buffer);
118 b.getOutputStream().write(buffer, 0, n);
119 b.getOutputStream().flush();
121 } catch(IOException e) {
122 return; // for debugging
128 Runnable ba = new Runnable() {
132 byte buffer[] = new byte[256];
133 while(b.isConnected() && a.isConnected()) {
134 int n = b.getInputStream().read(buffer);
137 a.getOutputStream().write(buffer, 0, n);
138 a.getOutputStream().flush();
140 } catch(IOException e) {
141 return; // for debugging
145 String t = Thread.currentThread().getName();
146 Thread tab = new Thread(ab, t + " " + aName + "->" + bName);
147 Thread tba = new Thread(ba, t + " " + bName + "->" + aName);
153 while(tba.isAlive() && tab.isAlive()) {
156 } catch (InterruptedException e) {
162 } catch (IOException e) {
169 public static void main(String[] args) {
170 Proxy proxy = new Proxy();