Skip to content

Commit bbbb1ca

Browse files
committed
start refactoring - separate protocol engine from storage
1 parent 6020e9a commit bbbb1ca

11 files changed

+228
-159
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package net.sharksystem.asap;
2+
3+
import net.sharksystem.asap.protocol.ASAP_1_0;
4+
import net.sharksystem.asap.protocol.ASAP_AssimilationPDU_1_0;
5+
import net.sharksystem.asap.protocol.ASAP_Interest_PDU_1_0;
6+
import net.sharksystem.asap.protocol.ASAP_OfferPDU_1_0;
7+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.OutputStream;
11+
12+
public class ASAPDefaultProtocolEngine implements ASAPProtocolEngine {
13+
@Override
14+
public void handleASAPInterest(ASAP_Interest_PDU_1_0 asapInterest, ASAP_1_0 protocol, OutputStream os) throws ASAPException, IOException {
15+
16+
}
17+
18+
@Override
19+
public void handleASAPOffer(ASAP_OfferPDU_1_0 asapOffer, ASAP_1_0 protocol, OutputStream os) throws ASAPException, IOException {
20+
21+
}
22+
23+
@Override
24+
public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asap_assimilation, ASAP_1_0 protocol, InputStream is, OutputStream os, ASAPChunkReceivedListener listener) throws ASAPException, IOException {
25+
26+
}
27+
28+
@Override
29+
public void setBehaviourDropDeliveredChunks(boolean drop) throws IOException {
30+
31+
}
32+
33+
@Override
34+
public void setBehaviourSendReceivedChunks(boolean on) throws IOException {
35+
36+
}
37+
}

src/net/sharksystem/asap/ASAPEngine.java

