Initial checkin of unified hierarchy of WPILib 2015

This commit is contained in:
Brad Miller
2013-12-15 18:30:16 -05:00
commit 3178911eef
1560 changed files with 410007 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.wpi.first.wpilib.networktables.cpp</groupId>
<artifactId>include</artifactId>
<packaging>a</packaging>
<parent>
<groupId>edu.wpi.first.wpilib.templates</groupId>
<artifactId>include</artifactId>
<version>0.1.0-SNAPSHOT</version>
</parent>
<repositories>
<repository>
<id>WPILib Repository</id>
<url>http://frcbuilder.wpi.edu:8348/</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,47 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
#ifndef _ERROR_BASE_H
#define _ERROR_BASE_H
#if (defined __vxworks || defined WIN32)
#include <semLib.h>
#ifdef __vxworks
#include <vxWorks.h>
#endif
#define DISALLOW_COPY_AND_ASSIGN(ErrorBase)
#define wpi_setErrnoErrorWithContext(context)
#define wpi_setErrnoError()
#define wpi_setImaqErrorWithContext(code, context)
#define wpi_setErrorWithContext(code, context)
#define wpi_setError(code)
#define wpi_setStaticErrorWithContext(object, code, context)
#define wpi_setStaticError(object, code)
#define wpi_setGlobalErrorWithContext(code, context)
#define wpi_setGlobalError(code)
#define wpi_setWPIErrorWithContext(error, context)
#define wpi_setWPIError(error)
#define wpi_setStaticWPIErrorWithContext(object, error, context)
#define wpi_setStaticWPIError(object, error)
#define wpi_setGlobalWPIErrorWithContext(error, context)
#define wpi_setGlobalWPIError(error)
/**
* Base class for most objects.
* ErrorBase is the base class for most objects since it holds the generated error
* for that object. In addition, there is a single instance of a global error object
*/
class ErrorBase
{
//TODO: Consider initializing instance variables and cleanup in destructor
public:
};
#endif
#endif // __vxworks

View File

@@ -0,0 +1,98 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
#ifndef NT_SYNCHRONIZED_H
#define NT_SYNCHRONIZED_H
#define NT_CRITICAL_REGION(s) { NTSynchronized _sync(s);
#define NT_END_REGION }
#if (defined __vxworks || defined WIN32)
#ifdef __vxworks
#include <vxWorks.h>
#endif
#include <semLib.h>
class NTReentrantSemaphore
{
public:
explicit NTReentrantSemaphore(){
m_semaphore = semMCreate(SEM_Q_PRIORITY | SEM_INVERSION_SAFE | SEM_DELETE_SAFE);
};
~NTReentrantSemaphore(){
semDelete(m_semaphore);
};
void take(){
semTake(m_semaphore, WAIT_FOREVER);
};
void give(){
semGive(m_semaphore);
};
private:
SEM_ID m_semaphore;
};
#else
#include <pthread.h>
class NTReentrantSemaphore
{
public:
explicit NTReentrantSemaphore(){
pthread_mutexattr_init(&mta);
pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&m_semaphore, &mta);
};
~NTReentrantSemaphore(){
pthread_mutex_unlock(&m_semaphore);
pthread_mutex_destroy(&m_semaphore);
};
void take(){
pthread_mutex_lock(&m_semaphore);
};
void give(){
pthread_mutex_unlock(&m_semaphore);
};
private:
pthread_mutexattr_t mta;
pthread_mutex_t m_semaphore;
};
#endif // __vxworks
/**
* Provide easy support for critical regions.
* A critical region is an area of code that is always executed under mutual exclusion. Only
* one task can be executing this code at any time. The idea is that code that manipulates data
* that is shared between two or more tasks has to be prevented from executing at the same time
* otherwise a race condition is possible when both tasks try to update the data. Typically
* semaphores are used to ensure only single task access to the data.
* Synchronized objects are a simple wrapper around semaphores to help ensure that semaphores
* are always signaled (semGive) after a wait (semTake).
*/
class NTSynchronized
{
public:
explicit NTSynchronized(NTReentrantSemaphore&);
//TODO remove vxworks SEM_ID support
#if (defined __vxworks || defined WIN32)
explicit NTSynchronized(SEM_ID);
#endif
virtual ~NTSynchronized();
private:
#if (defined __vxworks || defined WIN32)
bool usingSem;
NTReentrantSemaphore* m_sem;
SEM_ID m_semaphore;
#else
NTReentrantSemaphore& m_semaphore;
#endif
};
#endif

View File

@@ -0,0 +1,76 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
#ifndef __NTTASK_H__
#define __NTTASK_H__
#if (defined __vxworks || defined WIN32)
#include "NTErrorBase.h"
#ifdef __vxworks
#include <vxWorks.h>
#endif
/**
* WPI task is a wrapper for the native Task object.
* All WPILib tasks are managed by a static task manager for simplified cleanup.
**/
class NTTask : public ErrorBase
{
public:
static const UINT32 kDefaultPriority = 101;
static const INT32 kInvalidTaskID = -1;
NTTask(const char* name, FUNCPTR function, INT32 priority = kDefaultPriority, UINT32 stackSize = 20000);
virtual ~NTTask();
#ifdef WIN32
bool Start(void * arg0);
#else
bool Start(UINT32 arg0 = 0, UINT32 arg1 = 0, UINT32 arg2 = 0, UINT32 arg3 = 0, UINT32 arg4 = 0,
UINT32 arg5 = 0, UINT32 arg6 = 0, UINT32 arg7 = 0, UINT32 arg8 = 0, UINT32 arg9 = 0);
#endif
bool Restart();
bool Stop();
bool IsReady();
bool IsSuspended();
bool Suspend();
bool Resume();
bool Verify();
INT32 GetPriority();
bool SetPriority(INT32 priority);
const char* GetName();
INT32 GetID();
#ifdef WIN32
FUNCPTR m_function;
void * m_Arg;
#endif
private:
char* m_taskName;
#ifdef WIN32
bool StartInternal();
HANDLE m_Handle;
DWORD m_ID;
#else
FUNCPTR m_function;
INT32 m_taskID;
#endif
UINT32 m_stackSize;
INT32 m_priority;
bool HandleError(STATUS results);
DISALLOW_COPY_AND_ASSIGN(NTTask);
};
#endif // __vxworks
#endif // __TASK_H__

View File

@@ -0,0 +1,341 @@
#ifndef _NETWORKTABLE_H_
#define _NETWORKTABLE_H_
class NetworkTable;
class NetworkTableKeyCache;
class EntryCache;
#include <map>
#include <string>
#include "tables/ITable.h"
#include "tables/IRemote.h"
#include "networktables2/thread/NTThreadManager.h"
#include "networktables2/NetworkTableEntry.h"
#include "networktables2/util/StringCache.h"
#include "networktables/NetworkTableProvider.h"
#include "networktables/NetworkTableMode.h"
#include "networktables2/thread/DefaultThreadManager.h"
#include "networktables/NetworkTableConnectionListenerAdapter.h"
#include "networktables/NetworkTableListenerAdapter.h"
#include "tables/IRemoteConnectionListener.h"
using namespace std;
class NetworkTableKeyCache: public StringCache{
private:
const std::string path;
public:
NetworkTableKeyCache(std::string path);
~NetworkTableKeyCache();
std::string Calc(const std::string& key);
};
class EntryCache {
private:
map<std::string, NetworkTableEntry*> cache;
std::string& path;
public:
EntryCache(std::string& path);
~EntryCache();
NetworkTableEntry* Get(std::string& key);
};
class NetworkTable : public ITable, IRemote {
private:
static DefaultThreadManager threadManager;
static NetworkTableProvider* staticProvider;
static NetworkTableNode* staticNode;
static void* streamFactory;
static NetworkTableEntryTypeManager* typeManager;
static NetworkTableMode* mode;
static int port;
static std::string ipAddress;
static NTReentrantSemaphore STATIC_LOCK;
std::string path;
EntryCache entryCache;
NetworkTableKeyCache absoluteKeyCache;
NetworkTableProvider& provider;
NetworkTableNode& node;
NTReentrantSemaphore LOCK;
map<IRemoteConnectionListener*, NetworkTableConnectionListenerAdapter*> connectionListenerMap;
multimap<ITableListener*, ITableListener*> listenerMap;
static void CheckInit();
NetworkTableEntry* GetEntry(std::string key);
public:
static const char PATH_SEPARATOR_CHAR;
/**
* The path separator for sub-tables and keys
*
*/
static const std::string PATH_SEPARATOR;
/**
* The default port that network tables operates on
*/
static const int DEFAULT_PORT;
/**
* @throws IOException
*/
static void Initialize();
static void Shutdown();
/**
* set the table provider for static network tables methods
* This must be called before getTable
*/
static void SetTableProvider(NetworkTableProvider* provider);
/**
* set that network tables should be a client
* This must be called before initalize or GetTable
*/
static void SetClientMode();
/**
* set that network tables should be a server
* This must be called before initalize or GetTable
*/
static void SetServerMode();
/**
* set the team the robot is configured for (this will set the ip address that network tables will connect to in client mode)
* This must be called before initalize or GetTable
* @param team the team number
*/
static void SetTeam(int team);
/**
* @param address the adress that network tables will connect to in client mode
*/
static void SetIPAddress(const char* address);
/**
* Gets the table with the specified key. If the table does not exist, a new table will be created.<br>
* This will automatically initialize network tables if it has not been already
*
* @param key
* the key name
* @return the network table requested
*/
static NetworkTable* GetTable(std::string key);
NetworkTable(std::string path, NetworkTableProvider& provider);
virtual ~NetworkTable();
bool IsConnected();
bool IsServer();
void AddConnectionListener(IRemoteConnectionListener* listener, bool immediateNotify);
void RemoveConnectionListener(IRemoteConnectionListener* listener);
void AddTableListener(ITableListener* listener);
void AddTableListener(ITableListener* listener, bool immediateNotify);
void AddTableListener(std::string key, ITableListener* listener, bool immediateNotify);
void AddSubTableListener(ITableListener* listener);
void RemoveTableListener(ITableListener* listener);
/**
* Returns the table at the specified key. If there is no table at the
* specified key, it will create a new table
*
* @param key
* the key name
* @return the networktable to be returned
*/
NetworkTable* GetSubTable(std::string key);
/**
* Checks the table and tells if it contains the specified key
*
* @param key
* the key to be checked
*/
bool ContainsKey(std::string key);
bool ContainsSubTable(std::string key);
/**
* Maps the specified key to the specified value in this table. The key can
* not be null. The value can be retrieved by calling the get method with a
* key that is equal to the original key.
*
* @param key
* the key
* @param value
* the value
*/
void PutNumber(std::string key, double value);
/**
* Returns the key that the name maps to.
*
* @param key
* the key name
* @return the key
* @throws TableKeyNotDefinedException
* if the specified key is null
*/
double GetNumber(std::string key);
/**
* Returns the key that the name maps to. If the key is null, it will return
* the default value
*
* @param key
* the key name
* @param defaultValue
* the default value if the key is null
* @return the key
*/
double GetNumber(std::string key, double defaultValue);
/**
* Maps the specified key to the specified value in this table. The key can
* not be null. The value can be retrieved by calling the get method with a
* key that is equal to the original key.
*
* @param key
* the key
* @param value
* the value
*/
void PutString(std::string key, std::string value);
/**
* Returns the key that the name maps to.
*
* @param key
* the key name
* @return the key
* @throws TableKeyNotDefinedException
* if the specified key is null
*/
std::string GetString(std::string key);
/**
* Returns the key that the name maps to. If the key is null, it will return
* the default value
*
* @param key
* the key name
* @param defaultValue
* the default value if the key is null
* @return the key
*/
std::string GetString(std::string key, std::string defaultValue);
/**
* Maps the specified key to the specified value in this table. The key can
* not be null. The value can be retrieved by calling the get method with a
* key that is equal to the original key.
*
* @param key
* the key
* @param value
* the value
*/
void PutBoolean(std::string key, bool value);
/**
* Returns the key that the name maps to.
*
* @param key
* the key name
* @return the key
* @throws TableKeyNotDefinedException
* if the specified key is null
*/
bool GetBoolean(std::string key);
/**
* Returns the key that the name maps to. If the key is null, it will return
* the default value
*
* @param key
* the key name
* @param defaultValue
* the default value if the key is null
* @return the key
*/
bool GetBoolean(std::string key, bool defaultValue);
void PutValue(std::string key, NetworkTableEntryType* type, EntryValue value);
void RetrieveValue(std::string key, ComplexData& externalValue);
/**
* Maps the specified key to the specified value in this table. The key can
* not be null. The value can be retrieved by calling the get method with a
* key that is equal to the original key.
*
* @param key the key name
* @param value the value to be put
*/
void PutValue(std::string key, ComplexData& value);
/**
* Returns the key that the name maps to.
* NOTE: If the value is a double, it will return a Double object,
* not a primitive. To get the primitive, use GetDouble
*
* @param key
* the key name
* @return the key
* @throws TableKeyNotDefinedException
* if the specified key is null
*/
EntryValue GetValue(std::string key);
/**
* Returns the key that the name maps to. If the key is null, it will return
* the default value
* NOTE: If the value is a double, it will return a Double object,
* not a primitive. To get the primitive, use GetDouble
*
* @param key
* the key name
* @param defaultValue
* the default value if the key is null
* @return the key
*/
EntryValue GetValue(std::string key, EntryValue defaultValue);
};
#endif

View File

@@ -0,0 +1,31 @@
/*
* NetworkTableConnectionListenerAdapter.h
*
* Created on: Oct 17, 2012
* Author: Mitchell Wills
*/
#ifndef NETWORKTABLECONNECTIONLISTENERADAPTER_H_
#define NETWORKTABLECONNECTIONLISTENERADAPTER_H_
class NetworkTableConnectionListenerAdapter;
#include "tables/IRemote.h"
#include "tables/IRemoteConnectionListener.h"
class NetworkTableConnectionListenerAdapter : public IRemoteConnectionListener{
private:
IRemote* targetSource;
IRemoteConnectionListener* targetListener;
public:
NetworkTableConnectionListenerAdapter(IRemote* targetSource, IRemoteConnectionListener* targetListener);
virtual ~NetworkTableConnectionListenerAdapter();
void Connected(IRemote* remote);
void Disconnected(IRemote* remote);
};
#endif /* NETWORKTABLECONNECTIONLISTENERADAPTER_H_ */

View File

@@ -0,0 +1,34 @@
/*
* NetworkTableKeyListenerAdapter.h
*
* Created on: Oct 17, 2012
* Author: Mitchell Wills
*/
#ifndef NETWORKTABLEKEYLISTENERADAPTER_H_
#define NETWORKTABLEKEYLISTENERADAPTER_H_
class NetworkTableKeyListenerAdapter;
#include "tables/ITableListener.h"
#include "tables/ITable.h"
#include <string>
#include "networktables/NetworkTable.h"
class NetworkTableKeyListenerAdapter : public ITableListener{
private:
std::string relativeKey;
std::string fullKey;
NetworkTable* targetSource;
ITableListener* targetListener;
public:
NetworkTableKeyListenerAdapter(std::string relativeKey, std::string fullKey, NetworkTable* targetSource, ITableListener* targetListener);
virtual ~NetworkTableKeyListenerAdapter();
void ValueChanged(ITable* source, const std::string& key, EntryValue value, bool isNew);
};
#endif /* NETWORKTABLEKEYLISTENERADAPTER_H_ */

View File

@@ -0,0 +1,30 @@
/*
* NetworkTableListenerAdapter.h
*
* Created on: Oct 17, 2012
* Author: Mitchell Wills
*/
#ifndef NETWORKTABLELISTENERADAPTER_H_
#define NETWORKTABLELISTENERADAPTER_H_
class NetworkTableListenerAdapter;
#include "tables/ITableListener.h"
#include "tables/ITable.h"
#include <string>
class NetworkTableListenerAdapter : public ITableListener{
private:
std::string prefix;
ITable* targetSource;
ITableListener* targetListener;
public:
NetworkTableListenerAdapter(std::string prefix, ITable* targetSource, ITableListener* targetListener);
virtual ~NetworkTableListenerAdapter();
void ValueChanged(ITable* source, const std::string& key, EntryValue value, bool isNew);
};
#endif /* NETWORKTABLELISTENERADAPTER_H_ */

View File

@@ -0,0 +1,50 @@
#ifndef _NETWORKTABLEMODE_H_
#define _NETWORKTABLEMODE_H_
class NetworkTableMode;
class NetworkTableServerMode;
class NetworkTableClientMode;
#include <string>
#include "networktables2/NetworkTableNode.h"
#include "networktables2/thread/NTThreadManager.h"
typedef void (*StreamDeleter)(void*);
/**
*
* Represents a different modes that network tables can be configured in
*
* @author Mitchell
*
*/
class NetworkTableMode {
public:
/**
* @param ipAddress the IP address configured by the user
* @param port the port configured by the user
* @param threadManager the thread manager that should be used for threads in the node
* @return a new node that can back a network table
* @throws IOException
*/
virtual NetworkTableNode* CreateNode(const char* ipAddress, int port, NTThreadManager& threadManager, void*& streamFactory_out, StreamDeleter& streamDeleter_out, NetworkTableEntryTypeManager*& typeManager_out) = 0;
static NetworkTableServerMode Server;
static NetworkTableClientMode Client;
};
class NetworkTableServerMode : public NetworkTableMode{
public:
NetworkTableServerMode();
virtual NetworkTableNode* CreateNode(const char* ipAddress, int port, NTThreadManager& threadManager, void*& streamFactory_out, StreamDeleter& streamDeleter_out, NetworkTableEntryTypeManager*& typeManager_out);
};
class NetworkTableClientMode : public NetworkTableMode{
public:
NetworkTableClientMode();
virtual NetworkTableNode* CreateNode(const char* ipAddress, int port, NTThreadManager& threadManager, void*& streamFactory_out, StreamDeleter& streamDeleter_out, NetworkTableEntryTypeManager*& typeManager_out);
};
#endif

View File

@@ -0,0 +1,48 @@
#ifndef _NETWORKTABLEPROVIDER_H_
#define _NETWORKTABLEPROVIDER_H_
class NetworkTableProvider;
#include <string>
#include "tables/ITableProvider.h"
#include "networktables2/NetworkTableNode.h"
#include "networktables/NetworkTable.h"
using namespace std;
class NetworkTableProvider : public ITableProvider
{
private:
NetworkTableNode& node;
map<std::string, NetworkTable*> tables;
/**
* Create a new NetworkTableProvider for a given NetworkTableNode
* @param node the node that handles the actual network table
*/
public:
NetworkTableProvider(NetworkTableNode& node);
virtual ~NetworkTableProvider();
ITable* GetRootTable();
ITable* GetTable(std::string key);
/**
* @return the Network Table node that backs the Tables returned by this provider
*/
NetworkTableNode& GetNode() {
return node;
};
/**
* close the backing network table node
*/
void Close() {
node.Close();
};
};
#endif

View File

@@ -0,0 +1,38 @@
/*
* NetworkTableSubListenerAdapter.h
*
* Created on: Oct 17, 2012
* Author: Mitchell Wills
*/
#ifndef NETWORKTABLESUBLISTENERADAPTER_H_
#define NETWORKTABLESUBLISTENERADAPTER_H_
class NetworkTableSubListenerAdapter;
#include "tables/ITableListener.h"
#include "tables/ITable.h"
#include <string>
#include <set>
#include "networktables/NetworkTable.h"
using namespace std;
class NetworkTableSubListenerAdapter : public ITableListener{
private:
std::string& prefix;
NetworkTable* targetSource;
ITableListener* targetListener;
set<std::string> notifiedTables;
public:
NetworkTableSubListenerAdapter(std::string& prefix, NetworkTable* targetSource, ITableListener* targetListener);
virtual ~NetworkTableSubListenerAdapter();
void ValueChanged(ITable* source, const std::string& key, EntryValue value, bool isNew);
};
#endif /* NETWORKTABLESUBLISTENERADAPTER_H_ */

View File

@@ -0,0 +1,78 @@
/*
* AbstractNetworkTableEntryStore.h
*
* Created on: Sep 16, 2012
* Author: Mitchell Wills
*/
#ifndef ABSTRACTNETWORKTABLEENTRYSTORE_H_
#define ABSTRACTNETWORKTABLEENTRYSTORE_H_
class TableListenerManager;
class AbstractNetworkTableEntryStore;
#include "OSAL/Synchronized.h"
#include <string>
#include "networktables2/NetworkTableEntry.h"
#include "networktables2/IncomingEntryReceiver.h"
#include "networktables2/OutgoingEntryReceiver.h"
#include "networktables2/type/NetworkTableEntryType.h"
#include "tables/ITable.h"
#include "tables/ITableListener.h"
#include <map>
#include <vector>
class TableListenerManager{
public:
virtual ~TableListenerManager(){}
virtual void FireTableListeners(std::string& name, EntryValue value, bool isNew) = 0;
};
class AbstractNetworkTableEntryStore : public IncomingEntryReceiver{
protected:
std::map<EntryId,NetworkTableEntry*> idEntries;
std::map<std::string,NetworkTableEntry*> namedEntries;
TableListenerManager& listenerManager;
AbstractNetworkTableEntryStore(TableListenerManager& lstnManager);
OutgoingEntryReceiver* outgoingReceiver;
OutgoingEntryReceiver* incomingReceiver;
virtual bool addEntry(NetworkTableEntry* entry) = 0;
virtual bool updateEntry(NetworkTableEntry* entry, SequenceNumber sequenceNumber, EntryValue value) = 0;
public:
virtual ~AbstractNetworkTableEntryStore();
NTReentrantSemaphore LOCK;
NetworkTableEntry* GetEntry(EntryId entryId);
NetworkTableEntry* GetEntry(std::string& name);
std::vector<std::string>* keys();
void clearEntries();
void clearIds();
void SetOutgoingReceiver(OutgoingEntryReceiver* receiver);
void SetIncomingReceiver(OutgoingEntryReceiver* receiver);
void PutOutgoing(std::string& name, NetworkTableEntryType* type, EntryValue value);
void PutOutgoing(NetworkTableEntry* tableEntry, EntryValue value);
void offerIncomingAssignment(NetworkTableEntry* entry);
void offerIncomingUpdate(NetworkTableEntry* entry, EntryId sequenceNumber, EntryValue value);
void notifyEntries(ITable* table, ITableListener* listener);
};
#endif /* ABSTRACTNETWORKTABLEENTRYSTORE_H_ */

View File

@@ -0,0 +1,25 @@
/*
* FlushableOutgoingEntryReceiver.h
*
* Created on: Sep 22, 2012
* Author: Mitchell Wills
*/
#ifndef FLUSHABLEOUTGOINGENTRYRECEIVER_H_
#define FLUSHABLEOUTGOINGENTRYRECEIVER_H_
class FlushableOutgoingEntryReceiver;
#include "networktables2/OutgoingEntryReceiver.h"
class FlushableOutgoingEntryReceiver : public OutgoingEntryReceiver
{
public:
virtual ~FlushableOutgoingEntryReceiver()
{
}
virtual void flush() = 0;
virtual void ensureAlive() = 0;
};
#endif /* FLUSHABLEOUTGOINGENTRYRECEIVER_H_ */

View File

@@ -0,0 +1,24 @@
/*
* IncomingEntryReceiver.h
*
* Created on: Sep 19, 2012
* Author: Mitchell Wills
*/
#ifndef INCOMINGENTRYRECEIVER_H_
#define INCOMINGENTRYRECEIVER_H_
#include "networktables2/NetworkTableEntry.h"
#include "tables/ITable.h"
class IncomingEntryReceiver
{
public:
virtual ~IncomingEntryReceiver()
{
}
virtual void offerIncomingAssignment(NetworkTableEntry* entry) = 0;
virtual void offerIncomingUpdate(NetworkTableEntry* entry, SequenceNumber entrySequenceNumber, EntryValue value) = 0;
};
#endif /* INCOMINGENTRYRECEIVER_H_ */

View File

