Skip to content

Commit f4302f3

Browse files
committed
about implementing closed channels and introducing concept of asap management storage
1 parent 435d66d commit f4302f3

File tree

9 files changed

+176
-170
lines changed

9 files changed

+176
-170
lines changed

src/net/sharksystem/asap/ASAPEngine.java

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @see ASAPStorage
1818
* @author thsc
1919
*/
20-
public abstract class ASAPEngine implements ASAPStorage, ASAPProtocolEngine {
20+
public abstract class ASAPEngine implements ASAPStorage, ASAPProtocolEngine, ASAPManagementStorage {
2121
public static final String ANONYMOUS_OWNER = "anon";
2222
static String DEFAULT_OWNER = ANONYMOUS_OWNER;
2323
static int DEFAULT_INIT_ERA = 0;
@@ -91,17 +91,54 @@ public CharSequence getExtra(CharSequence uri, String key) throws IOException {
9191
}
9292

9393
@Override
94-
public void createChannel(CharSequence urlTarget, List<CharSequence> recipients) throws IOException {
94+
public void createChannel(CharSequence urlTarget, List<CharSequence> recipients) throws IOException, ASAPException {
9595
this.setRecipients(urlTarget, recipients);
96+
97+
// inform recipients about that event
98+
if(this.isASAPManagementStorageSet()) {
99+
ASAPManagementStorage asapManagementStorage = this.getASAPManagementStorage();
100+
asapManagementStorage.addCreateClosedASAPChannelMessage(this.format, urlTarget, recipients);
101+
} else {
102+
System.out.println(this.getLogStart()
103+
+ "asap management storage not set - no propagation of channel creation");
104+
}
96105
}
97106

98107
@Override
99-
public void createChannel(CharSequence urlTarget, CharSequence recipient) throws IOException {
108+
public void createChannel(CharSequence urlTarget, CharSequence recipient) throws IOException, ASAPException {
100109
ArrayList<CharSequence> recipients = new ArrayList<>();
101110
recipients.add(recipient);
102-
this.setRecipients(urlTarget, recipients);
111+
this.createChannel(urlTarget, recipients);
112+
}
113+
114+
private ASAPManagementStorage asapManagementStorage = null;
115+
public void setASAPManagementStorage(ASAPManagementStorage asapManagementStorage) {
116+
this.asapManagementStorage = asapManagementStorage;
117+
}
118+
119+
public ASAPManagementStorage getASAPManagementStorage() throws IOException, ASAPException {
120+
if(this.asapManagementStorage == null) {
121+
throw new ASAPException("ASAP Management Storage not set");
122+
}
123+
124+
return this.asapManagementStorage;
125+
}
126+
127+
public boolean isASAPManagementStorageSet() {
128+
return this.asapManagementStorage != null;
129+
}
130+
131+
public void addCreateClosedASAPChannelMessage(CharSequence appName, CharSequence channelUri,
132+
List<CharSequence> recipients)
133+
throws ASAPException, IOException {
134+
135+
byte[] createClosedASAPChannelMessage =
136+
ASAPManagementMessage.getCreateClosedASAPChannelMessage(
137+
this.getOwner(), appName, channelUri, recipients);
138+
139+
// put into create channel
140+
this.add(ASAPManagementStorage.ASAP_CREATE_CHANNEL, createClosedASAPChannelMessage);
103141

104-
// inform recipients about that event
105142
}
106143

107144
public void addRecipient(CharSequence urlTarget, CharSequence recipient) throws IOException {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package net.sharksystem.asap;
2+
3+
import java.io.IOException;
4+
import java.util.List;
5+
6+
public interface ASAPManagementStorage {
7+
CharSequence ASAP_CREATE_CHANNEL = "asap://createChannel";
8+
9+
void addCreateClosedASAPChannelMessage(CharSequence appName, CharSequence channelUri,
10+
List<CharSequence> storageRecipients) throws ASAPException, IOException;
11+
}

src/net/sharksystem/asap/ASAPStorage.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ public interface ASAPStorage {
6262
* @param recipients
6363
* @throws IOException
6464
*/
65-
void createChannel(CharSequence urlTarget, List<CharSequence> recipients) throws IOException;
65+
void createChannel(CharSequence urlTarget, List<CharSequence> recipients) throws IOException, ASAPException;
6666

6767
/**
6868
* Create a channel with only two members - creator and recipient
6969
* @param urlTarget
7070
* @param recipient
7171
* @throws IOException
7272
*/
73-
void createChannel(CharSequence urlTarget, CharSequence recipient) throws IOException;
73+
void createChannel(CharSequence urlTarget, CharSequence recipient) throws IOException, ASAPException;
7474

7575
/**
7676
* Chunks are delivered when seeing other peers. This flag allows to decide whether delivered chunks
@@ -146,6 +146,10 @@ public interface ASAPStorage {
146146

147147
void detachASAPMessageAddListener(ASAPOnlineMessageSender asapOnlineMessageSender);
148148

149+
void setASAPManagementStorage(ASAPManagementStorage asapManagementStorage);
150+
151+
boolean isASAPManagementStorageSet();
152+
149153
/**
150154
* Create a new era
151155
*/

src/net/sharksystem/asap/MultiASAPEngineFS_Impl.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,15 @@ public void handleASAPManagementPDU(ASAP_PDU_1_0 asapPDU, ASAP_1_0 protocol,
367367
return;
368368
}
369369

370-
ASAPManagementCreateASAPStoragePDU asapManagementPDU =
371-
ASAPManagementProtocolPDU_Impl.parseASAPPDU(asap_assimilationPDU_1_0);
370+
CharSequence owner = asapPDU.getPeer();
371+
byte[] message = asap_assimilationPDU_1_0.getData();
372372

373-
CharSequence channelUri = asapManagementPDU.getChannelUri();
374-
CharSequence format = asapManagementPDU.getFormat();
375-
CharSequence owner = asapManagementPDU.getOwner();
376-
List<CharSequence> recipients = asapManagementPDU.getRecipients();
373+
ASAPManagementCreateASAPStorageMessage asapManagementCreateASAPStorageMessage =
374+
ASAPManagementMessage.parseASAPManagementMessage(owner, message);
375+
376+
CharSequence channelUri = asapManagementCreateASAPStorageMessage.getChannelUri();
377+
CharSequence format = asapManagementCreateASAPStorageMessage.getAppName();
378+
List<CharSequence> recipients = asapManagementCreateASAPStorageMessage.getRecipients();
377379

378380
b = new StringBuilder();
379381
b.append(this.getLogStart());

src/net/sharksystem/asap/protocol/ASAPManagementCreateASAPStoragePDU.java renamed to src/net/sharksystem/asap/protocol/ASAPManagementCreateASAPStorageMessage.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.util.List;
44

5-
public interface ASAPManagementCreateASAPStoragePDU {
5+
public interface ASAPManagementCreateASAPStorageMessage {
66
/**
77
* @return list of recipients of this storage/channel
88
*/
@@ -19,9 +19,5 @@ public interface ASAPManagementCreateASAPStoragePDU {
1919
*/
2020
CharSequence getOwner();
2121

22-
/**
23-
*
24-
* @return format/app in which this storage/channel is hosted
25-
*/
26-
CharSequence getFormat();
22+
CharSequence getAppName();
2723
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package net.sharksystem.asap.protocol;
2+
3+
import net.sharksystem.asap.ASAPException;
4+
5+
import java.io.*;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class ASAPManagementMessage {
10+
public static byte[] getCreateClosedASAPChannelMessage(
11+
CharSequence owner, CharSequence appName, CharSequence channelUri,
12+
List<CharSequence> recipients) throws ASAPException, IOException {
13+
14+
if(recipients == null || recipients.size() < 1) {
15+
throw new ASAPException("recipients in storage/channelUri must not be null or empty: ");
16+
}
17+
18+
// we have to put format and recipients into an assimilate message.
19+
20+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
21+
DataOutputStream dos = new DataOutputStream(baos);
22+
23+
// put appName uri second
24+
dos.writeUTF(appName.toString());
25+
26+
// put channelUri uri second
27+
dos.writeUTF(channelUri.toString());
28+
29+
// put recipients
30+
for(CharSequence recipient : recipients) {
31+
dos.writeUTF(recipient.toString());
32+
}
33+
34+
return baos.toByteArray();
35+
}
36+
37+
public static ASAPManagementCreateASAPStorageMessage parseASAPManagementMessage(CharSequence owner, byte[] message)
38+
throws IOException {
39+
40+
return new CreateASAPStorageMessage(owner, message);
41+
}
42+
43+
private static class CreateASAPStorageMessage implements ASAPManagementCreateASAPStorageMessage {
44+
private final List<CharSequence> recipients;
45+
private final CharSequence channelUri;
46+
private final CharSequence appName;
47+
private final CharSequence owner;
48+
49+
CreateASAPStorageMessage(CharSequence owner, byte[] message) throws IOException {
50+
this.owner = owner;
51+
52+
// convert to string
53+
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(message));
54+
this.appName = dis.readUTF();
55+
this.channelUri = dis.readUTF();
56+
57+
// there must be at least one recipient
58+
this.recipients = new ArrayList<>();
59+
this.recipients.add(dis.readUTF());
60+
61+
// and maybe some more
62+
try {
63+
for(;;) {
64+
this.recipients.add(dis.readUTF());
65+
}
66+
}
67+
catch(Throwable t) {
68+
// reach end - ok
69+
}
70+
}
71+
72+
@Override
73+
public List<CharSequence> getRecipients() {
74+
return this.recipients;
75+
}
76+
77+
@Override
78+
public CharSequence getChannelUri() {
79+
return this.channelUri;
80+
}
81+
82+
@Override
83+
public CharSequence getOwner() {
84+
return this.owner;
85+
}
86+
87+
@Override
88+
public CharSequence getAppName() {
89+
return this.appName;
90+
}
91+
}
92+
}

src/net/sharksystem/asap/protocol/ASAPManagementProtocolPDU_Impl.java

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

0 commit comments

Comments
 (0)