|
| 1 | +package net.sharksystem; |
| 2 | + |
| 3 | +import net.sharksystem.asap.ASAPEncounterManager; |
| 4 | +import net.sharksystem.asap.ASAPEncounterManagerImpl; |
| 5 | +import net.sharksystem.asap.ASAPException; |
| 6 | +import net.sharksystem.asap.ASAPPeer; |
| 7 | +import net.sharksystem.asap.utils.ASAPSerialization; |
| 8 | +import net.sharksystem.hub.peerside.ASAPHubManager; |
| 9 | +import net.sharksystem.hub.peerside.ASAPHubManagerImpl; |
| 10 | +import net.sharksystem.hub.peerside.HubConnectorDescription; |
| 11 | +import net.sharksystem.hub.peerside.HubConnectorFactory; |
| 12 | +import net.sharksystem.utils.Log; |
| 13 | + |
| 14 | +import java.io.ByteArrayInputStream; |
| 15 | +import java.io.ByteArrayOutputStream; |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +public class SharkPeerHubSupportImpl implements SharkPeerHubSupport { |
| 22 | + private ASAPEncounterManager asapEncounterManager; |
| 23 | + private ASAPPeer asapPeer; |
| 24 | + |
| 25 | + public SharkPeerHubSupportImpl() { } |
| 26 | + |
| 27 | + public SharkPeerHubSupportImpl(ASAPEncounterManager asapEncounterManager) { |
| 28 | + this.asapEncounterManager = asapEncounterManager; |
| 29 | + } |
| 30 | + |
| 31 | + private void init() { |
| 32 | + this.restoreHubDescriptions(); |
| 33 | + } |
| 34 | + |
| 35 | + public SharkPeerHubSupportImpl(ASAPPeer asapPeer) { |
| 36 | + this.asapPeer = asapPeer; |
| 37 | + this.init(); |
| 38 | + } |
| 39 | + |
| 40 | + protected void setASAPPeer(ASAPPeer asapPeer) { |
| 41 | + this.asapPeer = asapPeer; |
| 42 | + this.init(); |
| 43 | + } |
| 44 | + |
| 45 | + public ASAPPeer getASAPPeer() throws SharkException { |
| 46 | + if(this.asapPeer == null) throw new SharkException("asap peer not set (yet)"); |
| 47 | + return this.asapPeer; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public CharSequence getPeerID() throws SharkException { |
| 52 | + return this.getASAPPeer().getPeerID(); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + /////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 57 | + // hub description management // |
| 58 | + /////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 59 | + |
| 60 | + private static final CharSequence HUB_DESCRIPTIONS = "hubDescriptions"; |
| 61 | + private List<HubConnectorDescription> hubConnectorDescriptions = new ArrayList<>(); |
| 62 | + |
| 63 | + private void checkHubDescriptionsRestored() { |
| 64 | + if(!this.hubDescriptionsRestored) { |
| 65 | + this.restoreHubDescriptions(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void addHubDescription(HubConnectorDescription hubConnectorDescription) { |
| 71 | + this.checkHubDescriptionsRestored(); |
| 72 | + if(hubConnectorDescription == null) return; |
| 73 | + |
| 74 | + // duplicate suppression |
| 75 | + for(HubConnectorDescription hcd : this.hubConnectorDescriptions) { |
| 76 | + if(hcd.isSame(hubConnectorDescription)) return; |
| 77 | + } |
| 78 | + |
| 79 | + this.hubConnectorDescriptions.add(hubConnectorDescription); |
| 80 | + this.persistHubDescriptions(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void removeHubDescription(HubConnectorDescription hubConnectorDescription) { |
| 85 | + this.checkHubDescriptionsRestored(); |
| 86 | + HubConnectorDescription same = null; |
| 87 | + for(HubConnectorDescription hcd : this.hubConnectorDescriptions) { |
| 88 | + if(hubConnectorDescription.isSame(hcd)) { |
| 89 | + same = hcd; |
| 90 | + break; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if(same != null) this.hubConnectorDescriptions.remove(same); |
| 95 | + this.persistHubDescriptions(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public Collection<HubConnectorDescription> getHubDescriptions() { |
| 100 | + this.checkHubDescriptionsRestored(); |
| 101 | + return this.hubConnectorDescriptions; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public HubConnectorDescription getHubDescription(int index) throws SharkException { |
| 106 | + this.checkHubDescriptionsRestored(); |
| 107 | + if(this.hubConnectorDescriptions.size() <= index) throw new SharkException("index out of range"); |
| 108 | + |
| 109 | + return this.hubConnectorDescriptions.get(index); |
| 110 | + } |
| 111 | + |
| 112 | + private void persistHubDescriptions() { |
| 113 | + // not yet started or nothing to do |
| 114 | + if(this.hubConnectorDescriptions.isEmpty() || this.asapPeer == null) return; |
| 115 | + |
| 116 | + byte[][] serializedDescriptions = new byte[this.hubConnectorDescriptions.size()][]; |
| 117 | + int index = 0; |
| 118 | + try { |
| 119 | + for(HubConnectorDescription hcd : this.hubConnectorDescriptions) { |
| 120 | + serializedDescriptions[index++] = hcd.serialize(); |
| 121 | + } |
| 122 | + |
| 123 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 124 | + ASAPSerialization.writeByteArray(serializedDescriptions, baos); |
| 125 | + byte[] serial = baos.toByteArray(); |
| 126 | + |
| 127 | + this.asapPeer.putExtra(HUB_DESCRIPTIONS, serial); |
| 128 | + } catch (IOException | ASAPException e) { |
| 129 | + Log.writeLogErr(this, "cannot serialized hub description"); |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + private boolean hubDescriptionsRestored = false; |
| 136 | + private void restoreHubDescriptions() { |
| 137 | + if(this.asapPeer == null) return; // not yet started |
| 138 | + if(this.hubDescriptionsRestored) return; // only once |
| 139 | + |
| 140 | + this.hubDescriptionsRestored = true; |
| 141 | + |
| 142 | + byte[] serial = null; |
| 143 | + try { |
| 144 | + serial = this.asapPeer.getExtra(HUB_DESCRIPTIONS); |
| 145 | + if(serial == null) return; // ok - no descriptions stored |
| 146 | + } catch (ASAPException | IOException e) { |
| 147 | + Log.writeLog(this, "cannot read hub description - ok, maybe there are non"); |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + try { |
| 152 | + ByteArrayInputStream bais = new ByteArrayInputStream(serial); |
| 153 | + byte[][] serializedDescriptions = ASAPSerialization.readByte2DimArray(bais); |
| 154 | + |
| 155 | + for(int i = 0; i < serializedDescriptions.length; i++) { |
| 156 | + this.hubConnectorDescriptions.add( |
| 157 | + HubConnectorFactory.createHubConnectorByDescription(serializedDescriptions[i])); |
| 158 | + } |
| 159 | + } catch (IOException | ASAPException e) { |
| 160 | + Log.writeLogErr(this, "cannot deserialize hub description - seems to be a bug"); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 165 | + // extra data // |
| 166 | + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 167 | + @Override |
| 168 | + public void putExtra(CharSequence key, byte[] value) throws IOException, SharkException, ASAPException { |
| 169 | + if(this.asapPeer == null) { |
| 170 | + throw new SharkException("peer is not yet launched - initialize your shark system"); |
| 171 | + } |
| 172 | + this.asapPeer.putExtra(key, value); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public byte[] getExtra(CharSequence key) throws ASAPException, IOException, SharkException { |
| 177 | + if(this.asapPeer == null) { |
| 178 | + throw new SharkException("peer is not yet launched - initialize your shark system"); |
| 179 | + } |
| 180 | + return this.asapPeer.getExtra(key); |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments