From 3f6df8e6c73faf475c1520d911e8647d58cf828e Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Thu, 27 Jul 2017 16:43:09 +0200 Subject: [PATCH] Notifiers and ProgressListeners --- .../smackx/jft/JingleFileTransferManager.java | 17 ++++++--- .../IncomingFileOfferController.java | 4 +-- .../IncomingFileRequestController.java | 4 +-- .../JingleFileTransferController.java | 14 ++++++++ .../OutgoingFileOfferController.java | 2 +- .../OutgoingFileRequestController.java | 2 +- .../internal/AbstractJingleFileTransfer.java | 35 +++++++++++++++++-- .../jft/internal/JingleIncomingFileOffer.java | 10 ++++++ .../internal/JingleIncomingFileRequest.java | 10 ++++++ .../jft/internal/JingleOutgoingFileOffer.java | 10 ++++++ .../internal/JingleOutgoingFileRequest.java | 10 ++++++ .../smackx/jft/listener/ProgressListener.java | 13 +++++++ .../jingle/JingleDescriptionManager.java | 3 ++ .../jingle/components/JingleSession.java | 4 ++- 14 files changed, 122 insertions(+), 16 deletions(-) create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/JingleFileTransferController.java create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/jft/listener/ProgressListener.java diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/JingleFileTransferManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/JingleFileTransferManager.java index 333305363..467f9dc12 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/JingleFileTransferManager.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/JingleFileTransferManager.java @@ -123,15 +123,22 @@ public final class JingleFileTransferManager extends Manager implements JingleDe return AbstractJingleFileTransfer.NAMESPACE; } - @Override - public void notifySessionInitiate(JingleSession session) { - JingleContent content = session.getSoleContentOrThrow(); - AbstractJingleFileTransfer transfer = (AbstractJingleFileTransfer) content.getDescription(); - + private void notifyTransfer(AbstractJingleFileTransfer transfer) { if (transfer.isOffer()) { notifyIncomingFileOfferListeners((JingleIncomingFileOffer) transfer); } else { notifyIncomingFileRequestListeners((JingleIncomingFileRequest) transfer); } } + + @Override + public void notifySessionInitiate(JingleSession session) { + JingleContent content = session.getSoleContentOrThrow(); + notifyTransfer((AbstractJingleFileTransfer) content.getDescription()); + } + + @Override + public void notifyContentAdd(JingleContent content) { + notifyTransfer((AbstractJingleFileTransfer) content.getDescription()); + } } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/IncomingFileOfferController.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/IncomingFileOfferController.java index 2f0e36810..27ce595f7 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/IncomingFileOfferController.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/IncomingFileOfferController.java @@ -1,10 +1,8 @@ package org.jivesoftware.smackx.jft.controller; -import org.jivesoftware.smackx.jingle.JingleDescriptionController; - /** * Created by vanitas on 27.07.17. */ -public interface IncomingFileOfferController extends JingleDescriptionController { +public interface IncomingFileOfferController extends JingleFileTransferController { } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/IncomingFileRequestController.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/IncomingFileRequestController.java index fdb7527b2..66bc458ba 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/IncomingFileRequestController.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/IncomingFileRequestController.java @@ -1,10 +1,8 @@ package org.jivesoftware.smackx.jft.controller; -import org.jivesoftware.smackx.jingle.JingleDescriptionController; - /** * Created by vanitas on 27.07.17. */ -public interface IncomingFileRequestController extends JingleDescriptionController { +public interface IncomingFileRequestController extends JingleFileTransferController { } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/JingleFileTransferController.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/JingleFileTransferController.java new file mode 100644 index 000000000..4f0b65376 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/JingleFileTransferController.java @@ -0,0 +1,14 @@ +package org.jivesoftware.smackx.jft.controller; + +import org.jivesoftware.smackx.jft.listener.ProgressListener; +import org.jivesoftware.smackx.jingle.JingleDescriptionController; + +/** + * Created by vanitas on 27.07.17. + */ +public interface JingleFileTransferController extends JingleDescriptionController { + + void addProgressListener(ProgressListener listener); + + void removeProgressListener(ProgressListener listener); +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/OutgoingFileOfferController.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/OutgoingFileOfferController.java index be00959f0..9caa11ee3 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/OutgoingFileOfferController.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/OutgoingFileOfferController.java @@ -3,5 +3,5 @@ package org.jivesoftware.smackx.jft.controller; /** * Created by vanitas on 27.07.17. */ -public interface OutgoingFileOfferController { +public interface OutgoingFileOfferController extends JingleFileTransferController { } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/OutgoingFileRequestController.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/OutgoingFileRequestController.java index d3fd43d6a..373060c8d 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/OutgoingFileRequestController.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/controller/OutgoingFileRequestController.java @@ -3,5 +3,5 @@ package org.jivesoftware.smackx.jft.controller; /** * Created by vanitas on 27.07.17. */ -public interface OutgoingFileRequestController { +public interface OutgoingFileRequestController extends JingleFileTransferController { } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/AbstractJingleFileTransfer.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/AbstractJingleFileTransfer.java index aea876979..69762f0aa 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/AbstractJingleFileTransfer.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/AbstractJingleFileTransfer.java @@ -1,12 +1,18 @@ package org.jivesoftware.smackx.jft.internal; -import org.jivesoftware.smackx.jingle.components.JingleDescription; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.jivesoftware.smackx.jft.controller.JingleFileTransferController; import org.jivesoftware.smackx.jft.element.JingleFileTransferElement; +import org.jivesoftware.smackx.jft.listener.ProgressListener; +import org.jivesoftware.smackx.jingle.components.JingleDescription; /** * Created by vanitas on 22.07.17. */ -public abstract class AbstractJingleFileTransfer extends JingleDescription { +public abstract class AbstractJingleFileTransfer extends JingleDescription implements JingleFileTransferController { public static final String NAMESPACE_V5 = "urn:xmpp:jingle:apps:file-transfer:5"; public static final String NAMESPACE = NAMESPACE_V5; @@ -14,4 +20,29 @@ public abstract class AbstractJingleFileTransfer extends JingleDescription progressListeners = Collections.synchronizedList(new ArrayList()); + + @Override + public void addProgressListener(ProgressListener listener) { + progressListeners.add(listener); + //TODO: Notify new listener? + } + + @Override + public void removeProgressListener(ProgressListener listener) { + progressListeners.remove(listener); + } + + public void notifyProgressListeners(float progress) { + for (ProgressListener p : progressListeners) { + p.progress(progress); + } + } + + @Override + public State getState() { + return state; + } } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleIncomingFileOffer.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleIncomingFileOffer.java index 999307732..76313da7f 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleIncomingFileOffer.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleIncomingFileOffer.java @@ -31,4 +31,14 @@ public class JingleIncomingFileOffer extends AbstractJingleFileOffer return; } } + + @Override + public boolean isOffer() { + return true; + } + + @Override + public boolean isRequest() { + return false; + } } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleIncomingFileRequest.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleIncomingFileRequest.java index a26386cb4..53478f990 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleIncomingFileRequest.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleIncomingFileRequest.java @@ -13,6 +13,16 @@ public class JingleIncomingFileRequest extends AbstractJingleFileRequest impleme return null; } + @Override + public boolean isOffer() { + return false; + } + + @Override + public boolean isRequest() { + return true; + } + @Override public void onTransportReady(BytestreamSession bytestreamSession) { diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleOutgoingFileOffer.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleOutgoingFileOffer.java index cee8ec2ba..5e7d11a10 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleOutgoingFileOffer.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleOutgoingFileOffer.java @@ -19,4 +19,14 @@ public class JingleOutgoingFileOffer extends AbstractJingleFileOffer public void onTransportReady(BytestreamSession bytestreamSession) { } + + @Override + public boolean isOffer() { + return true; + } + + @Override + public boolean isRequest() { + return false; + } } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleOutgoingFileRequest.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleOutgoingFileRequest.java index 7c31f9557..de7769dde 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleOutgoingFileRequest.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleOutgoingFileRequest.java @@ -13,6 +13,16 @@ public class JingleOutgoingFileRequest extends AbstractJingleFileRequest impleme return null; } + @Override + public boolean isOffer() { + return false; + } + + @Override + public boolean isRequest() { + return true; + } + @Override public void onTransportReady(BytestreamSession bytestreamSession) { diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/listener/ProgressListener.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/listener/ProgressListener.java new file mode 100644 index 000000000..4632e7a64 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/listener/ProgressListener.java @@ -0,0 +1,13 @@ +package org.jivesoftware.smackx.jft.listener; + +/** + * Created by vanitas on 27.07.17. + */ +public interface ProgressListener { + + void started(); + + void progress(float percent); + + void finished(); +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/JingleDescriptionManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/JingleDescriptionManager.java index a896df644..db908830f 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/JingleDescriptionManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/JingleDescriptionManager.java @@ -16,6 +16,7 @@ */ package org.jivesoftware.smackx.jingle; +import org.jivesoftware.smackx.jingle.components.JingleContent; import org.jivesoftware.smackx.jingle.components.JingleSession; /** @@ -26,4 +27,6 @@ public interface JingleDescriptionManager { String getNamespace(); void notifySessionInitiate(JingleSession session); + + void notifyContentAdd(JingleContent content); } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/components/JingleSession.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/components/JingleSession.java index 6e62077eb..255faa977 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/components/JingleSession.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/components/JingleSession.java @@ -359,6 +359,8 @@ public class JingleSession { } for (final JingleContent content : descriptionCategory.getValue()) { + descriptionManager.notifyContentAdd(content); + ContentAddCallback callback = new ContentAddCallback() { @Override public void acceptContentAdd() { @@ -372,7 +374,7 @@ public class JingleSession { // TODO: Send content-reject } }; - descriptionManager.notifySessionInitiate(); + descriptionManager } }