@@ -0,0 +1,81 @@
#ifndef NETWORKTABLEENTRY_H_
#define NETWORKTABLEENTRY_H_
#include <stdlib.h>
#include <stdio.h>
#ifndef _WRS_KERNEL
#include <stdint.h>
#endif
typedef uint16_t EntryId;
typedef uint16_t SequenceNumber;
class NetworkTableEntry;
class TableListenerManager;
#include "networktables2/connection/DataIOStream.h"
#include "networktables2/connection/NetworkTableConnection.h"
#include "networktables2/type/NetworkTableEntryType.h"
#include "networktables2/util/IllegalStateException.h"
#include <string>
#include "tables/ITable.h"
/**
* An entry in a network table
*
* @author mwills
*
*/
class NetworkTableEntry {
public:
/**
* the id that represents that an id is unknown for an entry
*/
static const EntryId UNKNOWN_ID = 0xFFFF;
/**
* the name of the entry
*/
std::string name;
NetworkTableEntry(std::string& name, NetworkTableEntryType* type, EntryValue value);
NetworkTableEntry(EntryId id, std::string& name, SequenceNumber sequenceNumber, NetworkTableEntryType* type, EntryValue value);
virtual ~NetworkTableEntry();
EntryId GetId();
EntryValue GetValue();
NetworkTableEntryType* GetType();
bool PutValue(SequenceNumber newSequenceNumber, EntryValue newValue);
void ForcePut(SequenceNumber newSequenceNumber, EntryValue newValue);
void ForcePut(SequenceNumber newSequenceNumber, NetworkTableEntryType* type, EntryValue newValue);
void MakeDirty();
void MakeClean();
bool IsDirty();
void SendValue(DataIOStream& iostream);
SequenceNumber GetSequenceNumber();
void SetId(EntryId id);
void ClearId();
void Send(NetworkTableConnection& connection);
void FireListener(TableListenerManager& listenerManager);
private:
EntryId id;
SequenceNumber sequenceNumber;
/**
* the type of the entry
*/
NetworkTableEntryType* type;
EntryValue value;
volatile bool m_isNew;
volatile bool m_isDirty;
static const SequenceNumber HALF_OF_SEQUENCE_NUMBERS = 32768;
NetworkTableEntry& operator=(const NetworkTableEntry& other);
};
#endif /* NETWORKTABLEENTRY_H_ */

View File

@@ -0,0 +1,43 @@
/*
* NetworkTableMessageType.h
*
* Created on: Sep 22, 2012
* Author: Mitchell Wills
*/
#ifndef NETWORKTABLEMESSAGETYPE_H_
#define NETWORKTABLEMESSAGETYPE_H_
/**
* The definitions of all of the protocol message types
*
* @author mwills
*
*/
enum NetworkTableMessageType{
/**
* A keep alive message that the client sends
*/
KEEP_ALIVE = 0x00,
/**
* a client hello message that a client sends
*/
CLIENT_HELLO = 0x01,
/**
* a protocol version unsupported message that the server sends to a client
*/
PROTOCOL_VERSION_UNSUPPORTED = 0x02,
SERVER_HELLO_COMPLETE = 0x03,
/**
* an entry assignment message
*/
ENTRY_ASSIGNMENT = 0x10,
/**
* a field update message
*/
FIELD_UPDATE = 0x11
};
#endif /* NETWORKTABLEMESSAGETYPE_H_ */

View File

@@ -0,0 +1,103 @@
/*
* NetworkTableNode.h
*
* Created on: Sep 24, 2012
* Author: Mitchell Wills
*/
#ifndef NETWORKTABLENODE_H_
#define NETWORKTABLENODE_H_
class NetworkTableNode;
#include "networktables2/AbstractNetworkTableEntryStore.h"
#include "networktables2/client/ClientConnectionListenerManager.h"
#include "networktables2/type/NetworkTableEntryType.h"
#include "networktables2/type/ComplexData.h"
#include "networktables2/type/ComplexEntryType.h"
#include "tables/IRemote.h"
#include <string>
#include <vector>
/**
* represents a node (either a client or a server) in a network tables 2.0
* <br>
* implementers of the class must ensure that they call {@link #init(NetworkTableTransactionPool, AbstractNetworkTableEntryStore)} before calling any other methods on this class
*
* @author Mitchell
*
*/
class NetworkTableNode : public TableListenerManager, public ClientConnectionListenerManager, public IRemote{
protected:
AbstractNetworkTableEntryStore& entryStore;
NetworkTableNode(AbstractNetworkTableEntryStore& entryStore);
public:
/**
* @return the entry store used by this node
*/
AbstractNetworkTableEntryStore& GetEntryStore();
virtual ~NetworkTableNode();
void PutBoolean(std::string& name, bool value);
bool GetBoolean(std::string& name);
void PutDouble(std::string& name, double value);
double GetDouble(std::string& name);
void PutString(std::string& name, std::string& value);
std::string& GetString(std::string& name);
void PutComplex(std::string& name, ComplexData& value);
void retrieveValue(std::string& name, ComplexData& externalData);
/**
* Put a value with a specific network table type
* @param name the name of the entry to associate with the given value
* @param type the type of the entry
* @param value the actual value of the entry
*/
void PutValue(std::string& name, NetworkTableEntryType* type, EntryValue value);
void PutValue(NetworkTableEntry* entry, EntryValue value);
EntryValue GetValue(std::string& name);
/**
* @param key the key to check for existence
* @return true if the table has the given key
*/
bool ContainsKey(std::string& key);
/**
* close all networking activity related to this node
*/
virtual void Close() = 0;
private:
std::vector<IRemoteConnectionListener*> remoteListeners;
public:
void AddConnectionListener(IRemoteConnectionListener* listener, bool immediateNotify);
void RemoveConnectionListener(IRemoteConnectionListener* listener);
void FireConnectedEvent();
void FireDisconnectedEvent();
private:
std::vector<ITableListener*> tableListeners;
public:
void AddTableListener(ITableListener* listener, bool immediateNotify);
void RemoveTableListener(ITableListener* listener);
void FireTableListeners(std::string& key, EntryValue value, bool isNew);
};
#endif /* NETWORKTABLENODE_H_ */

View File

@@ -0,0 +1,34 @@
/*
* OutgoingEntryReceiver.h
*
* Created on: Sep 19, 2012
* Author: Mitchell Wills
*/
#ifndef OUTGOINGENTRYRECEIVER_H_
#define OUTGOINGENTRYRECEIVER_H_
class OutgoingEntryReceiver;
#include "networktables2/NetworkTableEntry.h"
class NetworkTableEntry;
class OutgoingEntryReceiver
{
public:
virtual ~OutgoingEntryReceiver()
{
}
virtual void offerOutgoingAssignment(NetworkTableEntry* entry) = 0;
virtual void offerOutgoingUpdate(NetworkTableEntry* entry) = 0;
};
class OutgoingEntryReceiver_NULL_t : public OutgoingEntryReceiver {
public:
void offerOutgoingAssignment(NetworkTableEntry* entry);
void offerOutgoingUpdate(NetworkTableEntry* entry);
};
extern OutgoingEntryReceiver_NULL_t OutgoingEntryReceiver_NULL;
#endif /* OUTGOINGENTRYRECEIVER_H_ */

View File

@@ -0,0 +1,46 @@
/*
* TableKeyExistsWithDifferentTypeException.h
*
* Created on: Sep 22, 2012
* Author: Mitchell Wills
*/
#ifndef TABLEKEYEXISTSWITHDIFFERENTTYPEEXCEPTION_H_
#define TABLEKEYEXISTSWITHDIFFERENTTYPEEXCEPTION_H_
class TableKeyExistsWithDifferentTypeException;
#include <exception>
#include "networktables2/type/NetworkTableEntryType.h"
/**
* Throw to indicate that an attempt to put data to a table is illegal because
* the specified key exists with a different data type than the put data type.
*
* @author Paul Malmsten <pmalmsten@gmail.com>
*/
class TableKeyExistsWithDifferentTypeException : public std::exception {
public:
/**
* Creates a new TableKeyExistsWithDifferentTypeException
*
* @param existingKey The name of the key which exists.
* @param existingType The type of the key which exists.
*/
TableKeyExistsWithDifferentTypeException(const std::string existingKey, NetworkTableEntryType* existingType);
TableKeyExistsWithDifferentTypeException(const std::string existingKey, NetworkTableEntryType* existingType, const char* message);
const char* what(){return "TableKeyExistsWithDifferentTypeException";};
virtual ~TableKeyExistsWithDifferentTypeException() throw ();
};
#endif /* TABLEKEYEXISTSWITHDIFFERENTTYPEEXCEPTION_H_ */

View File

@@ -0,0 +1,36 @@
/*
* TransactionDirtier.h
*
* Created on: Sep 24, 2012
* Author: Mitchell Wills
*/
#ifndef TRANSACTIONDIRTIER_H_
#define TRANSACTIONDIRTIER_H_
class TransactionDirtier;
#include "networktables2/OutgoingEntryReceiver.h"
/**
* A transaction receiver that marks all Table entries as dirty in the entry store. Entries will not be passed to the continuing receiver if they are already dirty
*
* @author Mitchell
*
*/
class TransactionDirtier : public OutgoingEntryReceiver {
private:
OutgoingEntryReceiver& continuingReceiver;
public:
TransactionDirtier(OutgoingEntryReceiver& continuingReceiver);
void offerOutgoingAssignment(NetworkTableEntry* entry);
void offerOutgoingUpdate(NetworkTableEntry* entry);
};
#endif /* TRANSACTIONDIRTIER_H_ */

View File

@@ -0,0 +1,87 @@
/*
* WriteManager.h
*
* Created on: Sep 25, 2012
* Author: Mitchell Wills
*/
#ifndef WRITEMANAGER_H_
#define WRITEMANAGER_H_
class AbstractNetworkTableEntryStore;
class WriteManager;
#include "networktables2/thread/PeriodicRunnable.h"
#include "networktables2/OutgoingEntryReceiver.h"
#include "networktables2/thread/NTThread.h"
#include "networktables2/thread/NTThreadManager.h"
#include "networktables2/FlushableOutgoingEntryReceiver.h"
#include "networktables2/NetworkTableEntry.h"
#include <queue>
#include "OSAL/Synchronized.h"
/**
* A write manager is a {@link IncomingEntryReceiver} that buffers transactions and then and then dispatches them to a flushable transaction receiver that is periodically offered all queued transaction and then flushed
*
* @author Mitchell
*
*/
class WriteManager : public OutgoingEntryReceiver, public PeriodicRunnable{
private:
const static size_t queueSize = 500;
NTReentrantSemaphore transactionsLock;
NTThread* thread;
FlushableOutgoingEntryReceiver& receiver;
NTThreadManager& threadManager;
AbstractNetworkTableEntryStore& entryStore;
unsigned long keepAliveDelay;
volatile std::queue<NetworkTableEntry*>* incomingAssignmentQueue;
volatile std::queue<NetworkTableEntry*>* incomingUpdateQueue;
volatile std::queue<NetworkTableEntry*>* outgoingAssignmentQueue;
volatile std::queue<NetworkTableEntry*>* outgoingUpdateQueue;
unsigned long lastWrite;
public:
/**
* Create a new Write manager
* @param receiver
* @param threadManager
* @param transactionPool
* @param entryStore
*/
WriteManager(FlushableOutgoingEntryReceiver& receiver, NTThreadManager& threadManager, AbstractNetworkTableEntryStore& entryStore, unsigned long keepAliveDelay);
virtual ~WriteManager();
/**
* start the write thread
*/
void start();
/**
* stop the write thread
*/
void stop();
void offerOutgoingAssignment(NetworkTableEntry* entry);
void offerOutgoingUpdate(NetworkTableEntry* entry);
/**
* the periodic method that sends all buffered transactions
*/
void run();
};
#endif /* WRITEMANAGER_H_ */

View File

@@ -0,0 +1,117 @@
/*
* ClientConnectionAdapter.h
*
* Created on: Nov 2, 2012
* Author: Mitchell Wills
*/
#ifndef CLIENTCONNECTIONADAPTER_H_
#define CLIENTCONNECTIONADAPTER_H_
class ClientConnectionAdapter;
#include "networktables2/connection/ConnectionAdapter.h"
#include "networktables2/IncomingEntryReceiver.h"
#include "networktables2/FlushableOutgoingEntryReceiver.h"
#include "networktables2/client/ClientNetworkTableEntryStore.h"
#include "networktables2/stream/IOStreamFactory.h"
#include "networktables2/thread/NTThreadManager.h"
#include "networktables2/thread/NTThread.h"
#include "networktables2/client/ClientConnectionState.h"
#include "networktables2/client/ClientConnectionListenerManager.h"
#include "networktables2/connection/ConnectionMonitorThread.h"
/**
* Object that adapts messages from a server
*
* @author Mitchell
*
*/
class ClientConnectionAdapter : public ConnectionAdapter, public IncomingEntryReceiver, public FlushableOutgoingEntryReceiver{
private:
ClientNetworkTableEntryStore& entryStore;
IOStreamFactory& streamFactory;
NTThreadManager& threadManager;
ClientConnectionState* connectionState;
ClientConnectionListenerManager& connectionListenerManager;
NTReentrantSemaphore LOCK;
NetworkTableEntryTypeManager& typeManager;
NTThread* readThread;
ConnectionMonitorThread* monitor;
NetworkTableConnection* connection;
void gotoState(ClientConnectionState* newState);
bool m_IsConnectionClosed; //Keep track of when this is closed to issue reconnect
public:
/**
* @return the state of the connection
*/
ClientConnectionState* getConnectionState();
/**
* @return if the client is connected to the server
*/
bool isConnected();
/**
* Create a new ClientConnectionAdapter
* @param entryStore
* @param threadManager
* @param streamFactory
* @param transactionPool
* @param connectionListenerManager
*/
ClientConnectionAdapter(ClientNetworkTableEntryStore& entryStore, NTThreadManager& threadManager, IOStreamFactory& streamFactory, ClientConnectionListenerManager& connectionListenerManager, NetworkTableEntryTypeManager& typeManager);
virtual ~ClientConnectionAdapter();
/*
* Connection management
*/
/**
* Reconnect the client to the server (even if the client is not currently connected)
*/
void reconnect();
/**
* Close the client connection
*/
void close();
/**
* Close the connection to the server and enter the given state
* @param newState
*/
void close(ClientConnectionState* newState);
void badMessage(BadMessageException& e);
void ioException(IOException& e);
NetworkTableEntry* GetEntry(EntryId id);
bool keepAlive();
void clientHello(ProtocolVersion protocolRevision);
void protocolVersionUnsupported(ProtocolVersion protocolRevision);
void serverHelloComplete();
void offerIncomingAssignment(NetworkTableEntry* entry);
void offerIncomingUpdate(NetworkTableEntry* entry, SequenceNumber sequenceNumber, EntryValue value);
void offerOutgoingAssignment(NetworkTableEntry* entry);
void offerOutgoingUpdate(NetworkTableEntry* entry);
void flush();
void ensureAlive();
};
#endif /* CLIENTCONNECTIONADAPTER_H_ */

