Skip to content

Examples

Marvin edited this page Sep 11, 2023 · 4 revisions

Example with Usage of ASAPHubManager

The following code snippets are showing all necessary steps to connect your ASAP application with the ASAPHub. These examples were taken from the class HubUsageTests. There are two ways for connection establishment:

1. integrate your application using the ASAPHubManager

This approach shows how to connect to the hub and exchange data using the ASAPHubManager. You should prefer this solution, because a peer an use more than one hub. The ASAPHubManager makes it easy to manage multiple hubs.

boolean multichannel = true;
int hubPort = 6690;
String FORMAT = "app/x-asapHubtest";
String URI = "asap://testuri";

// create hub description with connection settings
HubConnectorDescription localHostHubDescription = new TCPHubConnectorDescriptionImpl("localhost", hubPort, multichannel);
Collection<HubConnectorDescription> hubDescriptions = new ArrayList<>();
hubDescriptions.add(localHostHubDescription);

// define supported formats
Collection<CharSequence> formats = new ArrayList<>();
formats.add(FORMAT);

ASAPPeerFS alicePeer = new ASAPPeerFS("alice", formats);

// send a message
alicePeer.sendASAPMessage(FORMAT, URI, "Hi there".getBytes());

// connect to hub
ASAPEncounterManagerImpl aliceEncounterManager = new ASAPEncounterManagerImpl(aliceASAPPeer);

// setup hub manager and connect to the hub
ASAPHubManager aliceHubManager = ASAPHubManagerImpl.createASAPHubManager(aliceEncounterManager);
aliceHubManager.connectASAPHubs(hubDescriptions, alicePeer, true);

An E2E Test which shows how to start an encounter between Alice and Bob using the ASAPHubManager can be found here.

2. integrate your application using the HubConnectionManager

Another comfortable way to establish a connection with the ASAPHub is by using the HubConnectionManager interface. The Hub Manager provides methods to manage connections to one or more ASAPHubs from an ASAP peer. This includes establishing connections, displaying failed connection attempts, and disconnecting.

The idea behind the HubConnectionManager is to allow the developer to divide the application into a service and an app layer. The HubConnectionManager interface is implemented by the abstract class BasicHubConnectionManager. This class provides a base implementation for various implementations of a HubConnectionManager.

class_diagram_hub_connection_manager

An example of a complete implementation of the HubConnectionManager is provided by the class HubConnectionManagerImpl. It can be used to establish a connection to the hub from an ASAP peer implemented in Java. Since this implementation is not divided into service and app layers, HubConnectionManagerImpl inherits from and additionally implements the HubConnectionManagerMessageHandler interface. The HubConnectionManagerMessageHandler interface is designed to ensure communication between the app and service layers.

The implementation without a division into app and service layers (using the HubConnectionManagerImpl class) can be found in the HubTester demo application.

An example of an implementation of the HubConnectionManager where a separation between the app and service layers occurs can be found in the ASAPAndroid library.

In addition, a mini example of using the HubConnectionManagerImpl class is shown below. In this example, the HubConnectionManagerImpl class is used to manage the connection between ASAPPeer and the ASAPHub.

String host = "127.0.0.1";
int port = 6910;
boolean multiChannel = true;

HubConnectorDescription hcd = new TCPHubConnectorDescriptionImpl(host, port, multiChannel);

String peerId = "alice";
CharSequence format = "ASAP_EXAMPLE_APP";
ASAPPeerFS asapPeer = new ASAPTestPeerFS(peerId, new ArrayList<>(Collections.singletonList(format)));
HubConnectionManager hubConnectionManager = new HubConnectionManagerImpl(new ASAPEncounterManagerImpl(asapPeer), asapPeer);

// connect to hub
hubConnectionManager.connectHub(hcd);

// get list of connected hubs
List<HubConnectorDescription> hubConnectorDescriptions = hubConnectionManager.getConnectedHubs();

// get failed attempts
List<HubConnectionManager.FailedConnectionAttempt> failedConnectionAttempts = hubConnectionManager.getFailedConnectionAttempts();

// disconnect from hub
hubConnectionManager.disconnectHub(hcd);

3. integrate your application using the HubConnector

Alternatively, it's also possible to setup a hub connection without using the ASAPHubManager. For this approach it's mandatory to implement a listener which implements the interface NewConnectionListener.

boolean multichannel = true;
int hubPort = 6690;
String host = "localhost";

HubConnector aliceHubConnector = SharedTCPChannelConnectorPeerSide.createTCPHubConnector(host, hubPort);

// add NewConnection listener
HubConnectorTester aliceListener = new HubConnectorTester(ALICE_ID, messageA, messageB, pureBytes);
aliceHubConnector.addListener(aliceListener);

// connect to the ASAPHub
aliceHubConnector.connectHub(ALICE_ID, aliceCanCreateTCPConnections);

// get all peers which are reqistered on the hub
Collection<CharSequence> peerNames = aliceHubConnector.getPeerIDs();

// connect to the peer with the peer-id "bob"
aliceHubConnector.connectPeer("bob");

// disconnect from the hub
aliceHubConnector.disconnectHub();

An E2E Test for this approach can be found here.