From 27f7c686385d2d410e2a16d5e7bebedd0bf806d1 Mon Sep 17 00:00:00 2001 From: Thad House Date: Sat, 6 Feb 2016 15:13:49 -0800 Subject: [PATCH] Updates IRemote to return the ConnectionInfo of the connected remote Also added a delegate listener, to match the table listener changes. Fixes #22 --- .../TestNetworkTableAPIListeners.cs | 4 +-- NetworkTablesCore/NetworkTable.cs | 35 +++++++++++++++++-- NetworkTablesCore/Tables/IRemote.cs | 23 +++++++++++- .../Tables/IRemoteConnectionListener.cs | 8 +++-- 4 files changed, 63 insertions(+), 7 deletions(-) diff --git a/NetworkTablesCore.Test/TestNetworkTableAPIListeners.cs b/NetworkTablesCore.Test/TestNetworkTableAPIListeners.cs index 9b81f4e..d72cdf4 100644 --- a/NetworkTablesCore.Test/TestNetworkTableAPIListeners.cs +++ b/NetworkTablesCore.Test/TestNetworkTableAPIListeners.cs @@ -27,12 +27,12 @@ internal class MockConnectionListener : IRemoteConnectionListener public IRemote ConnectedRemote = null; public IRemote DisconnectedRemote = null; - public void Connected(IRemote remote) + public void Connected(IRemote remote, ConnectionInfo info) { ConnectedRemote = remote; } - public void Disconnected(IRemote remote) + public void Disconnected(IRemote remote, ConnectionInfo info) { DisconnectedRemote = remote; } diff --git a/NetworkTablesCore/NetworkTable.cs b/NetworkTablesCore/NetworkTable.cs index 5315992..a5c8723 100644 --- a/NetworkTablesCore/NetworkTable.cs +++ b/NetworkTablesCore/NetworkTable.cs @@ -938,6 +938,9 @@ public void RemoveTableListener(Action list private readonly Dictionary m_connectionListenerMap = new Dictionary(); + private readonly Dictionary, int> m_actionConnectionListenerMap + = new Dictionary, int>(); + /// public void AddConnectionListener(IRemoteConnectionListener listener, bool immediateNotify) { @@ -949,8 +952,8 @@ public void AddConnectionListener(IRemoteConnectionListener listener, bool immed ConnectionListenerFunction func = (uid, connected, conn) => { - if (connected) listener.Connected(this); - else listener.Disconnected(this); + if (connected) listener.Connected(this, conn); + else listener.Disconnected(this, conn); }; int id = CoreMethods.AddConnectionListener(func, immediateNotify); @@ -969,6 +972,34 @@ public void RemoveConnectionListener(IRemoteConnectionListener listener) } } + /// + public void AddConnectionListener(Action listener, bool immediateNotify) + { + if (m_actionConnectionListenerMap.ContainsKey(listener)) + { + throw new ArgumentException("Cannot add the same listener twice", nameof(listener)); + } + + ConnectionListenerFunction func = (uid, connected, conn) => + { + listener(this, conn, connected); + }; + + int id = CoreMethods.AddConnectionListener(func, immediateNotify); + + m_actionConnectionListenerMap.Add(listener, id); + } + + /// + public void RemoveConnectionListener(Action listener) + { + int val; + if (m_actionConnectionListenerMap.TryGetValue(listener, out val)) + { + CoreMethods.RemoveConnectionListener(val); + } + } + /// /// Gets if the NetworkTables is connected to a client or server. /// diff --git a/NetworkTablesCore/Tables/IRemote.cs b/NetworkTablesCore/Tables/IRemote.cs index c13cc84..4a16413 100644 --- a/NetworkTablesCore/Tables/IRemote.cs +++ b/NetworkTablesCore/Tables/IRemote.cs @@ -1,4 +1,6 @@ -namespace NetworkTables.Tables +using System; + +namespace NetworkTables.Tables { /// /// Represents an object that has a remote connection. @@ -19,6 +21,25 @@ public interface IRemote /// The listener to be unregistered. void RemoveConnectionListener(IRemoteConnectionListener listener); + /// + /// Register a delegate to listen for connection and disconnection events. + /// + /// The listener to be registered. + /// True if the listener object should be notified of the current + /// connection state immediately. + /// + /// The of the action is the current table, the + /// is the info of the connected or disconnected target, and the bool is true if the event is + /// a connect, otherwise false. + /// + void AddConnectionListener(Action listener, bool immediateNotify); + + /// + /// Unregister a listener delegate from connection events. + /// + /// The listener to be unregistered. + void RemoveConnectionListener(Action listener); + /// /// Gets if the current object is connected. /// diff --git a/NetworkTablesCore/Tables/IRemoteConnectionListener.cs b/NetworkTablesCore/Tables/IRemoteConnectionListener.cs index 21e0aaa..a915a1a 100644 --- a/NetworkTablesCore/Tables/IRemoteConnectionListener.cs +++ b/NetworkTablesCore/Tables/IRemoteConnectionListener.cs @@ -9,12 +9,16 @@ public interface IRemoteConnectionListener /// Called when an is connected /// /// The object that connected. - void Connected(IRemote remote); + /// An object containing information about the + /// connected remote. + void Connected(IRemote remote, ConnectionInfo info); /// /// Called when an is disconnected. /// /// The object that disconnected. - void Disconnected(IRemote remote); + /// An object containing information about the + /// disconnected remote. + void Disconnected(IRemote remote, ConnectionInfo info); } }