@@ -39,7 +39,7 @@ public SharkNetMessengerAppSupportingDistributedTesting(String peerName, int syn
3939 this .getSharkMessengerComponent ().addSharkMessagesReceivedListener (
4040 new SNMDistributedTestsMessageReceivedListener (this ));
4141
42- this .myEnvironment = new PeerHostingEnvironmentDescription ();
42+ this .myEnvironment = new PeerHostingEnvironmentDescription (this . getPeerName () );
4343 }
4444
4545 public String produceStringForMessage (CharSequence contentType , byte [] content ) {
@@ -112,36 +112,57 @@ void releaseReceived(CharSequence releaseChannelURI) {
112112 // test run //
113113 /////////////////////////////////////////////////////////////////////////////////////////////
114114
115+ private boolean beTestPeer = false ;
116+ public void becomeTestPeer () throws IOException , SharkException {
117+ this .beTestPeer = true ;
118+ // collect information
119+ CharSequence peerID = this .getSharkPeer ().getPeerID ();
120+ PeerHostingEnvironmentDescription scriptRQMessage =
121+ new PeerHostingEnvironmentDescription (peerID .toString ());
122+
123+ // send message
124+ this .getSharkMessengerComponent ().sendSharkMessage (
125+ SharkNetMessengerAppSupportingDistributedTesting .PEER_HOST_DESCRIPTION_FORMAT ,
126+ scriptRQMessage .getMessageBytes (),
127+ SharkNetMessengerAppSupportingDistributedTesting .SCRIPT_RQ_CHANNEL , // specific channel
128+ (CharSequence ) null , // no specific receiver
129+ false , // no signing
130+ false // no encryption
131+ );
132+ this .tellUI ("script request sent: " + scriptRQMessage );
133+ }
115134
116135 private int lastReceivedScriptIndex = -1 ;
117136 // peers willing to execute test script sent its environment description
118137 public void testScriptReceived (CharSequence testScriptChannel ) {
138+ if (!this .beTestPeer ) {
139+ this .tellUI ("test script received but no tester peer - ignore" );
140+ }
119141 // add information of this new volunteer and see if we have enough participant for one new test
120142 try {
121143 SharkNetMessageList scriptMessages =
122144 this .getSharkMessengerComponent ().getChannel (testScriptChannel ).getMessages ();
123145 for (int scriptIndex = this .lastScriptRQIndex + 1 ; scriptIndex < scriptMessages .size (); scriptIndex ++) {
124146 this .lastReceivedScriptIndex = scriptIndex ; // update each round - before possible exceptions
125147
126- SharkNetMessage testSharkMessage = scriptMessages .getSharkMessage (scriptIndex , true );
148+ SharkNetMessage testSharkMessage = scriptMessages .getSharkMessage (scriptIndex , false );
127149 TestScriptDescription testScriptDescription =
128150 new TestScriptDescription (testSharkMessage .getContent ());
129151
130152 // for me?
131- if (testScriptDescription .ipAddress .equalsIgnoreCase (this .myEnvironment .ipAddress )) {
153+ if (testScriptDescription .peerID .equalsIgnoreCase (this .getSharkPeer ().getPeerID ().toString ())) {
154+ this .tellUI ("test script received - prepare script runner" + testScriptDescription );
132155 // produce test running thread
133- ScriptRunnerThread scriptRunnerThread =
134- new ScriptRunnerThread (
135- Integer .toString (testScriptDescription .peerIndex ),
136- Integer .toString (testScriptDescription .testNumber ),
137- testScriptDescription .script );
138- this .tellUI ("run received test script" + testScriptDescription .script );
139- scriptRunnerThread .start ();
156+ new ScriptRunnerThread (
157+ Integer .toString (testScriptDescription .peerIndex ),
158+ Integer .toString (testScriptDescription .testNumber ),
159+ testScriptDescription .script ).start ();
160+ this .tellUI ("running script" + testScriptDescription .script );
140161 } else {
141162 this .tellUI ("test script received, not for me though. Index: " + scriptIndex );
142163 }
143164 }
144- } catch (SharkNetMessengerException | IOException | ASAPException e ) {
165+ } catch (IOException | SharkException e ) {
145166 this .tellUIError ("problems handling test script received channel: " + e .getLocalizedMessage ());
146167 }
147168 }
@@ -153,24 +174,52 @@ public void testScriptReceived(CharSequence testScriptChannel) {
153174 private Map <String , PeerHostingEnvironmentDescription > availablePeers = new HashMap <>();
154175
155176 private class OrchestratedTest {
156- List <PeerHostingEnvironmentDescription > requiredPeerEnvironment ;
177+ List <PeerHostingEnvironmentDescription > peerEnvironment ;
157178 List <String > scripts ;
158179 OrchestratedTest (List <PeerHostingEnvironmentDescription > requiredPeerEnvironment , List <String > scripts ) {
159- this .requiredPeerEnvironment = requiredPeerEnvironment ;
180+ this .peerEnvironment = requiredPeerEnvironment ;
160181 this .scripts = scripts ;
161182 }
162183 }
163184
185+ private boolean beTestOrchestrator = false ;
164186 private List <OrchestratedTest > orchestratedTestsWaiting = new ArrayList <>();
165187 private List <OrchestratedTest > orchestratedTestsReady = new ArrayList <>();
166188
189+ public void orchestrateTest (List <String > scripts ) {
190+ this .orchestrateTest (null , scripts );
191+ }
192+
167193 public void orchestrateTest (List <PeerHostingEnvironmentDescription > requiredPeerEnvironment , List <String > scripts ) {
194+ this .beTestOrchestrator = true ;
195+ if (scripts == null || scripts .isEmpty ()) {
196+ this .tellUIError ("scripts must not be empty" );
197+ return ;
198+ }
199+ if (requiredPeerEnvironment == null ) requiredPeerEnvironment = new ArrayList <>();
200+ if (requiredPeerEnvironment .size () > scripts .size ()) {
201+ this .tellUIError ("more required peers than scripts... Makes no sense. Give up" );
202+ return ;
203+ }
204+ if (requiredPeerEnvironment .size () < scripts .size ()) {
205+ this .tellUI ("less peer requirements than scripts... will take any available peer" );
206+ while (requiredPeerEnvironment .size () < scripts .size ()) {
207+ requiredPeerEnvironment .add (
208+ new PeerHostingEnvironmentDescription (null ,null ,null ,null ));
209+ }
210+ }
211+
168212 this .orchestratedTestsWaiting .add (new OrchestratedTest (requiredPeerEnvironment , scripts ));
169213 }
170214
171215 private int lastScriptRQIndex = -1 ;
172216 // peers willing to execute test script sent its environment description
173217 public void scriptRQReceived (CharSequence scriptRQChannel ) {
218+ if (!this .beTestOrchestrator ) {
219+ this .tellUI ("script request received - ignore - not a test orchestrator" );
220+ } else {
221+ this .tellUI ("script request received - try to stage a test" );
222+ }
174223 // add information of this new volunteer and see if we have enough participant for one new test
175224 synchronized (this ) {
176225 try {
@@ -179,12 +228,12 @@ public void scriptRQReceived(CharSequence scriptRQChannel) {
179228 for (int rqIndex = this .lastScriptRQIndex + 1 ; rqIndex < rqMessages .size (); rqIndex ++) {
180229 this .lastScriptRQIndex = rqIndex ; // update each round - before possible exceptions
181230
182- SharkNetMessage rqSharkMessage = rqMessages .getSharkMessage (rqIndex , true );
231+ SharkNetMessage rqSharkMessage = rqMessages .getSharkMessage (rqIndex , false );
183232 PeerHostingEnvironmentDescription peerHostDescription =
184233 new PeerHostingEnvironmentDescription (rqSharkMessage .getContent ());
185234
186235 // add or replace information
187- this .availablePeers .put (peerHostDescription .ipAddress , peerHostDescription );
236+ this .availablePeers .put (peerHostDescription .peerID , peerHostDescription );
188237
189238 // try to set up a test(s).
190239 this .stageTests ();
@@ -218,14 +267,18 @@ private void stageTests() throws UnknownHostException {
218267
219268 // make peers unavailable
220269 for (PeerHostingEnvironmentDescription env : testEnsemble .peerEnvironment ) {
221- this .availablePeers .remove (env .ipAddress );
270+ String key = env .peerID ;
271+ this .availablePeers .remove (key );
222272 }
223273 }
224274 } while (testEnsemble != null ); // do again until there is not further match
225275
226276 if (!this .orchestratedTestsReady .isEmpty ()) {
227277 // launch tests
278+ this .tellUI ("test staged .. going to launch" );
228279 this .launchTests ();
280+ } else {
281+ this .tellUI ("couldn't stage a test - not enough peers" );
229282 }
230283 }
231284
@@ -246,6 +299,7 @@ private class OrchestratedTestLauncher extends Thread {
246299 public final static int FINAL_WAIT_PERIODE_BEFORE_LAUNCH = 1000 ;
247300
248301 private static String scriptStart_OpenTCP = null ;
302+ private static String scriptEnd_Exit = null ;
249303
250304 OrchestratedTestLauncher (OrchestratedTest test2run ) throws UnknownHostException {
251305 this .test2run = test2run ;
@@ -258,9 +312,16 @@ private class OrchestratedTestLauncher extends Thread {
258312 sb .append (TestLanguageCompiler .CLI_OPEN_TCP );
259313 sb .append (TestLanguageCompiler .CLI_SPACE );
260314 sb .append (ORCHESTRATOR_PORT );
261- sb .append (";" );
315+ sb .append (TestLanguageCompiler . LANGUAGE_SEPARATOR );
262316 OrchestratedTestLauncher .scriptStart_OpenTCP = sb .toString ();
263317 }
318+ if (OrchestratedTestLauncher .scriptEnd_Exit == null ) {
319+ StringBuilder sb = new StringBuilder ();
320+ sb .append (TestLanguageCompiler .LANGUAGE_SEPARATOR );
321+ sb .append (TestLanguageCompiler .CLI_EXIT );
322+ sb .append (TestLanguageCompiler .LANGUAGE_SEPARATOR );
323+ OrchestratedTestLauncher .scriptEnd_Exit = sb .toString ();
324+ }
264325 }
265326
266327 private String getBlockTag4Peer (int peerIndex ) {
@@ -289,9 +350,14 @@ public void run() {
289350 sb .append (launchTag );
290351 sb .append (TestLanguageCompiler .CLI_SEPARATOR );
291352
353+ // TODO collect results - will be a single lsMessages in the right channel.
354+
355+ // finish peer
356+ sb .append (scriptEnd_Exit );
357+
292358 String orchestratorScript = sb .toString ();
293359
294- // produce preamble script for each peer
360+ // produce script for each peer
295361 String [] effectiveScripts = new String [this .test2run .scripts .size ()];
296362 for (int peerIndex = 0 ; peerIndex < this .test2run .scripts .size (); peerIndex ++) {
297363 sb = new StringBuilder ();
@@ -314,19 +380,22 @@ public void run() {
314380 sb .append (FINAL_WAIT_PERIODE_BEFORE_LAUNCH );
315381 sb .append (TestLanguageCompiler .CLI_SEPARATOR );
316382
317- // add to peer script
318- effectiveScripts [peerIndex ] = sb .toString () + this .test2run .scripts .get (peerIndex );
383+ // TODO send results - will be a couple of sendMessages with log files in the right channel.
384+
385+ // add to peer script and finish with exit in case test developer forgot
386+ effectiveScripts [peerIndex ] = sb .toString () + this .test2run .scripts .get (peerIndex ) + scriptEnd_Exit ;
319387 }
320388
321389 // send script to each peer
322390 try {
323- for (int i = 0 ; i < this .test2run .requiredPeerEnvironment .size (); i ++) {
324- PeerHostingEnvironmentDescription peerEnvironment = this .test2run .requiredPeerEnvironment .get (i );
391+ for (int i = 0 ; i < this .test2run .peerEnvironment .size (); i ++) {
392+ PeerHostingEnvironmentDescription peerEnvironment = this .test2run .peerEnvironment .get (i );
325393 TestScriptDescription testScriptDescription = new TestScriptDescription (
326- peerEnvironment .ipAddress , // peer IP Address
394+ peerEnvironment .toString () , // peer IP Address
327395 i , // peerName
328396 effectiveScripts [i ], // testscript to run
329- this .testNumber
397+ this .testNumber ,
398+ peerEnvironment .peerID
330399 );
331400
332401 // send message
@@ -388,15 +457,15 @@ private TestEnsemble findFittingPeers() {
388457 for (OrchestratedTest waitingTest : this .orchestratedTestsWaiting ) {
389458 waitingTestIndex ++;
390459 // walk through required environments
391- for (PeerHostingEnvironmentDescription requiredEnvironment : waitingTest .requiredPeerEnvironment ) {
460+ for (PeerHostingEnvironmentDescription requiredEnvironment : waitingTest .peerEnvironment ) {
392461 // let's look for enough peers to run the test
393462
394463 // it is a list... important since position fitting peer same as required environment
395464 List <PeerHostingEnvironmentDescription > fittingPeers = new ArrayList <>();
396465
397466 // let's walk through available peers to look for a match
398- for (String ipAdress : this .availablePeers .keySet ()) {
399- PeerHostingEnvironmentDescription availableEnvironment = this .availablePeers .get (ipAdress );
467+ for (String peerID : this .availablePeers .keySet ()) {
468+ PeerHostingEnvironmentDescription availableEnvironment = this .availablePeers .get (peerID );
400469 // match?
401470 boolean match = true ;
402471
@@ -417,7 +486,7 @@ private TestEnsemble findFittingPeers() {
417486 if (match ) {
418487 // indeed - found one
419488 fittingPeers .add (availableEnvironment );
420- if (fittingPeers .size () == waitingTest .requiredPeerEnvironment .size ()) {
489+ if (fittingPeers .size () == waitingTest .peerEnvironment .size ()) {
421490 // we have all required peers - done here.
422491 return new TestEnsemble (waitingTestIndex , fittingPeers );
423492 }
0 commit comments