Skip to content

ASAPStorage

Thomas Schwotzer edited this page Feb 8, 2022 · 33 revisions

There are two ASAP message sources.

  • applications use their local peers to deliver messages
  • peers receive message during an encounter and notify their listeners

All messages are stored in a local ASAPStorage.

Developers are free to use this storage. Developers can also choose to ignore this storage an implement their own data management. Application get informed about any received message and can store it e.g. in a local data base. You can ignore this chapter in that case.

Messenger like applications should consider using the storage. ASAP comes very close to a messenger by design. ASAPStorage offers methods which make implementing a messenger app very simple.

This code show how to get an object reference to peers' storage:

static final CharSequence YOUR_APP_NAME = "application/x-yourAppName";
static final CharSequence YOUR_URI = "yourSchema://example";
...
ASAPPeer alicePeer = ...;
ASAPStorage aliceStorage = alicePeer.getASAPStorage(appName)

Each application, each format, has got its own storage. You need you application name (e.g. YOUR_APP_NAME) to access a storage.

access sent messages

ASAPStorage has different methods to access messages. We highly recomment using ASAPChannel

ASAPChannel channel = aliceStorage.getChannel(YOUR_URI);

Each message is sent with a URI. (Uri null is considers a uri as well - the null-uri if you will). ASAPStorage can provide a set of uris.

List<CharSequence> uriList = aliceStorage.getChannelURIs();

Each message is sent (and tagged) with an era. An ASAPChannel hides eras and produces a sorted chain of messages. Developers only need a uri but do not have to care about an era.

The following code produces list of message sent by your application:

ASAPMessages messages = channel.getMessages(true);
int size = messages.size();
Iterator<byte[]> messIter = messages.getMessages();

You already know ASAPMessages from the message received listener example. Number of messages are relevant in most cases. Application can get an iterator the process any message, e.g. to present it in a GUI.

access send and received messages

ASAPMessages messages = channel.getMessages();

This code would produce a list messages including sent and received messages. Sent messages would come first, followed by received messages in an arbitrary order.

Your applications defines your message structure. Your application can define an order of message if their is one. You could implement a ASAPMessageCompare object in that case. It must only implement a single method:

boolean earlier(byte[] messageA, byte[] messageB);

We use this option in the SharkMessenger project: SharkMessageComparison

You could now produce an ordered list of all received and sent messages.

ASAPMessageCompare yourMessageCompare = ...;
// produce an ordered list of messages
ASAPMessages messages = channel.getMessages(yourMessageCompare);

access received messages

Received messages are stored in an ASAPStorage. Each peer gets its own sub-storage. The following code produces a list of peers from which messages arrived:

List<CharSequence> peerIDs = aliceStorage.getSender;
CharSequence firstPeerID = peerID.get(0); // assumed list is not empty
ASAPStorage sendersStorage = aliceStorage.getExistingIncomingStorage(firstPeerID);

The last line produces an object reference of this storage. This object reference is of type ASAPStorage again and provides means to get access to a channel. Those channel object would only contain messages received from the peer. The won't be any sender within that storage.

Clone this wiki locally