View File

@@ -0,0 +1,33 @@
/*
* ClientConnectionListenerManager.h
*
* Created on: Sep 24, 2012
* Author: Mitchell Wills
*/
#ifndef CLIENTCONNECTIONLISTENERMANAGER_H_
#define CLIENTCONNECTIONLISTENERMANAGER_H_
/**
* An object that manages connection listeners and fires events for other listeners
*
* @author Mitchell
*
*/
class ClientConnectionListenerManager
{
public:
virtual ~ClientConnectionListenerManager()
{
}
/**
* called when something is connected
*/
virtual void FireConnectedEvent() = 0;
/**
* called when something is disconnected
*/
virtual void FireDisconnectedEvent() = 0;
};
#endif /* CLIENTCONNECTIONLISTENERMANAGER_H_ */

View File

@@ -0,0 +1,99 @@
/*
* ClientConnectionState.h
*
* Created on: Nov 2, 2012
* Author: Mitchell Wills
*/
#ifndef CLIENTCONNECTIONSTATE_H_
#define CLIENTCONNECTIONSTATE_H_
class ClientConnectionState;
class ClientConnectionState_Error;
class ClientConnectionState_ProtocolUnsuppotedByServer;
#include <exception>
#include "networktables2/connection/NetworkTableConnection.h"
/**
* Represents a state that the client is in
*
* @author Mitchell
*
*/
class ClientConnectionState {
public:
/**
* indicates that the client is disconnected from the server
*/
static ClientConnectionState DISCONNECTED_FROM_SERVER;
/**
* indicates that the client is connected to the server but has not yet begun communication
*/
static ClientConnectionState CONNECTED_TO_SERVER;
/**
* represents that the client has sent the hello to the server and is waiting for a response
*/
static ClientConnectionState SENT_HELLO_TO_SERVER;
/**
* represents that the client is now in sync with the server
*/
static ClientConnectionState IN_SYNC_WITH_SERVER;
private:
const char* name;
protected:
ClientConnectionState(const char* name);
public:
virtual const char* toString();
};
/**
* Represents that a client received a message from the server indicating that the client's protocol revision is not supported by the server
* @author Mitchell
*
*/
class ClientConnectionState_ProtocolUnsuppotedByServer : public ClientConnectionState{
private:
const ProtocolVersion serverVersion;
public:
/**
* Create a new protocol unsupported state
* @param serverVersion
*/
ClientConnectionState_ProtocolUnsuppotedByServer(ProtocolVersion serverVersion);
/**
* @return the protocol version that the server reported it supports
*/
ProtocolVersion getServerVersion();
const char* toString();
};
/**
* Represents that the client is in an error state
*
* @author Mitchell
*
*/
class ClientConnectionState_Error : public ClientConnectionState{
private:
std::exception& e;
char msg[100];
public:
/**
* Create a new error state
* @param e
*/
ClientConnectionState_Error(std::exception& e);
/**
* @return the exception that caused the client to enter an error state
*/
std::exception& getException();
virtual const char* toString();
};
#endif /* CLIENTCONNECTIONSTATE_H_ */

View File

@@ -0,0 +1,51 @@
/*
* ClientNetworkTableEntryStore.h
*
* Created on: Nov 2, 2012
* Author: Mitchell Wills
*/
#ifndef CLIENTNETWORKTABLEENTRYSTORE_H_
#define CLIENTNETWORKTABLEENTRYSTORE_H_
class ClientNetworkTableEntryStore;
#include "networktables2/AbstractNetworkTableEntryStore.h"
#include "networktables2/NetworkTableEntry.h"
#include "OSAL/Synchronized.h"
/**
* The entry store for a {@link NetworkTableClient}
*
* @author Mitchell
*
*/
class ClientNetworkTableEntryStore : public AbstractNetworkTableEntryStore{
public:
/**
* Create a new ClientNetworkTableEntryStore
* @param transactionPool
* @param listenerManager
*/
ClientNetworkTableEntryStore(TableListenerManager& listenerManager);
virtual ~ClientNetworkTableEntryStore();
protected:
bool addEntry(NetworkTableEntry* newEntry);
bool updateEntry(NetworkTableEntry* entry, SequenceNumber sequenceNumber, EntryValue value);
public:
/**
* Send all unknown entries in the entry store to the given connection
* @param connection
* @throws IOException
*/
void sendUnknownEntries(NetworkTableConnection& connection);
};
#endif /* CLIENTNETWORKTABLEENTRYSTORE_H_ */

View File

@@ -0,0 +1,56 @@
/*
* NetworkTableClient.h
*
* Created on: Nov 3, 2012
* Author: Mitchell Wills
*/
#ifndef NETWORKTABLECLIENT_H_
#define NETWORKTABLECLIENT_H_
class NetworkTableClient;
#include "networktables2/NetworkTableNode.h"
#include "networktables2/client/ClientConnectionAdapter.h"
#include "networktables2/WriteManager.h"
#include "networktables2/TransactionDirtier.h"
/**
* A client node in NetworkTables 2.0
*
* @author Mitchell
*
*/
class NetworkTableClient : public NetworkTableNode{
private:
ClientConnectionAdapter& adapter;
WriteManager& writeManager;
TransactionDirtier* dirtier;
public:
/**
* Create a new NetworkTable Client
* @param streamFactory
* @param threadManager
* @param transactionPool
*/
NetworkTableClient(IOStreamFactory& streamFactory, NetworkTableEntryTypeManager& typeManager, NTThreadManager& threadManager);
~NetworkTableClient();
/**
* force the client to disconnect and reconnect to the server again. Will connect if the client is currently disconnected
*/
void reconnect();
void Close();
void stop();
bool IsConnected();
bool IsServer();
};
#endif /* NETWORKTABLECLIENT_H_ */

View File

@@ -0,0 +1,24 @@
/*
* BadMessageException.h
*
* Created on: Sep 16, 2012
* Author: Mitchell Wills
*/
#ifndef BADMESSAGEEXCEPTION_H_
#define BADMESSAGEEXCEPTION_H_
#include <exception>
#include <string>
class BadMessageException : public std::exception
{
public:
BadMessageException(const char* message);
~BadMessageException() throw ();
const char* what();
private:
std::string message;
};
#endif /* BADMESSAGEEXCEPTION_H_ */

View File

@@ -0,0 +1,46 @@
/*
* ConnectionAdapter.h
*
* Created on: Sep 16, 2012
* Author: Mitchell Wills
*/
#ifndef CONNECTIONADAPTER_H_
#define CONNECTIONADAPTER_H_
class ConnectionAdapter;
#include "networktables2/NetworkTableEntry.h"
#include "networktables2/connection/BadMessageException.h"
#include "networktables2/util/IOException.h"
#include "tables/ITable.h"
class ConnectionAdapter
{
public:
virtual ~ConnectionAdapter()
{
}
//returns true if the connection should still be alive
virtual bool keepAlive() = 0;
virtual void clientHello(ProtocolVersion protocolRevision) = 0;
virtual void serverHelloComplete() = 0;
virtual void protocolVersionUnsupported(ProtocolVersion protocolRevision) = 0;
virtual void offerIncomingAssignment(NetworkTableEntry* newEntry) = 0;
virtual void offerIncomingUpdate(NetworkTableEntry* newEntry, SequenceNumber sequenceNumber, EntryValue value) = 0;
virtual NetworkTableEntry* GetEntry(EntryId) = 0;
/**
* called if a bad message exception is thrown
* @param e
*/
virtual void badMessage(BadMessageException& e) = 0;
/**
* called if an io exception is thrown
* @param e
*/
virtual void ioException(IOException& e) = 0;
};
#endif /* CONNECTIONADAPTER_H_ */

View File

@@ -0,0 +1,45 @@
/*
* ConnectionMonitorThread.h
*
* Created on: Sep 22, 2012
* Author: Mitchell Wills
*/
#ifndef CONNECTIONMONITORTHREAD_H_
#define CONNECTIONMONITORTHREAD_H_
class ConnectionMonitorThread;
#include "networktables2/connection/ConnectionAdapter.h"
#include "networktables2/connection/NetworkTableConnection.h"
#include "networktables2/thread/PeriodicRunnable.h"
/**
* A periodic thread that repeatedly reads from a connection
* @author Mitchell
*
*/
class ConnectionMonitorThread : public PeriodicRunnable{
private:
ConnectionAdapter& adapter;
NetworkTableConnection& connection;
public:
/**
* create a new monitor thread
* @param adapter
* @param connection
*/
ConnectionMonitorThread(ConnectionAdapter& adapter, NetworkTableConnection& connection);
//This can be used it identify which connection adapter instance the thread procedure is running (read-only)
const NetworkTableConnection *GetNetworkTableConnection() const {return &connection;}
void run();
};
#endif /* CONNECTIONMONITORTHREAD_H_ */

View File

@@ -0,0 +1,51 @@
#ifndef DATAIOSTREAM_H_
#define DATAIOSTREAM_H_
#include <stdlib.h>
#include "networktables2/stream/IOStream.h"
#include <exception>
#include <string>
#ifndef _WRS_KERNEL
#include <stdint.h>
#endif
#include <stdlib.h>
#include <memory>
//WindRiver is 3.4 GCC. Recent GCC can do 0b01010101 style format, WR cannot
#define TO_HEX__(n) 0x##n##LU // LU for unsigned long
#define BINARY_LITERAL_VIA_HEX__(n) (((n & 0x00000001LU) ? 1 : 0)\
+ ((n & 0x00000010LU) ? 2 : 0) \
+ ((n & 0x00000100LU) ? 4 : 0) \
+ ((n & 0x00001000LU) ? 8 : 0) \
+ ((n & 0x00010000LU) ? 16 : 0) \
+ ((n & 0x00100000LU) ? 32 : 0) \
+ ((n & 0x01000000LU) ? 64 : 0) \
+ ((n & 0x10000000LU) ? 128 : 0))
#define b(n) ((unsigned char)BINARY_LITERAL_VIA_HEX__(TO_HEX__(n)))
class DataIOStream{
public:
DataIOStream(IOStream* stream);
virtual ~DataIOStream();
void writeByte(uint8_t b);
void write2BytesBE(uint16_t s);
void writeString(std::string& str);
void flush();
uint8_t readByte();
uint16_t read2BytesBE();
std::string* readString();
void close();
void SetIOStream(IOStream* stream);
//private:
IOStream *iostream;
};
#endif

View File

