Skip to content

Commit 2fd835b

Browse files
committed
changed something when working on hub management features in CLI in SharkMessenger. Added an additional way for sending a transient message.
1 parent 668a8f8 commit 2fd835b

File tree

5 files changed

+89
-18
lines changed

5 files changed

+89
-18
lines changed

src/main/java/net/sharksystem/asap/ASAPMessageSender.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,20 @@ void sendASAPMessage(CharSequence appName, CharSequence uri,
2525
*/
2626
void sendTransientASAPMessage(CharSequence appName, CharSequence uri, byte[] message)
2727
throws ASAPException, IOException;
28+
29+
/**
30+
* When calling this methode, this asap message is sent over any existing connection.
31+
* It is not stored on sender or receiver side. Message listeners are called as usual. Nothing happens (no
32+
* exception is thrown) if there is not a single peer encounter running.
33+
*
34+
* @param nextHopPeerID Peer will try to send a message only to this peer. An ASAPException is thrown if there
35+
* is no running encounter with that peer in place.
36+
* @param appName
37+
* @param uri
38+
* @param message
39+
* @throws ASAPException
40+
* @throws IOException
41+
*/
42+
void sendTransientASAPMessage(CharSequence nextHopPeerID, CharSequence appName, CharSequence uri, byte[] message)
43+
throws ASAPException, IOException;
2844
}

src/main/java/net/sharksystem/asap/ASAPPeerFS.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.sharksystem.asap.utils.ASAPLogHelper;
55
import net.sharksystem.utils.Log;
66

7+
import javax.swing.*;
78
import java.io.IOException;
89
import java.util.ArrayList;
910
import java.util.Collection;
@@ -147,6 +148,14 @@ public void sendTransientASAPMessage(CharSequence appName, CharSequence uri, byt
147148

148149
}
149150

151+
public void sendTransientASAPMessage(CharSequence nextHopPeerID,
152+
CharSequence appName, CharSequence uri, byte[] message) throws ASAPException, IOException {
153+
154+
Log.writeLog(this, this.getInternalPeer().getOwner().toString(),
155+
"try sending transient message over existing connections to peerID: " + nextHopPeerID);
156+
this.getInternalPeer().sendTransientASAPAssimilateMessage(appName, uri, nextHopPeerID, message);
157+
}
158+
150159
public String toString() {
151160
return this.getInternalPeer().getOwner().toString();
152161
}

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

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,57 @@ void setAsapRoutingAllowed(CharSequence applicationFormat, boolean allowed)
9292
void deactivateOnlineMessages();
9393

9494
/**
95-
* This message is only transmitted with open connections. Message is not stored. Nothing happens if no open
96-
* connection is present.
95+
* A transient message is not stored and not meant to be forwarded. Sending a transient message has no effect
96+
* without a running encounter. Despite that, it is an ordinary ASAP message - described by an application/format
97+
* and an optional uri.
98+
*
99+
* @param nextHopPeerIDs A peer can have multiple encounter at the same time. This list - if present, names
100+
* potential message receiver. If null, message is sent to any open connection. An exception
101+
* is <b>not thrown</b> if there is no connection to one or more peers in the list
102+
* @param format message application / format
103+
* @param urlTarget describe message within your app
104+
* @param messageAsBytes serialized message
105+
* @throws IOException
106+
* @throws ASAPException
107+
*/
108+
void sendTransientASAPAssimilateMessage(CharSequence format, CharSequence urlTarget,
109+
Set<CharSequence> nextHopPeerIDs, byte[] messageAsBytes) throws IOException, ASAPException;
110+
111+
/**
112+
* Send a transient message to a single peer.
113+
* <b>An exception is thrown if there is no open connection the the specified peer</b>
97114
* @param format
98115
* @param urlTarget
99-
* @param recipients
116+
* @param nextHopPeerID
100117
* @param messageAsBytes
101118
* @throws IOException
102119
* @throws ASAPException
103120
*/
104-
void sendTransientASAPAssimilateMessage(CharSequence format, CharSequence urlTarget, Set<CharSequence> recipients,
105-
byte[] messageAsBytes) throws IOException, ASAPException;
121+
void sendTransientASAPAssimilateMessage(CharSequence format, CharSequence urlTarget,
122+
CharSequence nextHopPeerID, byte[] messageAsBytes) throws IOException, ASAPException;
106123

