Skip to content

Commit 6d916c3

Browse files
committed
start working on an asap management protocol
1 parent f823a0c commit 6d916c3

File tree

8 files changed

+238
-21
lines changed

8 files changed

+238
-21
lines changed

src/net/sharksystem/asap/ASAPEngine.java

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

33
import net.sharksystem.asap.protocol.*;
4+
import net.sharksystem.asap.protocol.ASAPManagementProtocolEngine;
45
import net.sharksystem.asap.util.Log;
56

67
import java.io.IOException;
@@ -236,18 +237,40 @@ public void handleConnection(InputStream is, OutputStream os,
236237

237238
public void handleASAPOffer(ASAP_OfferPDU_1_0 asapOffer, ASAP_1_0 protocol, OutputStream os)
238239
throws ASAPException, IOException {
239-
//<<<<<<<<<<<<<<<<<<debug
240-
StringBuilder b = new StringBuilder();
241-
b.append(this.getLogStart());
242-
b.append("ASAP Offer is not handled in this implementation ");
243-
System.out.println(b.toString());
244-
//>>>>>>>>>>>>>>>>>>>debug
240+
241+
if(this.isASAPManagementMessage(asapOffer)) {
242+
//<<<<<<<<<<<<<<<<<<debug
243+
StringBuilder b = new StringBuilder();
244+
b.append(this.getLogStart());
245+
b.append("got asap management assimilate - not handled in this implementation.");
246+
System.out.println(b.toString());
247+
//>>>>>>>>>>>>>>>>>>>debug
248+
return;
249+
} else {
250+
//<<<<<<<<<<<<<<<<<<debug
251+
StringBuilder b = new StringBuilder();
252+
b.append(this.getLogStart());
253+
b.append("ASAP Offer are not processed in this implementation");
254+
System.out.println(b.toString());
255+
//>>>>>>>>>>>>>>>>>>>debug
256+
return;
257+
}
245258
}
246259

247260
public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimiliationPDU, ASAP_1_0 protocol,
248261
InputStream is, OutputStream os, ASAPChunkReceivedListener listener)
249262
throws ASAPException, IOException {
250263

264+
if(this.isASAPManagementMessage(asapAssimiliationPDU)) {
265+
//<<<<<<<<<<<<<<<<<<debug
266+
StringBuilder b = new StringBuilder();
267+
b.append(this.getLogStart());
268+
b.append("got asap management assimilate - it's the wrong place for that - multiengine should be called");
269+
System.out.println(b.toString());
270+
//>>>>>>>>>>>>>>>>>>>debug
271+
throw new ASAPException("got asap management assimilate - it's the wrong place for that - multiengine should be called");
272+
}
273+
251274
String sender = asapAssimiliationPDU.getPeer();
252275
int eraSender = asapAssimiliationPDU.getEra();
253276

@@ -385,9 +408,23 @@ public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimiliationPDU,
385408
}
386409
}
387410

