]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.testing/src/org/simantics/db/testing/impl/Proxy.java
Less memory use for TG import when NamespaceMigrationStep is involved
[simantics/platform.git] / bundles / org.simantics.db.testing / src / org / simantics / db / testing / impl / Proxy.java
1 package org.simantics.db.testing.impl;
2 /*******************************************************************************
3  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4  * in Industry THTH ry.
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
9  *
10  * Contributors:
11  *     VTT Technical Research Centre of Finland - initial API and implementation
12  *******************************************************************************/
13
14
15 import java.io.IOException;
16 import java.net.InetSocketAddress;
17 import java.net.ServerSocket;
18 import java.net.Socket;
19
20
21 public class Proxy {
22
23         private static int PORT = 6668;
24         private static String SERVER_ADDRESS="localhost:6666";
25         private boolean interrupted = false;
26         
27         public Proxy() {
28         }
29         
30         public void run() {
31                 ServerSocket service = null;
32                 try {
33                         service = new ServerSocket(PORT);
34                 } catch (IOException e) {
35                         e.printStackTrace();
36                         return;
37                 }
38                 
39                 System.out.println("Accepting connections");
40                 
41         int connectionNumber = 0;
42                 while(!interrupted) {
43                         try {
44                                 final Socket socket = service.accept();
45                                 String connectionName = "Connection" + ++connectionNumber;
46                                 System.out.println(connectionName + ": Client connection accepted.");
47                                 Runnable runnable = new Runnable() {
48                                         @Override
49                                         public void run() {
50                                                 try {
51                                                         serve(socket);
52                                                         socket.close();
53                                                 } catch(IOException e) {
54                                                         e.printStackTrace();
55                                                 }
56                                         }
57                                 };
58                                 Thread t = new Thread(runnable, connectionName);
59                                 t.start();
60                         } catch (IOException e) {
61                                 e.printStackTrace();
62                         }
63                 }
64                 
65                 // Shutdown service
66                 try {
67                         if(service != null)
68                                 service.close();
69                 } catch (IOException ignore) {
70                 }
71         }
72         
73         /**
74          * Serves socket connection.
75          * 
76          * @param socket
77          * @throws IOException
78          */
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");
83
84         InetSocketAddress dbAddress =  new InetSocketAddress(split[0], Integer.parseInt(split[1]));
85         Socket dbSocket = new Socket();
86         try {
87             dbSocket.connect(dbAddress);
88         } catch(IOException e) {
89             dbSocket.close();
90             System.out.println("serve() Couldn't connect to database '" + SERVER_ADDRESS +"'");
91             return;
92         }
93         String t = Thread.currentThread().getName();
94         System.out.println(t + ": Server connection ok.");
95         proxy(dbSocket, socket, "db",  "client");
96         dbSocket.close();
97         System.out.println(t + ": Client connection closed.");
98     }
99         
100         /**
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.
103          * 
104          * @param a
105          * @param b
106          */
107         private void proxy(final Socket a, final Socket b, final String aName, final String bName) {            
108                 // A -> B
109                 Runnable ab = new Runnable() {
110                         @Override
111                         public void run() {
112                                 try {
113                     byte buffer[] = new byte[256];
114                                         while(a.isConnected() && b.isConnected()) {
115                                                 int n = a.getInputStream().read(buffer);
116                                                 if (n < 1)
117                                                     break;
118                                                 b.getOutputStream().write(buffer, 0, n);
119                                                 b.getOutputStream().flush();
120                                         }
121                                 } catch(IOException e) {
122                                         return; // for debugging
123                                 }
124                         }
125                 };
126                 
127                 // B -> A
128                 Runnable ba = new Runnable() {
129                         @Override
130                         public void run() {
131                                 try {
132                     byte buffer[] = new byte[256];
133                                         while(b.isConnected() && a.isConnected()) {
134                                                 int n = b.getInputStream().read(buffer);
135                         if (n < 1)
136                             break;
137                                                 a.getOutputStream().write(buffer, 0, n);
138                                                 a.getOutputStream().flush();
139                                         }
140                                 } catch(IOException e) {
141                                     return; // for debugging
142                                 }                               
143                         }
144                 };
145                 String t = Thread.currentThread().getName();
146                 Thread tab = new Thread(ab, t + " " + aName + "->" + bName);
147                 Thread tba = new Thread(ba, t + " " + bName + "->" + aName);
148                 
149                 tab.start();
150                 tba.start();
151                 
152                 try {
153                         while(tba.isAlive() && tab.isAlive()) {
154                                 Thread.sleep(1000);
155                         }
156                 } catch (InterruptedException e) {
157                         e.printStackTrace();
158                 }
159                 try {
160                         a.close();
161                         b.close();
162                 } catch (IOException e) {
163                 }
164         }
165         
166         /**
167          * @param args
168          */
169         public static void main(String[] args) {
170                 Proxy proxy = new Proxy();
171                 proxy.run();
172         }
173
174 }