Lines changed: 61 additions & 136 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, ASAPManagementStorage {
20+
public abstract class ASAPEngine extends ASAPStorageImpl 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;
@@ -71,6 +71,63 @@ public void detachASAPMessageAddListener() {
7171
this.asapOnlineMessageSender = null;
7272
}
7373

74+
/**
75+
* We interpret an existing chunk with *no* recipients as
76+
* public chunk
77+
* @param chunk
78+
* @return
79+
*/
80+
private boolean isPublic(ASAPChunk chunk) {
81+
return chunk.getRecipients().isEmpty();
82+
}
83+
84+
@Override
85+
public void newEra() {
86+
StringBuilder sb = new StringBuilder();
87+
sb.append(this.getLogStart());
88+
sb.append("newEra() | owner: ");
89+
sb.append(this.owner);
90+
sb.append(" | format: ");
91+
sb.append(this.format);
92+
sb.append(" | ");
93+
94+
if(this.contentChanged) {
95+
sb.append("content changed - increment era...");
96+
System.out.println(sb.toString());
97+
try {
98+
int oldEra = this.era;
99+
int nextEra = this.getNextEra(this.era);
100+
101+
// set as fast as possible to make race conditions less likely
102+
this.contentChanged = false;
103+
104+
// we are done here - we are in a new era.
105+
this.era = nextEra;
106+
107+
// persistent values
108+
if(this.memento != null) this.memento.save(this);
109+
110+
// drop very very old chunks - if available
111+
this.chunkStorage.dropChunks(nextEra);
112+
113+
// setup new era - copy all chunks
114+
for(ASAPChunk chunk : this.chunkStorage.getChunks(oldEra)) {
115+
ASAPChunk copyChunk = this.chunkStorage.getChunk(chunk.getUri(), nextEra);
116+
copyChunk.clone(chunk);
117+
}
118+
119+
System.out.println(this.getLogStart() + "era incremented");
120+
} catch (IOException ex) {
121+
sb.append("IOException while incrementing era: ");
122+
sb.append(ex.getLocalizedMessage());
123+
System.err.println(sb.toString());
124+
}
125+
} else {
126+
sb.append("content not changed - era not changed");
127+
System.out.println(sb.toString());
128+
}
129+
}
130+
74131
//////////////////////////////////////////////////////////////////////
75132
// Writer //
76133
//////////////////////////////////////////////////////////////////////
@@ -288,38 +345,7 @@ private String getLogStart() {
288345

289346
return b.toString();
290347
}
291-
/*
292-
public void handleConnection(InputStream is, OutputStream os,
293-
ASAPChunkReceivedListener listener) {
294-
295-
try {
296-
ASAP_1_0 protocol = new ASAP_Modem_Impl();
297-
298-
// tell who I am - send a interest message
299-
protocol.interest(this.owner, null, null,
300-
null, -1, -1, os, false);
301348

302-
// read interest from the other side
303-
ASAP_PDU_1_0 asapPDU = protocol.readPDU(is);
304-
305-
// must be an interest
306-
if(asapPDU.getCommand() != ASAP_1_0.INTEREST_CMD) {
307-
throw new ASAPException("protocol error: expected asap interest - got something else");
308-
}
309-
310-
this.handleASAPInterest((ASAP_Interest_PDU_1_0) asapPDU, protocol, os);
311-
}
312-
catch(Exception e) {
313-
//<<<<<<<<<<<<<<<<<<debug
314-
StringBuilder b = new StringBuilder();
315-
b.append(this.getLogStart());
316-
b.append("Exception: ");
317-
b.append(e.getLocalizedMessage());
318-
System.out.println(b.toString());
319-
//>>>>>>>>>>>>>>>>>>>debug
320-
}
321-
}
322-
*/
323349
public void handleASAPOffer(ASAP_OfferPDU_1_0 asapOffer, ASAP_1_0 protocol, OutputStream os)
324350
throws ASAPException, IOException {
325351

@@ -487,18 +513,6 @@ public void handleASAPInterest(ASAP_Interest_PDU_1_0 asapInterest, ASAP_1_0 prot
487513
System.out.println(b.toString());
488514
//>>>>>>>>>>>>>>>>>>>debug
489515

490-
// check conflict
491-
if(!this.permission2ProceedConversation(peer)) {
492-
b = new StringBuilder();
493-
b.append(this.getLogStart());
494-
b.append("no permission to communicate with remote peer: ");
495-
b.append(peer);
496-
System.err.println(b.toString());
497-
throw new ASAPException("no permission to communicate with remote peer: " + peer);
498-
} else {
499-
System.out.println(this.getLogStart() + "permission ok, process interest");
500-
}
501-
502516
// era we are about to transmit
503517
int workingEra = this.getEraStartSync(peer);
504518
System.out.println(this.getLogStart() + "last_seen: " + workingEra + " | era: " + this.era);
@@ -564,7 +578,7 @@ private boolean isSendReceivedChunks() {
564578
return this.sendReceivedChunks;
565579
}
566580

567-
public void setSendReceivedChunks(boolean on) throws IOException {
581+
public void setBehaviourSendReceivedChunks(boolean on) throws IOException {
568582
this.sendReceivedChunks = on;
569583
this.saveStatus();
570584
}
@@ -689,7 +703,7 @@ private boolean isDropDeliveredChunks() {
689703
return this.dropDeliveredChunks;
690704
}
691705

692-
public void setDropDeliveredChunks(boolean drop) throws IOException {
706+
public void setBehaviourDropDeliveredChunks(boolean drop) throws IOException {
693707
this.dropDeliveredChunks = drop;
694708
this.saveStatus();
695709
}
@@ -727,101 +741,12 @@ public int getEra() {
727741
return this.era;
728742
}
729743

744+
@Override
730745
public CharSequence getFormat() {
731746
return this.format;
732747
}
733748

734749

735-
// public int getNextEra(int era) {
736-
// return this.
737-
// }
738-
739-
/* old
740-
* Peers in ad-hoc networks have the tendency to establish two channels
741-
* simultaneously. It's due to the lack of a controlling server instance
742-
* and the - wanted - equality of each peers. We should suppress those
743-
* useless double conversations.
744-
*
745-
* @param peer
746-
* @return
747-
*/
748-
749-
/**
750-
* Security / Permission module is due to undergo a deep review - returns always true until than.
751-
* @param peer
752-
* @return
753-
*/
754-
private synchronized boolean permission2ProceedConversation(String peer) {
755-
return true;
756-
// if that peer is not in the list - go ahead
757-
/*
758-
boolean goAhead = !this.activePeers.contains(peer);
759-
if(goAhead) {
760-
// add that peer - an other call will fail.
761-
this.activePeers.add(peer);
762-
}
763-
764-
return goAhead;
765-
*/
766-
}
767-
768-
/**
769-
* We interpret an existing chunk with *no* recipients as
770-
* public chunk
771-
* @param chunk
772-
* @return
773-
*/
774-
private boolean isPublic(ASAPChunk chunk) {
775-
return chunk.getRecipients().isEmpty();
776-
}
777-
778-
@Override
779-
public void newEra() {
780-
StringBuilder sb = new StringBuilder();
781-
sb.append(this.getLogStart());
782-
sb.append("newEra() | owner: ");
783-
sb.append(this.owner);
784-
sb.append(" | format: ");
785-
sb.append(this.format);
786-
sb.append(" | ");
787-
788-
if(this.contentChanged) {
789-
sb.append("content changed - increment era...");
790-
System.out.println(sb.toString());
791-
try {
792-
int oldEra = this.era;
793-
int nextEra = this.getNextEra(this.era);
794-
795-
// set as fast as possible to make race conditions less likely
796-
this.contentChanged = false;
797-
798-
// we are done here - we are in a new era.
799-
this.era = nextEra;
800-
801-
// persistent values
802-
if(this.memento != null) this.memento.save(this);
803-
804-
// drop very very old chunks - if available
805-
this.chunkStorage.dropChunks(nextEra);
806-
807-
// setup new era - copy all chunks
808-
for(ASAPChunk chunk : this.chunkStorage.getChunks(oldEra)) {
809-
ASAPChunk copyChunk = this.chunkStorage.getChunk(chunk.getUri(), nextEra);
810-
copyChunk.clone(chunk);
811-
}
812-
813-
System.out.println(this.getLogStart() + "era incremented");
814-
} catch (IOException ex) {
815-
sb.append("IOException while incrementing era: ");
816-
sb.append(ex.getLocalizedMessage());
817-
System.err.println(sb.toString());
818-
}
819-
} else {
820-
sb.append("content not changed - era not changed");
821-
System.out.println(sb.toString());
822-
}
823-
}
824-
825750
////////////////////////////////////////////////////////////////////////////////////////////////////////////
826751
// Online management //
827752
////////////////////////////////////////////////////////////////////////////////////////////////////////////

src/net/sharksystem/asap/ASAPProtocolEngine.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asap_assimilation, ASAP_1_0 p
2626
/**
2727
* Chunks are (tried to be) delivered to their recipients during each encounter
2828
* with another peer. After successful delivery, recipient is withdrawn from recipient
29-
* list to prevent mutliple delivery.
29+
* list to prevent multiple delivery.
3030
*
3131
* If this flag is set, chunk are removed permanently if their are delivered
32-
* to all their recipients. If no set, they remain intact.
32+
* to all their recipients. There are kept in local storage otherwise.
3333
* @param drop
3434
*/
35-
void setDropDeliveredChunks(boolean drop) throws IOException;
35+
void setBehaviourDropDeliveredChunks(boolean drop) throws IOException;
3636

3737
/**
3838
* engine can deliver local message but also received messages - default false - send no received messages
3939
* @param on
4040
*/
41-
void setSendReceivedChunks(boolean on) throws IOException;
41+
void setBehaviourSendReceivedChunks(boolean on) throws IOException;
4242
}
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+
public class ASAPSecurityException extends ASAPException {
4+
public ASAPSecurityException() {
5+
super();
6+
}
7+
public ASAPSecurityException(String message) {
8+
super(message);
9+
}
10+
}

src/net/sharksystem/asap/ASAPStorage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public interface ASAPStorage {
123123
* are to be deleted.
124124
* @param drop
125125
*/
126-
void setDropDeliveredChunks(boolean drop) throws IOException;
126+
// void setDropDeliveredChunks(boolean drop) throws IOException;
127127

128128
/**
129129
* Chunks are delivered when seeing other peers. Default behaviour is to send only message which
@@ -133,7 +133,7 @@ public interface ASAPStorage {
133133
*
134134
* @param drop
135135
*/
136-
void setSendReceivedChunks(boolean drop) throws IOException;
136+
// void setSendReceivedChunks(boolean drop) throws IOException;
137137

138138
/**
139139
/**
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package net.sharksystem.asap;
2+
3+
public abstract class ASAPStorageImpl implements ASAPStorage {
4+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package net.sharksystem.asap;
2+
3+
import java.io.IOException;
4+
5+
public class DefaultPeerSecurityAdministrator implements PeerSecurityAdministrator, PeerSecuritySettings {
6+
@Override
7+
public void setRememberEncounteredPeers(boolean on) throws IOException {
8+
9+
}
10+
11+
@Override
12+
public void setEncryptedMessagesOnly(boolean on) throws IOException {
13+
14+
}
15+
16+
@Override
17+
public void setSignedMessagesOnly(boolean on) throws IOException {
18+
19+
}
20+
21+
@Override
22+
public void setSetAllowedRemotePeers(AllowedRemotePeers safetyLevel) {
23+
24+
}
25+
26+
@Override
27+
public boolean setRevealEngineFormat(String peerName) {
28+
return false;
29+
}
30+
31+
@Override
32+
public boolean setSendOpenMessages(String peerName) {
33+
return false;
34+
}
35+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package net.sharksystem.asap;
2+
3+
interface PeerSecurityAdministrator {
4+
}

0 commit comments

Comments
 (0)