Skip to content

Commit b7b56e5

Browse files
committed
Implemented ASAPJavaApplication interface
1 parent d97d00b commit b7b56e5

11 files changed

+204
-23
lines changed

src/net/sharksystem/asap/ASAPChunk.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import java.io.IOException;
44
import java.io.InputStream;
5-
import java.util.HashMap;
6-
import java.util.Iterator;
7-
import java.util.List;
8-
import java.util.Set;
5+
import java.util.*;
96

107
/**
118
* An AASP chunk contains messages regarding a topic described by an
@@ -80,7 +77,7 @@ public interface ASAPChunk {
8077
* @param recipients
8178
* @throws IOException
8279
*/
83-
void setRecipients(Set<CharSequence> recipients) throws IOException;
80+
void setRecipients(Collection<CharSequence> recipients) throws IOException;
8481

8582
/**
8683
* recipient is removed

src/net/sharksystem/asap/ASAPChunkFS.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public void addRecipient(CharSequence recipient) throws IOException {
123123
}
124124

125125
@Override
126-
public void setRecipients(Set<CharSequence> newRecipients) throws IOException {
126+
public void setRecipients(Collection<CharSequence> newRecipients) throws IOException {
127127
this.recipients = new HashSet<>();
128128
for(CharSequence recipient : newRecipients) {
129129
this.recipients.add(recipient);

src/net/sharksystem/asap/ASAPEngine.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ public CharSequence getExtra(CharSequence uri, String key) throws IOException {
9292
}
9393

9494
@Override
95-
public void createChannel(CharSequence uri, Set<CharSequence> recipients) throws IOException, ASAPException {
95+
public void createChannel(CharSequence uri, Collection<CharSequence> recipients) throws IOException, ASAPException {
9696
this.createChannel(this.getOwner(), uri, recipients);
9797
}
9898

9999
@Override
100-
public void createChannel(CharSequence owner, CharSequence uri, Set<CharSequence> recipients)
100+
public void createChannel(CharSequence owner, CharSequence uri, Collection<CharSequence> recipients)
101101
throws IOException, ASAPException {
102102

103103
this.setRecipients(uri, recipients);
@@ -137,7 +137,7 @@ public boolean isASAPManagementStorageSet() {
137137
}
138138

139139
public void notifyChannelCreated(CharSequence appName, CharSequence owner,
140-
CharSequence uri, Set<CharSequence> recipients)
140+
CharSequence uri, Collection<CharSequence> recipients)
141141
throws ASAPException, IOException {
142142

143143
new ASAPManagementStorageImpl(this).notifyChannelCreated(appName, owner, uri, recipients);
@@ -147,7 +147,7 @@ public void addRecipient(CharSequence urlTarget, CharSequence recipient) throws
147147
this.chunkStorage.getChunk(urlTarget, this.era).addRecipient(recipient);
148148
}
149149

150-
public void setRecipients(CharSequence urlTarget, Set<CharSequence> recipients) throws IOException {
150+
public void setRecipients(CharSequence urlTarget, Collection<CharSequence> recipients) throws IOException {
151151
this.chunkStorage.getChunk(urlTarget, this.era).setRecipients(recipients);
152152
}
153153

src/net/sharksystem/asap/ASAPStorage.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.sharksystem.asap.management.ASAPManagementStorage;
55

66
import java.io.IOException;
7+
import java.util.Collection;
78
import java.util.List;
89
import java.util.Set;
910

@@ -62,21 +63,21 @@ public interface ASAPStorage {
6263
*
6364
* Peers must not forward messages from a closed to other peers than those in recipient list.
6465
*
65-
* @param urlTarget
66+
* @param uri
6667
* @param recipients
6768
* @throws IOException
6869
*/
69-
void createChannel(CharSequence urlTarget, Set<CharSequence> recipients) throws IOException, ASAPException;
70+
void createChannel(CharSequence uri, Collection<CharSequence> recipients) throws IOException, ASAPException;
7071

7172
/**
7273
* Create channel with a maybe different and than local peer - take care!
7374
* @param owner
74-
* @param urlTarget
75+
* @param uri
7576
* @param recipients
7677
* @throws IOException
7778
* @throws ASAPException
7879
*/
79-
void createChannel(CharSequence owner, CharSequence urlTarget, Set<CharSequence> recipients) throws IOException, ASAPException;
80+
void createChannel(CharSequence owner, CharSequence uri, Collection<CharSequence> recipients) throws IOException, ASAPException;
8081

