@@ -36,7 +36,7 @@ public ASAPPersistentConnection(InputStream is, OutputStream os, MultiASAPEngine
3636 }
3737
3838 private String getLogStart () {
39- return this .getClass ().getSimpleName () + ": " ;
39+ return this .getClass ().getSimpleName () + "(connected to: " + this . remotePeer + ") : " ;
4040 }
4141
4242 private void setRemotePeer (String remotePeerName ) {
@@ -225,26 +225,37 @@ public void run() {
225225 System .out .println (this .getLogStart () + "read valid pdu" );
226226 this .setRemotePeer (asappdu .getPeer ());
227227 // process received pdu
228- try {
229- this .executor =
230- this .multiASAPEngineFS .getExecutorThread (asappdu , this .is , this .os , this );
231- // get exclusive access to streams
232- System .out .println (this .startLog () + "asap pdu executor going to wait for stream access" );
233- this .wait4ExclusiveStreamsAccess ();
228+
229+ if (asappdu .getFormat ().equalsIgnoreCase (ASAP_1_0 .ASAP_MANAGEMENT_FORMAT .toString ())) {
230+ System .out .println (this .getLogStart ()
231+ + "got asap management message - not processed, took remote peer name only" );
232+ } else {
234233 try {
235- System .out .println (this .startLog () + "asap pdu executor got stream access - process pdu" );
236- this .runObservedThread (executor , maxExecutionTime );
237- } catch (ASAPExecTimeExceededException e ) {
238- System .out .println (this .startLog () + "asap pdu processing took longer than allowed" );
239- this .terminate ("asap pdu processing took longer than allowed" , e );
240- break ;
241- } finally {
242- // wake waiting thread if any
243- this .releaseStreamsLock ();
244- System .out .println (this .startLog () + "asap pdu executor release locks" );
234+ this .executor =
235+ new ASAPPDUExecutor (asappdu ,
236+ this .is , this .os ,
237+ this .multiASAPEngineFS .getEngineSettings (asappdu .getFormat ()),
238+ new ASAP_Modem_Impl (),
239+ this );
240+ // this.multiASAPEngineFS.getExecutorThread(asappdu, this.is, this.os, this);
241+ // get exclusive access to streams
242+ System .out .println (this .startLog () + "asap pdu executor going to wait for stream access" );
243+ this .wait4ExclusiveStreamsAccess ();
244+ try {
245+ System .out .println (this .startLog () + "asap pdu executor got stream access - process pdu" );
246+ this .runObservedThread (executor , maxExecutionTime );
247+ } catch (ASAPExecTimeExceededException e ) {
248+ System .out .println (this .startLog () + "asap pdu processing took longer than allowed" );
249+ this .terminate ("asap pdu processing took longer than allowed" , e );
250+ break ;
251+ } finally {
252+ // wake waiting thread if any
253+ this .releaseStreamsLock ();
254+ System .out .println (this .startLog () + "asap pdu executor release locks" );
255+ }
256+ } catch (ASAPException e ) {
257+ System .out .println (this .getLogStart () + " problem when executing asap received pdu: " + e );
245258 }
246- } catch (ASAPException e ) {
247- System .out .println (this .getLogStart () + " problem when executing asap received pdu: " + e );
248259 }
249260 }
250261 }
@@ -327,5 +338,84 @@ private StringBuilder startLog() {
327338
328339 return sb ;
329340 }
330- }
331341
342+ public static class ASAPPDUExecutor extends Thread {
343+ private final ASAP_PDU_1_0 asapPDU ;
344+ private final InputStream is ;
345+ private final OutputStream os ;
346+ private final EngineSetting engineSetting ;
347+ private final ASAP_1_0 protocol ;
348+ private final ThreadFinishedListener threadFinishedListener ;
349+
350+ public ASAPPDUExecutor (ASAP_PDU_1_0 asapPDU , InputStream is , OutputStream os ,
351+ EngineSetting engineSetting , ASAP_1_0 protocol ,
352+ ThreadFinishedListener threadFinishedListener ) {
353+ this .asapPDU = asapPDU ;
354+ this .is = is ;
355+ this .os = os ;
356+ this .engineSetting = engineSetting ;
357+ this .protocol = protocol ;
358+ this .threadFinishedListener = threadFinishedListener ;
359+
360+ StringBuilder sb = new StringBuilder ();
361+ sb .append ("ASAPPDUExecutor created - " );
362+ sb .append ("folder: " + engineSetting .folder + " | " );
363+ sb .append ("engine: " + engineSetting .engine .getClass ().getSimpleName () + " | " );
364+ if (engineSetting .listener != null ) {
365+ sb .append ("listener: " + engineSetting .listener .getClass ().getSimpleName ());
366+ }
367+
368+ System .out .println (sb .toString ());
369+ }
370+
371+ private void finish () {
372+ if (this .threadFinishedListener != null ) {
373+ this .threadFinishedListener .finished (this );
374+ }
375+ }
376+
377+ public void run () {
378+ if (engineSetting .engine == null ) {
379+ System .err .println ("ASAPPDUExecutor called without engine set - fatal" );
380+ this .finish ();
381+ return ;
382+ }
383+
384+ System .out .println ("ASAPPDUExecutor calls engine: " + engineSetting .engine .getClass ().getSimpleName ());
385+
386+ try {
387+ switch (asapPDU .getCommand ()) {
388+ case ASAP_1_0 .INTEREST_CMD :
389+ System .out .println ("ASAPPDUExecutor call handleASAPInterest" );
390+ engineSetting .engine .handleASAPInterest ((ASAP_Interest_PDU_1_0 ) asapPDU , protocol , os );
391+ break ;
392+ case ASAP_1_0 .OFFER_CMD :
393+ System .out .println ("ASAPPDUExecutor call handleASAPOffer" );
394+ engineSetting .engine .handleASAPOffer ((ASAP_OfferPDU_1_0 ) asapPDU , protocol , os );
395+ break ;
396+ case ASAP_1_0 .ASSIMILATE_CMD :
397+ System .out .println ("ASAPPDUExecutor call handleASAPAssimilate" );
398+ engineSetting .engine .handleASAPAssimilate ((ASAP_AssimilationPDU_1_0 ) asapPDU , protocol , is , os ,
399+ engineSetting .listener );
400+ break ;
401+
402+ default :
403+ System .err .println (
404+ this .getClass ().getSimpleName () + ": " + "unknown ASAP command: " + asapPDU .getCommand ());
405+ }
406+ }
407+ catch (IOException | ASAPException e ) {
408+ System .err .println ("Exception while processing ASAP PDU - close streams" + e .getLocalizedMessage ());
409+ try {
410+ os .close (); // more important to close than input stream - do it first
411+ is .close ();
412+ } catch (IOException ex ) {
413+ ex .printStackTrace ();
414+ }
415+ }
416+ finally {
417+ this .finish ();
418+ }
419+ }
420+ }
421+ }
0 commit comments