1010import java .util .*;
1111
1212public class MultiASAPEngineFS_Impl implements
13- MultiASAPEngineFS , ASAPManagementProtocolEngine , ASAPConnectionListener , ThreadFinishedListener {
13+ MultiASAPEngineFS , ASAPConnectionListener , ThreadFinishedListener , ASAPChunkReceivedListener {
1414 private final CharSequence rootFolderName ;
1515 private final ASAPChunkReceivedListener listener ;
1616 private CharSequence owner ;
@@ -74,6 +74,12 @@ private void setupFolderMap() throws IOException, ASAPException {
7474 this .folderMap .put (engine .format , setting );
7575 }
7676 }
77+
78+ // set yourself as listener to asap management app
79+ EngineSetting folderAndListener = folderMap .get (ASAP_1_0 .ASAP_MANAGEMENT_FORMAT );
80+ if (folderAndListener != null ) {
81+ folderAndListener .listener = this ;
82+ }
7783 }
7884
7985 /**
@@ -360,30 +366,40 @@ public void pushInterests(OutputStream os) throws IOException, ASAPException {
360366 }
361367 }
362368
363- public boolean handleASAPManagementPDU (ASAP_PDU_1_0 asapPDU , ASAP_1_0 protocol ,
364- InputStream is ) throws ASAPException , IOException {
369+ private String getLogStart () {
370+ return this .getClass ().getSimpleName () + "(" + this .getOwner () + "): " ;
371+ }
372+
373+ ///////////////////////////////////////////////////////////////////////////////////////////////////////
374+ // chunk received listener for asap management engine //
375+ ///////////////////////////////////////////////////////////////////////////////////////////////////////
376+
377+ @ Override
378+ public void chunkReceived (String sender , String uri , int era ) {
379+ System .out .println (this .getLogStart ()
380+ + "handle received chunk (sender/uri/era)" + sender + "/" + uri + "/" + era );
381+ try {
382+ ASAPEngine asapManagementEngine = this .getEngineByFormat (ASAP_1_0 .ASAP_MANAGEMENT_FORMAT );
383+ ASAPChunkStorage incomingChunkStorage = asapManagementEngine .getIncomingChunkStorage (sender );
384+ ASAPChunk chunk = incomingChunkStorage .getChunk (uri , era );
385+ Iterator <byte []> messageIter = chunk .getMessagesAsBytes ();
386+ System .out .println (this .getLogStart () + "iterate management messages" );
387+ while (messageIter .hasNext ()) {
388+ byte [] message = messageIter .next ();
389+ this .handleASAPManagementMessage (sender , message );
390+ }
391+ System .out .println (this .getLogStart () + "done iterating management messages" );
392+ } catch (ASAPException | IOException e ) {
393+ System .out .println ("could get asap management engine but received chunk - looks like a bug" );
394+ }
395+ }
365396
397+ private void handleASAPManagementMessage (CharSequence owner , byte [] message ) throws ASAPException , IOException {
366398 StringBuilder b = new StringBuilder ();
367399 b .append (this .getLogStart ());
368400 b .append ("start processing asap management pdu" );
369401 System .out .println (b .toString ());
370402
371- System .out .println (this .getLogStart () + asapPDU );
372-
373- ASAP_AssimilationPDU_1_0 asap_assimilationPDU_1_0 = null ;
374- if (asapPDU instanceof ASAP_AssimilationPDU_1_0 ) {
375- asap_assimilationPDU_1_0 = (ASAP_AssimilationPDU_1_0 ) asapPDU ;
376- } else {
377- b = new StringBuilder ();
378- b .append (this .getLogStart ());
379- b .append ("asap management pdu not an assimilate message - let ordinary engine to the job" );
380- System .out .println (b .toString ());
381- return false ;
382- }
383-
384- CharSequence owner = asapPDU .getPeer ();
385- byte [] message = asap_assimilationPDU_1_0 .getData ();
386-
387403 ASAPManagementCreateASAPStorageMessage asapManagementCreateASAPStorageMessage =
388404 ASAPManagementMessage .parseASAPManagementMessage (owner , message );
389405
@@ -395,7 +411,7 @@ public boolean handleASAPManagementPDU(ASAP_PDU_1_0 asapPDU, ASAP_1_0 protocol,
395411 List <CharSequence > recipients = new ArrayList <>();
396412
397413 // add owner
398- recipients .add (asapPDU . getPeer () );
414+ recipients .add (owner );
399415
400416 // add rest
401417 for (CharSequence r : receivedRecipients ) {
@@ -412,7 +428,6 @@ public boolean handleASAPManagementPDU(ASAP_PDU_1_0 asapPDU, ASAP_1_0 protocol,
412428 b .append (channelUri );
413429 b .append (" | #recipients: " );
414430 b .append (recipients .size ());
415- System .out .println (b .toString ());
416431
417432 // find storage / app - can throw an exception - that's ok
418433 ASAPStorage asapStorage = this .getEngineByFormat (format );
@@ -433,25 +448,18 @@ public boolean handleASAPManagementPDU(ASAP_PDU_1_0 asapPDU, ASAP_1_0 protocol,
433448 }
434449 }
435450 if (!found ) {
436- throw new ASAPException ("channel already exists but with different recipients" );
451+ throw new ASAPException ("channel already exists but with different recipients: " + b . toString () );
437452 }
438453 }
439454 // ok it the same
440- return false ;
455+ return ;
441456 } else {
442- throw new ASAPException ("channel already exists but with different settings" );
457+ throw new ASAPException ("channel already exists but with different settings: " + b . toString () );
443458 }
444459 }
445460
446461 // else - channel does not exist - create by setting recipients
447- System .out .println (this .getLogStart () + "create channel" );
462+ System .out .println (this .getLogStart () + "create channel: " + b . toString () );
448463 asapStorage .createChannel (channelUri , recipients );
449- System .err .println (this .getLogStart () + ".handleASAPManagementPDU(): TODO: return true - works / false not - but must be false..." );
450- // return true;
451- return false ;
452- }
453-
454- private String getLogStart () {
455- return this .getClass ().getSimpleName () + "(" + this .getOwner () + "): " ;
456464 }
457465}
0 commit comments