|
| 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 | +} |
0 commit comments