Skip to content

Commit eae0620

Browse files
committed
Merge branch 'debugconnection' into develop
2 parents abfa462 + a480a4b commit eae0620

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/main/java/bwapi/BWClient.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@
77
*/
88
public class BWClient {
99
private final BWEventListener eventListener;
10+
private final boolean debugConnection;
1011
private EventHandler handler;
1112

1213
public BWClient(final BWEventListener eventListener) {
14+
this(eventListener, false);
15+
}
16+
17+
public BWClient(final BWEventListener eventListener, final boolean debugConnection) {
1318
Objects.requireNonNull(eventListener);
19+
this.debugConnection = debugConnection;
1420
this.eventListener = eventListener;
1521
}
1622

@@ -31,7 +37,7 @@ public void startGame() {
3137
* @param autoContinue automatically continue playing the next game(s). false by default
3238
*/
3339
public void startGame(boolean autoContinue) {
34-
Client client = new Client();
40+
Client client = new Client(debugConnection);
3541
client.reconnect();
3642
handler = new EventHandler(eventListener, client);
3743

src/main/java/bwapi/Client.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ public interface EventHandler {
6060
private ByteBuffer mapFileHandle = null;
6161
private ByteBuffer gameTableFileHandle = null;
6262

63-
Client() {}
63+
private boolean debugConnection = false;
64+
65+
Client(boolean debugConnection) {
66+
this.debugConnection = debugConnection;
67+
}
6468

6569
/**
6670
* For test purposes only
@@ -89,6 +93,10 @@ void reconnect(){
8993
}
9094

9195
void disconnect() {
96+
if (debugConnection) {
97+
System.err.print("Disconnect called by: ");
98+
System.err.println(Thread.currentThread().getStackTrace()[2].getMethodName());
99+
}
92100
if (!connected) {
93101
return;
94102
}
@@ -126,6 +134,9 @@ boolean connect() {
126134
}
127135
catch (Exception e) {
128136
System.err.println("Game table mapping not found.");
137+
if (debugConnection) {
138+
e.printStackTrace();
139+
}
129140
return false;
130141
}
131142

@@ -135,6 +146,9 @@ boolean connect() {
135146
}
136147
catch (Exception e) {
137148
System.err.println("Unable to map Game table.");
149+
if (debugConnection) {
150+
e.printStackTrace();
151+
}
138152
return false;
139153
}
140154

@@ -166,6 +180,9 @@ boolean connect() {
166180
}
167181
catch (Exception e) {
168182
System.err.println("Unable to open communications pipe: " + communicationPipe);
183+
if (debugConnection) {
184+
e.printStackTrace();
185+
}
169186
gameTableFileHandle = null;
170187
return false;
171188
}
@@ -178,6 +195,9 @@ boolean connect() {
178195
}
179196
catch (Exception e) {
180197
System.err.println("Unable to open shared memory mapping: " + sharedMemoryName);
198+
if (debugConnection) {
199+
e.printStackTrace();
200+
}
181201
pipeObjectHandle = null;
182202
gameTableFileHandle = null;
183203
return false;
@@ -188,6 +208,9 @@ boolean connect() {
188208
}
189209
catch (Exception e) {
190210
System.err.println("Unable to map game data.");
211+
if (debugConnection) {
212+
e.printStackTrace();
213+
}
191214
return false;
192215
}
193216

@@ -205,8 +228,11 @@ boolean connect() {
205228
code = pipeObjectHandle.readByte();
206229
}
207230
catch (Exception e) {
208-
disconnect();
209231
System.err.println("Unable to read pipe object.");
232+
if (debugConnection) {
233+
e.printStackTrace();
234+
}
235+
disconnect();
210236
return false;
211237
}
212238
}
@@ -223,6 +249,9 @@ void update(final EventHandler handler) {
223249
}
224250
catch (Exception e) {
225251
System.err.println("failed, disconnecting");
252+
if (debugConnection) {
253+
e.printStackTrace();
254+
}
226255
disconnect();
227256
return;
228257
}
@@ -232,6 +261,9 @@ void update(final EventHandler handler) {
232261
}
233262
catch (Exception e) {
234263
System.err.println("failed, disconnecting");
264+
if (debugConnection) {
265+
e.printStackTrace();
266+
}
235267
disconnect();
236268
return;
237269
}

0 commit comments

Comments
 (0)