Skip to content

Commit 0995b7a

Browse files
committed
working on asap management message hopping
1 parent 6260638 commit 0995b7a

21 files changed

+352
-167
lines changed

src/net/sharksystem/asap/ASAPAbstractOnlineMessageSender.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
import java.io.ByteArrayOutputStream;
77
import java.io.IOException;
88
import java.io.OutputStream;
9-
import java.util.ArrayList;
10-
import java.util.HashMap;
11-
import java.util.List;
12-
import java.util.Map;
9+
import java.util.*;
1310

1411
public abstract class ASAPAbstractOnlineMessageSender implements ASAPOnlineMessageSender {
1512
private ASAPStorage source = null;
@@ -29,7 +26,7 @@ public void sendASAPAssimilate(CharSequence format, CharSequence uri, CharSequen
2926
if(recipient == null) {
3027
this.sendASAPAssimilate(format, uri, messageAsBytes, era);
3128
} else {
32-
List<CharSequence> recipients = new ArrayList<>();
29+
Set<CharSequence> recipients = new HashSet<>();
3330
recipients.add(recipient);
3431
this.sendASAPAssimilate(format, uri, recipients, messageAsBytes, era);
3532
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package net.sharksystem.asap;
2+
3+
import java.io.IOException;
4+
import java.util.Set;
5+
6+
public interface ASAPChannel {
7+
CharSequence getOwner() throws IOException;
8+
CharSequence getUri() throws IOException;
9+
Set<CharSequence> getRecipients() throws IOException;
10+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package net.sharksystem.asap;
2+
3+
import java.io.IOException;
4+
import java.util.Set;
5+
6+
public class ASAPChannelImpl implements ASAPChannel {
7+
private static final String CHANNEL_OWNER = "ASAPChannelOwner";
8+
private final ASAPEngine asapEngine;
9+
private final CharSequence uri;
10+
11+
public ASAPChannelImpl(ASAPEngine asapEngine, CharSequence uri) {
12+
this.asapEngine = asapEngine;
13+
this.uri = uri;
14+
}
15+
16+
@Override
17+
public CharSequence getOwner() throws IOException {
18+
return this.asapEngine.getExtra(this.getUri(), CHANNEL_OWNER);
19+
}
20+
21+
@Override
22+
public CharSequence getUri() throws IOException {
23+
return this.uri;
24+
}
25+
26+
@Override
27+
public Set<CharSequence> getRecipients() throws IOException {
28+
return this.asapEngine.getRecipients(this.getUri());
29+
}
30+
31+
public void setOwner(CharSequence owner) throws IOException {
32+
this.asapEngine.putExtra(this.getUri(), CHANNEL_OWNER, owner.toString());
33+
}
34+
}

src/net/sharksystem/asap/ASAPChunk.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.HashMap;
66
import java.util.Iterator;
77
import java.util.List;
8+
import java.util.Set;
89

910
/**
1011
* An AASP chunk contains messages regarding a topic described by an
@@ -65,7 +66,7 @@ public interface ASAPChunk {
6566
*
6667
* @return recipients of that chunk
6768
*/
68-
List<CharSequence> getRecipients();
69+
Set<CharSequence> getRecipients();
6970

7071
/**
7172
* add recipients

src/net/sharksystem/asap/ASAPChunkFS.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ASAPChunkFS implements ASAPChunk {
1616
private final ASAPChunkStorageFS storage;
1717
private String sender;
1818
private String uri = DEFAULT_URL;
19-
private List<CharSequence> recipients;
19+
private Set<CharSequence> recipients;
2020
private List<CharSequence> deliveredTo;
2121
private List<Long> messageStartOffsets = new ArrayList<>();
2222
private File metaFile;
@@ -92,7 +92,7 @@ private void initFiles(String trunkName) throws IOException {
9292
if(!this.readMetaData(this.metaFile)) {
9393
// no metadate to be read - set defaults
9494
this.writeMetaData(this.metaFile);
95-
this.recipients = new ArrayList<>();
95+
this.recipients = new HashSet<>();
9696
this.deliveredTo = new ArrayList<>();
9797
this.messageStartOffsets = new ArrayList<>();
9898
}
@@ -103,7 +103,7 @@ private void saveStatus() throws IOException {
103103
}
104104

105105
@Override
106-
public List<CharSequence> getRecipients() {
106+
public Set<CharSequence> getRecipients() {
107107
return this.recipients;
108108
}
109109

@@ -115,7 +115,7 @@ public void addRecipient(CharSequence recipient) throws IOException {
115115

116116
@Override
117117
public void setRecipients(List<CharSequence> newRecipients) throws IOException {
118-
this.recipients = new ArrayList<>();
118+
this.recipients = new HashSet<>();
119119
for(CharSequence recipient : newRecipients) {
120120
this.recipients.add(recipient);
121121
}
@@ -289,7 +289,7 @@ private boolean readMetaData(File metaFile) throws IOException {
289289
}
290290

291291
try {
292-
this.recipients = Helper.string2CharSequenceList(dis.readUTF());
292+
this.recipients = Helper.string2CharSequenceSet(dis.readUTF());
293293
this.deliveredTo = Helper.string2CharSequenceList(dis.readUTF());
294294

295295
// finally read offset list

src/net/sharksystem/asap/ASAPChunkStorageFS.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public boolean existsChunk(CharSequence uri, int era) throws IOException {
3535
}
3636

3737
String getChunkContentFilename(int era, CharSequence uri) {
38-
return this.getChunkFileTrunkname(era, uri.toString()) + "." + DATA_EXTENSION;
38+
return this.getChunkFileTrunkname(era, uri.toString()) + "." + META_DATA_EXTENSION;
3939
}
4040

4141
String getChunkFileTrunkname(int era, String uri) {

src/net/sharksystem/asap/ASAPEngine.java

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.sharksystem.asap;
22

3+
import net.sharksystem.asap.management.ASAPManagementMessage;
4+
import net.sharksystem.asap.management.ASAPManagementStorage;
35
import net.sharksystem.asap.protocol.*;
46
import net.sharksystem.asap.util.Log;
57

@@ -9,6 +11,7 @@
911
import java.util.ArrayList;
1012
import java.util.HashMap;
1113
import java.util.List;
14+
import java.util.Set;
1215

1316
/**
1417
* That ASAPEngine manages exchange of stored messages with peers.
@@ -91,13 +94,20 @@ public CharSequence getExtra(CharSequence uri, String key) throws IOException {
9194
}
9295

9396
@Override
94-
public void createChannel(CharSequence urlTarget, List<CharSequence> recipients) throws IOException, ASAPException {
95-
this.setRecipients(urlTarget, recipients);
97+
public void createChannel(CharSequence uri, List<CharSequence> recipients) throws IOException, ASAPException {
98+
this.createChannel(this.getOwner(), uri, recipients);
99+
}
100+
101+
@Override
102+
public void createChannel(CharSequence owner, CharSequence uri, List<CharSequence> recipients)
103+
throws IOException, ASAPException {
104+
105+
this.setRecipients(uri, recipients);
106+
this.getASAPChannelImpl(uri).setOwner(owner);
96107

97108
// inform recipients about that event
98109
if(this.isASAPManagementStorageSet()) {
99-
ASAPManagementStorage asapManagementStorage = this.getASAPManagementStorage();
100-
asapManagementStorage.addCreateClosedASAPChannelMessage(this.format, urlTarget, recipients);
110+
this.getASAPManagementStorage().notifyChannelCreated(this.format, owner, uri, recipients);
101111
} else {
102112
System.out.println(this.getLogStart()
103113
+ "asap management storage not set - no propagation of channel creation");
@@ -128,13 +138,13 @@ public boolean isASAPManagementStorageSet() {
128138
return this.asapManagementStorage != null;
129139
}
130140

131-
public void addCreateClosedASAPChannelMessage(CharSequence appName, CharSequence channelUri,
132-
List<CharSequence> recipients)
141+
public void notifyChannelCreated(CharSequence appName, CharSequence owner,
142+
CharSequence uri, List<CharSequence> recipients)
133143
throws ASAPException, IOException {
134144

135145
byte[] createClosedASAPChannelMessage =
136146
ASAPManagementMessage.getCreateClosedASAPChannelMessage(
137-
this.getOwner(), appName, channelUri, recipients);
147+
owner, appName, uri, recipients);
138148

139149
// put into create channel
140150
this.add(ASAPManagementStorage.ASAP_CREATE_CHANNEL, createClosedASAPChannelMessage);
@@ -149,7 +159,7 @@ public void setRecipients(CharSequence urlTarget, List<CharSequence> recipients)
149159
this.chunkStorage.getChunk(urlTarget, this.era).setRecipients(recipients);
150160
}
151161

152-
public List<CharSequence> getRecipients(CharSequence urlTarget) throws IOException {
162+
public Set<CharSequence> getRecipients(CharSequence urlTarget) throws IOException {
153163
return this.chunkStorage.getChunk(urlTarget, this.era).getRecipients();
154164
}
155165

@@ -201,6 +211,19 @@ public List<CharSequence> getChannelURIs() throws IOException {
201211
return uriList;
202212
}
203213

214+
@Override
215+
public ASAPChannel getChannel(CharSequence uri) throws ASAPException, IOException {
216+
return this.getASAPChannelImpl(uri);
217+
}
218+
219+
private ASAPChannelImpl getASAPChannelImpl(CharSequence uri) throws ASAPException, IOException {
220+
if(this.channelExists(uri)) {
221+
return new ASAPChannelImpl(this, uri);
222+
}
223+
224+
throw new ASAPException("channel with does not exist: " + uri);
225+
}
226+
204227
@Override
205228
public boolean channelExists(CharSequence uri) throws IOException {
206229
return this.chunkStorage.existsChunk(uri, this.getEra());
@@ -463,10 +486,6 @@ public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimiliationPDU,
463486
}
464487
}
465488

466-
private boolean isASAPManagementMessage(ASAP_PDU_1_0 asapPDU) {
467-
return asapPDU.getFormat().equalsIgnoreCase(ASAP_1_0.ASAP_MANAGEMENT_FORMAT);
468-
}
469-
470489
public void handleASAPInterest(ASAP_Interest_PDU_1_0 asapInterest, ASAP_1_0 protocol, OutputStream os)
471490
throws ASAPException, IOException {
472491

@@ -612,7 +631,7 @@ private void sendChunks(CharSequence sender, String recipient, ASAPChunkStorage
612631

613632
// is not a public chunk
614633
if (!this.isPublic(chunk)) {
615-
List<CharSequence> recipients = chunk.getRecipients();
634+
Set<CharSequence> recipients = chunk.getRecipients();
616635

617636
if (!recipients.contains(recipient)) {
618637
continue;

src/net/sharksystem/asap/ASAPManagementStorage.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/net/sharksystem/asap/ASAPOnlineMessageSender.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package net.sharksystem.asap;
22

33
import java.io.IOException;
4-
import java.util.List;
4+
import java.util.Set;
55

66
public interface ASAPOnlineMessageSender {
7-
void sendASAPAssimilate(CharSequence format, CharSequence urlTarget, List<CharSequence> recipients,
7+
void sendASAPAssimilate(CharSequence format, CharSequence urlTarget, Set<CharSequence> recipients,
88
byte[] messageAsBytes, int era) throws IOException, ASAPException;
99

1010
void sendASAPAssimilate(CharSequence format, CharSequence urlTarget, CharSequence recipient,

src/net/sharksystem/asap/ASAPOnlineMessageSenderEngineSide.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void sendASAPAssimilate(CharSequence format, CharSequence uri, byte[] mes
3636
throw new ASAPException("no online peers");
3737
}
3838

39-
List<CharSequence> onlinePeerList = new ArrayList<>();
39+
Set<CharSequence> onlinePeerList = new HashSet<>();
4040
for(CharSequence peerName : onlinePeers) {
4141
onlinePeerList.add(peerName);
4242
System.out.println(this.getLogStart() + peerName + "is online");
@@ -45,8 +45,8 @@ public void sendASAPAssimilate(CharSequence format, CharSequence uri, byte[] mes
4545
this.sendASAPAssimilate(format, uri, onlinePeerList, messageAsBytes, era);
4646
}
4747

48-
public void sendASAPAssimilate(CharSequence format, CharSequence uri, List<CharSequence> recipients,
49-
byte[] messageAsBytes, int era) throws IOException, ASAPException {
48+
public void sendASAPAssimilate(CharSequence format, CharSequence uri, Set<CharSequence> recipients,
49+
byte[] messageAsBytes, int era) throws IOException, ASAPException {
5050

5151
if(recipients == null || recipients.size() < 1) {
5252
this.sendASAPAssimilate(format, uri, messageAsBytes, era);

0 commit comments

Comments
 (0)