@@ -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
0 commit comments