-
Notifications
You must be signed in to change notification settings - Fork 0
Home
You are familar with programming an ASAP peer?. Very good. Let's talk about components.
But before we start:: Add two additional libraries to your project: SharkPeer and ASAPHub.
Your apps will most probably be very helpful in other applications. Developers do not like to read lengthy documents, though. We need a slim mutual understanding how to integrate code from others to your project or vice versa.
That is what we call Shark – Shared Knowledge. It is a concept: Implement decentralized apps (based on ASAP) and make them work together.
Just a few issues needs to be tackled by a component model:
-
Different applications must not use the same format. ASAP applications will exchange different data and provide different features. ASAP applications make themself distinguishable by defining an application name (e.g.
"application/x-yourAppName"
). We need to know those formats descriptions when we integrate another component. -
Initialization / Dependencies Some components need to be initialized before usage. Components use each other. We need a way to initialize and connect components during system start up.
-
Peer startup In the end, there is still just a single ASAP peer sending an receiving messages. This peer must be started after the initialization process.
Continue with how to describe your component.
Finally, we need to explain how to set up a united application. The SharkPeer represents this unification. It represents the collection of more than one ASAP application. There is one implementation of this interface now: SharkPeerFS.
static final CharSequence ALICE = "Alice";
static final CharSequence ROOTFOLDER = "sharkComponent";
static final CharSequence ALICE_ROOTFOLDER = ROOTFOLDER + "/" + ALICE;
...
SharkPeerFS sharkPeer = new SharkPeerFS(ALICE, ALICE_ROOTFOLDER);
// add another application
AnotherComponentFactory factory = new AnotherComponentFactory();
sharkPeer.addComponent(factory, AnotherComponent.class);
// add your application
YourComponentFactory factory = new YourComponentFactory();
sharkPeer.addComponent(factory, YourComponent.class);
// add an additional one with dependencies
AnotherComponent requiredComponent = sharkPeer.getComponent(AnotherComponent.class);
DependendComponentFactory factory = new DependendComponentFactory(requiredComponent);
sharkPeer.addComponent(factory, YourComponent.class);
// launch Shark peer
sharkPeer.start();
SharkPeerFS works with ASAPPeerFS. It requires a peer name and folder name to keep data. Components can be added. Users of your application should only need to read and understand your YourFacade
and must be able to create a factory object of yours (YourComponentFactory
).
- Note: Your application cannot choose peer name and rootfolder by itself any longer. Those parameters are defined with SharkPeer creation. If you need them: There are two ways to get them.
Here is the first way – not the preferred one, though: Define you factory constructor in that way, like
public class YourComponentFactory implements SharkComponentFactory {
YourComponentFactory(CharSequence peerName, CharSequence folder) {
// keep it
}
...
}
-
Note: Components which are required from others could and should be parameter of the factory as illustrated with
DependendComponentFactory
. -
Note: Components can only be added and removed until the Shark peer is started
The startup process is very simple: An ASAPPeer is created with the collection of all supported formats. Each component is informed about this action: The void onStart(ASAPPeer peer)
is called. Your component, your ASAP application has got access to the ASAPPeer object and can act as usual. This is the second and preferred way to get access to peer parameters like its name.
The following code is from the ASAP Certificate project
@ASAPFormats(formats = {ASAPCertificateStore.CREDENTIAL_APP_NAME, ASAPCertificateStorage.CERTIFICATE_APP_NAME})
public class SharkCertificateComponent implements SharkComponent, ASAPKeyStore, ASAPCertificateStore {
private FullAsapPKIStorage asapPKIStorage = null;
@Override
public void onStart(ASAPPeer asapPeer) throws SharkException {
try {
ASAPStorage asapStorage = asapPeer.getASAPStorage(asapPeer.getPeerName());
ASAPCertificateStorage asapAliceCertificateStorage =
new ASAPAbstractCertificateStore(asapStorage, asapPeer.getPeerName(), asapPeer.getPeerName());
InMemoASAPKeyStore inMemoASAPKeyStore = new InMemoASAPKeyStore(asapPeer.getPeerName());
this.asapPKIStorage = new FullAsapPKIStorage(asapAliceCertificateStorage, inMemoASAPKeyStore);
} catch (IOException | ASAPException e) {
throw new SharkException(e);
}
}
…
private void checkStatus() throws SharkStatusException {
if(this.asapPKIStorage == null) {
throw new SharkStatusException("ASAP peer not started component not yet initialized");
}
}
// just an example of a interface implementation
@Override
public PublicKey getPublicKey(CharSequence charSequence) throws ASAPSecurityException {
this.checkStatus(); // it would produce an exception if called before the Shark Peer was started
return this.asapPKIStorage.getPublicKey(charSequence); // delegate call
}
...
}
Details of the algorithm in the onStart
method are not relevant but one fact is:
The actual creation of this component is made in the onStart
method and not in the factory method. Calling any method before the SharkPeer was started would produce a SharkStatusException
, see line this.checkStatus();
in getPublicKey
. This pattern separates the creation of the component object from setting up its functions. This class also follows the Delegation pattern.
Here is the factory implementation:
public class SharkCertificateComponentFactory implements SharkComponentFactory {
private SharkCertificateComponent instance = null;
@Override
public SharkComponent getComponent() {
if(this.instance == null) {
this.instance = new SharkCertificateComponent();
}
return this.instance;
}
}
An object is created but nothing is initialized. Initialization requires an ASAPPeer and is made in void onStart(ASAPPeer)
instead.
This library add some functions to ASAPPeer. Most importantly:
- What's a hub
- hub information management
- connection management