Skip to content

Commit

Permalink
Merge pull request #23 from robotdotnet/RemoteChanges
Browse files Browse the repository at this point in the history
Updates IRemote to return the ConnectionInfo of the connected remote
  • Loading branch information
ThadHouse committed Feb 7, 2016
2 parents 6d9772c + 27f7c68 commit 70db749
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
4 changes: 2 additions & 2 deletions NetworkTablesCore.Test/TestNetworkTableAPIListeners.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
35 changes: 33 additions & 2 deletions NetworkTablesCore/NetworkTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,9 @@ public void RemoveTableListener(Action<ITable, string, object, NotifyFlags> list
private readonly Dictionary<IRemoteConnectionListener, int> m_connectionListenerMap =
new Dictionary<IRemoteConnectionListener, int>();

private readonly Dictionary<Action<IRemote, ConnectionInfo, bool>, int> m_actionConnectionListenerMap
= new Dictionary<Action<IRemote, ConnectionInfo, bool>, int>();

///<inheritdoc/>
public void AddConnectionListener(IRemoteConnectionListener listener, bool immediateNotify)
{
Expand All @@ -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);
Expand All @@ -969,6 +972,34 @@ public void RemoveConnectionListener(IRemoteConnectionListener listener)
}
}

/// <inheritdoc/>
public void AddConnectionListener(Action<IRemote, ConnectionInfo, bool> 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);
}

/// <inheritdoc/>
public void RemoveConnectionListener(Action<IRemote, ConnectionInfo, bool> listener)
{
int val;
if (m_actionConnectionListenerMap.TryGetValue(listener, out val))
{
CoreMethods.RemoveConnectionListener(val);
}
}

/// <summary>
/// Gets if the NetworkTables is connected to a client or server.
/// </summary>
Expand Down
23 changes: 22 additions & 1 deletion NetworkTablesCore/Tables/IRemote.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace NetworkTables.Tables
using System;

namespace NetworkTables.Tables
{
/// <summary>
/// Represents an object that has a remote connection.
Expand All @@ -19,6 +21,25 @@ public interface IRemote
/// <param name="listener">The listener to be unregistered.</param>
void RemoveConnectionListener(IRemoteConnectionListener listener);

/// <summary>
/// Register a delegate to listen for connection and disconnection events.
/// </summary>
/// <param name="listener">The listener to be registered.</param>
/// <param name="immediateNotify">True if the listener object should be notified of the current
/// connection state immediately.</param>
/// <remarks>
/// The <see cref="IRemote"/> of the action is the current table, the <see cref="ConnectionInfo"/>
/// is the info of the connected or disconnected target, and the bool is true if the event is
/// a connect, otherwise false.
/// </remarks>
void AddConnectionListener(Action<IRemote, ConnectionInfo, bool> listener, bool immediateNotify);

/// <summary>
/// Unregister a listener delegate from connection events.
/// </summary>
/// <param name="listener">The listener to be unregistered.</param>
void RemoveConnectionListener(Action<IRemote, ConnectionInfo, bool> listener);

/// <summary>
/// Gets if the current object is connected.
/// </summary>
Expand Down
8 changes: 6 additions & 2 deletions NetworkTablesCore/Tables/IRemoteConnectionListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ public interface IRemoteConnectionListener
/// Called when an <see cref="IRemote"/> is connected
/// </summary>
/// <param name="remote">The object that connected.</param>
void Connected(IRemote remote);
/// <param name="info">An object containing information about the
/// connected remote.</param>
void Connected(IRemote remote, ConnectionInfo info);

/// <summary>
/// Called when an <see cref="IRemote"/> is disconnected.
/// </summary>
/// <param name="remote">The object that disconnected.</param>
void Disconnected(IRemote remote);
/// <param name="info">An object containing information about the
/// disconnected remote.</param>
void Disconnected(IRemote remote, ConnectionInfo info);
}
}

0 comments on commit 70db749

Please sign in to comment.