@@ -18,19 +18,19 @@ public class BrowserStackLocal {
18
18
private static Logger logger ;
19
19
private static final int MAX_CONNECT_WAIT = 30000 ; // 30s x 2 = 60s
20
20
private static final int MAX_CONNECT_ATTEMPTS = 2 ;
21
-
21
+
22
22
private final File binaryFile ;
23
23
private final String argumentString ;
24
-
24
+
25
25
private Process process ;
26
26
private Thread processThread ;
27
-
27
+
28
28
private StringBuffer output ;
29
29
private String lastError ;
30
30
private TunnelState tunnelState ;
31
-
31
+
32
32
private BrowserStackLocalListener listener ;
33
-
33
+
34
34
private static final Object monitor = new Object ();
35
35
private static final Map <Pattern , TunnelState > stateMatchers = new HashMap <Pattern , TunnelState >();
36
36
@@ -44,13 +44,13 @@ protected BrowserStackLocal(File binaryFile, String argumentString) {
44
44
throw new IllegalArgumentException ("Invalid arguments" );
45
45
}
46
46
47
- this .binaryFile = binaryFile ;
48
- this .argumentString = argumentString ;
49
- this .output = new StringBuffer ();
50
- this .tunnelState = TunnelState .IDLE ;
51
- this .logger = BrowserStackTunnel .logger ;
47
+ this .binaryFile = binaryFile ;
48
+ this .argumentString = argumentString ;
49
+ this .output = new StringBuffer ();
50
+ this .tunnelState = TunnelState .IDLE ;
51
+ this .logger = BrowserStackTunnel .logger ;
52
52
53
- Runtime .getRuntime ().addShutdownHook (new Thread (new Runnable () {
53
+ Runtime .getRuntime ().addShutdownHook (new Thread (new Runnable () {
54
54
@ Override
55
55
public void run () {
56
56
BrowserStackLocal .this .kill ();
@@ -66,31 +66,32 @@ protected void run() {
66
66
processThread = new Thread (new Runnable () {
67
67
@ Override
68
68
public void run () {
69
- notifyTunnelStateChanged (TunnelState .CONNECTING );
70
-
71
- try {
72
- logger .fine ("Arguments -- " + argumentString );
73
- process = new ProcessBuilder ((binaryFile .getAbsolutePath () + " " + argumentString ).split (" " )).start ();
74
- BufferedReader br = new BufferedReader (new InputStreamReader (process .getInputStream ()));
75
-
76
- String line ;
77
- while ((line = br .readLine ()) != null ) {
78
- output .append (line ).append ("\n " );
79
-
80
- if (processOutput (output .toString ())) {
81
- logger .fine (output .toString ());
82
- output .setLength (0 );
83
- }
84
- }
85
- } catch (IOException e ) {
86
- if (listener != null ) {
87
- listener .onError (e .getMessage ());
88
- }
89
- } finally {
90
- logger .fine (output .toString ());
91
- output .setLength (0 );
92
- notifyTunnelStateChanged (TunnelState .DISCONNECTED );
93
- }
69
+ notifyTunnelStateChanged (TunnelState .CONNECTING );
70
+
71
+ try {
72
+ logger .fine ("Arguments -- " + argumentString );
73
+ process = new ProcessBuilder ((binaryFile .getAbsolutePath () + " " + argumentString )
74
+ .split (" " )).start ();
75
+ BufferedReader br = new BufferedReader (new InputStreamReader (process .getInputStream ()));
76
+
77
+ String line ;
78
+ while ((line = br .readLine ()) != null ) {
79
+ output .append (line ).append ("\n " );
80
+
81
+ if (processOutput (output .toString ())) {
82
+ logger .fine (output .toString ());
83
+ output .setLength (0 );
84
+ }
85
+ }
86
+ } catch (IOException e ) {
87
+ if (listener != null ) {
88
+ listener .onError (e .getMessage ());
89
+ }
90
+ } finally {
91
+ logger .fine (output .toString ());
92
+ output .setLength (0 );
93
+ notifyTunnelStateChanged (TunnelState .DISCONNECTED );
94
+ }
94
95
}
95
96
});
96
97
@@ -104,17 +105,17 @@ protected void run(BrowserStackLocalListener listener) {
104
105
105
106
protected void runSync (BrowserStackLocalListener listener ) {
106
107
setListener (listener );
107
-
108
+
108
109
if (process != null ) {
109
110
kill ();
110
111
}
111
-
112
+
112
113
notifyTunnelStateChanged (TunnelState .CONNECTING );
113
114
run ();
114
-
115
+
115
116
int connAttempts = 0 ;
116
117
boolean connFailed = false ;
117
-
118
+
118
119
synchronized (monitor ) {
119
120
while (tunnelState == TunnelState .CONNECTING ) {
120
121
logger .info ("Waiting: " + connAttempts );
@@ -123,14 +124,14 @@ protected void runSync(BrowserStackLocalListener listener) {
123
124
} catch (InterruptedException e ) {
124
125
logger .info ("Exc: " + e .getMessage () + " " + isConnected ());
125
126
}
126
-
127
+
127
128
if (MAX_CONNECT_ATTEMPTS > 0 && ++connAttempts >= MAX_CONNECT_ATTEMPTS ) {
128
129
connFailed = true ;
129
130
break ;
130
131
}
131
132
}
132
133
}
133
-
134
+
134
135
if (connFailed ) {
135
136
killWithError ("Failed to connect to BrowserStack" );
136
137
}
@@ -141,12 +142,12 @@ protected void kill() {
141
142
process .destroy ();
142
143
process = null ;
143
144
}
144
-
145
+
145
146
if (processThread != null && processThread .isAlive ()) {
146
147
processThread .interrupt ();
147
148
processThread = null ;
148
149
}
149
-
150
+
150
151
logger .fine (output .toString ());
151
152
output .setLength (0 );
152
153
tunnelState = TunnelState .DISCONNECTED ;
@@ -177,24 +178,24 @@ private void killWithError(String message) {
177
178
private boolean processOutput (final String output ) {
178
179
if (output != null && !output .trim ().isEmpty ()) {
179
180
String error ;
180
-
181
+
181
182
for (Map .Entry <Pattern , TunnelState > entry : stateMatchers .entrySet ()) {
182
183
Matcher m = entry .getKey ().matcher (output );
183
-
184
+
184
185
if (m .find ()) {
185
186
if (entry .getValue () == TunnelState .ERROR ) {
186
187
error = (m .groupCount () > 0 ) ? m .group (1 ) : output ;
187
188
} else {
188
189
error = null ;
189
190
}
190
-
191
+
191
192
setError (error );
192
193
notifyTunnelStateChanged (entry .getValue ());
193
194
return true ;
194
195
}
195
196
}
196
197
}
197
-
198
+
198
199
return false ;
199
200
}
200
201
@@ -203,20 +204,21 @@ private void notifyTunnelStateChanged(TunnelState state) {
203
204
if (listener != null ) {
204
205
listener .onTunnelStateChange (state );
205
206
}
206
-
207
+
207
208
synchronized (monitor ) {
208
209
monitor .notifyAll ();
209
210
}
210
211
}
211
-
212
+
212
213
tunnelState = state ;
213
214
}
214
215
215
216
private void setError (String message ) {
216
217
lastError = message ;
217
-
218
+
218
219
if (listener != null ) {
219
220
listener .lastError = lastError ;
220
221
}
221
222
}
223
+
222
224
}
0 commit comments