|
2 | 2 |
|
3 | 3 | import net.sharksystem.asap.EncounterConnectionType; |
4 | 4 | import net.sharksystem.asap.crypto.*; |
| 5 | +import net.sharksystem.asap.utils.ASAPSerialization; |
5 | 6 | import net.sharksystem.utils.Utils; |
6 | 7 | import net.sharksystem.asap.ASAP; |
7 | 8 | import net.sharksystem.asap.ASAPException; |
@@ -91,7 +92,7 @@ private ASAPInternalPeerFS(CharSequence owner, CharSequence rootFolderName, long |
91 | 92 | // owner id must not be a numerical value only - it would interfere with our era numbers |
92 | 93 | try { |
93 | 94 | Integer.parseInt(owner.toString()); |
94 | | - throw new ASAPException("peer id must not only a numeric number, like 42. " + |
| 95 | + throw new ASAPException("peer id must not only be a numeric number like 42. " + |
95 | 96 | "It can be ArthurDent_42, though: " + owner); |
96 | 97 | } |
97 | 98 | catch(NumberFormatException e) { |
@@ -155,6 +156,8 @@ private ASAPInternalPeerFS(CharSequence owner, CharSequence rootFolderName, long |
155 | 156 | } |
156 | 157 | } |
157 | 158 |
|
| 159 | + this.restoreExtraData(); |
| 160 | + |
158 | 161 | // System.out.println(this.getLogStart() + "SHOULD also set up engine " + FORMAT_UNDECRYPTABLE_MESSAGES); |
159 | 162 | } |
160 | 163 |
|
@@ -678,4 +681,127 @@ public ASAPKeyStore getASAPKeyStore() throws ASAPSecurityException { |
678 | 681 | } |
679 | 682 | return this.inMemoASAPKeyStore; |
680 | 683 | } |
| 684 | + |
| 685 | + /////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 686 | + // make extra data persist // |
| 687 | + /////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 688 | + |
| 689 | + public static final String EXTRA_FILE_NAME = ".extraData"; |
| 690 | + private Map<CharSequence, byte[]> extraData = new HashMap<>(); |
| 691 | + |
| 692 | + private File getExtraFile() { |
| 693 | + String extraFileName = this.rootFolderName + "/" + EXTRA_FILE_NAME; |
| 694 | + return new File(extraFileName); |
| 695 | + } |
| 696 | + |
| 697 | + /* |
| 698 | + Here is the catch... There can be - and in Android will - two instances share data over file system. The use |
| 699 | + same clock but run in different threads, most likely different processes. We have to synchronize both sides. |
| 700 | +
|
| 701 | + There will be two processes A and B. Let's assume we are in process A. |
| 702 | + a) We never written something, |
| 703 | +
|
| 704 | + */ |
| 705 | + |
| 706 | + private long timeStampSyncExtraData = -1; // never saved anything |
| 707 | + |
| 708 | + private void extraDataSync() throws IOException, ASAPException { |
| 709 | + InputStream is = null; |
| 710 | + try { |
| 711 | + is = new FileInputStream(this.getExtraFile()); |
| 712 | + } |
| 713 | + catch(IOException ioEx) { |
| 714 | + // no such file - save it instead - there is no conflict |
| 715 | + this.saveExtraData(); |
| 716 | + return; |
| 717 | + } |
| 718 | + |
| 719 | + // read time stamp |
| 720 | + long timeStampSaved = ASAPSerialization.readLongParameter(is); |
| 721 | + |
| 722 | + if(this.timeStampSyncExtraData < timeStampSaved) { |
| 723 | + // there is an external copy that was created after our last sync |
| 724 | + } |
| 725 | + |
| 726 | + // discard local changes and read from file |
| 727 | + this.restoreExtraData(); |
| 728 | + } |
| 729 | + |
| 730 | + public void saveExtraData() throws IOException { |
| 731 | + OutputStream os = new FileOutputStream(this.getExtraFile()); |
| 732 | + |
| 733 | + // write time stamp |
| 734 | + this.timeStampSyncExtraData = System.currentTimeMillis(); |
| 735 | + ASAPSerialization.writeLongParameter(this.timeStampSyncExtraData, os); |
| 736 | + ASAPSerialization.writeNonNegativeIntegerParameter(this.extraData.size(), os); |
| 737 | + |
| 738 | + for(CharSequence key : this.extraData.keySet()) { |
| 739 | + // write key |
| 740 | + ASAPSerialization.writeCharSequenceParameter(key, os); |
| 741 | + // value |
| 742 | + ASAPSerialization.writeByteArray(this.extraData.get(key), os); |
| 743 | + } |
| 744 | + |
| 745 | + os.close(); |
| 746 | + } |
| 747 | + |
| 748 | + /** |
| 749 | + * Always restore. If there is no such file - write it. |
| 750 | + * @throws IOException |
| 751 | + * @throws ASAPException |
| 752 | + */ |
| 753 | + public void restoreExtraData() throws IOException, ASAPException { |
| 754 | + InputStream is = null; |
| 755 | + try { |
| 756 | + is = new FileInputStream(this.getExtraFile()); |
| 757 | + } |
| 758 | + catch(IOException ioEx) { |
| 759 | + // no such file - nothing to do here |
| 760 | + return; |
| 761 | + } |
| 762 | + |
| 763 | + long timeStampSaved = ASAPSerialization.readLongParameter(is); |
| 764 | + if(timeStampSaved == this.timeStampSyncExtraData) { |
| 765 | + is.close(); |
| 766 | + return; |
| 767 | + } |
| 768 | + |
| 769 | + // something changed - get a fresh copy |
| 770 | + this.timeStampSyncExtraData = timeStampSaved; |
| 771 | + |
| 772 | + this.extraData = new HashMap<>(); |
| 773 | + int counter = ASAPSerialization.readIntegerParameter(is); |
| 774 | + |
| 775 | + while(counter-- > 0) { |
| 776 | + // read key |
| 777 | + CharSequence key = ASAPSerialization.readCharSequenceParameter(is); |
| 778 | + // value |
| 779 | + byte[] value = ASAPSerialization.readByteArray(is); |
| 780 | + |
| 781 | + // save in memory |
| 782 | + this.extraData.put(key, value); |
| 783 | + } |
| 784 | + is.close(); |
| 785 | + } |
| 786 | + |
| 787 | + /** |
| 788 | + * Make a value persistent with key |
| 789 | + * @param key |
| 790 | + * @param value |
| 791 | + */ |
| 792 | + public void putExtra(CharSequence key, byte[] value) throws IOException, ASAPException { |
| 793 | + this.extraDataSync(); |
| 794 | + this.extraData.put(key, value); |
| 795 | + this.saveExtraData(); |
| 796 | + } |
| 797 | + |
| 798 | + /** |
| 799 | + * Return a value - can be null if null was set as value. |
| 800 | + * @param key |
| 801 | + * @throws ASAPException key never used in putExtra |
| 802 | + */ |
| 803 | + public byte[] getExtra(CharSequence key) throws IOException, ASAPException { |
| 804 | + this.restoreExtraData(); |
| 805 | + return this.extraData.get(key); |
| 806 | + } |
681 | 807 | } |
0 commit comments