Skip to content
Browse files

Added template stream style for super quick usage

  • Loading branch information...
1 parent 5ffceed commit e2872e535b884e9f64528cd236b7d66ee25c815b @elliotwoods committed Aug 12, 2011
Showing with 795 additions and 679 deletions.
  1. +292 −262 src/TalkyBase.cpp
  2. +97 −88 src/TalkyBase.h
  3. +25 −0 src/TalkyBuffer.cpp
  4. +28 −0 src/TalkyBuffer.h
  5. +124 −122 src/TalkyBundleMessage.cpp
  6. +13 −11 src/TalkyBundleMessage.h
  7. +157 −155 src/TalkyMessage.cpp
  8. +59 −41 src/TalkyMessage.h
View
554 src/TalkyBase.cpp
@@ -8,313 +8,343 @@
#include "TalkyBase.h"
+namespace Talky {
-///////////////////////////////////////////////////////////////
-// PUBLIC
-//
-
-TalkyBase::TalkyBase() :
-nodeType(0),
-lastConnectAttempt(0),
-isConnected(false),
-isServerBound(false)
-{
-
-}
-
-TalkyBase::~TalkyBase()
-{
- if (nodeType != 0)
- throw("You need to implement a destructor in your Talky class. Check TalkyBase::~TalkyBase() for notes");
- /*
- //implement the following in destructor of TalkyBase implementing class. Example code from ofxTalky:
-
- if (nodeType==0)
- return;
-
- stopThread(true);
-
- if (nodeType == 1)
- {
- tcpClient->close();
- delete tcpClient;
- }
-
- if (nodeType == 2)
- {
- tcpServer->close();
- delete tcpServer;
- }
-
- */
-}
+ ///////////////////////////////////////////////////////////////
+ // PUBLIC
+ //
-//CLIENT SETUP
-void TalkyBase::setup(string remoteHost, int remotePort)
-{
- if (nodeType != 0)
+ TalkyBase::TalkyBase() :
+ nodeType(0),
+ lastConnectAttempt(0),
+ isConnected(false),
+ isServerBound(false)
{
- throw(string("Already initialised as node type ") + (nodeType==1 ? string("server") : string("client")));
- return;
+
}
-
-
- _remoteHost = remoteHost;
- _remotePort = remotePort;
-
- initClient();
- beginThread();
-
- nodeType = 1;
-
-}
-//SERVER SETUP
-void TalkyBase::setup(int localPort)
-{
- if (nodeType != 0)
+ TalkyBase::~TalkyBase()
{
- throw("Already initialised as node type " + (nodeType==1 ? string("server") : string("client")));
- return;
+ if (nodeType != 0)
+ throw("You need to implement a destructor in your Talky class. Check TalkyBase::~TalkyBase() for notes");
+ /*
+ //implement the following in destructor of TalkyBase implementing class. Example code from ofxTalky:
+
+ if (nodeType==0)
+ return;
+
+ stopThread(true);
+
+ if (nodeType == 1)
+ {
+ tcpClient->close();
+ delete tcpClient;
+ }
+
+ if (nodeType == 2)
+ {
+ tcpServer->close();
+ delete tcpServer;
+ }
+
+ */
}
-
- _localPort = localPort;
-
- initServer();
- beginThread();
-
- nodeType = 2;
-
-}
-bool TalkyBase::getIsServerBound()
-{
- if (nodeType==2)
- return isServerBound;
- else
- throw("I'm not a server node, so can't possibly be bound! so why are you asking?");
- return false;
-}
-int TalkyBase::getNumClients()
-{
- if (nodeType==2)
- return getNumServerClients();
- else
- throw ("I'm not a server node, so can't possibly have clients! so why are you asking?");
- return -1;
-}
+ //CLIENT SETUP
+ void TalkyBase::setup(string remoteHost, int remotePort)
+ {
+ if (nodeType != 0)
+ {
+ throw(string("Already initialised as node type ") + (nodeType==1 ? string("server") : string("client")));
+ return;
+ }
+
+
+ _remoteHost = remoteHost;
+ _remotePort = remotePort;
+
+ initClient();
+ beginThread();
+
+ nodeType = 1;
+
+ }
-float TalkyBase::getTimeUntilNextConnectNorm()
-{
- float frac = float(clock() / CLOCKS_PER_MILLISEC - lastConnectAttempt) / float(TALKY_RECONNECT_TIME);
-
- return (frac < 1.0f ? frac : 1.0f);
-}
+ //SERVER SETUP
+ void TalkyBase::setup(int localPort)
+ {
+ if (nodeType != 0)
+ {
+ throw("Already initialised as node type " + (nodeType==1 ? string("server") : string("client")));
+ return;
+ }
+
+ _localPort = localPort;
+
+ initServer();
+ beginThread();
+
+ nodeType = 2;
+
+ }
-string TalkyBase::getRemoteHost()
-{
- if (nodeType == 1)
- return _remoteHost;
- else
- return "";
-}
+ bool TalkyBase::getIsServerBound()
+ {
+ if (nodeType==2)
+ return isServerBound;
+ else
+ throw("I'm not a server node, so can't possibly be bound! so why are you asking?");
+ return false;
+ }
-void TalkyBase::send(TalkyMessage &msg)
-{
- sendQueue.push_back(msg);
-}
+ int TalkyBase::getNumClients()
+ {
+ if (nodeType==2)
+ return getNumServerClients();
+ else
+ throw ("I'm not a server node, so can't possibly have clients! so why are you asking?");
+ return -1;
+ }
-bool TalkyBase::popMessage(unsigned int i, TalkyMessage &msg)
-{
- if (!lockThread())
- return false;
-
- if (receiveQueue.size() > 0)
+ float TalkyBase::getTimeUntilNextConnectNorm()
{
- msg = receiveQueue.front();
- receiveQueue.erase(receiveQueue.begin());
+ float frac = float(clock() / CLOCKS_PER_MILLISEC - lastConnectAttempt) / float(TALKY_RECONNECT_TIME);
- unlockThread();
- return true;
- } else {
- unlockThread();
- return false;
+ return (frac < 1.0f ? frac : 1.0f);
}
+ string TalkyBase::getRemoteHost(){
+ if (nodeType == 1)
+ return _remoteHost;
+ else
+ return "";
+ }
-}
-
-void TalkyBase::clearMessages()
-{
- while (!lockThread())
- sleep(10);
+ //-----------------------------------------------------------
- receiveQueue.clear();
+ TalkyBase& TalkyBase::operator<<(TalkyMessage& msg) {
+ send(msg);
+ return *this;
+ }
- unlockThread();
-}
+ bool TalkyBase::operator>>(TalkyMessage& msg) {
+ return popMessage(msg);
+ }
+ //-----------------------------------------------------------
+
-///////////////////////////////////////////////////////////////
-// PROTECTED
-//
+ void TalkyBase::send(TalkyMessage &msg)
+ {
+ lockThread();
+ sendQueue.push_back(msg);
+ unlockThread();
+ }
-void TalkyBase::update()
-{
- if (nodeType == 0)
- return;
-
- /////////////////////////////////////
- // CHECK RECONNECTS
- /////////////////////////////////////
- //
- int currentTime = clock() / CLOCKS_PER_MILLISEC;
- if (currentTime - lastConnectAttempt > TALKY_RECONNECT_TIME)
+ bool TalkyBase::popMessage(TalkyMessage &msg)
{
+ if (!lockThread())
+ return false;
- if (nodeType == 1)
- //client
- isConnected = isClientConnected();
- else
- //server
- isConnected = isServerConnected();
-
-
- if (!isConnected && nodeType==1)
+ if (receiveQueue.size() > 0)
{
- //client
- startClient();
+ msg = receiveQueue.front();
+ receiveQueue.erase(receiveQueue.begin());
+
+ unlockThread();
+ return true;
+ } else {
+ unlockThread();
+ return false;
}
+ }
+
+ void TalkyBase::clearMessages()
+ {
+ while (!lockThread())
+ sleep(10);
- if (!isServerBound && nodeType==2)
- {
- //server
- startServer();
- }
+ receiveQueue.clear();
- lastConnectAttempt = currentTime;
+ unlockThread();
}
+
+
+ ///////////////////////////////////////////////////////////////
+ // PROTECTED
//
- /////////////////////////////////////
-
-
-
- /////////////////////////////////////
- // RECEIVE DATA
- /////////////////////////////////////
- //
- int availableBytes;
-
- if (nodeType == 1)
- {
- if (isClientConnected())
- {
- //client
- availableBytes = rxClient(bufferIn, TALKY_BUFFER_IN_SIZE);
-
- if (availableBytes > 0)
- processIncomingBytes(availableBytes);
+
+ void TalkyBase::update()
+ {
+ if (nodeType == 0)
+ return;
+
+ /////////////////////////////////////
+ // CHECK RECONNECTS
+ /////////////////////////////////////
+ //
+ int currentTime = clock() / CLOCKS_PER_MILLISEC;
+ if (currentTime - lastConnectAttempt > TALKY_RECONNECT_TIME)
+ {
+
+ if (nodeType == 1) {
+ //client
+ if (!isConnected)
+ {
+ if (isClientConnected())
+ {
+ isConnected = true;
+ notifyClientIsNowConnected();
+ }
+ } else
+ if (!isClientConnected())
+ isConnected = false;
+ } else {
+ //server
+ isConnected = isServerConnected();
+ }
+
+
+ if (!isConnected && nodeType==1) {
+ //client
+ startClient();
+ isConnected = isClientConnected();
+
+ if (isConnected)
+ notifyClientIsNowConnected();
+ }
+
+
+ if (!isServerBound && nodeType==2) {
+ //server
+ startServer();
+ isConnected = isServerConnected();
+ }
+
+ lastConnectAttempt = currentTime;
}
+ //
+ /////////////////////////////////////
+
+
- } else {
+ /////////////////////////////////////
+ // RECEIVE DATA
+ /////////////////////////////////////
+ //
+ int availableBytes;
- //server
- if (getNumServerClients() > 0) {
+ if (nodeType == 1)
+ {
+ if (isClientConnected())
+ {
+ //client
+ availableBytes = rxClient(bufferIn, TALKY_BUFFER_IN_SIZE);
+
+ if (availableBytes > 0)
+ processIncomingBytes(availableBytes);
+ }
- if (lockServer())
- {
- for (int iRemote=0; iRemote<getNumServerClients() ; iRemote++)
+ } else {
+
+ //server
+ if (getNumServerClients() > 0) {
+
+ if (lockServer())
{
- if (!isServersClientConnected(iRemote))
- continue;
-
- availableBytes = rxServer(iRemote, bufferIn, TALKY_BUFFER_IN_SIZE);
-
- if (availableBytes > 0)
- processIncomingBytes(availableBytes);
-
+ for (int iRemote=0; iRemote<getNumServerClients() ; iRemote++)
+ {
+ if (!isServersClientConnected(iRemote))
+ continue;
+
+ availableBytes = rxServer(iRemote, bufferIn, TALKY_BUFFER_IN_SIZE);
+
+ if (availableBytes > 0)
+ processIncomingBytes(availableBytes);
+
+ }
+ unlockServer();
}
- unlockServer();
+
}
-
}
- }
-
- //
- /////////////////////////////////////
-
-
-
- /////////////////////////////////////
- // SEND DATA
- /////////////////////////////////////
- //
- if (isConnected)
- {
- int availableBytes = TALKY_BUFFER_OUT_SIZE;
- char * bufferPointer = bufferOut;
- bool hasMessage = false;
-
- vector<TalkyMessage>::iterator it;
-
- while (sendQueue.size() > 0)
- {
- //point to the one we want to deal with
- it = sendQueue.begin();
-
- //let's try to serialise
- if (!it->serialise(bufferPointer, availableBytes))
- break;
-
- //if we were able to put this message into the buffer
- //i.e. there was sufficient space in the buffer, no exceptions
- //then remove it from send message queue
- sendQueue.erase(it);
- hasMessage = true;
- }
+ //
+ /////////////////////////////////////
+
- if (hasMessage)
- if (nodeType == 1)
-
- //client
- txClient(bufferOut, TALKY_BUFFER_OUT_SIZE - availableBytes);
- else
+ /////////////////////////////////////
+ // SEND DATA
+ /////////////////////////////////////
+ //
+ if (isConnected)
+ {
+ int availableBytes = TALKY_BUFFER_OUT_SIZE;
+ char * bufferPointer = bufferOut;
+ bool hasMessage = false;
+
+ vector<TalkyMessage>::iterator it;
+
+ while (sendQueue.size() > 0)
+ {
+ //point to the one we want to deal with
+ it = sendQueue.begin();
- //server
- for (int iClient=0; iClient < getNumServerClients(); iClient++)
- txServer(iClient, bufferOut, TALKY_BUFFER_OUT_SIZE - availableBytes);
+ //let's try to serialise
+ if (!it->serialise(bufferPointer, availableBytes))
+ break;
+
+ //if we were able to put this message into the buffer
+ //i.e. there was sufficient space in the buffer, no exceptions
+ //then remove it from send message queue
+ sendQueue.erase(it);
+
+ hasMessage = true;
+ }
+
+ if (hasMessage)
+ if (nodeType == 1)
+
+ //client
+ txClient(bufferOut, TALKY_BUFFER_OUT_SIZE - availableBytes);
+
+ else
+
+ //server
+ for (int iClient=0; iClient < getNumServerClients(); iClient++)
+ txServer(iClient, bufferOut, TALKY_BUFFER_OUT_SIZE - availableBytes);
+
+ }
+ //
+ /////////////////////////////////////
+
}
- //
- /////////////////////////////////////
-
-
-}
-void TalkyBase::processIncomingBytes(int nBytes)
-{
- lockThread();
-
- char * messagePointer = bufferIn;
-
- //perhaps recode this so we dont copy
- TalkyMessage currentMessage;
-
- while (nBytes > 0 && currentMessage.deSerialise(messagePointer, nBytes))
- receiveQueue.push_back(currentMessage);
-
- sort(receiveQueue.begin(), receiveQueue.end());
-
- //trigger message available event
- //processing will be performed in this
- //thread
- int msgCount = receiveQueue.size();
- notifyReceiveEvent(msgCount);
-
- unlockThread();
-}
+ void TalkyBase::processIncomingBytes(int nBytes)
+ {
+ lockThread();
+
+ char * messagePointer = bufferIn;
+
+ //perhaps recode this so we dont copy
+ TalkyMessage currentMessage;
+
+ while (nBytes > 0 && currentMessage.deSerialise(messagePointer, nBytes))
+ receiveQueue.push_back(currentMessage);
+
+ sort(receiveQueue.begin(), receiveQueue.end());
+
+ //trigger message available event.
+ //processing will be performed in this
+ //thread
+ int msgCount = receiveQueue.size();
+ notifyReceiveEvent(msgCount);
+
+ unlockThread();
+ }
+}
View
185 src/TalkyBase.h
@@ -23,92 +23,101 @@
using namespace std;
-class TalkyBase
-{
-public:
- TalkyBase();
- virtual ~TalkyBase();
-
- ////////////////////////////////
- // MAY NEED SOME OVERRIDING
- // FOR ERROR HANDLING
- ////////////////////////////////
- //
- //setup as client
- virtual void setup(string remoteHost, int remotePort);
-
- //setup as server
- virtual void setup(int localPort);
+namespace Talky {
- virtual bool getIsServerBound();
- virtual int getNumClients();
- //
- ////////////////////////////////
-
- bool getIsConnected() { return isConnected; }
- float getTimeUntilNextConnectNorm();
-
- string getRemoteHost();
-
- void send(TalkyMessage &msg);
-
- vector<TalkyMessage> receiveQueue;
-
- bool popMessage(unsigned int i, TalkyMessage &msg);
- void clearMessages();
-
-protected:
- void update();
-
-
- ////////////////////////////////
- // INTERFACE
- ////////////////////////////////
- //
- virtual void beginThread() = 0;
-
- virtual bool lockThread() = 0;
- virtual void unlockThread() = 0;
- virtual bool lockServer() { return true; };
- virtual void unlockServer() { };
-
- virtual void initClient() = 0;
- virtual void initServer() = 0;
-
- virtual void startClient() = 0;
- virtual void startServer() = 0;
-
- virtual bool isClientConnected() = 0;
- virtual bool isServerConnected() = 0;
- virtual bool isServersClientConnected(int iClient) = 0;
-
- virtual int getNumServerClients() = 0;
-
- virtual int rxServer(int iClient, char *buffer, int bufferSize) = 0;
- virtual int rxClient(char *buffer, int bufferSize) = 0;
-
- virtual void txServer(int iClient, char *buffer, int messageSize) = 0;
- virtual void txClient(char *buffer, int messageSize) = 0;
-
- virtual void notifyReceiveEvent(int msgCount) = 0;
- //
- ////////////////////////////////
-
- vector<TalkyMessage> sendQueue;
-
- void processIncomingBytes(int nBytes);
-
- char bufferIn[TALKY_BUFFER_IN_SIZE];
- char bufferOut[TALKY_BUFFER_OUT_SIZE];
-
- int _localPort;
-
- string _remoteHost;
- int _remotePort;
-
- int nodeType; //0=none, 1=client, 2=server
-
- bool isServerBound; // have we got the port?
- bool isConnected;
- long lastConnectAttempt;
-};
+ class TalkyBase
+ {
+ public:
+ TalkyBase();
+ virtual ~TalkyBase();
+
+ ////////////////////////////////
+ // MAY NEED SOME OVERRIDING
+ // FOR ERROR HANDLING
+ ////////////////////////////////
+ //
+ //setup as client
+ virtual void setup(string remoteHost, int remotePort);
+
+ //setup as server
+ virtual void setup(int localPort);
+
+ virtual bool getIsServerBound();
+ virtual int getNumClients();
+ //
+ ////////////////////////////////
+
+ bool getIsConnected() { return isConnected; }
+ float getTimeUntilNextConnectNorm();
+
+ string getRemoteHost(); ///< Get the hostname of remote machine. Perhaps split this into 2 functions? or warn if we use this for client?
+
+ TalkyBase& operator<<(TalkyMessage& msg);
+ bool operator>>(TalkyMessage& msg);
+
+ void send(TalkyMessage &msg);
+ bool popMessage(TalkyMessage &msg);
+ void clearMessages();
+
+
+ vector<TalkyMessage> receiveQueue;
+ vector<TalkyMessage> const &getSendQueue() { return sendQueue;};
+
+
+ protected:
+ void update();
+
+
+ ////////////////////////////////
+ // INTERFACE
+ ////////////////////////////////
+ //
+ virtual void beginThread() = 0;
+
+ virtual bool lockThread() = 0;
+ virtual void unlockThread() = 0;
+ virtual bool lockServer() { return true; };
+ virtual void unlockServer() { };
+
+ virtual void initClient() = 0;
+ virtual void initServer() = 0;
+
+ virtual void startClient() = 0;
+ virtual void startServer() = 0;
+
+ virtual bool isClientConnected() = 0;
+ virtual bool isServerConnected() = 0;
+ virtual bool isServersClientConnected(int iClient) = 0;
+
+ virtual int getNumServerClients() = 0;
+
+ virtual int rxServer(int iClient, char *buffer, int bufferSize) = 0;
+ virtual int rxClient(char *buffer, int bufferSize) = 0;
+
+ virtual void txServer(int iClient, char *buffer, int messageSize) = 0;
+ virtual void txClient(char *buffer, int messageSize) = 0;
+
+ virtual void notifyClientIsNowConnected() = 0;
+ virtual void notifyReceiveEvent(int msgCount) = 0;
+ //
+ ////////////////////////////////
+
+ vector<TalkyMessage> sendQueue;
+
+ void processIncomingBytes(int nBytes);
+
+ char bufferIn[TALKY_BUFFER_IN_SIZE];
+ char bufferOut[TALKY_BUFFER_OUT_SIZE];
+
+ int _localPort;
+
+ string _remoteHost;
+ int _remotePort;
+
+ int nodeType; //0=none, 1=client, 2=server
+
+ bool isServerBound; // have we got the port?
+ bool isConnected;
+ long lastConnectAttempt;
+ };
+}
View
25 src/TalkyBuffer.cpp
@@ -0,0 +1,25 @@
+//
+// TalkyBuffer.cpp
+// Talky
+//
+// Created by Elliot Woods on 11/08/2011.
+// Copyright 2011 Kimchi and Chips. All rights reserved.
+//
+
+#include "TalkyBuffer.h"
+
+namespace Talky {
+
+ TalkyBuffer::TalkyBuffer() {
+ isAllocated = false;
+ }
+
+ TalkyBuffer::~TalkyBuffer() {
+ delete[] data;
+ }
+
+ TalkyBuffer::TalkyBuffer(BufferOffset size) {
+ data = new char[size];
+ }
+
+}
View
28 src/TalkyBuffer.h
@@ -0,0 +1,28 @@
+#pragma once
+//
+// TalkyBuffer.h
+// Talky
+//
+// Created by Elliot Woods on 11/08/2011.
+// Copyright 2011 Kimchi and Chips. All rights reserved.
+//
+
+namespace Talky {
+
+ /** BufferOffset is a Talky type.
+ We use it for defining indexes within the
+ TalkyBuffer.
+ */
+ typedef unsigned short BufferOffset;
+
+ class TalkyBuffer {
+ public:
+ TalkyBuffer();
+ ~TalkyBuffer();
+ TalkyBuffer(BufferOffset size);
+
+ protected:
+ char* data;
+ bool isAllocated;
+ };
+}
View
246 src/TalkyBundleMessage.cpp
@@ -8,130 +8,132 @@
#include "TalkyBundleMessage.h"
-TalkyMessage TalkyBundleMessage::pack()
-{
-
- ///////////////////////////////////
- // WRITE PAYLOAD
- ///////////////////////////////////
- //
-
-
- //setup message
- TalkyMessage bundleMsg;
- //
-
- //calculate payload size
- //
- //2 + sum(message + 2)
- //
- vector<TalkyMessage>::iterator it;
- unsigned short totalPayloadLength = 2;
- //
- for (it = bundle.begin(); it != bundle.end(); it++)
- totalPayloadLength += it->getPayloadLength() + 2;
- //
-
-
- //setup single master payload in message
- bundleMsg.initPayload(totalPayloadLength);
- char * tempPayload = bundleMsg.getPayload();
- //
- //nMessages
- * (unsigned short *)(tempPayload) = bundle.size();
- tempPayload += 2;
- //
-
-
- //add individual payloads to master payload
- unsigned short msgLength;
- //
- for (it = bundle.begin(); it != bundle.end(); it++)
- {
- msgLength = it->getPayloadLength();
-
- //payload length
- * (unsigned short *)(tempPayload) = msgLength;
- tempPayload += 2;
-
- //payload
- memcpy(tempPayload, it->getPayload(), msgLength);
- tempPayload += msgLength;
- }
- //
- ///////////////////////////////////
-
- return bundleMsg;
-}
+namespace Talky {
+ TalkyMessage TalkyBundleMessage::pack()
+ {
+
+ ///////////////////////////////////
+ // WRITE PAYLOAD
+ ///////////////////////////////////
+ //
+
+
+ //setup message
+ TalkyMessage bundleMsg;
+ //
+
+ //calculate payload size
+ //
+ //2 + sum(message + 2)
+ //
+ vector<TalkyMessage>::iterator it;
+ unsigned short totalPayloadLength = 2;
+ //
+ for (it = bundle.begin(); it != bundle.end(); it++)
+ totalPayloadLength += it->getPayloadLength() + 2;
+ //
+
+
+ //setup single master payload in message
+ bundleMsg.initPayload(totalPayloadLength);
+ char * tempPayload = bundleMsg.getPayload();
+ //
+ //nMessages
+ * (unsigned short *)(tempPayload) = bundle.size();
+ tempPayload += 2;
+ //
+
+
+ //add individual payloads to master payload
+ unsigned short msgLength;
+ //
+ for (it = bundle.begin(); it != bundle.end(); it++)
+ {
+ msgLength = it->getPayloadLength();
+
+ //payload length
+ * (unsigned short *)(tempPayload) = msgLength;
+ tempPayload += 2;
+
+ //payload
+ memcpy(tempPayload, it->getPayload(), msgLength);
+ tempPayload += msgLength;
+ }
+ //
+ ///////////////////////////////////
+
+ return bundleMsg;
+ }
-void TalkyBundleMessage::unpack(TalkyMessage &bundleMsg, unsigned short newType)
-{
-
- ///////////////////////////////////
- // OVERRIDE FOR SPECIFIC TYPE
- ///////////////////////////////////
- //
- if (newType == -1)
- newType = bundleMsg.ContentsType;
- //
- ///////////////////////////////////
-
-
-
- ///////////////////////////////////
- // READ PAYLOAD TO BUNDLE VECTOR
- ///////////////////////////////////
- //
- int remainingBytes;
- char * tempPayload = bundleMsg.getPayload(remainingBytes);
+ void TalkyBundleMessage::unpack(TalkyMessage &bundleMsg, unsigned short newType)
+ {
+
+ ///////////////////////////////////
+ // OVERRIDE FOR SPECIFIC TYPE
+ ///////////////////////////////////
+ //
+ if (newType == -1)
+ newType = bundleMsg.ContentsType;
+ //
+ ///////////////////////////////////
+
+
+
+ ///////////////////////////////////
+ // READ PAYLOAD TO BUNDLE VECTOR
+ ///////////////////////////////////
+ //
+ int remainingBytes;
+ char * tempPayload = bundleMsg.getPayload(remainingBytes);
-
- //count messages
- unsigned short nMessages = * (unsigned short*) tempPayload;
- tempPayload+=2;
- remainingBytes -= 2;
- //
- bundle.resize(nMessages);
- //
-
- /////////////////////////
- //set individual messages
- unsigned short msgLength;
- for (unsigned short i=0; i<nMessages; i++)
- {
- msgLength = * (unsigned short*) tempPayload;
- tempPayload += 2;
- remainingBytes -= 2;
-
- if (remainingBytes < msgLength)
- throw("TalkyMessageBundle::deSerialse : Insufficient bytes in message");
-
- //set header
- TalkyMessage pushMsg;
- //
- pushMsg.Company[0] = bundleMsg.Company[0];
- pushMsg.Company[1] = bundleMsg.Company[1];
- pushMsg.Protocol[0] = bundleMsg.Protocol[0];
- pushMsg.Protocol[1] = bundleMsg.Protocol[1];
- pushMsg.ContentsType = newType;
- pushMsg.Version = bundleMsg.Version;
- pushMsg.Timestamp = bundleMsg.Timestamp;
- //
-
- pushMsg.setPayload (tempPayload, msgLength);
- tempPayload += msgLength;
- remainingBytes -= msgLength;
-
- bundle[i] = pushMsg;
- }
- /////////////////////////
-
- //
- ///////////////////////////////////
+
+ //count messages
+ unsigned short nMessages = * (unsigned short*) tempPayload;
+ tempPayload+=2;
+ remainingBytes -= 2;
+ //
+ bundle.resize(nMessages);
+ //
+
+ /////////////////////////
+ //set individual messages
+ unsigned short msgLength;
+ for (unsigned short i=0; i<nMessages; i++)
+ {
+ msgLength = * (unsigned short*) tempPayload;
+ tempPayload += 2;
+ remainingBytes -= 2;
+
+ if (remainingBytes < msgLength)
+ throw("TalkyMessageBundle::deSerialse : Insufficient bytes in message");
+
+ //set header
+ TalkyMessage pushMsg;
+ //
+ pushMsg.Company[0] = bundleMsg.Company[0];
+ pushMsg.Company[1] = bundleMsg.Company[1];
+ pushMsg.Protocol[0] = bundleMsg.Protocol[0];
+ pushMsg.Protocol[1] = bundleMsg.Protocol[1];
+ pushMsg.ContentsType = newType;
+ pushMsg.Version = bundleMsg.Version;
+ pushMsg.Timestamp = bundleMsg.Timestamp;
+ //
+
+ pushMsg.setPayload (tempPayload, msgLength);
+ tempPayload += msgLength;
+ remainingBytes -= msgLength;
+
+ bundle[i] = pushMsg;
+ }
+ /////////////////////////
+
+ //
+ ///////////////////////////////////
-}
+ }
-void TalkyBundleMessage::push(TalkyMessage &msg)
-{
- bundle.push_back(msg);
+ void TalkyBundleMessage::push(TalkyMessage &msg)
+ {
+ bundle.push_back(msg);
+ }
}
View
24 src/TalkyBundleMessage.h
@@ -12,14 +12,16 @@
#include <vector>
using namespace std;
-class TalkyBundleMessage
-{
- public:
- TalkyMessage pack();
- void unpack(TalkyMessage &bundleMsg, unsigned short newType= -1);
-
- void push(TalkyMessage &msg);
-
- vector<TalkyMessage> bundle;
-
-};
+namespace Talky {
+ class TalkyBundleMessage
+ {
+ public:
+ TalkyMessage pack();
+ void unpack(TalkyMessage &bundleMsg, unsigned short newType= -1);
+
+ void push(TalkyMessage &msg);
+
+ vector<TalkyMessage> bundle;
+
+ };
+}
View
312 src/TalkyMessage.cpp
@@ -9,165 +9,167 @@
#include "TalkyMessage.h"
-TalkyMessage::TalkyMessage() :
-hasPayload(false),
-PayloadLength(0),
-Version(0),
-Timestamp(clock() / CLOCKS_PER_MILLISEC),
-ContentsType(0)
-{
-}
-
-bool TalkyMessage::serialise(char* &message, int &remainingAvailableBytes)
-{
-
- int len = getTotalLength();
- if (len > remainingAvailableBytes)
- return false;
-
- * (unsigned char *) message = 0;
-
- memcpy(message+1, Company, 2);
- memcpy(message+3, Protocol, 2);
-
- * (unsigned short *) (message + 5) = Version;
- * (unsigned long *) (message + 7) = Timestamp;
- * (unsigned short *) (message + 11) = ContentsType;
- * (unsigned short *) (message + 13) = PayloadLength;
-
- memcpy(message+15, Payload, PayloadLength);
-
- message[len-1] = TALKY_ENDCHAR;
-
- message += len;
- remainingAvailableBytes -= len;
-
- return true;
-}
-
-bool TalkyMessage::deSerialise(char* &message, int &remainingBytesReceived)
-{
- if (* (unsigned char *) message != 0)
- throw("TalkyMessage::deSerialise : We're trying to deserialise a non-simple message to a simple talkymessage");
-
- memcpy(Company, message+1, 2);
- memcpy(Protocol, message+3, 2);
-
- unsigned short tempLength;
- Version = * (unsigned short *) (message + 5);
- Timestamp = * (unsigned long *) (message + 7);
- ContentsType = * (unsigned short *) (message + 11);
- tempLength = * (unsigned short *) (message + 13);
-
- initPayload(tempLength);
-
- if (getTotalLength() > remainingBytesReceived)
- throw("TalkyMessage::deSerialise : We've run out of message to deserialise");
-
- memcpy(Payload, message+15, PayloadLength);
-
- if (message[getTotalLength()-1] != TALKY_ENDCHAR)
- throw("TalkyMessage::deSerialise : End charachter of message is wrong when deserialising");
-
- message += getTotalLength();
- remainingBytesReceived -= getTotalLength();
-
- return true;
-}
-
-char *TalkyMessage::getPayload(int &length) const
-{
- length = PayloadLength;
- return getPayload();
-}
-
-char *TalkyMessage::getPayload() const
-{
- return Payload;
-}
-
-void TalkyMessage::setPayload(char* const message, unsigned short length)
-{
- initPayload(length);
- memcpy(Payload, message, length);
-}
-
-int TalkyMessage::getTotalLength()
-{
- return 1 // message type
- + 2 // company
- + 2 // protocol
- + 2 // version
- + 4 // timestamp
- + 2 // contents type
- + 2 // Payload length
- + PayloadLength //Payload
- + 1; //endchar
-}
-
-unsigned short TalkyMessage::getPayloadLength()
-{
- return PayloadLength;
-}
-
-string TalkyMessage::toString()
-{
-
- stringstream out;
-
- out << "Company:\t" << string(Company, 2) << "\n";
- out << "Protocol:\t" << string(Protocol, 2) << "\n";
- out << "Version:\t" << Version << "\n";
- out << "Timestamp:\t" << Timestamp << "\n";
- out << "ContentsType:\t" << ContentsType << "\n";
- out << "PayloadLength:\t" << PayloadLength << "\n";
-
- out << "Payload:\n";
-
- for (int i=0; i<PayloadLength; i++)
+namespace Talky {
+ TalkyMessage::TalkyMessage() :
+ hasPayload(false),
+ PayloadLength(0),
+ Version(0),
+ Timestamp(clock() / CLOCKS_PER_MILLISEC),
+ ContentsType(0)
{
- if (Payload[i] > 32)
- out << Payload[i];
- else
- out << ".";
}
-
- out << "\n\n";
-
- return out.str();
-}
-
-void TalkyMessage::setCompany(const char * s)
-{
- memcpy(Company, s, 2);
-}
-
-void TalkyMessage::setProtocol(const char * s)
-{
- memcpy(Protocol, s, 2);
-}
-
-void TalkyMessage::setTimestamp()
-{
- Timestamp = clock() / CLOCKS_PER_MILLISEC;
-}
-
-bool TalkyMessage::operator<(const TalkyMessage& other) const
-{
- return Timestamp < other.Timestamp;
-}
-
-void TalkyMessage::initPayload(unsigned short length)
-{
- if (hasPayload && PayloadLength != length)
+
+ bool TalkyMessage::serialise(char* &message, int &remainingAvailableBytes)
+ {
+
+ int len = getTotalLength();
+ if (len > remainingAvailableBytes)
+ return false;
+
+ * (unsigned char *) message = 0;
+
+ memcpy(message+1, Company, 2);
+ memcpy(message+3, Protocol, 2);
+
+ * (unsigned short *) (message + 5) = Version;
+ * (unsigned long *) (message + 7) = Timestamp;
+ * (unsigned short *) (message + 11) = ContentsType;
+ * (unsigned short *) (message + 13) = PayloadLength;
+
+ memcpy(message+15, Payload, PayloadLength);
+
+ message[len-1] = TALKY_ENDCHAR;
+
+ message += len;
+ remainingAvailableBytes -= len;
+
+ return true;
+ }
+
+ bool TalkyMessage::deSerialise(char* &message, int &remainingBytesReceived)
+ {
+ if (* (unsigned char *) message != 0)
+ throw("TalkyMessage::deSerialise : We're trying to deserialise a non-simple message to a simple talkymessage");
+
+ memcpy(Company, message+1, 2);
+ memcpy(Protocol, message+3, 2);
+
+ unsigned short tempLength;
+ Version = * (unsigned short *) (message + 5);
+ Timestamp = * (unsigned long *) (message + 7);
+ ContentsType = * (unsigned short *) (message + 11);
+ tempLength = * (unsigned short *) (message + 13);
+
+ initPayload(tempLength);
+
+ if (getTotalLength() > remainingBytesReceived)
+ throw("TalkyMessage::deSerialise : We've run out of message to deserialise");
+
+ memcpy(Payload, message+15, PayloadLength);
+
+ if (message[getTotalLength()-1] != TALKY_ENDCHAR)
+ throw("TalkyMessage::deSerialise : End charachter of message is wrong when deserialising");
+
+ message += getTotalLength();
+ remainingBytesReceived -= getTotalLength();
+
+ return true;
+ }
+
+ char *TalkyMessage::getPayload(int &length) const
+ {
+ length = PayloadLength;
+ return getPayload();
+ }
+
+ char *TalkyMessage::getPayload() const
{
- delete[] Payload;
- hasPayload = false;
+ return Payload;
}
-
- if (!hasPayload)
+
+ void TalkyMessage::setPayload(void* const message, unsigned short length)
+ {
+ initPayload(length);
+ memcpy(Payload, message, length);
+ }
+
+ int TalkyMessage::getTotalLength()
+ {
+ return 1 // message type
+ + 2 // company
+ + 2 // protocol
+ + 2 // version
+ + 4 // timestamp
+ + 2 // contents type
+ + 2 // Payload length
+ + PayloadLength //Payload
+ + 1; //endchar
+ }
+
+ unsigned short TalkyMessage::getPayloadLength()
+ {
+ return PayloadLength;
+ }
+
+ string TalkyMessage::toString()
+ {
+
+ stringstream out;
+
+ out << "Company:\t" << string(Company, 2) << "\n";
+ out << "Protocol:\t" << string(Protocol, 2) << "\n";
+ out << "Version:\t" << Version << "\n";
+ out << "Timestamp:\t" << Timestamp << "\n";
+ out << "ContentsType:\t" << ContentsType << "\n";
+ out << "PayloadLength:\t" << PayloadLength << "\n";
+
+ out << "Payload:\n";
+
+ for (int i=0; i<PayloadLength; i++)
+ {
+ if (Payload[i] > 32)
+ out << Payload[i];
+ else
+ out << ".";
+ }
+
+ out << "\n\n";
+
+ return out.str();
+ }
+
+ void TalkyMessage::setCompany(const char * s)
+ {
+ memcpy(Company, s, 2);
+ }
+
+ void TalkyMessage::setProtocol(const char * s)
+ {
+ memcpy(Protocol, s, 2);
+ }
+
+ void TalkyMessage::setTimestamp()
+ {
+ Timestamp = clock() / CLOCKS_PER_MILLISEC;
+ }
+
+ bool TalkyMessage::operator<(const TalkyMessage& other) const
+ {
+ return Timestamp < other.Timestamp;
+ }
+
+ void TalkyMessage::initPayload(unsigned short length)
{
- Payload = new char[length];
- PayloadLength = length;
+ if (hasPayload && PayloadLength != length)
+ {
+ delete[] Payload;
+ hasPayload = false;
+ }
+
+ if (!hasPayload)
+ {
+ Payload = new char[length];
+ PayloadLength = length;
+ }
}
}
View
100 src/TalkyMessage.h
@@ -18,44 +18,62 @@ using namespace std;
#define CLOCKS_PER_MILLISEC (CLOCKS_PER_SEC / 1000)
#define TALKY_ENDCHAR '\n'
-
-class TalkyMessage
-{
-public:
- TalkyMessage();
-
- virtual bool serialise(char* &message, int &remainingAvailableBytes);
- virtual bool deSerialise(char* &message, int &remainingBytesReceived);
-
- char *getPayload(int &length) const;
- char *getPayload() const;
-
- void setPayload(char* const message, unsigned short length);
- int getTotalLength();
- unsigned short getPayloadLength();
- void initPayload(unsigned short length);
-
- string toString();
- void setCompany(const char * s);
- void setProtocol(const char * s);
- void setTimestamp();
-
- //for sorting by timestamp
- bool operator<(const TalkyMessage& other) const;
-
- //contents
- char Company[2];
-
- char Protocol[2];
- unsigned short Version;
- unsigned long Timestamp;
-
- unsigned short ContentsType;
-
-protected:
- unsigned short PayloadLength;
- char *Payload;
-
- bool hasPayload;
-
-};
+namespace Talky {
+ class TalkyMessage
+ {
+ public:
+ TalkyMessage();
+
+ bool serialise(char* &message, int &remainingAvailableBytes);
+ bool deSerialise(char* &message, int &remainingBytesReceived);
+
+ template <class T>
+ TalkyMessage& operator<<(T &object)
+ {
+ setPayload(&object, sizeof(T));
+ }
+
+ template <class T>
+ bool operator>>(T &object)
+ {
+ if (getPayloadLength() == sizeof(object))
+ {
+ object = *(T*)getPayload();
+ return true;
+ } else
+ return false;
+ }
+
+ char *getPayload(int &length) const;
+ char *getPayload() const;
+
+ void setPayload(void* const message, unsigned short length);
+ int getTotalLength();
+ unsigned short getPayloadLength();
+ void initPayload(unsigned short length);
+
+ string toString();
+ void setCompany(const char * s);
+ void setProtocol(const char * s);
+ void setTimestamp();
+
+ //for sorting by timestamp
+ bool operator<(const TalkyMessage& other) const;
+
+ //contents
+ char Company[2];
+
+ char Protocol[2];
+ unsigned short Version;
+ unsigned long Timestamp;
+
+ unsigned short ContentsType;
+
+ protected:
+ unsigned short PayloadLength;
+ char *Payload;
+
+ bool hasPayload;
+
+ };
+}

0 comments on commit e2872e5

Please sign in to comment.
Something went wrong with that request. Please try again.