2015-08-02 21:47:01 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#ifndef NT_NOTIFIER_H_
|
|
|
|
|
#define NT_NOTIFIER_H_
|
|
|
|
|
|
2015-12-28 08:28:24 -08:00
|
|
|
#include <functional>
|
2015-08-02 21:47:01 -07:00
|
|
|
|
|
|
|
|
#include "atomic_static.h"
|
|
|
|
|
#include "ntcore_cpp.h"
|
2015-12-28 08:28:24 -08:00
|
|
|
#include "SafeThread.h"
|
2015-08-02 21:47:01 -07:00
|
|
|
|
|
|
|
|
namespace nt {
|
|
|
|
|
|
|
|
|
|
class Notifier {
|
|
|
|
|
friend class NotifierTest;
|
|
|
|
|
public:
|
|
|
|
|
static Notifier& GetInstance() {
|
|
|
|
|
ATOMIC_STATIC(Notifier, instance);
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
~Notifier();
|
|
|
|
|
|
|
|
|
|
void Start();
|
|
|
|
|
void Stop();
|
|
|
|
|
|
2015-09-25 11:54:17 -07:00
|
|
|
bool local_notifiers() const { return m_local_notifiers; }
|
2015-09-14 22:00:22 -07:00
|
|
|
static bool destroyed() { return s_destroyed; }
|
2015-08-02 21:47:01 -07:00
|
|
|
|
2015-12-20 20:42:09 -08:00
|
|
|
void SetOnStart(std::function<void()> on_start) { m_on_start = on_start; }
|
|
|
|
|
void SetOnExit(std::function<void()> on_exit) { m_on_exit = on_exit; }
|
|
|
|
|
|
2015-08-02 21:47:01 -07:00
|
|
|
unsigned int AddEntryListener(StringRef prefix,
|
2015-09-23 00:56:08 -07:00
|
|
|
EntryListenerCallback callback,
|
2015-09-25 11:54:17 -07:00
|
|
|
unsigned int flags);
|
2015-08-02 21:47:01 -07:00
|
|
|
void RemoveEntryListener(unsigned int entry_listener_uid);
|
|
|
|
|
|
2015-09-25 11:54:17 -07:00
|
|
|
void NotifyEntry(StringRef name, std::shared_ptr<Value> value,
|
|
|
|
|
unsigned int flags, EntryListenerCallback only = nullptr);
|
2015-08-02 21:47:01 -07:00
|
|
|
|
|
|
|
|
unsigned int AddConnectionListener(ConnectionListenerCallback callback);
|
|
|
|
|
void RemoveConnectionListener(unsigned int conn_listener_uid);
|
|
|
|
|
|
2015-08-28 00:13:56 -07:00
|
|
|
void NotifyConnection(bool connected, const ConnectionInfo& conn_info,
|
|
|
|
|
ConnectionListenerCallback only = nullptr);
|
2015-08-02 21:47:01 -07:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Notifier();
|
|
|
|
|
|
2015-12-28 08:28:24 -08:00
|
|
|
class Thread;
|
|
|
|
|
SafeThreadOwner<Thread> m_owner;
|
2015-08-02 21:47:01 -07:00
|
|
|
|
2015-09-23 00:56:08 -07:00
|
|
|
std::atomic_bool m_local_notifiers;
|
2015-08-02 21:47:01 -07:00
|
|
|
|
2015-12-20 20:42:09 -08:00
|
|
|
std::function<void()> m_on_start;
|
|
|
|
|
std::function<void()> m_on_exit;
|
|
|
|
|
|
2015-08-02 21:47:01 -07:00
|
|
|
ATOMIC_STATIC_DECL(Notifier)
|
2015-09-14 22:00:22 -07:00
|
|
|
static bool s_destroyed;
|
2015-08-02 21:47:01 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace nt
|
|
|
|
|
|
|
|
|
|
#endif // NT_NOTIFIER_H_
|