@@ -0,0 +1,57 @@
/**
* An abstraction for the NetworkTable protocol
*
* @author mwills
*
*/
#ifndef NETWORK_TABLE_CONNECTION_H_
#define NETWORK_TABLE_CONNECTION_H_
#include <stdio.h>
#include <stdlib.h>
#include "OSAL/Synchronized.h"
#ifndef _WRS_KERNEL
#include <stdint.h>
#endif
class NetworkTableConnection;
typedef uint16_t ProtocolVersion;
#include "networktables2/connection/DataIOStream.h"
#include "networktables2/NetworkTableEntry.h"
#include "networktables2/type/NetworkTableEntryType.h"
#include "networktables2/type/NetworkTableEntryTypeManager.h"
#include "networktables2/connection/ConnectionAdapter.h"
#include "networktables2/NetworkTableMessageType.h"
class NetworkTableConnection{
public:
static const ProtocolVersion PROTOCOL_REVISION = 0x0200;
NetworkTableConnection(IOStream* stream, NetworkTableEntryTypeManager& typeManager);
~NetworkTableConnection();
void close();
void flush();
void sendKeepAlive();
void sendClientHello();
void sendServerHelloComplete();
void sendProtocolVersionUnsupported();
void sendEntryAssignment(NetworkTableEntry& entry);
void sendEntryUpdate(NetworkTableEntry& entry);
void read(ConnectionAdapter& adapter);
void SetIOStream(IOStream* stream);
private:
NTReentrantSemaphore WRITE_LOCK;
DataIOStream* const ioStream;
NetworkTableEntryTypeManager& typeManager;
bool isValid;
void sendMessageHeader(NetworkTableMessageType messageType);
};
#endif

View File

@@ -0,0 +1,67 @@
/*
* NetworkTableServer.h
*
* Created on: Sep 27, 2012
* Author: Mitchell Wills
*/
#ifndef NETWORKTABLESERVER_H_
#define NETWORKTABLESERVER_H_
class NetworkTableServer;
#include "networktables2/TransactionDirtier.h"
#include "networktables2/NetworkTableNode.h"
#include "networktables2/server/ServerIncomingStreamMonitor.h"
#include "networktables2/server/ServerIncomingConnectionListener.h"
#include "networktables2/WriteManager.h"
#include "networktables2/stream/IOStreamProvider.h"
#include "networktables2/server/ServerConnectionList.h"
/**
* A server node in NetworkTables 2.0
*
* @author Mitchell
*
*/
class NetworkTableServer : public NetworkTableNode, public ServerIncomingConnectionListener{
private:
IOStreamProvider& streamProvider;
ServerIncomingStreamMonitor incomingStreamMonitor;
ServerConnectionList connectionList;
WriteManager writeManager;
TransactionDirtier continuingReceiver;
public:
/**
* Create a NetworkTable Server
*
* @param streamProvider
* @param threadManager
* @param transactionPool
*/
NetworkTableServer(IOStreamProvider& streamProvider, NetworkTableEntryTypeManager& typeManager, NTThreadManager& threadManager);
~NetworkTableServer();
/**
* Create a NetworkTable Server
*
* @param streamProvider
*/
NetworkTableServer(IOStreamProvider& streamProvider);
void Close();
void OnNewConnection(ServerConnectionAdapter& connectionAdapter);
bool IsConnected();
bool IsServer();
};
#endif /* NETWORKTABLESERVER_H_ */

View File

@@ -0,0 +1,34 @@
/*
* ServerAdapterManager.h
*
* Created on: Sep 26, 2012
* Author: Mitchell Wills
*/
#ifndef SERVERADAPTERMANAGER_H_
#define SERVERADAPTERMANAGER_H_
class ServerAdapterManager;
#include "networktables2/server/ServerConnectionAdapter.h"
/**
* A class that manages connections to a server
*
* @author Mitchell
*
*/
class ServerAdapterManager
{
public:
virtual ~ServerAdapterManager()
{
}
/**
* Called when a connection adapter has been closed
* @param connectionAdapter the adapter that was closed
*/
virtual void close(ServerConnectionAdapter& connectionAdapter, bool closeStream) = 0;
};
#endif /* SERVERADAPTERMANAGER_H_ */

View File

@@ -0,0 +1,102 @@
/*
* ServerConnectionAdapter.h
*
* Created on: Sep 26, 2012
* Author: Mitchell Wills
*/
#ifndef SERVERCONNECTIONADAPTER_H_
#define SERVERCONNECTIONADAPTER_H_
class ServerConnectionAdapter;
#include "networktables2/connection/ConnectionMonitorThread.h"
#include "networktables2/NetworkTableEntry.h"
#include "networktables2/connection/ConnectionAdapter.h"
#include "networktables2/stream/IOStream.h"
#include "networktables2/IncomingEntryReceiver.h"
#include "networktables2/FlushableOutgoingEntryReceiver.h"
#include "networktables2/server/ServerNetworkTableEntryStore.h"
#include "networktables2/server/ServerAdapterManager.h"
#include "networktables2/server/ServerConnectionState.h"
#include "networktables2/thread/NTThread.h"
#include "networktables2/thread/NTThreadManager.h"
/**
* Object that adapts messages from a client to the server
*
* @author Mitchell
*
*/
class ServerConnectionAdapter : public ConnectionAdapter, public IncomingEntryReceiver, public FlushableOutgoingEntryReceiver{
private:
ServerNetworkTableEntryStore& entryStore;
IncomingEntryReceiver& transactionReceiver;
ServerAdapterManager& adapterListener;
public:
/**
* the connection this adapter uses
*/
NetworkTableConnection connection;
private:
NTThread* readThread;
ConnectionMonitorThread monitorThread;
private:
ServerConnectionState* connectionState;
void gotoState(ServerConnectionState* newState);
bool m_IsAdapterListenerClosed;
public:
/**
* Create a server connection adapter for a given stream
*
* @param stream
* @param transactionPool
* @param entryStore
* @param transactionReceiver
* @param adapterListener
* @param threadManager
*/
ServerConnectionAdapter(IOStream* stream, ServerNetworkTableEntryStore& entryStore, IncomingEntryReceiver& transactionReceiver, ServerAdapterManager& adapterListener, NetworkTableEntryTypeManager& typeManager, NTThreadManager& threadManager);
virtual ~ServerConnectionAdapter();
/**
* stop the read thread and close the stream
*/
void shutdown(bool closeStream);
void offerOutgoingAssignment(NetworkTableEntry* entry);
void offerOutgoingUpdate(NetworkTableEntry* entry);
void flush();
/**
* @return the state of the connection
*/
ServerConnectionState* getConnectionState();
void ensureAlive();
//return true once the close has been issued
bool IsAdapterListenerClosed() const {return m_IsAdapterListenerClosed;}
ConnectionAdapter &AsConnectionAdapter() {return *this;}
protected: //from ConnectionAdapter
bool keepAlive();
void badMessage(BadMessageException& e);
void ioException(IOException& e);
void clientHello(ProtocolVersion protocolRevision);
void protocolVersionUnsupported(ProtocolVersion protocolRevision);
void serverHelloComplete();
void offerIncomingAssignment(NetworkTableEntry* entry);
void offerIncomingUpdate(NetworkTableEntry* entry, SequenceNumber sequenceNumber, EntryValue value);
NetworkTableEntry* GetEntry(EntryId id);
};
#endif /* SERVERCONNECTIONADAPTER_H_ */

View File

@@ -0,0 +1,59 @@
/*
* ServerConnectionList.h
*
* Created on: Sep 26, 2012
* Author: Mitchell Wills
*/
#ifndef SERVERCONNECTIONLIST_H_
#define SERVERCONNECTIONLIST_H_
class ServerConnectionList;
class ServerIncomingStreamMonitor;
#include "networktables2/FlushableOutgoingEntryReceiver.h"
#include "networktables2/NetworkTableEntry.h"
#include "networktables2/server/ServerAdapterManager.h"
#include "networktables2/server/ServerConnectionAdapter.h"
#include <vector>
/**
* A list of connections that the server currently has
*
* @author Mitchell
*
*/
class ServerConnectionList : public FlushableOutgoingEntryReceiver, public ServerAdapterManager{
private:
NTReentrantSemaphore connectionsLock;
std::vector<ServerConnectionAdapter*> connections;
ServerIncomingStreamMonitor * const m_Factory; //make call to close connection
public:
ServerConnectionList(ServerIncomingStreamMonitor *Factory);
virtual ~ServerConnectionList();
/**
* Add a connection to the list
* @param connection
*/
void add(ServerConnectionAdapter& connection);
void close(ServerConnectionAdapter& connectionAdapter, bool closeStream);
/**
* close all connections and remove them
*/
void closeAll();
void offerOutgoingAssignment(NetworkTableEntry* entry);
void offerOutgoingUpdate(NetworkTableEntry* entry);
void flush();
void ensureAlive();
};
#endif /* SERVERCONNECTIONLIST_H_ */

View File

@@ -0,0 +1,75 @@
/*
* ServerConnectionState.h
*
* Created on: Sep 26, 2012
* Author: Mitchell Wills
*/
#ifndef SERVERCONNECTIONSTATE_H_
#define SERVERCONNECTIONSTATE_H_
class ServerConnectionState;
class ServerConnectionState_Error;
#include <exception>
/**
* Represents the state of a connection to the server
*
* @author Mitchell
*
*/
class ServerConnectionState
{
public:
/**
* represents that the server has received the connection from the client but has not yet received the client hello
*/
static ServerConnectionState GOT_CONNECTION_FROM_CLIENT;
/**
* represents that the client is in a connected non-error state
*/
static ServerConnectionState CONNECTED_TO_CLIENT;
/**
* represents that the client has disconnected from the server
*/
static ServerConnectionState CLIENT_DISCONNECTED;
private:
const char* name;
protected:
ServerConnectionState(const char* name);
public:
virtual const char* toString();
virtual ~ServerConnectionState()
{
}
};
/**
* Represents that the client is in an error state
*
* @author Mitchell
*
*/
class ServerConnectionState_Error : public ServerConnectionState
{
private:
std::exception& e;
public:
/**
* Create a new error state
* @param e
*/
ServerConnectionState_Error(std::exception& e);
virtual const char* toString();
/**
* @return the exception that caused the client connection to enter an error state
*/
std::exception& getException();
};
#endif /* SERVERCONNECTIONSTATE_H_ */

View File

@@ -0,0 +1,32 @@
/*
* ServerIncomingConnectionListener.h
*
* Created on: Sep 26, 2012
* Author: Mitchell Wills
*/
#ifndef SERVERINCOMINGCONNECTIONLISTENER_H_
#define SERVERINCOMINGCONNECTIONLISTENER_H_
#include "networktables2/server/ServerConnectionAdapter.h"
/**
* Listener for new incoming server connections
* @author Mitchell
*
*/
class ServerIncomingConnectionListener
{
public:
virtual ~ServerIncomingConnectionListener()
{
}
/**
*
* Called on create of a new connection
* @param connectionAdapter the server connection adapter
*/
virtual void OnNewConnection(ServerConnectionAdapter& connectionAdapter) = 0;
};
#endif /* SERVERINCOMINGCONNECTIONLISTENER_H_ */

View File

@@ -0,0 +1,79 @@
/*
* ServerIncomingStreamMonitor.h
*
* Created on: Sep 26, 2012
* Author: Mitchell Wills
*/
#ifndef SERVERINCOMINGSTREAMMONITOR_H_
#define SERVERINCOMINGSTREAMMONITOR_H_
class ServerIncomingStreamMonitor;
#include "networktables2/thread/PeriodicRunnable.h"
#include "networktables2/thread/NTThreadManager.h"
#include "networktables2/thread/NTThread.h"
#include "networktables2/stream/IOStreamProvider.h"
#include "networktables2/server/ServerIncomingConnectionListener.h"
#include "networktables2/server/ServerNetworkTableEntryStore.h"
#include "networktables2/server/ServerAdapterManager.h"
#include "networktables2/server/ServerConnectionAdapter.h"
/**
* Thread that monitors for incoming connections
*
* @author Mitchell
*
*/
class ServerIncomingStreamMonitor : PeriodicRunnable{
private:
IOStreamProvider& streamProvider;
ServerNetworkTableEntryStore& entryStore;
ServerIncomingConnectionListener& incomingListener;
ServerAdapterManager& adapterListener;
NetworkTableEntryTypeManager& typeManager;
NTThreadManager& threadManager;
NTThread* monitorThread;
NTReentrantSemaphore BlockDeletionList;
std::vector<ServerConnectionAdapter *> m_DeletionList;
public:
/**
* Create a new incoming stream monitor
* @param streamProvider the stream provider to retrieve streams from
* @param entryStore the entry store for the server
* @param transactionPool transaction pool for the server
* @param incomingListener the listener that is notified of new connections
* @param adapterListener the listener that will listen to adapter events
* @param threadManager the thread manager used to create the incoming thread and provided to the Connection Adapters
*/
ServerIncomingStreamMonitor(IOStreamProvider& streamProvider, ServerNetworkTableEntryStore& entryStore,
ServerIncomingConnectionListener& incomingListener,
ServerAdapterManager& adapterListener,
NetworkTableEntryTypeManager& typeManager, NTThreadManager& threadManager);
~ServerIncomingStreamMonitor();
/**
* Start the monitor thread
*/
void start();
/**
* Stop the monitor thread
*/
void stop();
void run();
void close(ServerConnectionAdapter *Adapter);
};
#endif /* SERVERINCOMINGSTREAMMONITOR_H_ */

