001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.test.proxyconnect;
006:
007: import com.tc.net.proxy.TCPProxy;
008: import com.tc.util.PortChooser;
009:
010: import java.io.File;
011: import java.net.InetAddress;
012: import java.lang.RuntimeException;
013:
014: public class ProxyConnectManagerImpl implements ProxyConnectManager {
015: private TCPProxy proxy = null;
016: private int proxyPort, dsoPort;
017: private int downtime = 100;
018: private int waittime = 20 * 1000;
019: private Thread td = null;
020: private volatile boolean toStop = false;
021:
022: public ProxyConnectManagerImpl() {
023: PortChooser pc = new PortChooser();
024: proxyPort = pc.chooseRandomPort();
025: dsoPort = pc.chooseRandomPort();
026: }
027:
028: public void setupProxy() {
029: try {
030: proxy = new TCPProxy(proxyPort, InetAddress.getLocalHost(),
031: dsoPort, 0L, false, new File("."));
032: proxy.setReuseAddress(true);
033: } catch (Exception x) {
034: throw new RuntimeException("setupProxy failed! " + x);
035: }
036: }
037:
038: public void setProxyPort(int port) {
039: proxyPort = port;
040: }
041:
042: public int getProxyPort() {
043: return (proxyPort);
044: }
045:
046: public void setDsoPort(int port) {
047: dsoPort = port;
048: }
049:
050: public int getDsoPort() {
051: return (dsoPort);
052: }
053:
054: public void proxyDown() {
055: System.out.println("XXX stop proxy");
056: proxy.fastStop();
057: }
058:
059: public void proxyUp() {
060: // wait until backend is ready
061: int i = 0;
062: while (!proxy.probeBackendConnection()) {
063: try {
064: Thread.sleep(100);
065: } catch (Exception e) {
066: //
067: }
068: if (i++ > 600) {
069: throw new RuntimeException("L2 is not ready!");
070: }
071: }
072:
073: try {
074: System.out.println("XXX start proxy at " + proxyPort
075: + " to " + dsoPort);
076: proxy.start();
077: } catch (Exception x) {
078: throw new RuntimeException("proxyUp failed! " + x);
079: }
080: }
081:
082: public void stopProxyTest() {
083: toStop = true;
084: proxyDown();
085: if (td.isAlive())
086: td.interrupt();
087: while (td.isAlive()) {
088: try {
089: Thread.sleep(10);
090: } catch (Exception x) {
091: //
092: }
093: if (td.isAlive())
094: td.interrupt();
095: }
096: System.out.println("XXX proxy thread stopped");
097: }
098:
099: public void startProxyTest() {
100: toStop = false;
101: td = new Thread(new Runnable() {
102: public void run() {
103: runProxy();
104: }
105: });
106: td.start();
107: }
108:
109: private void runProxy() {
110: if (toStop)
111: return;
112:
113: try {
114: Thread.sleep(waittime);
115: } catch (Exception x) {
116: // throw new RuntimeException("proxy wait time failed! " + x);
117: // return;
118: }
119: if (toStop)
120: return;
121: proxyDown();
122:
123: try {
124: Thread.sleep(downtime);
125: } catch (Exception x) {
126: // throw new RuntimeException("proxy down time failed! " + x);
127: // return;
128: }
129: if (toStop)
130: return;
131:
132: proxyUp();
133: }
134:
135: public void setProxyDownTime(int milliseconds) {
136: downtime = milliseconds;
137: }
138:
139: public int getProxyDownTime() {
140: return (downtime);
141: }
142:
143: public void setProxyWaitTime(int milliseconds) {
144: waittime = milliseconds;
145: }
146:
147: public int getProxyWaitTime() {
148: return (waittime);
149: }
150:
151: public void close() {
152: this .proxy.stop();
153: }
154:
155: public void closeClientConnections() {
156: this .proxy.closeClientConnections(true, true);
157: }
158:
159: }
|