11package net .sharksystem .asap .protocol ;
22
3+ import net .sharksystem .asap .ASAP ;
34import net .sharksystem .asap .ASAPEncounterConnectionType ;
45import net .sharksystem .asap .ASAPException ;
6+ import net .sharksystem .asap .engine .ASAPEngine ;
57import net .sharksystem .asap .engine .ASAPInternalPeer ;
68import net .sharksystem .asap .engine .ASAPUndecryptableMessageHandler ;
79import net .sharksystem .asap .engine .EngineSetting ;
1214import java .util .ArrayList ;
1315import java .util .List ;
1416
15- public class ASAPPersistentConnection extends ASAPProtocolEngine
17+ public class ASAPSessionImpl extends ASAPProtocolEngine
1618 implements ASAPConnection , Runnable , ThreadFinishedListener {
1719
1820 private final List <ASAPConnectionListener > asapConnectionListener ;
@@ -29,12 +31,12 @@ public class ASAPPersistentConnection extends ASAPProtocolEngine
2931 private Thread threadWaiting4StreamsLock ;
3032 private boolean terminated = false ;
3133
32- public ASAPPersistentConnection (InputStream is , OutputStream os , ASAPInternalPeer asapInternalPeer ,
33- ASAP_1_0 protocol , ASAPUndecryptableMessageHandler unencryptableMessageHandler ,
34- ASAPKeyStore ASAPKeyStore ,
35- long maxExecutionTime , ASAPConnectionListener asapConnectionListener ,
36- ThreadFinishedListener threadFinishedListener ,
37- boolean encrypt , boolean sign , ASAPEncounterConnectionType connectionType ) {
34+ public ASAPSessionImpl (InputStream is , OutputStream os , ASAPInternalPeer asapInternalPeer ,
35+ ASAP_1_0 protocol , ASAPUndecryptableMessageHandler unencryptableMessageHandler ,
36+ ASAPKeyStore ASAPKeyStore ,
37+ long maxExecutionTime , ASAPConnectionListener asapConnectionListener ,
38+ ThreadFinishedListener threadFinishedListener ,
39+ boolean encrypt , boolean sign , ASAPEncounterConnectionType connectionType ) {
3840
3941 super (is , os , protocol , unencryptableMessageHandler , ASAPKeyStore );
4042
@@ -57,10 +59,6 @@ public void removeASAPConnectionListener(ASAPConnectionListener asapConnectionLi
5759 this .asapConnectionListener .remove (asapConnectionListener );
5860 }
5961
60- private String getLogStart () {
61- return this .getClass ().getSimpleName () + " " + this .getLogParameter ();
62- }
63-
6462 private String getLogParameter () {
6563 String s = "to: " ;
6664 s += this .encounteredPeer != null ? this .encounteredPeer : "unknown yet" ;
@@ -146,6 +144,11 @@ private void terminate(String message, Throwable t) {
146144 sb .append (t .getLocalizedMessage ());
147145 // debugging
148146 //t.printStackTrace();
147+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
148+ t .printStackTrace (new PrintStream (baos ));
149+ sb .append ("\n >>>>>>>>>>>>> stack trace:\n " );
150+ sb .append (baos .toString ());
151+ sb .append ("<<<<<<<<<<<< stack trace" );
149152 }
150153 Log .writeLog (this , this .getLogParameter (), sb .toString ());
151154
@@ -259,15 +262,29 @@ public void run() {
259262 ASAP_PDU_1_0 asappdu = pduReader .getASAPPDU ();
260263 /////////////////////////////// process
261264 if (asappdu != null ) {
262- Log .writeLog (this , this .getLogParameter (), "read valid pdu" );
265+ Log .writeLog (this , this .getLogParameter (),
266+ "read valid pdu, remember meeting this peer and going to process pdu" );
263267 this .setEncounteredPeer (asappdu .getSender ());
264268
269+ EngineSetting engineSettings = null ;
265270 try {
271+ engineSettings = this .asapInternalPeer .getEngineSettings (asappdu .getFormat ());
272+ } catch (ASAPException e ) {
273+ // can happen with transient messages
274+ Log .writeLog (this , this .getLogParameter (), "no engine setting - set defaults" );
275+ }
276+
277+ try {
278+ if (engineSettings == null ) {
279+ ASAPEngine asapEngine = this .asapInternalPeer .getASAPEngine (asappdu .getFormat ());
280+ engineSettings = this .asapInternalPeer .getEngineSettings (asappdu .getFormat ());
281+ engineSettings .engine = asapEngine ;
282+ Log .writeLog (this , this .getLogParameter (), engineSettings .toString ());
283+ }
284+
266285 this .executor = new ASAPPDUExecutor (asappdu ,
267- this .encounteredPeer ,
268- this .is , this .os ,
269- this .asapInternalPeer .getEngineSettings (asappdu .getFormat ()),
270- protocol ,this .connectionType , this );
286+ this .encounteredPeer , this .is , this .os , engineSettings ,
287+ protocol ,this .connectionType , this );
271288
272289 // get exclusive access to streams
273290 Log .writeLog (this , this .getLogParameter (), "asap pdu executor going to wait for stream access" );
@@ -284,8 +301,9 @@ public void run() {
284301 this .releaseStreamsLock ();
285302 Log .writeLog (this , this .getLogParameter (), "asap pdu executor release locks" );
286303 }
287- } catch (ASAPException e ) {
288- Log .writeLog (this , this .getLogParameter (), " problem when executing asap received pdu: " + e );
304+ } catch (ASAPException | IOException e ) {
305+ // Log.writeLog(this, this.getLogParameter(), "problem when executing asap received pdu: " + e);
306+ this .terminate ("problem when executing asap received pdu: " , e );
289307 }
290308 }
291309 }
@@ -392,7 +410,7 @@ public ASAPPDUExecutor(ASAP_PDU_1_0 asapPDU, String encounteredPeer, InputStream
392410 }
393411 sb .append ("folder: " + engineSetting .folder );
394412
395- Log .writeLog (this , ASAPPersistentConnection .this .getLogParameter (), sb .toString ());
413+ Log .writeLog (this , ASAPSessionImpl .this .getLogParameter (), sb .toString ());
396414 }
397415
398416 private void finish () {
@@ -403,20 +421,20 @@ private void finish() {
403421
404422 public void run () {
405423 if (engineSetting .engine == null ) {
406- Log .writeLogErr (this , ASAPPersistentConnection .this .getLogParameter (),
424+ Log .writeLogErr (this , ASAPSessionImpl .this .getLogParameter (),
407425 "ASAPPDUExecutor called without engine set - fatal" );
408426 this .finish ();
409427 return ;
410428 }
411429
412- Log .writeLog (this , ASAPPersistentConnection .this .getLogParameter (),
430+ Log .writeLog (this , ASAPSessionImpl .this .getLogParameter (),
413431 "ASAPPDUExecutor calls engine: " + engineSetting .engine .getClass ().getSimpleName ());
414432
415433 try {
416434 switch (asapPDU .getCommand ()) {
417435 // TODO add encrypt / sign as parameter..
418436 case ASAP_1_0 .INTEREST_CMD :
419- Log .writeLog (this , ASAPPersistentConnection .this .getLogParameter (),
437+ Log .writeLog (this , ASAPSessionImpl .this .getLogParameter (),
420438 "ASAPPDUExecutor call handleASAPInterest" );
421439 engineSetting .engine .handleASAPInterest (
422440 (ASAP_Interest_PDU_1_0 ) asapPDU , this .protocol ,
@@ -425,7 +443,7 @@ public void run() {
425443 this .connectionType );
426444 break ;
427445 case ASAP_1_0 .ASSIMILATE_CMD :
428- Log .writeLog (this , ASAPPersistentConnection .this .getLogParameter (),
446+ Log .writeLog (this , ASAPSessionImpl .this .getLogParameter (),
429447 "ASAPPDUExecutor call handleASAPAssimilate" );
430448 engineSetting .engine .handleASAPAssimilate (
431449 (ASAP_AssimilationPDU_1_0 ) this .asapPDU ,
@@ -436,22 +454,22 @@ public void run() {
436454 break ;
437455
438456 default :
439- Log .writeLogErr (this , ASAPPersistentConnection .this .getLogParameter (),
457+ Log .writeLogErr (this , ASAPSessionImpl .this .getLogParameter (),
440458 "unknown ASAP command: " + asapPDU .getCommand ());
441459 }
442460 }
443461 catch (ASAPException asape ) {
444- Log .writeLog (this , ASAPPersistentConnection .this .getLogParameter (),
462+ Log .writeLog (this , ASAPSessionImpl .this .getLogParameter (),
445463 "while processing PDU (go ahead): " + asape .getLocalizedMessage ());
446464 }
447465 catch (IOException ioe ) {
448- Log .writeLogErr (this ,ASAPPersistentConnection .this .getLogParameter (),
466+ Log .writeLogErr (this , ASAPSessionImpl .this .getLogParameter (),
449467 "IOException while processing ASAP PDU - close streams: " + ioe .getLocalizedMessage ());
450468 try {
451469 os .close (); // more important to close than input stream - try first
452470 is .close ();
453471 } catch (IOException ex ) {
454- Log .writeLog (this , ASAPPersistentConnection .this .getLogParameter (), ex .getLocalizedMessage ());
472+ Log .writeLog (this , ASAPSessionImpl .this .getLogParameter (), ex .getLocalizedMessage ());
455473 //ex.printStackTrace();
456474 }
457475 }
@@ -501,10 +519,10 @@ public void run() {
501519 //while((encrypt && !this.asapPDU.encrypted()) || (sign && !this.asapPDU.verified()));
502520 } catch (IOException e ) {
503521 this .ioException = e ;
504- Log .writeLog (this , ASAPPersistentConnection .this .getLogParameter (),
522+ Log .writeLog (this , ASAPSessionImpl .this .getLogParameter (),
505523 "IOException when reading from stream" );
506524 } catch (ASAPException e ) {
507- Log .writeLog (this , ASAPPersistentConnection .this .getLogParameter (),
525+ Log .writeLog (this , ASAPSessionImpl .this .getLogParameter (),
508526 "ASAPException when reading from stream" );
509527 this .asapException = e ;
510528 }
0 commit comments