View File

@@ -0,0 +1,55 @@
/*
* ServerNetworkTableEntryStore.h
*
* Created on: Sep 26, 2012
* Author: Mitchell Wills
*/
#ifndef SERVERNETWORKTABLEENTRYSTORE_H_
#define SERVERNETWORKTABLEENTRYSTORE_H_
class ServerNetworkTableEntryStore;
#include "networktables2/AbstractNetworkTableEntryStore.h"
#include "networktables2/NetworkTableEntry.h"
#include "OSAL/Synchronized.h"
/**
* The entry store for a {@link NetworkTableServer}
*
* @author Mitchell
*
*/
class ServerNetworkTableEntryStore : public AbstractNetworkTableEntryStore{
private:
EntryId nextId;
public:
/**
* Create a new Server entry store
* @param transactionPool the transaction pool
* @param listenerManager the listener manager that fires events from this entry store
*/
ServerNetworkTableEntryStore(TableListenerManager& listenerManager);
virtual ~ServerNetworkTableEntryStore();
protected:
bool addEntry(NetworkTableEntry* newEntry);
bool updateEntry(NetworkTableEntry* entry, SequenceNumber sequenceNumber, EntryValue value);
public:
/**
* Send all entries in the entry store as entry assignments in a single transaction
* @param connection
* @throws IOException
*/
void sendServerHello(NetworkTableConnection& connection);
};
#endif /* SERVERNETWORKTABLEENTRYSTORE_H_ */

View File

@@ -0,0 +1,36 @@
/*
* FDIOStream.h
*
* Created on: Sep 27, 2012
* Author: Mitchell Wills
*/
#ifndef FDIOSTREAM_H_
#define FDIOSTREAM_H_
class FDIOStream;
#include "networktables2/stream/IOStream.h"
#include <stdio.h>
class FDIOStream : public IOStream{
private:
//FILE* f;
int fd;
public:
FDIOStream(int fd);
virtual ~FDIOStream();
int read(void* ptr, int numbytes);
int write(const void* ptr, int numbytes);
void flush();
void close();
};
#endif /* FDIOSTREAM_H_ */

View File

@@ -0,0 +1,25 @@
/*
* IOStream.h
*
* Created on: Sep 27, 2012
* Author: Mitchell Wills
*/
#ifndef IOSTREAM_H_
#define IOSTREAM_H_
class IOStream;
class IOStream
{
public:
virtual ~IOStream()
{
}
virtual int read(void* ptr, int numbytes) = 0;
virtual int write(const void* ptr, int numbytes) = 0;
virtual void flush() = 0;
virtual void close() = 0;
};
#endif /* IOSTREAM_H_ */

View File

@@ -0,0 +1,32 @@
/*
* IOStreamFactory.h
*
* Created on: Sep 22, 2012
* Author: Mitchell Wills
*/
#ifndef IOSTREAMFACTORY_H_
#define IOSTREAMFACTORY_H_
#include "networktables2/stream/IOStream.h"
/**
* A factory that will create the same IOStream. A stream returned by this factory should be closed before calling createStream again
*
* @author Mitchell
*
*/
class IOStreamFactory
{
public:
virtual ~IOStreamFactory()
{
}
/**
* @return create a new stream
* @throws IOException
*/
virtual IOStream* createStream() = 0;
};
#endif /* IOSTREAMFACTORY_H_ */

View File

@@ -0,0 +1,38 @@
/*
* IOStreamProvider.h
*
* Created on: Sep 22, 2012
* Author: Mitchell Wills
*/
#ifndef IOSTREAMPROVIDER_H_
#define IOSTREAMPROVIDER_H_
#include "networktables2/stream/IOStream.h"
/**
* An object that will provide the IOStream of clients to a NetworkTable Server
*
* @author mwills
*
*/
class IOStreamProvider
{
public:
virtual ~IOStreamProvider()
{
}
/**
*
* @return a new IOStream normally from a server
* @throws IOException
*/
virtual IOStream* accept() = 0;
/**
* Close the source of the IOStreams. {@link #accept()} should not be called after this is called
* @throws IOException
*/
virtual void close() = 0;
};
#endif /* IOSTREAMPROVIDER_H_ */

View File

@@ -0,0 +1,33 @@
/*
* SocketServerStreamProvider.h
*
* Created on: Sep 27, 2012
* Author: Mitchell Wills
*/
#ifndef SOCKETSERVERSTREAMPROVIDER_H_
#define SOCKETSERVERSTREAMPROVIDER_H_
class SocketServerStreamProvider;
#include "networktables2/stream/IOStream.h"
#include "networktables2/stream/IOStreamProvider.h"
class SocketServerStreamProvider : public IOStreamProvider{
private:
int serverSocket;
public:
SocketServerStreamProvider(int port);
virtual ~SocketServerStreamProvider();
IOStream* accept();
void close();
};
#endif /* SOCKETSERVERSTREAMPROVIDER_H_ */

View File

@@ -0,0 +1,36 @@
/*
* SocketStreamFactory.h
*
* Created on: Nov 3, 2012
* Author: Mitchell Wills
*/
#ifndef SOCKETSTREAMFACTORY_H_
#define SOCKETSTREAMFACTORY_H_
class SocketStreamFactory;
#include "networktables2/stream/IOStreamFactory.h"
#include <string>
/**
*
* @author mwills
*
*/
class SocketStreamFactory : public IOStreamFactory{
private:
const char* host;
const int port;
public:
SocketStreamFactory(const char* host, int port);
virtual ~SocketStreamFactory();
IOStream* createStream();
};
#endif /* SOCKETSTREAMFACTORY_H_ */

View File

@@ -0,0 +1,48 @@
/*
* SocketStreams.h
*
* Created on: Sep 27, 2012
* Author: Mitchell Wills
*/
#ifndef SOCKETSTREAMS_H_
#define SOCKETSTREAMS_H_
class SocketStreams;
#include "networktables2/stream/IOStreamFactory.h"
#include "networktables2/stream/IOStreamProvider.h"
/**
* Static factory for socket stream factories and providers
*
* @author Mitchell
*
*/
class SocketStreams {
public:
/**
* Create a new IOStream factory
* @param host
* @param port
* @return a IOStreamFactory that will create Socket Connections on the given host and port
* @throws IOException
*/
static IOStreamFactory& newStreamFactory(const char* host, int port);
/**
* Create a new IOStream provider
* @param port
* @return an IOStreamProvider for a socket server on the given port
* @throws IOException
*/
static IOStreamProvider& newStreamProvider(int port);
};
#endif /* SOCKETSTREAMS_H_ */

View File

@@ -0,0 +1,55 @@
/*
* DefaultThreadManager.h
* Desktop
*
* Created on: Sep 21, 2012
* Author: Mitchell Wills
*/
#ifndef DEFAULTTHREADMANAGER_H_
#define DEFAULTTHREADMANAGER_H_
class DefaultThreadManager;
class PeriodicNTThread;
#include "networktables2/thread/PeriodicRunnable.h"
#include "networktables2/thread/NTThreadManager.h"
#include "networktables2/thread/NTThread.h"
#if (defined __vxworks || defined WIN32)
#include "OSAL/Task.h"
#else
#include <pthread.h>
#endif
class DefaultThreadManager : public NTThreadManager{
public:
virtual NTThread* newBlockingPeriodicThread(PeriodicRunnable* r, const char* name);
};
class PeriodicNTThread : public NTThread {
private:
#if (defined __vxworks || defined WIN32)
const char* name;
NTTask* thread;
#else
pthread_t thread;
#endif
PeriodicRunnable* r;
bool run;
#if (defined __vxworks || defined WIN32)
int _taskMain();
static int taskMain(PeriodicNTThread* o);
#else//TODO make return int for pthread as well
void _taskMain();
static void* taskMain(PeriodicNTThread* o);
#endif
public:
PeriodicNTThread(PeriodicRunnable* r, const char* name);
virtual ~PeriodicNTThread();
virtual void stop();
virtual bool isRunning();
};
#endif /* DEFAULTTHREADMANAGER_H_ */

View File

@@ -0,0 +1,34 @@
/*
* NTThread.h
*
* Created on: Sep 21, 2012
* Author: Mitchell Wills
*/
#ifndef NTTHREAD_H_
#define NTTHREAD_H_
/**
* Represents a thread in the network tables system
* @author mwills
*
*/
class NTThread
{
public:
virtual ~NTThread()
{
}
;
/**
* stop the thread
*/
virtual void stop() = 0;
/**
* @return true if the thread is running
*/
virtual bool isRunning() = 0;
};
#endif /* NTTHREAD_H_ */

View File

@@ -0,0 +1,36 @@
/*
* NTThreadManager.h
*
* Created on: Sep 21, 2012
* Author: Mitchell Wills
*/
#ifndef NTTHREADMANAGER_H_
#define NTTHREADMANAGER_H_
class NTThreadManager;
#include "networktables2/thread/NTThread.h"
#include "networktables2/thread/PeriodicRunnable.h"
/**
* A thread manager that can be used to obtain new threads
*
* @author Mitchell
*
*/
class NTThreadManager
{
public:
virtual ~NTThreadManager()
{
}
/**
* @param r
* @param name the name of the thread
* @return a thread that will run the provided runnable repeatedly with the assumption that the runnable will block
*/
virtual NTThread* newBlockingPeriodicThread(PeriodicRunnable* r, const char* name) = 0;
};
#endif /* NTTHREADMANAGER_H_ */

View File

@@ -0,0 +1,31 @@
/*
* PeriodicRunnable.h
*
* Created on: Sep 21, 2012
* Author: Mitchell Wills
*/
#ifndef PERIODICRUNNABLE_H_
#define PERIODICRUNNABLE_H_
/**
* A runnable where the run method will be called periodically
*
* @author Mitchell
*
*/
class PeriodicRunnable
{
public:
virtual ~PeriodicRunnable()
{
}
/**
* the method that will be called periodically on a thread
*
* @throws InterruptedException thrown when the thread is supposed to be interrupted and stop (implementers should always let this exception fall through)
*/
virtual void run() = 0;
};
#endif /* PERIODICRUNNABLE_H_ */

View File

@@ -0,0 +1,88 @@
/*
* ArrayData.h
*
* Created on: Nov 14, 2012
* Author: Mitchell Wills
*/
#ifndef ARRAYDATA_H_
#define ARRAYDATA_H_
class ArrayData;
#include "networktables2/type/ArrayEntryType.h"
#include "networktables2/type/ComplexData.h"
#include "networktables2/NetworkTableEntry.h"
/**
* Defines the internal representation for an array.
*/
class ArrayData : public ComplexData{
private:
ArrayEntryType& m_data_type;
unsigned int m_size;
EntryValue* data;
public:
/**
* Creates a new ArrayData of the given type.
*
* @param type The ArrayEntryType representing the type
* information that this ArrayData should satisfy.
*/
ArrayData(ArrayEntryType& type);
virtual ~ArrayData();
protected:
/**
* Gets the value stored at the specified index.
*
* @param index The array index to retrieve.
*/
EntryValue _get(unsigned int index);
/**
* Updates the value stored at the specified index.
*
* The value currently stored at the given index is deleted.
*
* @param index The array index to update.
* @param value The value to store. This value must have
* the same type as the ArrayEntryType indicates.
*/
void _set(unsigned int index, EntryValue value);
/**
* Appends the given value to the end of this array.
*
* @param value The value to store. This value must have
* the same type as the ArrayEntryType indicates.
*/
void _add(EntryValue value);
public:
/**
* Removes and deletes the value stored at the specified index.
*
* @param index The index of the value to remove.
*/
void remove(unsigned int index);
/**
* Sets the new size of this array data structure.
*
* @param size The new size that this array should assume.
*/
void setSize(unsigned int size);
/**
* Gets the current size of this array data structure.
*
* @return The current number of elements that this array may contain.
*/
unsigned int size();
friend class ArrayEntryType;
};
#endif /* ARRAYDATA_H_ */

View File