8182
/**
8283
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package net.sharksystem.asap.apps;
2+
3+
import net.sharksystem.asap.ASAPException;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.OutputStream;
8+
import java.util.Collection;
9+
10+
public interface ASAPJavaApplication {
11+
/**
12+
* send an asap message - as soon as possible - to all recipients. If recipients are set null - message is delivered
13+
* to any peer whatsoever.
14+
* @param format
15+
* @param uri
16+
* @param recipients white list of recipients. If null - means anybody
17+
* @param message
18+
* @throws ASAPException e.g. format no supported
19+
*/
20+
void sendASAPMessage(CharSequence format, CharSequence uri, Collection<CharSequence> recipients, byte[] message)
21+
throws ASAPException, IOException;
22+
23+
/**
24+
* add listener for incomming messages for a given format
25+
* @param format
26+
* @param listener
27+
* @throws ASAPException format not supported
28+
*/
29+
void setASAPMessageReceivedListener(CharSequence format, ASAPMessageReceivedListener listener) throws ASAPException, IOException;
30+
31+
/**
32+
* Run an asap session with that those streams
33+
* @param is
34+
* @param os
35+
* @throws IOException
36+
* @throws ASAPException
37+
*/
38+
void handleConnection(InputStream is, OutputStream os) throws IOException, ASAPException;
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package net.sharksystem.asap.apps;
2+
3+
import net.sharksystem.asap.*;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.OutputStream;
8+
import java.util.*;
9+
10+
public class ASAPJavaApplicationFS implements ASAPJavaApplication {
11+
private MultiASAPEngineFS multiEngine;
12+
private final CharSequence owner;
13+
private final CharSequence rootFolder;
14+
15+
public static ASAPJavaApplication createASAPJavaApplication(CharSequence owner, CharSequence rootFolder,
16+
Collection<CharSequence> supportedFormats)
17+
throws IOException, ASAPException {
18+
19+
return new ASAPJavaApplicationFS(owner, rootFolder, supportedFormats);
20+
}
21+
22+
private ASAPJavaApplicationFS(CharSequence owner, CharSequence rootFolder, Collection<CharSequence> supportedFormats)
23+
throws IOException, ASAPException {
24+
25+
this.owner = owner;
26+
this.rootFolder = rootFolder;
27+
this.multiEngine = this.getMulitEngine();
28+
29+
if(supportedFormats != null && !supportedFormats.isEmpty()) {
30+
// ensure that supported format engine are up and running
31+
for(CharSequence format : supportedFormats) {
32+
this.multiEngine.createEngineByFormat(format);
33+
}
34+
}
35+
}
36+
37+
private MultiASAPEngineFS getMulitEngine() throws IOException, ASAPException {
38+
// TODO: re-create any time - keep track of potential changes in external storage (file system)?
39+
return MultiASAPEngineFS_Impl.createMultiEngine(owner, rootFolder,
40+
MultiASAPEngineFS.DEFAULT_MAX_PROCESSING_TIME, null);
41+
}
42+
43+
@Override
44+
public void sendASAPMessage(CharSequence format, CharSequence uri, Collection<CharSequence> recipients,
45+
byte[] message) throws ASAPException, IOException {
46+
47+
ASAPEngine engine = this.getMulitEngine().getEngineByFormat(format);
48+
49+
engine.createChannel(uri, recipients);
50+
engine.add(uri, message);
51+
}
52+
53+
private String getLogStart() {
54+
return this.getClass().getSimpleName() + ": ";
55+
}
56+
57+
private Map<CharSequence, ASAPMessageReceivedListener> messageReceivedListener = new HashMap<>();
58+
59+
@Override
60+
public void setASAPMessageReceivedListener(CharSequence format, ASAPMessageReceivedListener listener)
61+
throws ASAPException, IOException {
62+
63+
// wrap receiver and add listener to multiengine
64+
this.getMulitEngine().setASAPChunkReceivedListener(format, new MessageChunkReceivedListenerWrapper(listener));
65+
66+
// set with multi engine
67+
this.messageReceivedListener.put(format, listener);
68+
}
69+
70+
@Override
71+
public void handleConnection(InputStream is, OutputStream os) throws IOException, ASAPException {
72+
this.getMulitEngine().handleConnection(is, os);
73+
}
74+
75+
private class MessageChunkReceivedListenerWrapper implements ASAPChunkReceivedListener {
76+
private final ASAPMessageReceivedListener listener;
77+
78+
public MessageChunkReceivedListenerWrapper(ASAPMessageReceivedListener listener) throws ASAPException {
79+
if(listener == null) throw new ASAPException("listener must not be null");
80+
this.listener = listener;
81+
}
82+
83+
@Override
84+
public void chunkReceived(String format, String sender, String uri, int era) {
85+
System.out.println(getLogStart() + "chunk received - convert to asap message received");
86+
try {
87+
ASAPEngine engine = ASAPJavaApplicationFS.this.multiEngine.getEngineByFormat(format);
88+
ASAPMessages messages = engine.getChannel(uri).getMessages();
89+
this.listener.asapMessagesReceived(messages);
90+
} catch (ASAPException | IOException e) {
91+
System.out.println(getLogStart() + e.getLocalizedMessage());
92+
}
93+
}
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package net.sharksystem.asap.apps;
2+
3+
public interface ASAPMessageReceivedListener {
4+
void asapMessagesReceived(ASAPMessages messages);
5+
}

src/net/sharksystem/asap/management/ASAPManagementMessage.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
import net.sharksystem.asap.protocol.ASAP_PDU_1_0;
66

77
import java.io.*;
8-
import java.util.ArrayList;
9-
import java.util.HashSet;
10-
import java.util.List;
11-
import java.util.Set;
8+
import java.util.*;
129

1310
public class ASAPManagementMessage {
1411
public static byte[] getCreateClosedASAPChannelMessage(
1512
CharSequence owner, CharSequence appName, CharSequence channelUri,
16-
Set<CharSequence> recipients) throws ASAPException, IOException {
13+
Collection<CharSequence> recipients) throws ASAPException, IOException {
1714

1815
if(recipients == null || recipients.size() < 1) {
1916
throw new ASAPException("recipients in storage/channelUri must not be null or empty: ");

src/net/sharksystem/asap/management/ASAPManagementStorage.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import net.sharksystem.asap.ASAPException;
44

55
import java.io.IOException;
6-
import java.util.Set;
6+
import java.util.Collection;
77

88
public interface ASAPManagementStorage {
99
CharSequence ASAP_CREATE_CHANNEL = "asap://createChannel";
1010

1111
void notifyChannelCreated(CharSequence appName, CharSequence channelUri,
12-
CharSequence uri, Set<CharSequence> storageRecipients) throws ASAPException, IOException;
12+
CharSequence uri, Collection<CharSequence> storageRecipients) throws ASAPException, IOException;
1313
}

src/net/sharksystem/asap/management/ASAPManagementStorageImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import net.sharksystem.asap.ASAPException;
55

66
import java.io.IOException;
7-
import java.util.Set;
7+
import java.util.Collection;
88

99
public class ASAPManagementStorageImpl implements ASAPManagementStorage {
1010
private final ASAPEngine asapEngine;
@@ -15,7 +15,7 @@ public ASAPManagementStorageImpl(ASAPEngine asapEngine) {
1515

1616
@Override
1717
public void notifyChannelCreated(CharSequence appName, CharSequence channelUri, CharSequence uri,
18-
Set<CharSequence> recipients) throws ASAPException, IOException {
18+
Collection<CharSequence> recipients) throws ASAPException, IOException {
1919

2020
byte[] createClosedASAPChannelMessage = ASAPManagementMessage.getCreateClosedASAPChannelMessage(
2121
this.asapEngine.getOwner(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package net.sharksystem.asap;
2+
3+
import net.sharksystem.asap.apps.ASAPJavaApplication;
4+
import net.sharksystem.asap.apps.ASAPJavaApplicationFS;
5+
import net.sharksystem.asap.apps.ASAPMessageReceivedListener;
6+
import net.sharksystem.asap.apps.ASAPMessages;
7+
import org.junit.Test;
8+
9+
import java.io.IOException;
10+
import java.util.Collection;
11+
import java.util.HashSet;
12+
13+
public class ASAPJavaApplicationTests {
14+
public static final String ALICE = "Alice";
15+
public static final String BOB = "Bob";
16+
public static final String ALICE_ROOT_FOLDER = "tests/Alice";
17+
private static final CharSequence ALICE_APP_FORMAT = "TEST_FORMAT";
18+
private static final byte[] TESTMESSAGE = "TestMessage".getBytes();
19+
20+
@Test
21+
public void usageTest() throws IOException, ASAPException {
22+
Collection<CharSequence> formats = new HashSet<>();
23+
formats.add(ALICE_APP_FORMAT);
24+
25+
ASAPJavaApplication asapJavaApplication =
26+
ASAPJavaApplicationFS.createASAPJavaApplication(ALICE, ALICE_ROOT_FOLDER, formats);
27+
28+
Collection<CharSequence> recipients = new HashSet<>();
29+
recipients.add(BOB);
30+
31+
asapJavaApplication.sendASAPMessage(ALICE_APP_FORMAT, "yourSchema://yourURI", recipients, TESTMESSAGE);
32+
33+
asapJavaApplication.setASAPMessageReceivedListener(ALICE_APP_FORMAT, new ListenerExample());
34+
}
35+
36+
private class ListenerExample implements ASAPMessageReceivedListener {
37+
38+
@Override
39+
public void asapMessagesReceived(ASAPMessages messages) {
40+
try {
41+
System.out.println("#message == " + messages.size());
42+
} catch (IOException e) {
43+
// do something with it.
44+
}
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)