Skip to content

Commit 9394387

Browse files
committed
we have got a much better bug reporting methode now.
1 parent 24114d6 commit 9394387

File tree

3 files changed

+91
-35
lines changed

3 files changed

+91
-35
lines changed

src/net/sharksystem/asap/util/Batchprocessor.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,29 @@
1010
import java.util.ArrayList;
1111
import java.util.List;
1212

13-
public class Batchprocessor {
14-
private final boolean cleanup;
13+
public class Batchprocessor extends Thread {
1514
List<String> cmdList = new ArrayList<>();
15+
private CmdLineUI cmdLineUI;
16+
private PrintStream printStream;
17+
private ByteArrayInputStream inputStream;
1618

1719
public Batchprocessor() {
1820
this(true);
1921
}
2022

2123
public Batchprocessor(boolean cleanup) {
22-
this.cleanup = cleanup;
24+
if(cleanup) {
25+
System.out.println("clean asap peers folders");
26+
this.cmdLineUI = new CmdLineUI();
27+
}
2328
}
2429

2530
public void addCommand(String cmd) {
2631
this.cmdList.add(cmd);
2732
}
2833

29-
public void execute() throws IOException, ASAPException {
34+
@Override
35+
public void run() {
3036
// prepare output
3137
ByteArrayOutputStream baos = new ByteArrayOutputStream();
3238
PrintStream ps = new PrintStream(baos);
@@ -37,12 +43,16 @@ public void execute() throws IOException, ASAPException {
3743

3844
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
3945

40-
CmdLineUI cmdLineUI = new CmdLineUI(System.out, bais);
41-
if(this.cleanup) {
42-
System.out.println("clean asap peers folders");
43-
cmdLineUI.doResetASAPStorages();
44-
}
46+
this.printStream = System.out;
47+
this.inputStream = bais;
48+
49+
this.cmdLineUI.runCommandLoop(this.printStream, this.inputStream);
4550

46-
cmdLineUI.runCommandLoop();
51+
// in any case - give it some time to tidy up
52+
try {
53+
Thread.sleep(1000);
54+
} catch (InterruptedException e) {
55+
// ignore
56+
}
4757
}
4858
}

src/net/sharksystem/cmdline/CmdLineUI.java

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public class CmdLineUI {
2828
public static final String SLEEP = "sleep";
2929
public static final String SHOW_LOG = "showlog";
3030

31-
private final PrintStream consoleOutput;
32-
private final BufferedReader userInput;
31+
private PrintStream consoleOutput;
32+
private BufferedReader userInput;
3333

3434
public static final String PEERS_ROOT_FOLDER = "asapPeers";
3535
private Map<String, ASAPPeer> peers = new HashMap();
@@ -48,6 +48,15 @@ public CmdLineUI(PrintStream out) throws IOException, ASAPException {
4848
this(out, null);
4949
}
5050

51+
/**
52+
* only for batch processing - removes anything from the past
53+
* @throws IOException
54+
* @throws ASAPException
55+
*/
56+
public CmdLineUI() {
57+
this.doResetASAPStorages();
58+
}
59+
5160
public CmdLineUI(PrintStream os, InputStream is) throws IOException, ASAPException {
5261
this.consoleOutput = os;
5362
this.userInput = is != null ? new BufferedReader(new InputStreamReader(is)) : null;
@@ -126,7 +135,7 @@ public void printUsage() {
126135
this.consoleOutput.println(b.toString());
127136
}
128137

129-
public void printUsage(String cmdString, String comment) {
138+
public void printUsage(String cmdString, String comment) throws ASAPException {
130139
PrintStream out = this.consoleOutput;
131140

132141
if(comment == null) comment = " ";
@@ -213,10 +222,18 @@ public void printUsage(String cmdString, String comment) {
213222
default:
214223
out.println("unknown command: " + cmdString);
215224
}
225+
throw new ASAPException("had to print usage");
216226
}
217227

218228
private List<String> cmds = new ArrayList<>();
219229

230+
public void runCommandLoop(PrintStream os, InputStream is) {
231+
this.consoleOutput = os;
232+
this.userInput = is != null ? new BufferedReader(new InputStreamReader(is)) : null;
233+
234+
this.runCommandLoop();
235+
}
236+
220237
public void runCommandLoop() {
221238
boolean again = true;
222239

@@ -290,6 +307,8 @@ public void runCommandLoop() {
290307
rememberCommand = false;
291308
break;
292309
}
310+
} catch (ASAPException ex) {
311+
rememberCommand = false;
293312
} catch (IOException ex) {
294313
this.consoleOutput.println("cannot read from input stream");
295314
System.exit(0);
@@ -380,7 +399,7 @@ public void streamCreated(TCPStream channel) {
380399
// method implementations //
381400
////////////////////////////////////////////////////////////////////////////////////////////////////////
382401

383-
public void doConnect(String parameterString) {
402+
public void doConnect(String parameterString) throws ASAPException {
384403
StringTokenizer st = new StringTokenizer(parameterString);
385404

386405
try {
@@ -408,7 +427,7 @@ public void doConnect(String parameterString) {
408427
}
409428
}
410429

411-
public void doOpen(String parameterString) {
430+
public void doOpen(String parameterString) throws ASAPException {
412431
StringTokenizer st = new StringTokenizer(parameterString);
413432

414433
try {
@@ -427,7 +446,7 @@ public void doOpen(String parameterString) {
427446
}
428447
}
429448

430-
public void doList() {
449+
public void doList() throws ASAPException {
431450
this.consoleOutput.println("connections:");
432451
for(String connectionName : this.streams.keySet()) {
433452
this.consoleOutput.println(connectionName);
@@ -436,7 +455,7 @@ public void doList() {
436455
this.doPrintAllInformation();
437456
}
438457

439-
public void doKill(String parameterString) {
458+
public void doKill(String parameterString) throws ASAPException {
440459
StringTokenizer st = new StringTokenizer(parameterString);
441460

442461
try {
@@ -466,7 +485,7 @@ public void doKill(String parameterString) {
466485
}
467486
}
468487

469-
public void doSetWaiting(String parameterString) {
488+
public void doSetWaiting(String parameterString) throws ASAPException {
470489
StringTokenizer st = new StringTokenizer(parameterString);
471490

472491
try {
@@ -479,7 +498,7 @@ public void doSetWaiting(String parameterString) {
479498
}
480499
}
481500

482-
public void doCreateASAPPeer(String parameterString) {
501+
public void doCreateASAPPeer(String parameterString) throws ASAPException {
483502
StringTokenizer st = new StringTokenizer(parameterString);
484503

485504
try {
@@ -493,7 +512,7 @@ public void doCreateASAPPeer(String parameterString) {
493512
}
494513
}
495514

496-
public void doCreateASAPApp(String parameterString) {
515+
public void doCreateASAPApp(String parameterString) throws ASAPException {
497516
StringTokenizer st = new StringTokenizer(parameterString);
498517

499518
try {
@@ -518,7 +537,7 @@ public void doCreateASAPApp(String parameterString) {
518537
}
519538
}
520539

521-
public void doCreateASAPChannel(String parameterString) {
540+
public void doCreateASAPChannel(String parameterString) throws ASAPException {
522541
StringTokenizer st = new StringTokenizer(parameterString);
523542

524543
try {
@@ -550,7 +569,7 @@ public void doCreateASAPChannel(String parameterString) {
550569
}
551570
}
552571

553-
public void doCreateASAPMessage(String parameterString) {
572+
public void doCreateASAPMessage(String parameterString) throws ASAPException {
554573
StringTokenizer st = new StringTokenizer(parameterString);
555574

556575
try {
@@ -576,9 +595,12 @@ public void doCreateASAPMessage(String parameterString) {
576595

577596
public void doResetASAPStorages() {
578597
ASAPEngineFS.removeFolder(PEERS_ROOT_FOLDER);
598+
File rootFolder = new File(PEERS_ROOT_FOLDER);
599+
rootFolder.mkdirs();
600+
579601
}
580602

581-
public void doSetSendReceivedMessage(String parameterString) {
603+
public void doSetSendReceivedMessage(String parameterString) throws ASAPException {
582604
StringTokenizer st = new StringTokenizer(parameterString);
583605

584606
try {
@@ -595,7 +617,7 @@ public void doSetSendReceivedMessage(String parameterString) {
595617
}
596618
}
597619

598-
public void doPrintAllInformation() {
620+
public void doPrintAllInformation() throws ASAPException {
599621
try {
600622
this.consoleOutput.println(this.peers.keySet().size() + " peers in folder: " + PEERS_ROOT_FOLDER);
601623
for(String peername : this.peers.keySet()) {
@@ -618,7 +640,7 @@ public void doPrintAllInformation() {
618640
}
619641
}
620642

621-
public void doPrintStorageInformation(String parameterString) {
643+
public void doPrintStorageInformation(String parameterString) throws ASAPException {
622644
StringTokenizer st = new StringTokenizer(parameterString);
623645

624646
try {
@@ -645,7 +667,7 @@ public void doPrintStorageInformation(String parameterString) {
645667
}
646668
}
647669

648-
public void doSleep(String parameterString) {
670+
public void doSleep(String parameterString) throws ASAPException {
649671
StringTokenizer st = new StringTokenizer(parameterString);
650672

651673
try {
@@ -675,7 +697,7 @@ private void doShowLog() {
675697
this.consoleOutput.println("\"");
676698
}
677699

678-
public void doPrintChannelInformation(String parameterString) {
700+
public void doPrintChannelInformation(String parameterString) throws ASAPException {
679701
// out.println("example: " + PRINT_CHANNEL_INFORMATION + " Alice chat sn2://abChat");
680702
StringTokenizer st = new StringTokenizer(parameterString);
681703

test/net/sharksystem/asap/BatchprocessorTest.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,38 @@
77

88
public class BatchprocessorTest {
99
@Test
10-
public void basicTest() throws IOException, ASAPException {
11-
Batchprocessor claraCommands = new Batchprocessor();
12-
13-
claraCommands.addCommand(
14-
"resetstorage\n" +
15-
"newpeer Alice\n" +
10+
public void secondMessageNotSendTest() throws IOException, ASAPException, InterruptedException {
11+
Batchprocessor aliceCommands = new Batchprocessor();
12+
Batchprocessor bobCommands = new Batchprocessor();
13+
aliceCommands.addCommand(
14+
"newpeer Alice\n" +
1615
"newapp Alice chat\n" +
17-
"sleep 1000");
18-
claraCommands.execute();
16+
"newmessage Alice chat sn2://abChat HiBob\n" +
17+
"connect 7070 Alice\n" +
18+
"sleep 2000\n" +
19+
"newmessage Alice chat sn2://abChat HiBob2\n" +
20+
"sleep 1000\n" +
21+
"kill all\n" +
22+
"open 7071 Alice\n" +
23+
"sleep 5000"
24+
);
25+
26+
27+
bobCommands.addCommand(
28+
"newpeer Bob\n" +
29+
"newapp Bob chat\n" +
30+
"newmessage Bob chat sn2://abChat HiAlice\n" +
31+
"open 7070 Bob\n" +
32+
"sleep 1000\n" +
33+
"connect 7071 Bob\n" +
34+
"sleep 1000"
35+
);
36+
37+
// lets go
38+
aliceCommands.start();
39+
bobCommands.start();
40+
41+
aliceCommands.join();
42+
bobCommands.join();
1943
}
2044
}

0 commit comments

Comments
 (0)