Skip to content

Commit

Permalink
hub support integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
thsc42 committed Aug 13, 2021
1 parent de63186 commit f0d18d9
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 52 deletions.
Binary file modified libs/ASAPHub.jar
Binary file not shown.
Binary file modified libs/ASAPJava.jar
Binary file not shown.
20 changes: 1 addition & 19 deletions src/net/sharksystem/SharkPeer.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package net.sharksystem;

import net.sharksystem.asap.ASAPException;
import net.sharksystem.asap.ASAPPeer;

import java.io.IOException;
import java.util.Collection;
import java.util.Set;

/**
Expand All @@ -19,7 +16,7 @@
* @see SharkComponentFactory
* @see ASAPPeer
*/
public interface SharkPeer {
public interface SharkPeer extends SharkPeerHubSupport {
/**
* Add a component to the Shark app
* @param componentFactory
Expand Down Expand Up @@ -97,19 +94,4 @@ void removeComponent(Class<? extends SharkComponent> facade)

boolean samePeer(CharSequence otherPeerID) throws SharkException;

CharSequence getPeerID() throws SharkException;

/**
* Make a value persistent with key
* @param key
* @param value
*/
void putExtra(CharSequence key, byte[] value) throws IOException, SharkException, ASAPException;

/**
* Return a value. Throws an exception if not set
* @param key
* @throws ASAPException key never used in putExtra
*/
byte[] getExtra(CharSequence key) throws ASAPException, IOException, SharkException;
}
38 changes: 5 additions & 33 deletions src/net/sharksystem/SharkPeerFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
import net.sharksystem.asap.ASAPException;
import net.sharksystem.asap.ASAPPeer;
import net.sharksystem.asap.ASAPPeerFS;
import net.sharksystem.hub.*;
import net.sharksystem.utils.Log;

import java.io.IOException;
import java.util.*;

public class SharkPeerFS implements SharkPeer {
public class SharkPeerFS extends SharkPeerHubSupportImpl implements SharkPeer {
protected final CharSequence owner;
protected final CharSequence rootFolder;
private HashMap<CharSequence, SharkComponentFactory> factories = new HashMap<>();
protected HashMap<CharSequence, SharkComponent> components = new HashMap<>();
private SharkPeerStatus status = SharkPeerStatus.NOT_INITIALIZED;
private ASAPPeer asapPeer;

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

@Override
public void start(ASAPPeer asapPeer) throws SharkException {
this.asapPeer = asapPeer;
this.setASAPPeer(asapPeer);

boolean fullSuccess = true; // optimistic
for(SharkComponent component : this.components.values()) {
try {
component.onStart(this.asapPeer);
component.onStart(asapPeer);
} catch (SharkException e) {
Log.writeLogErr(this, "could not start component: " + e.getLocalizedMessage());
throw e;
Expand Down Expand Up @@ -178,39 +177,12 @@ public ASAPPeer getASAPPeer() throws SharkException {
throw new SharkException("Shark Peer is not running");
}

if(this.asapPeer == null) {
throw new SharkException("That's a bug: ASAP peer not created");
}

return this.asapPeer;
return super.getASAPPeer();
}

@Override
public Set<CharSequence> getFormats() {
return this.components.keySet();
}

@Override
public CharSequence getPeerID() throws SharkException {
return this.getASAPPeer().getPeerID();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// extra data //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Override
public void putExtra(CharSequence key, byte[] value) throws IOException, SharkException, ASAPException {
if(this.asapPeer == null) {
throw new SharkException("peer is not yet launched - initialize your shark system");
}
this.asapPeer.putExtra(key, value);
}

@Override
public byte[] getExtra(CharSequence key) throws ASAPException, IOException, SharkException {
if(this.asapPeer == null) {
throw new SharkException("peer is not yet launched - initialize your shark system");
}
return this.asapPeer.getExtra(key);
}
}
36 changes: 36 additions & 0 deletions src/net/sharksystem/SharkPeerHubSupport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.sharksystem;

import net.sharksystem.asap.ASAPException;
import net.sharksystem.hub.peerside.HubConnectorDescription;

import java.io.IOException;
import java.util.Collection;

/**
* Shark peer allows storing settings, like hub management. Those features are described here.
*/
public interface SharkPeerHubSupport {
CharSequence getPeerID() throws SharkException;

void addHubDescription(HubConnectorDescription hubConnectorDescription);

void removeHubDescription(HubConnectorDescription hubConnectorDescription);

Collection<HubConnectorDescription> getHubDescriptions();

HubConnectorDescription getHubDescription(int index) throws SharkException;

/**
* Make a value persistent with key
* @param key
* @param value
*/
void putExtra(CharSequence key, byte[] value) throws IOException, SharkException, ASAPException;

/**
* Return a value. Throws an exception if not set
* @param key
* @throws ASAPException key never used in putExtra
*/
byte[] getExtra(CharSequence key) throws ASAPException, IOException, SharkException;
}
183 changes: 183 additions & 0 deletions src/net/sharksystem/SharkPeerHubSupportImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package net.sharksystem;

import net.sharksystem.asap.ASAPEncounterManager;
import net.sharksystem.asap.ASAPEncounterManagerImpl;
import net.sharksystem.asap.ASAPException;
import net.sharksystem.asap.ASAPPeer;
import net.sharksystem.asap.utils.ASAPSerialization;
import net.sharksystem.hub.peerside.ASAPHubManager;
import net.sharksystem.hub.peerside.ASAPHubManagerImpl;
import net.sharksystem.hub.peerside.HubConnectorDescription;
import net.sharksystem.hub.peerside.HubConnectorFactory;
import net.sharksystem.utils.Log;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class SharkPeerHubSupportImpl implements SharkPeerHubSupport {
private ASAPEncounterManager asapEncounterManager;
private ASAPPeer asapPeer;

public SharkPeerHubSupportImpl() { }

public SharkPeerHubSupportImpl(ASAPEncounterManager asapEncounterManager) {
this.asapEncounterManager = asapEncounterManager;
}

private void init() {
this.restoreHubDescriptions();
}

public SharkPeerHubSupportImpl(ASAPPeer asapPeer) {
this.asapPeer = asapPeer;
this.init();
}

protected void setASAPPeer(ASAPPeer asapPeer) {
this.asapPeer = asapPeer;
this.init();
}

public ASAPPeer getASAPPeer() throws SharkException {
if(this.asapPeer == null) throw new SharkException("asap peer not set (yet)");
return this.asapPeer;
}

@Override
public CharSequence getPeerID() throws SharkException {
return this.getASAPPeer().getPeerID();
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////
// hub description management //
///////////////////////////////////////////////////////////////////////////////////////////////////////////

private static final CharSequence HUB_DESCRIPTIONS = "hubDescriptions";
private List<HubConnectorDescription> hubConnectorDescriptions = new ArrayList<>();

private void checkHubDescriptionsRestored() {
if(!this.hubDescriptionsRestored) {
this.restoreHubDescriptions();
}
}

@Override
public void addHubDescription(HubConnectorDescription hubConnectorDescription) {
this.checkHubDescriptionsRestored();
if(hubConnectorDescription == null) return;

// duplicate suppression
for(HubConnectorDescription hcd : this.hubConnectorDescriptions) {
if(hcd.isSame(hubConnectorDescription)) return;
}

this.hubConnectorDescriptions.add(hubConnectorDescription);
this.persistHubDescriptions();
}

@Override
public void removeHubDescription(HubConnectorDescription hubConnectorDescription) {
this.checkHubDescriptionsRestored();
HubConnectorDescription same = null;
for(HubConnectorDescription hcd : this.hubConnectorDescriptions) {
if(hubConnectorDescription.isSame(hcd)) {
same = hcd;
break;
}
}

if(same != null) this.hubConnectorDescriptions.remove(same);
this.persistHubDescriptions();
}

@Override
public Collection<HubConnectorDescription> getHubDescriptions() {
this.checkHubDescriptionsRestored();
return this.hubConnectorDescriptions;
}

@Override
public HubConnectorDescription getHubDescription(int index) throws SharkException {
this.checkHubDescriptionsRestored();
if(this.hubConnectorDescriptions.size() <= index) throw new SharkException("index out of range");

return this.hubConnectorDescriptions.get(index);
}

private void persistHubDescriptions() {
// not yet started or nothing to do
if(this.hubConnectorDescriptions.isEmpty() || this.asapPeer == null) return;

byte[][] serializedDescriptions = new byte[this.hubConnectorDescriptions.size()][];
int index = 0;
try {
for(HubConnectorDescription hcd : this.hubConnectorDescriptions) {
serializedDescriptions[index++] = hcd.serialize();
}

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ASAPSerialization.writeByteArray(serializedDescriptions, baos);
byte[] serial = baos.toByteArray();

this.asapPeer.putExtra(HUB_DESCRIPTIONS, serial);
} catch (IOException | ASAPException e) {
Log.writeLogErr(this, "cannot serialized hub description");
return;
}

}

private boolean hubDescriptionsRestored = false;
private void restoreHubDescriptions() {
if(this.asapPeer == null) return; // not yet started
if(this.hubDescriptionsRestored) return; // only once

this.hubDescriptionsRestored = true;

byte[] serial = null;
try {
serial = this.asapPeer.getExtra(HUB_DESCRIPTIONS);
if(serial == null) return; // ok - no descriptions stored
} catch (ASAPException | IOException e) {
Log.writeLog(this, "cannot read hub description - ok, maybe there are non");
return;
}

try {
ByteArrayInputStream bais = new ByteArrayInputStream(serial);
byte[][] serializedDescriptions = ASAPSerialization.readByte2DimArray(bais);

for(int i = 0; i < serializedDescriptions.length; i++) {
this.hubConnectorDescriptions.add(
HubConnectorFactory.createHubConnectorByDescription(serializedDescriptions[i]));
}
} catch (IOException | ASAPException e) {
Log.writeLogErr(this, "cannot deserialize hub description - seems to be a bug");
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// extra data //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Override
public void putExtra(CharSequence key, byte[] value) throws IOException, SharkException, ASAPException {
if(this.asapPeer == null) {
throw new SharkException("peer is not yet launched - initialize your shark system");
}
this.asapPeer.putExtra(key, value);
}

@Override
public byte[] getExtra(CharSequence key) throws ASAPException, IOException, SharkException {
if(this.asapPeer == null) {
throw new SharkException("peer is not yet launched - initialize your shark system");
}
return this.asapPeer.getExtra(key);
}

}
26 changes: 26 additions & 0 deletions tests/net/sharksystem/SharkComponentTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.sharksystem.asap.ASAPException;
import net.sharksystem.asap.utils.DateTimeHelper;
import net.sharksystem.hub.peerside.TCPHubConnectorDescription;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -85,4 +86,29 @@ public void sendAMessage() throws SharkException, ASAPException, IOException, In
// Alice should have received Bob broadcast on format A but nothing on format B
Assert.assertEquals(1, aliceListener.counter);
}

@Test
public void hubDescriptions() throws SharkException, IOException {
////////////// setup Alice
SharkTestPeerFS.removeFolder(ALICE_ROOTFOLDER);
SharkTestPeerFS aliceSharkPeer = new SharkTestPeerFS(ALICE, ALICE_ROOTFOLDER);
YourComponent aliceComponent = this.setupComponent(aliceSharkPeer);

// Start alice peer
aliceSharkPeer.start();

aliceSharkPeer.addHubDescription(new TCPHubConnectorDescription("exampleHost_A", 1234));
aliceSharkPeer.addHubDescription(new TCPHubConnectorDescription("exampleHost_B", 1235));
aliceSharkPeer.addHubDescription(new TCPHubConnectorDescription("exampleHost_C", 1265));

// relaunch
aliceSharkPeer = new SharkTestPeerFS(ALICE, ALICE_ROOTFOLDER);
aliceComponent = this.setupComponent(aliceSharkPeer);

aliceSharkPeer.start();

aliceSharkPeer.getHubDescription(0);
aliceSharkPeer.getHubDescription(1);
aliceSharkPeer.getHubDescription(2);
}
}

0 comments on commit f0d18d9

Please sign in to comment.