@@ -0,0 +1,113 @@
/*
* ArrayEntryType.h
*
* Created on: Nov 14, 2012
* Author: Mitchell Wills
*/
#ifndef ARRAYENTRYTYPE_H_
#define ARRAYENTRYTYPE_H_
#include <stdlib.h>
#include <stdio.h>
#ifndef _WRS_KERNEL
#include <stdint.h>
#endif
class ArrayEntryType;
#include "networktables2/type/ArrayData.h"
#include "networktables2/type/ComplexEntryType.h"
struct ArrayEntryData{
uint8_t length;
EntryValue* array;
};
/**
* Represents the size and contents of an array.
*/
typedef struct ArrayEntryData ArrayEntryData;
/**
* Represents the type of an array entry value.
*/
class ArrayEntryType : public ComplexEntryType {//TODO allow for array of complex type
private:
NetworkTableEntryType& m_elementType;
public:
/**
* Creates a new ArrayEntryType.
*
* @param id The ID which identifies this type to other nodes on
* across the network.
* @param elementType The type of the elements this array contains.
*/
ArrayEntryType(TypeId id, NetworkTableEntryType& elementType);
/**
* Creates a copy of an value which is of the type contained by
* this array.
*
* @param value The element, of this array's contained type, to
* copy.
* @return A copy of the given value.
*/
EntryValue copyElement(EntryValue value);
/**
* Deletes a entry value which is of the type contained by
* this array.
*
* After calling this method, the given entry value is
* no longer valid.
*
* @param value The value to delete.
*/
void deleteElement(EntryValue value);
/**
* Compares two elements of the type of the array
*
* @return true if the elements are equal
*/
bool areElementsEqual(EntryValue v1, EntryValue v2);
/**
* See {@link NetworkTableEntryType}::sendValue
*/
void sendValue(EntryValue value, DataIOStream& os);
/**
* See {@link NetworkTableEntryType}::readValue
*/
EntryValue readValue(DataIOStream& is);
/**
* See {@link NetworkTableEntryType}::copyValue
*/
EntryValue copyValue(EntryValue value);
/**
* See {@link NetworkTableEntryType}::deleteValue
*/
void deleteValue(EntryValue value);
bool areEqual(EntryValue v1, EntryValue v2);
/**
* See {@link ComplexEntryType}::internalizeValue
*/
EntryValue internalizeValue(std::string& key, ComplexData& externalRepresentation, EntryValue currentInteralValue);
/**
* See {@link ComplexEntryType}::exportValue
*/
void exportValue(std::string& key, EntryValue internalData, ComplexData& externalRepresentation);
};
#endif /* ARRAYENTRYTYPE_H_ */

View File

@@ -0,0 +1,31 @@
/*
* BooleanArray.h
*
* Created on: Nov 16, 2012
* Author: Mitchell Wills
*/
#ifndef BOOLEANARRAY_H_
#define BOOLEANARRAY_H_
#include "networktables2/type/ArrayData.h"
#include "networktables2/type/ArrayEntryType.h"
//TODO: BooleanArray appears unused; replace with namespace?
class BooleanArray : public ArrayData{
public:
static const TypeId BOOLEAN_ARRAY_RAW_ID;
static ArrayEntryType TYPE;
BooleanArray();
bool get(int index);
void set(int index, bool value);
void add(bool value);
};
#endif /* BOOLEANARRAY_H_ */

View File

@@ -0,0 +1,43 @@
/*
* ComplexData.h
*
* Created on: Sep 24, 2012
* Author: Mitchell Wills
*/
#ifndef COMPLEXDATA_H_
#define COMPLEXDATA_H_
class ComplexData;
//#include "networktables2/type/ComplexEntryType.h" can't do this cause it causes order of definition issues
class ComplexEntryType;
/**
* Base class for non-primitive data structures.
*/
class ComplexData{
private:
ComplexEntryType& type;
public:
/**
* Creates a new ComplexData of the given type.
*
* @param type The type of this data structure.
*/
ComplexData(ComplexEntryType& type);
/**
* Gets the type of this data structure.
*
* @return The type of this data structure.
*/
ComplexEntryType& GetType();
virtual ~ComplexData(){};
};
#endif /* COMPLEXDATA_H_ */

View File

@@ -0,0 +1,54 @@
/*
* ComplexEntryType.h
*
* Created on: Sep 24, 2012
* Author: Mitchell Wills
*/
#ifndef COMPLEXENTRYTYPE_H_
#define COMPLEXENTRYTYPE_H_
class ComplexEntryType;
#include "networktables2/type/NetworkTableEntryType.h"
/**
* Represents a non-primitive data type (i.e. not a string, double,
* or boolean).
*/
class ComplexEntryType : public NetworkTableEntryType
{
protected:
ComplexEntryType(TypeId id, const char* name);
public:
virtual ~ComplexEntryType()
{
}
/**
* See {@link NetworkTableEntryType}::isComplex.
*/
virtual bool isComplex();
/**
* Updates the internal representation for an entry of this type with the
* given value.
*
* @param key The name of the field to update.
* @param externalRepresentation The existing data structure to update.
* @param currentInternalValue The value to update the external representation with.
*/
virtual EntryValue internalizeValue(std::string& key, ComplexData& externalRepresentation, EntryValue currentInteralValue) = 0;
/**
* Updates the given external representation for an entry of this type with
* the given internal value.
*
* @param key The name of the field to export.
* @param internalData The current value to reference.
* @param externalRepresentation The external representation to update.
*/
virtual void exportValue(std::string& key, EntryValue internalData, ComplexData& externalRepresentation) = 0;
};
#endif /* COMPLEXENTRYTYPE_H_ */

View File

@@ -0,0 +1,106 @@
/*
* DefaultEntryTypes.h
*
* Created on: Sep 24, 2012
* Author: Mitchell Wills
*/
#ifndef DEFAULTENTRYTYPES_H_
#define DEFAULTENTRYTYPES_H_
class DefaultEntryTypes;
#include "networktables2/type/NetworkTableEntryTypeManager.h"
static const TypeId BOOLEAN_RAW_ID = 0x00;
static const TypeId DOUBLE_RAW_ID = 0x01;
static const TypeId STRING_RAW_ID = 0x02;
class DefaultEntryTypes{
private:
/**
* a boolean entry type
*/
class BOOLEAN_t : public NetworkTableEntryType{
public:
BOOLEAN_t();
/**
* See {@link NetworkTableEntryType}::sendValue
*/
virtual void sendValue(EntryValue value, DataIOStream& os);
/**
* See {@link NetworkTableEntryType}::readValue
*/
virtual EntryValue readValue(DataIOStream& is);
virtual bool areEqual(EntryValue v1, EntryValue v2);
};
/**
* a double floating point entry type
*/
class DOUBLE_t : public NetworkTableEntryType{
public:
DOUBLE_t();
/**
* See {@link NetworkTableEntryType}::sendValue
*/
virtual void sendValue(EntryValue value, DataIOStream& os);
/**
* See {@link NetworkTableEntryType}::sendValue
*/
virtual EntryValue readValue(DataIOStream& is);
virtual bool areEqual(EntryValue v1, EntryValue v2);
};
/**
* a string entry type
*/
class STRING_t : public NetworkTableEntryType{
public:
STRING_t();
/**
* See {@link NetworkTableEntryType}::sendValue
*/
virtual void sendValue(EntryValue value, DataIOStream& os);
/**
* See {@link NetworkTableEntryType}::sendValue
*/
virtual EntryValue readValue(DataIOStream& is);
/**
* See {@link NetworkTableEntryType}::copyValue
*/
virtual EntryValue copyValue(EntryValue value);
/**
* See {@link NetworkTableEntryType}::deleteValue
*/
virtual void deleteValue(EntryValue value);
virtual bool areEqual(EntryValue v1, EntryValue v2);
};
public:
/**
* Registers the set of default entry types with the given
* entry type manager.
*
* @param typeManager The entry type manager to update. Pointer must not be null.
*/
static void registerTypes(NetworkTableEntryTypeManager* typeManager);
static BOOLEAN_t BOOLEAN;
static DOUBLE_t DOUBLE;
static STRING_t STRING;
};
#endif /* DEFAULTENTRYTYPES_H_ */

View File

@@ -0,0 +1,102 @@
/*
* NetworkTableEntryType.h
*
* Created on: Sep 16, 2012
* Author: Mitchell Wills
*/
#ifndef NETWORKTABLEENTRYTYPE_H_
#define NETWORKTABLEENTRYTYPE_H_
#include <stdlib.h>
#include <stdio.h>
#ifndef _WRS_KERNEL
#include <stdint.h>
#endif
/**
* An ID which identifies a type to other nodes on the network.
*/
typedef uint8_t TypeId;
class NetworkTableEntryType;
#define MAX_NUM_TABLE_ENTRY_TYPES 256
#include <string>
#include "networktables2/connection/DataIOStream.h"
#include "networktables2/NetworkTableEntry.h"
#include "tables/ITable.h"
/**
* Represents the type associated with a network tables value.
*/
class NetworkTableEntryType{
public:
const TypeId id;
const char* name;
/**
* Determines whether this data type is complex (i.e. it does not
* represent a number, string, or boolean).
*
* @return True of this type is complex. False otherwise.
*/
virtual bool isComplex();
/**
* Serializes the given value on the given output stream.
*
* @param value The value to serialize.
* @param os The output stream to use.
*/
virtual void sendValue(EntryValue value, DataIOStream& os) = 0;
/**
* Unserializes the a value of this type from the given
* input stream.
*
* @param is The input stream to read from.
* @return The unserialized value.
*/
virtual EntryValue readValue(DataIOStream& is) = 0;
/**
* Creates a copy of the given value of this type.
*
* @param value The value to copy.
* @return A copy of the given value of this type.
*/
virtual EntryValue copyValue(EntryValue value);
/**
* Compares two values to determine if they are equal
* and should not push an update to other nodes
*
* @param v1
* @param v2
* @return true if the two values are equal
*/
virtual bool areEqual(EntryValue v1, EntryValue v2) = 0;
/**
* Deletes a value of this type.
*
* After calling this function, the given value is
* no longer valid.
*
* @param value The value to delete.
*/
virtual void deleteValue(EntryValue value);
virtual ~NetworkTableEntryType();
protected:
/**
* Creates a new NetworkTablesEntryType.
*
* @param id The numeric ID associated with this type. This ID
* represents this data type to other nodes on the network.
* @param name The string name associated with this type.
*/
NetworkTableEntryType(TypeId id, const char* name);
};
#endif /* NETWORKTABLEENTRYTYPE_H_ */

View File

@@ -0,0 +1,47 @@
/*
* NetworkTableEntryTypeManager.h
*
* Created on: Sep 16, 2012
* Author: Mitchell Wills
*/
#ifndef NETWORKTABLEENTRYTYPEMANAGER_H_
#define NETWORKTABLEENTRYTYPEMANAGER_H_
class NetworkTableEntryTypeManager;
#include "networktables2/type/NetworkTableEntryType.h"
class NetworkTableEntryTypeManager
{
private:
NetworkTableEntryType* entryTypes[MAX_NUM_TABLE_ENTRY_TYPES];
public:
/**
* Creates a new NetworkTableEntryTypeManager, with the default
* types registered under their respective TypeID's, and all other
* TypeID's null.
*/
NetworkTableEntryTypeManager();
/**
* Retrieves the entry type associated with the given
* type id, if one exists.
*
* @param type The identifier by which to retrieve a type.
* @return The NetworkTableEntryType associated with the given
* type identifier, if one exists. Otherwise, null.
*/
NetworkTableEntryType* GetType(TypeId type);
/**
* Registers the given NetworkTableEntryType with this type
* manager, such that is is returned by subsequent calls
* to GetType with the appropriate ID.
*
* @param type The type to register.
*/
void RegisterType(NetworkTableEntryType& type);
};
#endif /* NETWORKTABLEENTRYTYPEMANAGER_H_ */

View File

@@ -0,0 +1,31 @@
/*
* NumberArray.h
*
* Created on: Nov 16, 2012
* Author: Mitchell Wills
*/
#ifndef NUMBERARRAY_H_
#define NUMBERARRAY_H_
#include "networktables2/type/ArrayData.h"
#include "networktables2/type/ArrayEntryType.h"
//TODO: NumberArray appears unused; replace with namespace?
class NumberArray : public ArrayData{
public:
static const TypeId NUMBER_ARRAY_RAW_ID;
static ArrayEntryType TYPE;
NumberArray();
double get(int index);
void set(int index, double value);
void add(double value);
};
#endif /* NUMBERARRAY_H_ */

View File

@@ -0,0 +1,30 @@
/*
* StringArray.h
*
* Created on: Nov 16, 2012
* Author: Mitchell Wills
*/
#ifndef STRINGARRAY_H_
#define STRINGARRAY_H_
#include "networktables2/type/ArrayData.h"
#include "networktables2/type/ArrayEntryType.h"
//TODO: StringArray appears unused; replace with namespace?
class StringArray : public ArrayData{
public:
static const TypeId STRING_ARRAY_RAW_ID;
static ArrayEntryType TYPE;
StringArray();
std::string get(int index);
void set(int index, std::string value);
void add(std::string value);
};
#endif /* STRINGARRAY_H_ */

