From fa1633e3b4d75a8217459cdc5fe64e9ee5ace65a Mon Sep 17 00:00:00 2001 From: Alex Clayton Date: Mon, 18 Jan 2016 12:30:15 +0000 Subject: Some Jingle Fixes Some fixes to the Jingle classes for the previous patch. Test-information: By code inspection. Ran Unit Tests there were no failures. Change-Id: I61210dd1ec2c7e0dd11d67a45a0ced63952804c7 diff --git a/src/com/isode/stroke/base/Listenable.java b/src/com/isode/stroke/base/Listenable.java index c0aa091..1f7eed1 100755 --- a/src/com/isode/stroke/base/Listenable.java +++ b/src/com/isode/stroke/base/Listenable.java @@ -11,15 +11,12 @@ package com.isode.stroke.base; -import java.util.Vector; -import com.isode.stroke.signals.Slot; -import com.isode.stroke.signals.Slot1; -import com.isode.stroke.signals.Slot2; -import com.isode.stroke.signals.Slot3; +import java.util.Set; +import java.util.concurrent.CopyOnWriteArraySet; public class Listenable { - private Vector listeners = new Vector(); + private Set listeners = new CopyOnWriteArraySet(); public void addListener(T listener) { listeners.add(listener); @@ -30,30 +27,31 @@ public class Listenable { listeners.remove(listener); } } - - //Swiften code calls event(i), which is not yet done. - public void notifyListeners(Slot event) { - for (T i : listeners) { - event.call(); - } - } - - //Swiften code calls event(i), which is not yet done. - public void notifyListeners(Slot1 event, T1 p1) { - for (T i : listeners) { - event.call(p1); - } + + // Swiften code takes advantage of boost binding + // which we can't do in java. Easiest solution seems + // to be to pass listener to the caller to handle which + // method should be called + public void notifyListeners(ListenableCallback callback) { + for (T listener : listeners) { + callback.call(listener); + } } - - public void notifyListeners(Slot2 event, T1 p1, T2 p2) { - for (T i : listeners) { - event.call(p1, p2); - } - } - - public void notifyListeners(Slot3 event, T1 p1, T2 p2, T3 p3) { - for (T i : listeners) { - event.call(p1, p2, p3); - } + + /** + * Callback for subclasses to specify how the listers should be + * called + * @param Type of listener that can registered with the {@link Listenable} + */ + public static interface ListenableCallback { + + /** + * Called by the parent {@link Listenable} class for all + * registered listeners. + * @param listener A registered listener. May be {@code null} + * if a {@code null} was registered as a listener. + */ + public void call(S listener); + } } \ No newline at end of file diff --git a/src/com/isode/stroke/jingle/FakeJingleSession.java b/src/com/isode/stroke/jingle/FakeJingleSession.java index 927c262..9a77ef6 100644 --- a/src/com/isode/stroke/jingle/FakeJingleSession.java +++ b/src/com/isode/stroke/jingle/FakeJingleSession.java @@ -152,52 +152,63 @@ public class FakeJingleSession extends JingleSession { } public void handleSessionTerminateReceived(final JinglePayload.Reason reason) { - notifyListeners(new Slot1() { - @Override - public void call(JinglePayload.Reason reason) { - handleSessionTerminateReceived(reason); - } - }, reason); + notifyListeners(new ListenableCallback() { + + @Override + public void call(JingleSessionListener listener) { + listener.handleSessionTerminateReceived(reason); + } + + }); } - public void handleSessionAcceptReceived(final JingleContentID id, JingleDescription desc, JingleTransportPayload transport) { - notifyListeners(new Slot3() { - @Override - public void call(JingleContentID d, JingleDescription n, JingleTransportPayload p) { - handleSessionAcceptReceived(d, n, p); - } - }, id, desc, transport); + public void handleSessionAcceptReceived(final JingleContentID id,final JingleDescription desc, + final JingleTransportPayload transport) { + notifyListeners(new ListenableCallback() { + + @Override + public void call(JingleSessionListener listener) { + listener.handleSessionAcceptReceived(id, desc, transport); + } + + }); } public void handleSessionInfoReceived(JinglePayload payload) { } - public void handleTransportReplaceReceived(final JingleContentID id, JingleTransportPayload payload) { - notifyListeners(new Slot2() { - @Override - public void call(JingleContentID d, JingleTransportPayload p) { - handleTransportReplaceReceived(d, p); - } - }, id, payload); + public void handleTransportReplaceReceived(final JingleContentID id,final JingleTransportPayload payload) { + notifyListeners(new ListenableCallback() { + + @Override + public void call(JingleSessionListener listener) { + listener.handleTransportReplaceReceived(id, payload); + } + + }); } - public void handleTransportAcceptReceived(final JingleContentID id, JingleTransportPayload payload) { - notifyListeners(new Slot2() { - @Override - public void call(JingleContentID d, JingleTransportPayload p) { - handleTransportAcceptReceived(d, p); - } - }, id, payload); + public void handleTransportAcceptReceived(final JingleContentID id,final JingleTransportPayload payload) { + notifyListeners(new ListenableCallback() { + + @Override + public void call(JingleSessionListener listener) { + listener.handleTransportAcceptReceived(id, payload); + } + + }); } - public void handleTransportInfoReceived(final JingleContentID id, JingleTransportPayload payload) { - notifyListeners(new Slot2() { - @Override - public void call(JingleContentID d, JingleTransportPayload p) { - handleTransportInfoReceived(d, p); - } - }, id, payload); + public void handleTransportInfoReceived(final JingleContentID id,final JingleTransportPayload payload) { + notifyListeners(new ListenableCallback() { + + @Override + public void call(JingleSessionListener listener) { + listener.handleTransportInfoReceived(id, payload);; + } + + }); } public void handleTransportRejectReceived(final JingleContentID id, JingleTransportPayload payload) { diff --git a/src/com/isode/stroke/jingle/Jingle.java b/src/com/isode/stroke/jingle/Jingle.java index 14dbb68..db041b8 100644 --- a/src/com/isode/stroke/jingle/Jingle.java +++ b/src/com/isode/stroke/jingle/Jingle.java @@ -18,9 +18,9 @@ import java.util.Vector; public class Jingle { public JingleContentPayload getContentWithDescription(final Vector contents, T payload) { - for (int i = 0; i < contents.size(); ++i) { - if (contents.get(i).getDescription(payload) != null) { - return contents.get(i); + for (JingleContentPayload jingleContentPayload : contents) { + if (jingleContentPayload.getDescription(payload) != null) { + return jingleContentPayload; } } return null; diff --git a/src/com/isode/stroke/jingle/JingleSessionImpl.java b/src/com/isode/stroke/jingle/JingleSessionImpl.java index 8ce60d7..33f5cb0 100644 --- a/src/com/isode/stroke/jingle/JingleSessionImpl.java +++ b/src/com/isode/stroke/jingle/JingleSessionImpl.java @@ -142,23 +142,27 @@ public class JingleSessionImpl extends JingleSession { sendSetRequest(payload); } - void handleIncomingAction(JinglePayload action) { + void handleIncomingAction(final JinglePayload action) { if (JinglePayload.Action.SessionTerminate.equals(action.getAction())) { - /*notifyListeners(new Slot1() { - @Override - public void call(JinglePayload.Reason reason) { - handleSessionTerminateReceived(reason); - } - }, action.getReason());*/ + notifyListeners(new ListenableCallback() { + + @Override + public void call(JingleSessionListener listener) { + listener.handleSessionTerminateReceived(action.getReason()); + } + + }); return; } if (JinglePayload.Action.SessionInfo.equals(action.getAction())) { - /*notifyListeners(new Slot1() { - @Override - public void call(JinglePayload p) { - handleSessionInfoReceived(p); - } - }, action);*/ + notifyListeners(new ListenableCallback() { + + @Override + public void call(JingleSessionListener listener) { + listener.handleSessionInfoReceived(action); + } + + }); return; } @@ -167,49 +171,59 @@ public class JingleSessionImpl extends JingleSession { logger_.fine("no content payload!\n"); return; } - JingleContentID contentID = new JingleContentID(content.getName(), content.getCreator()); - JingleDescription description = content.getDescriptions().isEmpty() ? null : content.getDescriptions().get(0); - JingleTransportPayload transport = content.getTransports().isEmpty() ? null : content.getTransports().get(0); + final JingleContentID contentID = new JingleContentID(content.getName(), content.getCreator()); + final JingleDescription description = content.getDescriptions().isEmpty() ? null : content.getDescriptions().get(0); + final JingleTransportPayload transport = content.getTransports().isEmpty() ? null : content.getTransports().get(0); switch(action.getAction()) { case SessionAccept: - /*notifyListeners(new Slot3() { - @Override - public void call(JingleContentID id, JingleDescription des, JingleTransportPayload tr) { - handleSessionAcceptReceived(id, des, tr); - } - }, contentID, description, transport);*/ + notifyListeners(new ListenableCallback() { + + @Override + public void call(JingleSessionListener listener) { + listener.handleSessionAcceptReceived(contentID, description, transport); + } + + }); return; case TransportAccept: - /*notifyListeners(new Slot2() { - @Override - public void call(JingleContentID id, JingleTransportPayload tr) { - handleTransportAcceptReceived(id, tr); - } - }, contentID, transport);*/ + notifyListeners(new ListenableCallback() { + + @Override + public void call(JingleSessionListener listener) { + listener.handleTransportAcceptReceived(contentID, transport); + } + + }); return; case TransportInfo: - /*notifyListeners(new Slot2() { - @Override - public void call(JingleContentID id, JingleTransportPayload tr) { - handleTransportInfoReceived(id, tr); - } - }, contentID, transport);*/ + notifyListeners(new ListenableCallback() { + + @Override + public void call(JingleSessionListener listener) { + listener.handleTransportInfoReceived(contentID, transport); + } + + }); return; case TransportReject: - /*notifyListeners(new Slot2() { - @Override - public void call(JingleContentID id, JingleTransportPayload tr) { - handleTransportRejectReceived(id, tr); - } - }, contentID, transport);*/ + notifyListeners(new ListenableCallback() { + + @Override + public void call(JingleSessionListener listener) { + listener.handleTransportRejectReceived(contentID, transport); + } + + }); return; case TransportReplace: - /*notifyListeners(new Slot2() { - @Override - public void call(JingleContentID id, JingleTransportPayload tr) { - handleTransportReplaceReceived(id, tr); - } - }, contentID, transport);*/ + notifyListeners(new ListenableCallback() { + + @Override + public void call(JingleSessionListener listener) { + listener.handleTransportReplaceReceived(contentID, transport); + } + + }); return; // following unused Jingle actions case ContentAccept: @@ -249,15 +263,17 @@ public class JingleSessionImpl extends JingleSession { return payload; } - private void handleRequestResponse(GenericRequest request) { + private void handleRequestResponse(final GenericRequest request) { assert (pendingRequests.containsKey(request)); if (JinglePayload.Action.TransportInfo.equals(request.getPayloadGeneric().getAction())) { - /*notifyListeners(new Slot1() { - @Override - public void call(String s) { - handleTransportInfoAcknowledged(s); - } - }, request.getID());*/ + notifyListeners(new ListenableCallback() { + + @Override + public void call(JingleSessionListener listener) { + listener.handleTransportInfoAcknowledged(request.getID()); + } + + }); } pendingRequests.get(request).disconnect(); pendingRequests.remove(request); diff --git a/src/com/isode/stroke/jingle/JingleSessionManager.java b/src/com/isode/stroke/jingle/JingleSessionManager.java index 284e8cd..85d1280 100644 --- a/src/com/isode/stroke/jingle/JingleSessionManager.java +++ b/src/com/isode/stroke/jingle/JingleSessionManager.java @@ -15,6 +15,7 @@ import java.util.logging.Logger; import java.util.Vector; import java.util.Map; import java.util.HashMap; + import com.isode.stroke.queries.IQRouter; import com.isode.stroke.elements.JingleContentPayload; import com.isode.stroke.jid.JID; @@ -26,12 +27,12 @@ public class JingleSessionManager { private Vector incomingSessionHandlers = new Vector(); private Logger logger_ = Logger.getLogger(this.getClass().getName()); - private class JIDSession { + private static class JIDSession { public JIDSession(final JID initiator, final String session) { this.initiator = initiator; this.session = session; } - public int compareTo(JIDSession other) { + public int compareTo(JIDSession other) { if(other == null) { return -1; } @@ -42,8 +43,39 @@ public class JingleSessionManager { return initiator.compareTo(other.initiator); } } - public JID initiator; - public String session; + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + ((initiator == null) ? 0 : initiator.hashCode()); + result = prime * result + + ((session == null) ? 0 : session.hashCode()); + return result; + } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + JIDSession other = (JIDSession) obj; + if (initiator == null) { + if (other.initiator != null) + return false; + } else if (!initiator.equals(other.initiator)) + return false; + if (session == null) { + if (other.session != null) + return false; + } else if (!session.equals(other.session)) + return false; + return true; + } + public final JID initiator; + public final String session; }; private Map sessions = new HashMap(); -- cgit v0.10.2-6-g49f6