|
17 | 17 | * @see ASAPStorage |
18 | 18 | * @author thsc |
19 | 19 | */ |
20 | | -public abstract class ASAPEngine implements ASAPStorage, ASAPProtocolEngine, ASAPManagementStorage { |
| 20 | +public abstract class ASAPEngine extends ASAPStorageImpl implements ASAPStorage, ASAPProtocolEngine, ASAPManagementStorage { |
21 | 21 | public static final String ANONYMOUS_OWNER = "anon"; |
22 | 22 | static String DEFAULT_OWNER = ANONYMOUS_OWNER; |
23 | 23 | static int DEFAULT_INIT_ERA = 0; |
@@ -71,6 +71,63 @@ public void detachASAPMessageAddListener() { |
71 | 71 | this.asapOnlineMessageSender = null; |
72 | 72 | } |
73 | 73 |
|
| 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 | + |
74 | 131 | ////////////////////////////////////////////////////////////////////// |
75 | 132 | // Writer // |
76 | 133 | ////////////////////////////////////////////////////////////////////// |
@@ -288,38 +345,7 @@ private String getLogStart() { |
288 | 345 |
|
289 | 346 | return b.toString(); |
290 | 347 | } |
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); |
301 | 348 |
|
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 | | -*/ |
323 | 349 | public void handleASAPOffer(ASAP_OfferPDU_1_0 asapOffer, ASAP_1_0 protocol, OutputStream os) |
324 | 350 | throws ASAPException, IOException { |
325 | 351 |
|
@@ -487,18 +513,6 @@ public void handleASAPInterest(ASAP_Interest_PDU_1_0 asapInterest, ASAP_1_0 prot |
487 | 513 | System.out.println(b.toString()); |
488 | 514 | //>>>>>>>>>>>>>>>>>>>debug |
489 | 515 |
|
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 | | - |
502 | 516 | // era we are about to transmit |
503 | 517 | int workingEra = this.getEraStartSync(peer); |
504 | 518 | System.out.println(this.getLogStart() + "last_seen: " + workingEra + " | era: " + this.era); |
@@ -564,7 +578,7 @@ private boolean isSendReceivedChunks() { |
564 | 578 | return this.sendReceivedChunks; |
565 | 579 | } |
566 | 580 |
|
567 | | - public void setSendReceivedChunks(boolean on) throws IOException { |
| 581 | + public void setBehaviourSendReceivedChunks(boolean on) throws IOException { |
568 | 582 | this.sendReceivedChunks = on; |
569 | 583 | this.saveStatus(); |
570 | 584 | } |
@@ -689,7 +703,7 @@ private boolean isDropDeliveredChunks() { |
689 | 703 | return this.dropDeliveredChunks; |
690 | 704 | } |
691 | 705 |
|
692 | | - public void setDropDeliveredChunks(boolean drop) throws IOException { |
| 706 | + public void setBehaviourDropDeliveredChunks(boolean drop) throws IOException { |
693 | 707 | this.dropDeliveredChunks = drop; |
694 | 708 | this.saveStatus(); |
695 | 709 | } |
@@ -727,101 +741,12 @@ public int getEra() { |
727 | 741 | return this.era; |
728 | 742 | } |
729 | 743 |
|
| 744 | + @Override |
730 | 745 | public CharSequence getFormat() { |
731 | 746 | return this.format; |
732 | 747 | } |
733 | 748 |
|
734 | 749 |
|
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 | | - |
825 | 750 | //////////////////////////////////////////////////////////////////////////////////////////////////////////// |
826 | 751 | // Online management // |
827 | 752 | //////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
0 commit comments