411+
private boolean isASAPManagementMessage(ASAP_PDU_1_0 asapPDU) {
412+
return asapPDU.getFormat().equalsIgnoreCase(ASAP_1_0.ASAP_MANAGEMENT_FORMAT);
413+
}
414+
388415
public void handleASAPInterest(ASAP_Interest_PDU_1_0 asapInterest, ASAP_1_0 protocol, OutputStream os)
389416
throws ASAPException, IOException {
390417

418+
if(this.isASAPManagementMessage(asapInterest)) {
419+
//<<<<<<<<<<<<<<<<<<debug
420+
StringBuilder b = new StringBuilder();
421+
b.append(this.getLogStart());
422+
b.append("got asap management interest - not handled in this implementation.");
423+
System.out.println(b.toString());
424+
//>>>>>>>>>>>>>>>>>>>debug
425+
return;
426+
}
427+
391428
// get remote peer
392429
String peer = asapInterest.getPeer();
393430

src/net/sharksystem/asap/MultiASAPEngineFS_Impl.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import java.io.OutputStream;
1010
import java.util.*;
1111

12-
public class MultiASAPEngineFS_Impl implements MultiASAPEngineFS, ASAPConnectionListener, ThreadFinishedListener {
12+
public class MultiASAPEngineFS_Impl implements
13+
MultiASAPEngineFS, ASAPManagementProtocolEngine, ASAPConnectionListener, ThreadFinishedListener {
1314
private final CharSequence rootFolderName;
1415
private final ASAPChunkReceivedListener listener;
1516
private CharSequence owner;
@@ -346,18 +347,23 @@ public void pushInterests(OutputStream os) throws IOException, ASAPException {
346347
protocol.interest(this.owner, null, format,null, -1, -1, os, false);
347348
}
348349
}
349-
/*
350-
public Thread getExecutorThread(ASAP_PDU_1_0 asappdu, InputStream is, OutputStream os,
351-
ThreadFinishedListener threadFinishedListener) throws ASAPException {
352-
// process pdu
353-
return new ASAPPersistentConnection.ASAPPDUExecutor(asappdu, is, os,
354-
this.getEngineSettings(asappdu.getFormat()),
355-
new ASAP_Modem_Impl(), threadFinishedListener);
350+
351+
public void handleASAPManagementPDU(ASAP_AssimilationPDU_1_0 asapPDU, ASAP_1_0 protocol,
352+
InputStream is, OutputStream os) throws ASAPException, IOException {
353+
354+
StringBuilder b = new StringBuilder();
355+
b.append(this.getLogStart());
356+
b.append("start processing asap management pdu");
357+
System.out.println(b.toString());
358+
//>>>>>>>>>>>>>>>>>>>debug
359+
360+
ASAPManagementCreateASAPStoragePDU asapManagementPDU =
361+
ASAPManagementProtocolPDU_Impl.parseASAPPDU(asapPDU);
362+
363+
356364
}
357-
*/
358365

359-
private String getLogStart() {
366+
private String getLogStart() {
360367
return this.getClass().getSimpleName() + ": ";
361368
}
362-
363369
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package net.sharksystem.asap.protocol;
2+
3+
import java.util.List;
4+
5+
public interface ASAPManagementCreateASAPStoragePDU {
6+
/**
7+
* @return list of recipients of this storage/channel
8+
*/
9+
List<CharSequence> getRecipients();
10+
11+
/**
12+
* @return channel uri
13+
*/
14+
CharSequence getChannelUri();
15+
16+
/**
17+
*
18+
* @return storage/channel owner
19+
*/
20+
CharSequence getOwner();
21+
22+
/**
23+
*
24+
* @return format/app in which this storage/channel is hosted
25+
*/
26+
CharSequence getFormat();
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package net.sharksystem.asap.protocol;
2+
3+
import net.sharksystem.asap.ASAPChunkReceivedListener;
4+
import net.sharksystem.asap.ASAPException;
5+
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.OutputStream;
9+
10+
public interface ASAPManagementProtocolEngine {
11+
/**
12+
* handle asap management pdu
13+
* @param asapPDU received pdu
14+
* @param protocol protocol engine
15+
* @param is inputstream
16+
* @param os outputstream - to send data back
17+
* @throws ASAPException
18+
* @throws IOException
19+
*/
20+
void handleASAPManagementPDU(ASAP_AssimilationPDU_1_0 asapPDU, ASAP_1_0 protocol,
21+
InputStream is, OutputStream os) throws ASAPException, IOException;
22+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
import static net.sharksystem.asap.protocol.ASAP_1_0.ERA_NOT_DEFINED;
10+
11+
public class ASAPManagementProtocolPDU_Impl {
12+
public static void sendCreateASAPStoragePDU(
13+
CharSequence owner, List<CharSequence> storageRecipients,
14+
CharSequence format, CharSequence channel,
15+
OutputStream os, boolean signed) throws IOException, ASAPException {
16+
17+
if(format == null || format.length() < 1) {
18+
throw new ASAPException("format must not be null or empty");
19+
}
20+
21+
if(storageRecipients == null || storageRecipients.size() < 1) {
22+
throw new ASAPException("recipients in storage/channel must not be null or empty: " + format);
23+
}
24+
25+
// we have to put format and recipients into an assimilate message.
26+
27+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
28+
DataOutputStream dos = new DataOutputStream(baos);
29+
30+
// put format first
31+
dos.writeUTF(format.toString());
32+
33+
// remember following offset
34+
List<Long> offsets = new ArrayList<>();
35+
36+
for(CharSequence recipient : storageRecipients) {
37+
// remember offset
38+
offsets.add((long) baos.size());
39+
// write recipient
40+
dos.writeUTF(recipient.toString());
41+
}
42+
43+
// we have compiled the messageBytes
44+
byte[] messageBytes = baos.toByteArray();
45+
ByteArrayInputStream bais = new ByteArrayInputStream(messageBytes);
46+
47+
AssimilationPDU_Impl.sendPDU(
48+
owner, // storage owner is also sender
49+
null,
50+
ASAP_1_0.ASAP_MANAGEMENT_FORMAT, //
51+
channel, // channel that shall be created
52+
ERA_NOT_DEFINED,
53+
messageBytes.length,
54+
offsets,
55+
bais,
56+
os,
57+
signed);
58+
}
59+
60+
public static ASAPManagementCreateASAPStoragePDU parseASAPPDU(ASAP_AssimilationPDU_1_0 pdu)
61+
throws IOException {
62+
63+
return new CreateASAPStoragePDU(pdu);
64+
}
65+
66+
private static class CreateASAPStoragePDU implements ASAPManagementCreateASAPStoragePDU {
67+
private final List<CharSequence> recipients;
68+
private final CharSequence channelUri;
69+
private final CharSequence owner;
70+
private final CharSequence format;
71+
72+
CreateASAPStoragePDU(ASAP_AssimilationPDU_1_0 pdu) throws IOException {
73+
this.owner = pdu.getPeer(); // sender is owner
74+
75+
// channel in pdu
76+
if(pdu.channelSet()) {
77+
this.channelUri = pdu.getChannel();
78+
} else {
79+
this.channelUri = null;
80+
}
81+
82+
// format and recipients are stored in content
83+
List<Integer> offsets = pdu.getMessageOffsets();
84+
InputStream is = pdu.getInputStream();
85+
86+
// read format
87+
byte[] stringAsBytes = new byte[offsets.remove(0)];
88+
is.read(stringAsBytes);
89+
90+
// convert to string
91+
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(stringAsBytes));
92+
this.format = dis.readUTF();
93+
94+
// read recipients
95+
this.recipients = new ArrayList<>();
96+
for(int offset : offsets) {
97+
dis = new DataInputStream(new ByteArrayInputStream(new byte[offset]));
98+
this.recipients.add(dis.readUTF());
99+
}
100+
}
101+
102+
@Override
103+
public List<CharSequence> getRecipients() {
104+
return this.recipients;
105+
}
106+
107+
@Override
108+
public CharSequence getChannelUri() {
109+
return this.channelUri;
110+
}
111+
112+
@Override
113+
public CharSequence getOwner() {
114+
return this.owner;
115+
}
116+
117+
@Override
118+
public CharSequence getFormat() {
119+
return this.format;
120+
}
121+
}
122+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public void run() {
210210
}
211211

212212
System.out.println(this.getLogStart() + "back from reading");
213-
if(terminated) break; // could be killed in the meantime
213+
if(terminated) break; // thread could be killed in the meantime
214214

215215
if (pduReader.getIoException() != null || pduReader.getAsapException() != null) {
216216
Exception e = pduReader.getIoException() != null ?

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ public interface ASAP_1_0 {
1414
byte OFFER_CMD = 'O';
1515
byte INTEREST_CMD = 'I';
1616
byte ASSIMILATE_CMD = 'A';
17-
CharSequence ANY_FORMAT = "any_asap";
18-
CharSequence ASAP_MANAGEMENT_FORMAT = "asap/control";
17+
String ANY_FORMAT = "any_asap";
18+
String ASAP_MANAGEMENT_FORMAT = "asap/control";
19+
int ERA_NOT_DEFINED = -1;
1920

2021
/*
2122
OFFER: An peer (optional) in an range of era (optional) offers data for

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.io.OutputStream;
88
import java.util.List;
99

10+
import static net.sharksystem.asap.protocol.ASAP_1_0.ERA_NOT_DEFINED;
11+
1012
abstract class PDU_Impl implements ASAP_PDU_1_0{
1113
public static final int PEER_BIT_POSITION = 0;
1214
public static final int CHANNEL_BIT_POSITION = 1;
@@ -256,7 +258,7 @@ static int setFlag(List<Long> parameter, int flags, int bit_position) {
256258
}
257259

258260
static int setFlag(int parameter, int flags, int bit_position) {
259-
if(parameter > -1) {
261+
if(parameter != ERA_NOT_DEFINED) {
260262
int newFlag = 1;
261263
newFlag = newFlag << bit_position;
262264

0 commit comments

Comments
 (0)