Skip to content

Commit f0d18d9

Browse files
committed
hub support integrated
1 parent de63186 commit f0d18d9

7 files changed

Lines changed: 251 additions & 52 deletions

File tree

libs/ASAPHub.jar

7.11 KB
Binary file not shown.

libs/ASAPJava.jar

247 Bytes
Binary file not shown.

src/net/sharksystem/SharkPeer.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package net.sharksystem;
22

3-
import net.sharksystem.asap.ASAPException;
43
import net.sharksystem.asap.ASAPPeer;
54

6-
import java.io.IOException;
7-
import java.util.Collection;
85
import java.util.Set;
96

107
/**
@@ -19,7 +16,7 @@
1916
* @see SharkComponentFactory
2017
* @see ASAPPeer
2118
*/
22-
public interface SharkPeer {
19+
public interface SharkPeer extends SharkPeerHubSupport {
2320
/**
2421
* Add a component to the Shark app
2522
* @param componentFactory
@@ -97,19 +94,4 @@ void removeComponent(Class<? extends SharkComponent> facade)
9794

9895
boolean samePeer(CharSequence otherPeerID) throws SharkException;
9996

100-
CharSequence getPeerID() throws SharkException;
101-
102-
/**
103-
* Make a value persistent with key
104-
* @param key
105-
* @param value
106-
*/
107-
void putExtra(CharSequence key, byte[] value) throws IOException, SharkException, ASAPException;
108-
109-
/**
110-
* Return a value. Throws an exception if not set
111-
* @param key
112-
* @throws ASAPException key never used in putExtra
113-
*/
114-
byte[] getExtra(CharSequence key) throws ASAPException, IOException, SharkException;
11597
}

src/net/sharksystem/SharkPeerFS.java

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@
33
import net.sharksystem.asap.ASAPException;
44
import net.sharksystem.asap.ASAPPeer;
55
import net.sharksystem.asap.ASAPPeerFS;
6-
import net.sharksystem.hub.*;
76
import net.sharksystem.utils.Log;
87

98
import java.io.IOException;
109
import java.util.*;
1110

12-
public class SharkPeerFS implements SharkPeer {
11+
public class SharkPeerFS extends SharkPeerHubSupportImpl implements SharkPeer {
1312
protected final CharSequence owner;
1413
protected final CharSequence rootFolder;
1514
private HashMap<CharSequence, SharkComponentFactory> factories = new HashMap<>();
1615
protected HashMap<CharSequence, SharkComponent> components = new HashMap<>();
1716
private SharkPeerStatus status = SharkPeerStatus.NOT_INITIALIZED;
18-
private ASAPPeer asapPeer;
1917

2018
public SharkPeerFS(CharSequence owner, CharSequence rootFolder) {
2119
this.owner = owner;
@@ -132,11 +130,12 @@ public void start() throws SharkException {
132130

133131
@Override
134132
public void start(ASAPPeer asapPeer) throws SharkException {
135-
this.asapPeer = asapPeer;
133+
this.setASAPPeer(asapPeer);
134+
136135
boolean fullSuccess = true; // optimistic
137136
for(SharkComponent component : this.components.values()) {
138137
try {
139-
component.onStart(this.asapPeer);
138+
component.onStart(asapPeer);
140139
} catch (SharkException e) {
141140
Log.writeLogErr(this, "could not start component: " + e.getLocalizedMessage());
142141
throw e;
@@ -178,39 +177,12 @@ public ASAPPeer getASAPPeer() throws SharkException {
178177
throw new SharkException("Shark Peer is not running");
179178
}
180179

181-
if(this.asapPeer == null) {
182-
throw new SharkException("That's a bug: ASAP peer not created");
183-
}
184-
185-
return this.asapPeer;
180+
return super.getASAPPeer();
186181
}
187182

188183
@Override
189184
public Set<CharSequence> getFormats() {
190185
return this.components.keySet();
191186
}
192187

193-
@Override
194-
public CharSequence getPeerID() throws SharkException {
195-
return this.getASAPPeer().getPeerID();
196-
}
197-
198-
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
199-
// extra data //
200-
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
201-
@Override
202-
public void putExtra(CharSequence key, byte[] value) throws IOException, SharkException, ASAPException {
203-
if(this.asapPeer == null) {
204-
throw new SharkException("peer is not yet launched - initialize your shark system");
205-
}
206-
this.asapPeer.putExtra(key, value);
207-
}
208-
209-
@Override
210-
public byte[] getExtra(CharSequence key) throws ASAPException, IOException, SharkException {
211-
if(this.asapPeer == null) {
212-
throw new SharkException("peer is not yet launched - initialize your shark system");
213-
}
214-
return this.asapPeer.getExtra(key);
215-
}
216188
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package net.sharksystem;
2+
3+
import net.sharksystem.asap.ASAPException;
4+
import net.sharksystem.hub.peerside.HubConnectorDescription;
5+
6+
import java.io.IOException;
7+
import java.util.Collection;
8+
9+
/**
10+
* Shark peer allows storing settings, like hub management. Those features are described here.
11+
*/
12+
public interface SharkPeerHubSupport {
13+
CharSequence getPeerID() throws SharkException;
14+
15+
void addHubDescription(HubConnectorDescription hubConnectorDescription);
16+
17+
void removeHubDescription(HubConnectorDescription hubConnectorDescription);
18+
19+
Collection<HubConnectorDescription> getHubDescriptions();
20+
21+
HubConnectorDescription getHubDescription(int index) throws SharkException;
22+
23+
/**
24+
* Make a value persistent with key
25+
* @param key
26+
* @param value
27+
*/
28+
void putExtra(CharSequence key, byte[] value) throws IOException, SharkException, ASAPException;
29+
30+
/**
31+
* Return a value. Throws an exception if not set
32+
* @param key
33+
* @throws ASAPException key never used in putExtra
34+
*/
35+
byte[] getExtra(CharSequence key) throws ASAPException, IOException, SharkException;
36+
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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+
}

tests/net/sharksystem/SharkComponentTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import net.sharksystem.asap.ASAPException;
44
import net.sharksystem.asap.utils.DateTimeHelper;
5+
import net.sharksystem.hub.peerside.TCPHubConnectorDescription;
56
import org.junit.Assert;
67
import org.junit.Test;
78

@@ -85,4 +86,29 @@ public void sendAMessage() throws SharkException, ASAPException, IOException, In
8586
// Alice should have received Bob broadcast on format A but nothing on format B
8687
Assert.assertEquals(1, aliceListener.counter);
8788
}
89+
90+
@Test
91+
public void hubDescriptions() throws SharkException, IOException {
92+
////////////// setup Alice
93+
SharkTestPeerFS.removeFolder(ALICE_ROOTFOLDER);
94+
SharkTestPeerFS aliceSharkPeer = new SharkTestPeerFS(ALICE, ALICE_ROOTFOLDER);
95+
YourComponent aliceComponent = this.setupComponent(aliceSharkPeer);
96+
97+
// Start alice peer
98+
aliceSharkPeer.start();
99+
100+
aliceSharkPeer.addHubDescription(new TCPHubConnectorDescription("exampleHost_A", 1234));
101+
aliceSharkPeer.addHubDescription(new TCPHubConnectorDescription("exampleHost_B", 1235));
102+
aliceSharkPeer.addHubDescription(new TCPHubConnectorDescription("exampleHost_C", 1265));
103+
104+
// relaunch
105+
aliceSharkPeer = new SharkTestPeerFS(ALICE, ALICE_ROOTFOLDER);
106+
aliceComponent = this.setupComponent(aliceSharkPeer);
107+
108+
aliceSharkPeer.start();
109+
110+
aliceSharkPeer.getHubDescription(0);
111+
aliceSharkPeer.getHubDescription(1);
112+
aliceSharkPeer.getHubDescription(2);
113+
}
88114
}

0 commit comments

Comments
 (0)