View File

@@ -0,0 +1,35 @@
/*
* EOFException.h
*
* Created on: Oct 1, 2012
* Author: Mitchell Wills
*/
#ifndef EOFEXCEPTION_H_
#define EOFEXCEPTION_H_
#include "networktables2/util/IOException.h"
/**
* Indicates that an EOF was encountered during an I/O operation,
* and therefore the operation could not be completed.
*/
class EOFException : public IOException{
public:
/**
* Creates an EOFException.
*/
EOFException();
virtual ~EOFException() throw ();
/**
* Implements {@link IOException}::isEOF()
*/
virtual bool isEOF();
};
#endif /* EOFEXCEPTION_H_ */

View File

@@ -0,0 +1,56 @@
/*
* IOException.h
*
* Created on: Oct 1, 2012
* Author: Mitchell Wills
*/
#ifndef IOEXCEPTION_H_
#define IOEXCEPTION_H_
#include <exception>
/**
* Inidcates that an unrecoverable I/O failure occured.
*/
class IOException : public std::exception{
public:
/**
* Creates a new IOException with the given message.
*
* @param message The message to associate with this exception.
*/
IOException(const char* message);
/**
* Creates a new IOException with the given message and
* error value.
*
* @param message The message to associate with this exception.
* @param errorValue The integer code to associate with this exception.
*/
IOException(const char* message, int errorValue);
/**
* Gets the message associated with this exception.
*
* @return The message associated with this exception.
*/
const char* what();
/**
* Determines whether this exception indicates that an EOF
* was encountered.
*
* @return True if this exception indicates that an EOF was encountered.
* False otherwise.
*/
virtual bool isEOF();
virtual ~IOException() throw ();
private:
const char* message;
int errorValue;
};
#endif /* IOEXCEPTION_H_ */

View File

@@ -0,0 +1,23 @@
/*
* IllegalStateException.h
*
* Created on: Sep 16, 2012
* Author: Mitchell Wills
*/
#ifndef ILLEGALSTATEEXCEPTION_H_
#define ILLEGALSTATEEXCEPTION_H_
#include <exception>
#include <string>
class IllegalStateException : public std::exception{
public:
IllegalStateException(const char* message);
const char* what(){return message.c_str();};
~IllegalStateException() throw ();
private:
std::string message;
};
#endif /* ILLEGALSTATEEXCEPTION_H_ */

View File

@@ -0,0 +1,39 @@
#ifndef _STRINGCACHE_H_
#define _STRINGCACHE_H_
#include <map>
#include <string>
using namespace std;
/**
* A simple cache that allows for caching the mapping of one string to another calculated one
*
* @author Mitchell
*
*/
class StringCache {
private:
map<std::string, std::string> cache;
/**
* @param input
* @return the value for a given input
*/
public:
StringCache();
virtual ~StringCache();
std::string& Get(const std::string& input);
/**
* Will only be called if a value has not already been calculated
* @param input
* @return the calculated value for a given input
*/
virtual std::string Calc(const std::string& input) = 0;
};
#endif

View File

@@ -0,0 +1,37 @@
/*
* System.h
*
* For some platform specific code related to the system
*
* Created on: Sep 25, 2012
* Author: Mitchell Wills
*/
#ifndef TIME_H_
#define TIME_H_
/**
* Causes the current thread to sleep at least the
* given number of milliseconds.
*
* @param ms The number of milliseconds to sleep.
*/
void sleep_ms(unsigned long ms);
/**
* Retrieves the current time in milliseconds.
*
* @return The current time in milliseconds.
*/
unsigned long currentTimeMillis();
/**
* Writes a warning message to the standard error
* stream.
*
* @param message The string message to write.
*/
void writeWarning(const char* message);
#endif /* TIME_H_ */

View File

@@ -0,0 +1,55 @@
/*
* IRemote.h
*
* Created on: Sep 22, 2012
* Author: Mitchell Wills
*/
#ifndef IREMOTE_H_
#define IREMOTE_H_
class IRemote;
#include "tables/IRemoteConnectionListener.h"
/**
* Represents an object that has a remote connection
*
* @author Mitchell
*
*/
class IRemote {
public:
/**
* Register an object to listen for connection and disconnection events
*
* @param listener the listener to be register
* @param immediateNotify if the listener object should be notified of the current connection state
*/
virtual void AddConnectionListener(IRemoteConnectionListener* listener, bool immediateNotify) = 0;
/**
* Unregister a listener from connection events
*
* @param listener the listener to be unregistered
*/
virtual void RemoveConnectionListener(IRemoteConnectionListener* listener) = 0;
/**
* Get the current state of the objects connection
* @return the current connection state
*/
virtual bool IsConnected() = 0;
/**
* If the object is acting as a server
* @return if the object is a server
*/
virtual bool IsServer() = 0;
};
#endif /* IREMOTE_H_ */

View File

@@ -0,0 +1,40 @@
/*
* IRemoteConnectionListener.h
*
* Created on: Sep 22, 2012
* Author: Mitchell Wills
*/
#ifndef IREMOTECONNECTIONLISTENER_H_
#define IREMOTECONNECTIONLISTENER_H_
class IRemoteConnectionListener;
#include "tables/IRemote.h"
/**
* A listener that listens for connection changes in a {@link IRemote} object
*
* @author Mitchell
*
*/
class IRemoteConnectionListener {
public:
/**
* Called when an IRemote is connected
* @param remote the object that connected
*/
virtual void Connected(IRemote* remote) = 0;
/**
* Called when an IRemote is disconnected
* @param remote the object that disconnected
*/
virtual void Disconnected(IRemote* remote) = 0;
};
#endif /* IREMOTECONNECTIONLISTENER_H_ */

View File

@@ -0,0 +1,194 @@
/*
* ITable.h
*
* Created on: Sep 19, 2012
* Author: Mitchell Wills
*/
#ifndef ITABLE_H_
#define ITABLE_H_
class ITable;
union EntryValue{
void* ptr;
bool b;
double f;
};
typedef union EntryValue EntryValue;
#include <string>
#include "networktables2/type/ComplexData.h"
#include "tables/ITableListener.h"
class ITable {
public:
/**
* Determines whether the given key is in this table.
*
* @param key the key to search for
* @return true if the table as a value assigned to the given key
*/
virtual bool ContainsKey(std::string key) = 0;
/**
* Determines whether there exists a non-empty subtable for this key
* in this table.
*
* @param key the key to search for
* @return true if there is a subtable with the key which contains at least one key/subtable of its own
*/
virtual bool ContainsSubTable(std::string key) = 0;
/**
* Gets the subtable in this table for the given name.
*
* @param key the name of the table relative to this one
* @return a sub table relative to this one
*/
virtual ITable* GetSubTable(std::string key) = 0;
/**
* Gets the value associated with a key as an object
*
* @param key the key of the value to look up
* @return the value associated with the given key
* @throws TableKeyNotDefinedException if there is no value associated with the given key
*/
virtual EntryValue GetValue(std::string key) = 0;
/**
* Put a value in the table
*
* @param key the key to be assigned to
* @param value the value that will be assigned
* @throws IllegalArgumentException when the value is not supported by the table
*/
virtual void PutValue(std::string key, ComplexData& value) = 0;
virtual void RetrieveValue(std::string key, ComplexData& externalValue) = 0;
/**
* Put a number in the table
*
* @param key the key to be assigned to
* @param value the value that will be assigned
*/
virtual void PutNumber(std::string key, double value) = 0;
/**
* Gets the number associated with the given name.
*
* @param key the key to look up
* @return the value associated with the given key
* @throws TableKeyNotDefinedException if there is no value associated with the given key
*/
virtual double GetNumber(std::string key) = 0;
/**
* Gets the number associated with the given name.
*
* @param key the key to look up
* @param defaultValue the value to be returned if no value is found
* @return the value associated with the given key or the given default value if there is no value associated with the key
*/
virtual double GetNumber(std::string key, double defaultValue) = 0;
/**
* Put a std::string& in the table
*
* @param key the key to be assigned to
* @param value the value that will be assigned
*/
virtual void PutString(std::string key, std::string value) = 0;
/**
* Gets the string associated with the given name.
*
* @param key the key to look up
* @return the value associated with the given key
* @throws TableKeyNotDefinedException if there is no value associated with the given key
*/
virtual std::string GetString(std::string key) = 0;
/**
* Gets the string associated with the given name.
*
* @param key the key to look up
* @param defaultValue the value to be returned if no value is found
* @return the value associated with the given key or the given default value if there is no value associated with the key
*/
virtual std::string GetString(std::string key, std::string defaultValue) = 0;
/**
* Put a boolean in the table
*
* @param key the key to be assigned to
* @param value the value that will be assigned
*/
virtual void PutBoolean(std::string key, bool value) = 0;
/**
* Gets the boolean associated with the given name.
*
* @param key the key to look up
* @return the value associated with the given key
* @throws TableKeyNotDefinedException if there is no value associated with the given key
*/
virtual bool GetBoolean(std::string key) = 0;
/**
* Gets the boolean associated with the given name.
*
* @param key the key to look up
* @param defaultValue the value to be returned if no value is found
* @return the value associated with the given key or the given default value if there is no value associated with the key
*/
virtual bool GetBoolean(std::string key, bool defaultValue) = 0;
/**
* Add a listener for changes to the table
*
* @param listener the listener to add
*/
virtual void AddTableListener(ITableListener* listener) = 0;
/**
* Add a listener for changes to the table
*
* @param listener the listener to add
* @param immediateNotify if true then this listener will be notified of all current entries (marked as new)
*/
virtual void AddTableListener(ITableListener* listener, bool immediateNotify) = 0;
/**
* Add a listener for changes to a specific key the table
*
* @param key the key to listen for
* @param listener the listener to add
* @param immediateNotify if true then this listener will be notified of all current entries (marked as new)
*/
virtual void AddTableListener(std::string key, ITableListener* listener, bool immediateNotify) = 0;
/**
* This will immediately notify the listener of all current sub tables
*
* @param listener
*/
virtual void AddSubTableListener(ITableListener* listener) = 0;
/**
* Remove a listener from receiving table events
*
* @param listener the listener to be removed
*/
virtual void RemoveTableListener(ITableListener* listener) = 0;
};
#endif /* ITABLE_H_ */

View File

@@ -0,0 +1,42 @@
/*
* ITableListener.h
*
* Created on: Sep 19, 2012
* Author: Mitchell Wills
*/
#ifndef ITABLELISTENER_H_
#define ITABLELISTENER_H_
class ITableListener;
#include "tables/ITable.h"
/**
* A listener that listens to changes in values in a {@link ITable}
*
* @author Mitchell
*
*/
class ITableListener {
public:
virtual ~ITableListener(){};
/**
* Called when a key-value pair is changed in a {@link ITable}
* WARNING: If a new key-value is put in this method value changed will immediatly be called which could lead to recursive code
* @param source the table the key-value pair exists in
* @param key the key associated with the value that changed
* @param value the new value
* @param isNew true if the key did not previously exist in the table, otherwise it is false
*/
virtual void ValueChanged(ITable* source, const std::string& key, EntryValue value, bool isNew) = 0;
};
#endif /* ITABLELISTENER_H_ */

View File

@@ -0,0 +1,33 @@
/*
* ITableProvider.h
*
* Created on: Sep 22, 2012
* Author: Mitchell Wills
*/
#ifndef ITABLEPROVIDER_H_
#define ITABLEPROVIDER_H_
class ITableProvider;
#include "tables/ITable.h"
/**
* A simple interface to provide tables
*
* @author Mitchell
*
*/
class ITableProvider {
public:
/**
* Get a table by name
* @param name the name of the table
* @return a Table with the given name
*/
virtual ITable* GetTable(std::string name) = 0;
};
#endif /* ITABLEPROVIDER_H_ */

View File

@@ -0,0 +1,40 @@
/*
* TableKeyNotDefinedException.h
*
* Created on: Sep 22, 2012
* Author: Mitchell Wills
*/
#ifndef TABLEKEYNOTDEFINEDEXCEPTION_H_
#define TABLEKEYNOTDEFINEDEXCEPTION_H_
class TableKeyNotDefinedException;
#include <exception>
#include <string>
/**
* An exception throw when the lookup a a key-value fails in a {@link ITable}
*
* @author Mitchell
*
*/
class TableKeyNotDefinedException : public std::exception {
public:
/**
* @param key the key that was not defined in the table
*/
TableKeyNotDefinedException(const std::string key);
~TableKeyNotDefinedException() throw();
const char* what();
private:
const std::string msg;
};
#endif /* TABLEKEYNOTDEFINEDEXCEPTION_H_ */