1212import java .io .PrintStream ;
1313import java .util .*;
1414
15+ /**
16+ * This class provides a command-line tool to test the hub from different machines.
17+ */
1518public class HubTester {
1619
1720 private final HubConnectionManager hubConnectionManager ;
@@ -28,33 +31,57 @@ public HubTester(String peerId, CharSequence format) throws IOException, ASAPExc
2831 printStream = System .out ;
2932 }
3033
34+ /**
35+ * Writes the ASAPHub logs into a file instead of printing them to the console.
36+ * @param fileName filename of the output file
37+ * @throws FileNotFoundException throws an exception if file couldn't be created
38+ */
3139 public void redirectASAPLogs (String fileName ) throws FileNotFoundException {
3240 printStream = System .out ;
3341 PrintStream o = new PrintStream (fileName );
3442 // write logs into file
3543 System .setOut (o );
3644 }
3745
38-
46+ /**
47+ * Establishes a connection to the ASAPHub using a HubConnectionManager
48+ * @param hubConnectorDescription
49+ * @throws SharkException
50+ * @throws IOException
51+ */
3952 public void connectHub (HubConnectorDescription hubConnectorDescription ) throws SharkException , IOException {
4053 hubConnectionManager .connectHub (hubConnectorDescription );
4154 }
4255
4356 public void sendMessages (String message , CharSequence uri , int messageCount , int delay ) throws InterruptedException , ASAPException {
4457 for (int i = 0 ; i < messageCount ; i ++) {
58+ String messageToSend = String .format ("%s_%03d" , message , i );
59+ this .printStream .printf ("sending message '%s'%n" , messageToSend );
4560 asapPeer .sendASAPMessage (this .format , uri , String .format ("%s_%03d" , message , i ).getBytes ());
4661 Thread .sleep (delay );
4762 }
4863 }
4964
65+ /**
66+ * Adds an ASAPMessageReceivedListener to the ASAPPeer which prints all incoming message to the console.
67+ */
5068 public void receiveMessages () {
5169 asapPeer .addASAPMessageReceivedListener (this .format , new CustomMessageReceivedListener ((String ) asapPeer .getPeerID (), printStream ));
5270 }
5371
72+ /**
73+ * Closes the connection to the ASAPHub
74+ * @param hubConnectorDescription HubConnectorDescription object which contains the connection information
75+ * @throws SharkException
76+ * @throws IOException
77+ */
5478 public void shutDown (HubConnectorDescription hubConnectorDescription ) throws SharkException , IOException {
5579 this .hubConnectionManager .disconnectHub (hubConnectorDescription );
5680 }
5781
82+ /**
83+ * This class implements an ASAPMessageReceivedListener which prints all received messages using a PrintStream
84+ */
5885 static class CustomMessageReceivedListener implements ASAPMessageReceivedListener {
5986 private final String peerName ;
6087 private final PrintStream printStream ;
@@ -73,6 +100,12 @@ public void asapMessagesReceived(ASAPMessages asapMessages, String senderE2E, Li
73100 new String (getLastElement (yourPDUIter )));
74101 }
75102
103+ /**
104+ * Gets the last element of an Iterator
105+ * @param iterator Iterator object
106+ * @return last element of the iterator
107+ * @param <T> type of elements present within an Iterator
108+ */
76109 public static <T > T getLastElement (Iterator <T > iterator ) {
77110 T last = null ;
78111 while (iterator .hasNext ()) {
@@ -97,7 +130,7 @@ public static void main(String[] args) throws IOException, SharkException, Inter
97130 " --uri=<uri> optional argument to set the URI of the ASAP application (default: asap://testuri)\n " +
98131 " -V, --version Print version information and exit.\n " +
99132 "If the HubTester is to be used for sending messages, the following argument must be passed:\n " +
100- " --send [baseMessage] [count] [delay]" ;
133+ " --send [baseMessage] [count] [delay in milliseconds ]" ;
101134
102135 Map <String , List <String >> params = new HashMap <>();
103136 List <String > options = null ;
@@ -157,17 +190,24 @@ public static void main(String[] args) throws IOException, SharkException, Inter
157190 hubTester .redirectASAPLogs (logRedirectPath );
158191 }
159192 hubTester .connectHub (hubDescription );
193+ hubTester .receiveMessages ();
160194 if (sendArgsList != null ) {
161195 hubTester .sendMessages (message , uri , count , delay );
162- } else {
163- hubTester .receiveMessages ();
164196 }
165197 }catch (IllegalArgumentException | NoSuchElementException e ){
166198 System .err .println (e .getLocalizedMessage ());
167199 System .out .println (helpText );
168200 }
169201 }
170202
203+ /**
204+ * Gets the first item of the List-value of a map.
205+ * @param argsMap Map containing the arguments
206+ * @param key argument name
207+ * @param defaultValue default return value if no value was found for the given key; set argument to null if
208+ * the argument is required
209+ * @return first item of the list value
210+ */
171211 private static String getMapValue (Map <String , List <String >> argsMap , String key , String defaultValue ) {
172212 if (argsMap .get (key ) != null ) {
173213 return argsMap .get (key ).get (0 );
0 commit comments