mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-24 01:31:46 +00:00
CMake Changes
This is the changes made by Patrick Plenefisch converting the native code to use CMake and the CMake Maven Plugin, as opposed to the native Maven plugin. This is to allow for compatibility with newer versions of the GCC toolchain. All the cpp sources were moved from maven style directories to cpp style directories for CMake. Change-Id: I67f5e3608948f37c83b0990d232105a3784f8593
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* BadMessageException.cpp
|
||||
*
|
||||
* Created on: Sep 16, 2012
|
||||
* Author: Mitchell Wills
|
||||
*/
|
||||
|
||||
#include "networktables2/connection/BadMessageException.h"
|
||||
|
||||
BadMessageException::BadMessageException(const char* msg)
|
||||
: message(msg)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
BadMessageException::~BadMessageException() throw ()
|
||||
{
|
||||
}
|
||||
|
||||
const char* BadMessageException::what()
|
||||
{
|
||||
return message.c_str();
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* ConnectionMonitorThread.cpp
|
||||
*
|
||||
* Created on: Sep 22, 2012
|
||||
* Author: Mitchell Wills
|
||||
*/
|
||||
|
||||
#include "networktables2/connection/ConnectionMonitorThread.h"
|
||||
#include "networktables2/connection/BadMessageException.h"
|
||||
#include "networktables2/util/System.h"
|
||||
|
||||
ConnectionMonitorThread::ConnectionMonitorThread(ConnectionAdapter& _adapter, NetworkTableConnection& _connection) :
|
||||
adapter(_adapter), connection(_connection) {
|
||||
}
|
||||
|
||||
void ConnectionMonitorThread::run() {
|
||||
|
||||
if (adapter.keepAlive())
|
||||
{
|
||||
try{
|
||||
connection.read(adapter);
|
||||
} catch(BadMessageException& e){
|
||||
adapter.badMessage(e);
|
||||
} catch(IOException& e){
|
||||
adapter.ioException(e);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sleep_ms(10); //avoid busy-wait
|
||||
//Test to see this working properly
|
||||
//printf("--ConnectionMonitorThread::run Waiting to close\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
#include "networktables2/connection/DataIOStream.h"
|
||||
|
||||
//TODO remove this when alloca is solved
|
||||
#ifdef WIN32
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
|
||||
///This is used in case NULL is passed so all logic calls to IOstream can continue to assume there is never a null pointer
|
||||
class InertStream : public IOStream
|
||||
{
|
||||
protected:
|
||||
virtual int read(void* ptr, int numbytes) {return numbytes;} //return success
|
||||
virtual int write(const void* ptr, int numbytes) {return numbytes;}
|
||||
virtual void flush() {}
|
||||
virtual void close() {}
|
||||
};
|
||||
|
||||
static InertStream s_InertStream;
|
||||
|
||||
DataIOStream::DataIOStream(IOStream* _iostream) :
|
||||
iostream(_iostream)
|
||||
{
|
||||
}
|
||||
DataIOStream::~DataIOStream()
|
||||
{
|
||||
close();
|
||||
if (iostream!=&s_InertStream)
|
||||
delete iostream;
|
||||
}
|
||||
|
||||
void DataIOStream::SetIOStream(IOStream* stream)
|
||||
{
|
||||
IOStream *temp=iostream;
|
||||
iostream=stream ? stream : &s_InertStream; //We'll never assign NULL
|
||||
if (temp!=&s_InertStream)
|
||||
delete temp;
|
||||
}
|
||||
|
||||
void DataIOStream::close()
|
||||
{
|
||||
iostream->close();
|
||||
}
|
||||
|
||||
void DataIOStream::writeByte(uint8_t b)
|
||||
{
|
||||
iostream->write(&b, 1);
|
||||
}
|
||||
void DataIOStream::write2BytesBE(uint16_t s)
|
||||
{
|
||||
writeByte((uint8_t)(s >> 8));
|
||||
writeByte((uint8_t)s);
|
||||
}
|
||||
void DataIOStream::writeString(std::string& str)
|
||||
{
|
||||
write2BytesBE(str.length());
|
||||
iostream->write(str.c_str(), str.length());
|
||||
}
|
||||
void DataIOStream::flush()
|
||||
{
|
||||
iostream->flush();
|
||||
}
|
||||
uint8_t DataIOStream::readByte()
|
||||
{
|
||||
uint8_t value;
|
||||
iostream->read(&value, 1);
|
||||
return value;
|
||||
}
|
||||
uint16_t DataIOStream::read2BytesBE()
|
||||
{
|
||||
uint16_t value;
|
||||
value = readByte()<<8 | readByte();
|
||||
return value;
|
||||
}
|
||||
std::string* DataIOStream::readString()
|
||||
{
|
||||
|
||||
unsigned int byteLength = read2BytesBE();
|
||||
#ifndef WIN32
|
||||
uint8_t bytes[byteLength+1];//FIXME figure out why this doesn't work on windows
|
||||
#else
|
||||
uint8_t* bytes = (uint8_t*)alloca(byteLength+1);
|
||||
#endif
|
||||
iostream->read(bytes, byteLength);
|
||||
bytes[byteLength] = 0;
|
||||
return new std::string((char*)bytes);//FIXME implement UTF-8 aware version
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
/**
|
||||
* An abstraction for the NetworkTable protocol
|
||||
*
|
||||
* @author mwills
|
||||
*
|
||||
*/
|
||||
|
||||
#include "networktables2/connection/NetworkTableConnection.h"
|
||||
#include "networktables2/connection/BadMessageException.h"
|
||||
|
||||
|
||||
NetworkTableConnection::NetworkTableConnection(IOStream* _ioStream, NetworkTableEntryTypeManager& _typeManager) :
|
||||
ioStream(new DataIOStream(_ioStream)), typeManager(_typeManager) {
|
||||
isValid = true;
|
||||
}
|
||||
NetworkTableConnection::~NetworkTableConnection(){
|
||||
delete ioStream;
|
||||
}
|
||||
|
||||
void NetworkTableConnection::SetIOStream(IOStream* stream)
|
||||
{
|
||||
ioStream->SetIOStream(stream); //just passing through
|
||||
}
|
||||
|
||||
void NetworkTableConnection::close() {
|
||||
{
|
||||
NTSynchronized sync(WRITE_LOCK);
|
||||
if (isValid) {
|
||||
isValid = false;
|
||||
ioStream->close();
|
||||
}
|
||||
}
|
||||
}
|
||||
void NetworkTableConnection::flush() {
|
||||
{
|
||||
NTSynchronized sync(WRITE_LOCK);
|
||||
ioStream->flush();
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkTableConnection::sendMessageHeader(
|
||||
NetworkTableMessageType messageType) {
|
||||
{
|
||||
NTSynchronized sync(WRITE_LOCK);
|
||||
ioStream->writeByte((uint8_t) messageType);
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkTableConnection::sendKeepAlive() {
|
||||
{
|
||||
NTSynchronized sync(WRITE_LOCK);
|
||||
sendMessageHeader(KEEP_ALIVE);
|
||||
flush();
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkTableConnection::sendClientHello() {
|
||||
{
|
||||
NTSynchronized sync(WRITE_LOCK);
|
||||
sendMessageHeader(CLIENT_HELLO);
|
||||
ioStream->write2BytesBE(PROTOCOL_REVISION);
|
||||
flush();
|
||||
}
|
||||
}
|
||||
void NetworkTableConnection::sendServerHelloComplete() {
|
||||
{
|
||||
NTSynchronized sync(WRITE_LOCK);
|
||||
sendMessageHeader(SERVER_HELLO_COMPLETE);
|
||||
flush();
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkTableConnection::sendProtocolVersionUnsupported() {
|
||||
{
|
||||
NTSynchronized sync(WRITE_LOCK);
|
||||
sendMessageHeader(PROTOCOL_VERSION_UNSUPPORTED);
|
||||
ioStream->write2BytesBE(PROTOCOL_REVISION);
|
||||
flush();
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkTableConnection::sendEntryAssignment(NetworkTableEntry& entry) {
|
||||
{
|
||||
NTSynchronized sync(WRITE_LOCK);
|
||||
sendMessageHeader(ENTRY_ASSIGNMENT);
|
||||
ioStream->writeString(entry.name);
|
||||
ioStream->writeByte(entry.GetType()->id);
|
||||
ioStream->write2BytesBE(entry.GetId());
|
||||
ioStream->write2BytesBE(entry.GetSequenceNumber());
|
||||
entry.SendValue(*ioStream);
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkTableConnection::sendEntryUpdate(NetworkTableEntry& entry) {
|
||||
{
|
||||
NTSynchronized sync(WRITE_LOCK);
|
||||
sendMessageHeader(FIELD_UPDATE);
|
||||
ioStream->write2BytesBE(entry.GetId());
|
||||
ioStream->write2BytesBE(entry.GetSequenceNumber());
|
||||
entry.SendValue(*ioStream);
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkTableConnection::read(ConnectionAdapter& adapter) {
|
||||
int messageType = ioStream->readByte();
|
||||
switch (messageType) {
|
||||
case KEEP_ALIVE:
|
||||
adapter.keepAlive();
|
||||
return;
|
||||
case CLIENT_HELLO: {
|
||||
uint16_t protocolRevision = ioStream->read2BytesBE();
|
||||
adapter.clientHello(protocolRevision);
|
||||
return;
|
||||
}
|
||||
case SERVER_HELLO_COMPLETE: {
|
||||
adapter.serverHelloComplete();
|
||||
return;
|
||||
}
|
||||
case PROTOCOL_VERSION_UNSUPPORTED: {
|
||||
uint16_t protocolRevision = ioStream->read2BytesBE();
|
||||
adapter.protocolVersionUnsupported(protocolRevision);
|
||||
return;
|
||||
}
|
||||
case ENTRY_ASSIGNMENT: {
|
||||
std::string* entryName = ioStream->readString();
|
||||
TypeId typeId = ioStream->readByte();
|
||||
NetworkTableEntryType* entryType = typeManager.GetType(typeId);
|
||||
if (!entryType){
|
||||
char exceptionMessageBuffer[50];
|
||||
sprintf (exceptionMessageBuffer, "Unknown data type: %#x", typeId);
|
||||
throw BadMessageException(exceptionMessageBuffer);
|
||||
}
|
||||
EntryId entryId = ioStream->read2BytesBE();
|
||||
SequenceNumber entrySequenceNumber = ioStream->read2BytesBE();
|
||||
EntryValue value = entryType->readValue(*ioStream);
|
||||
adapter.offerIncomingAssignment(new NetworkTableEntry(entryId, *entryName, entrySequenceNumber, entryType, value));
|
||||
entryType->deleteValue(value);
|
||||
delete entryName;
|
||||
return;
|
||||
}
|
||||
case FIELD_UPDATE: {
|
||||
EntryId entryId = ioStream->read2BytesBE();
|
||||
SequenceNumber entrySequenceNumber = ioStream->read2BytesBE();
|
||||
NetworkTableEntry* entry = adapter.GetEntry(entryId);
|
||||
if (!entry){
|
||||
char exceptionMessageBuffer[50];
|
||||
sprintf (exceptionMessageBuffer, "Received update for unknown entry id: %d", entryId);
|
||||
throw BadMessageException(exceptionMessageBuffer);
|
||||
}
|
||||
EntryValue value = entry->GetType()->readValue(*ioStream);
|
||||
|
||||
adapter.offerIncomingUpdate(entry, entrySequenceNumber, value);
|
||||
entry->GetType()->deleteValue(value);
|
||||
return;
|
||||
}
|
||||
default:
|
||||
char exceptionMessageBuffer[50];
|
||||
sprintf (exceptionMessageBuffer, "Unknown Network Table Message Type: %d", messageType);
|
||||
throw BadMessageException(exceptionMessageBuffer);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user