Skip to content

Commit 3f820a3

Browse files
committed
extracted extra data management from internal peer to net.sharksystem.utils.ExtraData
1 parent 8789e41 commit 3f820a3

File tree

9 files changed

+207
-122
lines changed

9 files changed

+207
-122
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package net.sharksystem;
2+
3+
public class SharkException extends Exception {
4+
public SharkException() { super(); }
5+
public SharkException(String message) {
6+
super(message);
7+
}
8+
public SharkException(String message, Throwable cause) {
9+
super(message, cause);
10+
}
11+
public SharkException(Throwable cause) {
12+
super(cause);
13+
}
14+
}

src/net/sharksystem/asap/ASAPException.java

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

3+
import net.sharksystem.SharkException;
4+
35
/**
46
*
57
* @author thsc
68
*/
7-
public class ASAPException extends Exception {
9+
public class ASAPException extends SharkException {
810
public ASAPException() { super(); }
911
public ASAPException(String message) {
1012
super(message);

src/net/sharksystem/asap/ASAPInternalPeerWrapper.java

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

3+
import net.sharksystem.SharkException;
34
import net.sharksystem.asap.engine.ASAPInternalOnlinePeersChangedListener;
45
import net.sharksystem.asap.engine.ASAPInternalPeer;
56
import net.sharksystem.asap.protocol.ASAPConnection;
@@ -105,7 +106,11 @@ public void notifyOnlinePeersChanged(Set<CharSequence> peerList) {
105106
* @param value
106107
*/
107108
public void putExtra(CharSequence key, byte[] value) throws IOException, ASAPException {
108-
this.peer.putExtra(key, value);
109+
try {
110+
this.peer.putExtra(key, value);
111+
} catch (SharkException e) {
112+
throw new ASAPException(e);
113+
}
109114
}
110115

111116
/**
@@ -114,7 +119,10 @@ public void putExtra(CharSequence key, byte[] value) throws IOException, ASAPExc
114119
* @throws ASAPException key never used in putExtra
115120
*/
116121
public byte[] getExtra(CharSequence key) throws ASAPException, IOException {
117-
return this.peer.getExtra(key);
122+
try {
123+
return this.peer.getExtra(key);
124+
} catch (SharkException e) {
125+
throw new ASAPException(e);
126+
}
118127
}
119-
120128
}

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.sharksystem.asap.ASAPSecurityException;
66
import net.sharksystem.asap.protocol.*;
77
import net.sharksystem.asap.crypto.ASAPKeyStore;
8+
import net.sharksystem.utils.ExtraData;
89

910
import java.io.FileNotFoundException;
1011
import java.io.IOException;
@@ -23,7 +24,7 @@
2324
*
2425
* That interface hides those different engines.
2526
*/
26-
public interface ASAPInternalPeer extends ASAPConnectionHandler {
27+
public interface ASAPInternalPeer extends ASAPConnectionHandler, ExtraData {
2728
long DEFAULT_MAX_PROCESSING_TIME = Long.MAX_VALUE;
2829

2930
/**
@@ -114,18 +115,4 @@ void sendOnlineASAPAssimilateMessage(CharSequence format, CharSequence urlTarget
114115
ASAPKeyStore getASAPKeyStore() throws ASAPSecurityException;
115116

116117
void setSecurityAdministrator(DefaultSecurityAdministrator securityAdministrator);
117-
118-
/**
119-
* Make a value persistent with key
120-
* @param key
121-
* @param value
122-
*/
123-
void putExtra(CharSequence key, byte[] value) throws IOException, ASAPException;
124-
125-
/**
126-
* Return a value. Throws an exception if not set
127-
* @param key
128-
* @throws ASAPException key never used in putExtra
129-
*/
130-
byte[] getExtra(CharSequence key) throws ASAPException, IOException;
131118
}

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

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

3+
import net.sharksystem.SharkException;
34
import net.sharksystem.asap.EncounterConnectionType;
45
import net.sharksystem.asap.crypto.*;
56
import net.sharksystem.asap.utils.ASAPSerialization;
7+
import net.sharksystem.utils.ExtraData;
8+
import net.sharksystem.utils.ExtraDataFS;
69
import net.sharksystem.utils.Utils;
710
import net.sharksystem.asap.ASAP;
811
import net.sharksystem.asap.ASAPException;
@@ -146,7 +149,7 @@ private ASAPInternalPeerFS(CharSequence owner, CharSequence rootFolderName, long
146149
}
147150
}
148151

149-
this.restoreExtraData();
152+
//this.restoreExtraData();
150153

151154
// System.out.println(this.getLogStart() + "SHOULD also set up engine " + FORMAT_UNDECRYPTABLE_MESSAGES);
152155
}
@@ -681,14 +684,24 @@ public ASAPKeyStore getASAPKeyStore() throws ASAPSecurityException {
681684
///////////////////////////////////////////////////////////////////////////////////////////////////////////
682685
// make extra data persist //
683686
///////////////////////////////////////////////////////////////////////////////////////////////////////////
687+
private ExtraData extraData = null;
684688

685-
public static final String EXTRA_FILE_NAME = ".extraData";
689+
private ExtraData getExtraData() throws SharkException, IOException {
690+
if(this.extraData == null) {
691+
this.extraData = new ExtraDataFS(this.rootFolderName);
692+
}
693+
694+
return this.extraData;
695+
}
696+
/*
697+
private static final String EXTRA_FILE_NAME = ".extraData";
686698
private Map<CharSequence, byte[]> extraData = new HashMap<>();
687699
688700
private File getExtraFile() {
689701
String extraFileName = this.rootFolderName + "/" + EXTRA_FILE_NAME;
690702
return new File(extraFileName);
691703
}
704+
*/
692705

693706
/*
694707
Here is the catch... There can be - and in Android will - two instances share data over file system. The use
@@ -699,105 +712,11 @@ private File getExtraFile() {
699712
700713
*/
701714

702-
private long timeStampSyncExtraData = -1; // never saved anything
703-
704-
private void extraDataSync() throws IOException, ASAPException {
705-
InputStream is = null;
706-
try {
707-
is = new FileInputStream(this.getExtraFile());
708-
}
709-
catch(IOException ioEx) {
710-
// no such file - save it instead - there is no conflict
711-
this.saveExtraData();
712-
return;
713-
}
714-
715-
// read time stamp
716-
long timeStampSaved = ASAPSerialization.readLongParameter(is);
717-
718-
if(this.timeStampSyncExtraData < timeStampSaved) {
719-
// there is an external copy that was created after our last sync
720-
}
721-
722-
// discard local changes and read from file
723-
this.restoreExtraData();
724-
}
725-
726-
public void saveExtraData() throws IOException {
727-
OutputStream os = new FileOutputStream(this.getExtraFile());
728-
729-
// write time stamp
730-
this.timeStampSyncExtraData = System.currentTimeMillis();
731-
ASAPSerialization.writeLongParameter(this.timeStampSyncExtraData, os);
732-
ASAPSerialization.writeNonNegativeIntegerParameter(this.extraData.size(), os);
733-
734-
for(CharSequence key : this.extraData.keySet()) {
735-
// write key
736-
ASAPSerialization.writeCharSequenceParameter(key, os);
737-
// value
738-
ASAPSerialization.writeByteArray(this.extraData.get(key), os);
739-
}
740-
741-
os.close();
742-
}
743-
744-
/**
745-
* Always restore. If there is no such file - write it.
746-
* @throws IOException
747-
* @throws ASAPException
748-
*/
749-
public void restoreExtraData() throws IOException, ASAPException {
750-
InputStream is = null;
751-
try {
752-
is = new FileInputStream(this.getExtraFile());
753-
}
754-
catch(IOException ioEx) {
755-
// no such file - nothing to do here
756-
return;
757-
}
758-
759-
long timeStampSaved = ASAPSerialization.readLongParameter(is);
760-
if(timeStampSaved == this.timeStampSyncExtraData) {
761-
is.close();
762-
return;
763-
}
764-
765-
// something changed - get a fresh copy
766-
this.timeStampSyncExtraData = timeStampSaved;
767-
768-
this.extraData = new HashMap<>();
769-
int counter = ASAPSerialization.readIntegerParameter(is);
770-
771-
while(counter-- > 0) {
772-
// read key
773-
CharSequence key = ASAPSerialization.readCharSequenceParameter(is);
774-
// value
775-
byte[] value = ASAPSerialization.readByteArray(is);
776-
777-
// save in memory
778-
this.extraData.put(key, value);
779-
}
780-
is.close();
781-
}
782-
783-
/**
784-
* Make a value persistent with key
785-
* @param key
786-
* @param value
787-
*/
788-
public void putExtra(CharSequence key, byte[] value) throws IOException, ASAPException {
789-
this.extraDataSync();
790-
this.extraData.put(key, value);
791-
this.saveExtraData();
715+
public void putExtra(CharSequence key, byte[] value) throws IOException, SharkException {
716+
this.getExtraData().putExtra(key, value);
792717
}
793718

794-
/**
795-
* Return a value - can be null if null was set as value.
796-
* @param key
797-
* @throws ASAPException key never used in putExtra
798-
*/
799-
public byte[] getExtra(CharSequence key) throws IOException, ASAPException {
800-
this.restoreExtraData();
801-
return this.extraData.get(key);
719+
public byte[] getExtra(CharSequence key) throws IOException, SharkException {
720+
return this.getExtraData().getExtra(key);
802721
}
803722
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package net.sharksystem.utils;
2+
3+
import net.sharksystem.SharkException;
4+
5+
import java.io.IOException;
6+
7+
public interface ExtraData {
8+
void putExtra(CharSequence key, byte[] value) throws IOException, SharkException;
9+
byte[] getExtra(CharSequence key) throws IOException, SharkException;
10+
}

0 commit comments

Comments
 (0)