124+
/**
125+
* Send a transient message to any peer we have an open connection to.
126+
*
127+
* @param format
128+
* @param urlTarget
129+
* @param messageAsBytes
130+
* @throws IOException
131+
* @throws ASAPException
132+
*/
107133
void sendTransientASAPAssimilateMessage(CharSequence format, CharSequence urlTarget, byte[] messageAsBytes)
108134
throws IOException, ASAPException;
109135

136+
/**
137+
* @deprecated use sendTransientASAPAssimilateMessage instead
138+
* @param format
139+
* @param urlTarget
140+
* @param era
141+
* @param recipients
142+
* @param messageAsBytes
143+
* @throws IOException
144+
* @throws ASAPException
145+
*/
110146
void sendOnlineASAPAssimilateMessage(CharSequence format, CharSequence urlTarget, int era,
111147
Set<CharSequence> recipients, byte[] messageAsBytes) throws IOException, ASAPException;
112148

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -610,25 +610,29 @@ public void setASAPBasicKeyStorage(ASAPKeyStore ASAPKeyStore) {
610610

611611
public void sendTransientASAPAssimilateMessage(CharSequence format, CharSequence uri, byte[] messageAsBytes)
612612
throws IOException, ASAPException {
613-
this.sendTransientASAPAssimilateMessage(format, uri, null, messageAsBytes);
613+
this.sendTransientASAPAssimilateMessage(format, uri, (Set<CharSequence>)null, messageAsBytes);
614614
}
615615

616616
public void sendTransientASAPAssimilateMessage(CharSequence format, CharSequence uri,
617-
Set<CharSequence> receiver, byte[] messageAsBytes)
618-
throws IOException, ASAPException {
617+
Set<CharSequence> nextHopPeerIDs, byte[] messageAsBytes) throws IOException, ASAPException {
619618

620-
// int era = ASAP.TRANSIENT_ERA; // init
619+
this.sendOnlineASAPAssimilateMessage(format, uri, ASAP.TRANSIENT_ERA, nextHopPeerIDs, messageAsBytes);
620+
}
621621

622-
/*
623-
try {
624-
era = this.getASAPEngine(format).getEra();
625-
} catch (ASAPException e) {
626-
// no engine.. ok
627-
Log.writeLog(this, "send message with format but no engine exists (yet): " + format);
622+
public void sendTransientASAPAssimilateMessage(CharSequence format, CharSequence uri,
623+
CharSequence nextHopPeerID, byte[] messageAsBytes) throws IOException, ASAPException {
624+
625+
if(nextHopPeerID == null) throw new ASAPException("next hop peer id must not be null");
626+
627+
if(!this.existASAPConnection(nextHopPeerID)) {
628+
String log = "cannot send transient message. No open connection to peer with id: " + nextHopPeerID;
629+
Log.writeLog(this, log);
630+
throw new ASAPException(log);
628631
}
629-
*/
630632

631-
this.sendOnlineASAPAssimilateMessage(format, uri, ASAP.TRANSIENT_ERA, receiver, messageAsBytes);
633+
Set<CharSequence> nextHopPeerIDs = new HashSet<>();
634+
nextHopPeerIDs.add(nextHopPeerID);
635+
this.sendOnlineASAPAssimilateMessage(format, uri, ASAP.TRANSIENT_ERA, nextHopPeerIDs, messageAsBytes);
632636
}
633637

634638
public void sendOnlineASAPAssimilateMessage(CharSequence format, CharSequence uri, int era, byte[] messageAsBytes)

src/main/java/net/sharksystem/utils/tcp/SocketFactory.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ public void run() {
7171
Log.writeLog(this, "resume waiting for new connections on port " + this.port);
7272
} while(this.remainOpen);
7373
} catch (IOException e) {
74-
e.printStackTrace();
74+
if(!remainOpen) {
75+
Log.writeLog(this, "socket factory terminated, closed port " + this.port);
76+
} else {
77+
Log.writeLogErr(this, "socket factory terminated but not from an app(?): "
78+
+ e.getLocalizedMessage());
79+
}
80+
7581
}
7682
}
7783

0 commit comments

Comments
 (0)