Skip to content

Commit 9cd8758

Browse files
committed
renamed PersistentOnlineConnection to ASAPSessionImpl. Much better name. Its implementation is partly still a mess. Never cleaned up after introducing transient messages. Anyway, postpone cleaning up until a serious problem comes up.
1 parent 70643e0 commit 9cd8758

File tree

5 files changed

+56
-34
lines changed

5 files changed

+56
-34
lines changed

src/main/java/net/sharksystem/asap/crypto/ASAPKeyStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ public interface ASAPKeyStore extends ASAPCryptoParameterStorage {
3232
*/
3333
void generateKeyPair() throws ASAPSecurityException;
3434

35-
// void setMementoTarget(ExtraData extraData);
35+
void setMementoTarget(ExtraData extraData);
3636
}

src/main/java/net/sharksystem/asap/crypto/InMemoASAPKeyStore.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ public void setMementoTarget(ExtraData extraData) {
280280
byte[] memento = this.mementoExtraData.getExtra(ASAP_KEYSTORE_MEMENTO_KEY);
281281
this.restoreMemento(memento);
282282
} catch (IOException | SharkException e) {
283-
// no memento - okay
283+
// no memento - hm, maybe was set when this keystore was already running - better save
284+
this.save();
284285
}
285286
}
286287

src/main/java/net/sharksystem/asap/engine/ASAPInternalPeerFS.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public ASAPEngine getASAPEngine(CharSequence format)
294294
}
295295

296296
public EngineSetting getEngineSettings(CharSequence format) throws ASAPException {
297-
EngineSetting folderAndListener = folderMap.get(format);
297+
EngineSetting folderAndListener = this.folderMap.get(format);
298298
if(folderAndListener == null)
299299
throw new ASAPException("no folder for owner / format: " + owner + "/" + format);
300300

@@ -346,7 +346,7 @@ public ASAPConnection handleConnection(
346346
if(appsBlackList == null) appsBlackList = new HashSet<>(); // empty set no null
347347

348348
// TODO add white / black list.
349-
ASAPPersistentConnection asapConnection = new ASAPPersistentConnection(
349+
ASAPSessionImpl asapConnection = new ASAPSessionImpl(
350350
is, os, this, new ASAP_Modem_Impl(),
351351
this, this.ASAPKeyStore,
352352
maxExecutionTime, this, this, encrypt, sign, connectionType);

src/main/java/net/sharksystem/asap/protocol/ASAPPersistentConnection.java renamed to src/main/java/net/sharksystem/asap/protocol/ASAPSessionImpl.java

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package net.sharksystem.asap.protocol;
22

3+
import net.sharksystem.asap.ASAP;
34
import net.sharksystem.asap.ASAPEncounterConnectionType;
45
import net.sharksystem.asap.ASAPException;
6+
import net.sharksystem.asap.engine.ASAPEngine;
57
import net.sharksystem.asap.engine.ASAPInternalPeer;
68
import net.sharksystem.asap.engine.ASAPUndecryptableMessageHandler;
79
import net.sharksystem.asap.engine.EngineSetting;
@@ -12,7 +14,7 @@
1214
import java.util.ArrayList;
1315
import 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
}

src/test/java/net/sharksystem/asap/engine/MultihopTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ public void connectionWithNoDataExchange() throws IOException, ASAPException, In
139139
System.out.println("**************************************************************************");
140140

141141
// kill connections
142-
ui.doKill("all");
142+
try { ui.doKill("all"); }
143+
catch(Exception e) {
144+
System.out.println("exception - but we called kill(); what would we expect? + " + e.getLocalizedMessage());
145+
}
143146
}
144147

145148
// @Test

0 commit comments

Comments
 (0)