2013-06-16 19:54:02 -04:00
|
|
|
#include "protocol_manager.hpp"
|
|
|
|
|
|
|
|
#include "protocol.hpp"
|
2013-06-17 14:06:02 -04:00
|
|
|
|
2013-06-16 19:54:02 -04:00
|
|
|
#include <assert.h>
|
2013-06-17 14:06:02 -04:00
|
|
|
#include <stdio.h>
|
2013-06-18 18:25:03 -04:00
|
|
|
#include <cstdlib>
|
2013-06-17 14:06:02 -04:00
|
|
|
|
2013-06-18 18:25:03 -04:00
|
|
|
#define RAND_MAX 65536
|
2013-06-16 19:54:02 -04:00
|
|
|
|
2013-06-17 08:48:44 -04:00
|
|
|
ProtocolManager::ProtocolManager()
|
2013-06-16 19:54:02 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ProtocolManager::~ProtocolManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtocolManager::messageReceived(uint8_t* data)
|
|
|
|
{
|
|
|
|
assert(data);
|
2013-06-17 08:48:44 -04:00
|
|
|
m_messagesToProcess.push_back(data);
|
2013-06-16 19:54:02 -04:00
|
|
|
}
|
|
|
|
|
2013-06-18 18:25:03 -04:00
|
|
|
int ProtocolManager::startProtocol(Protocol* protocol)
|
2013-06-16 19:54:02 -04:00
|
|
|
{
|
2013-06-18 11:31:04 -04:00
|
|
|
ProtocolInfo protocolInfo;
|
2013-06-18 18:25:03 -04:00
|
|
|
protocolInfo.state = PROTOCOL_STATE_RUNNING;
|
|
|
|
assignProtocolId(protocolInfo);
|
2013-06-18 11:31:04 -04:00
|
|
|
protocolInfo.protocol = protocol;
|
|
|
|
m_protocols.push_back(protocolInfo);
|
2013-06-17 08:48:44 -04:00
|
|
|
protocol->setListener(this);
|
2013-06-16 19:54:02 -04:00
|
|
|
protocol->setup();
|
|
|
|
protocol->start();
|
2013-06-18 11:31:04 -04:00
|
|
|
printf("*** PROTOCOL MANAGER *** - A new protocol has been started. There are %ld protocols running.\n", m_protocols.size());
|
2013-06-18 18:25:03 -04:00
|
|
|
return protocolInfo.id;
|
2013-06-16 19:54:02 -04:00
|
|
|
}
|
|
|
|
void ProtocolManager::stopProtocol(Protocol* protocol)
|
|
|
|
{
|
|
|
|
|
2013-06-18 11:31:04 -04:00
|
|
|
}
|
|
|
|
void ProtocolManager::pauseProtocol(Protocol* protocol)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < m_protocols.size(); i++)
|
|
|
|
{
|
2013-06-18 18:25:03 -04:00
|
|
|
if (m_protocols[i].protocol == protocol && m_protocols[i].state == PROTOCOL_STATE_RUNNING)
|
2013-06-18 11:31:04 -04:00
|
|
|
{
|
2013-06-18 18:25:03 -04:00
|
|
|
m_protocols[i].state = PROTOCOL_STATE_PAUSED;
|
2013-06-18 11:31:04 -04:00
|
|
|
m_protocols[i].protocol->pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void ProtocolManager::unpauseProtocol(Protocol* protocol)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < m_protocols.size(); i++)
|
|
|
|
{
|
2013-06-18 18:25:03 -04:00
|
|
|
if (m_protocols[i].protocol == protocol && m_protocols[i].state == PROTOCOL_STATE_PAUSED)
|
2013-06-18 11:31:04 -04:00
|
|
|
{
|
2013-06-18 18:25:03 -04:00
|
|
|
m_protocols[i].state = PROTOCOL_STATE_RUNNING;
|
2013-06-18 11:31:04 -04:00
|
|
|
m_protocols[i].protocol->unpause();
|
|
|
|
}
|
|
|
|
}
|
2013-06-16 19:54:02 -04:00
|
|
|
}
|
2013-06-17 08:48:44 -04:00
|
|
|
void ProtocolManager::protocolTerminated(Protocol* protocol)
|
|
|
|
{
|
|
|
|
int offset = 0;
|
|
|
|
for (unsigned int i = 0; i < m_protocols.size(); i++)
|
|
|
|
{
|
2013-06-18 11:31:04 -04:00
|
|
|
if (m_protocols[i-offset].protocol == protocol)
|
2013-06-17 08:48:44 -04:00
|
|
|
{
|
2013-06-18 11:31:04 -04:00
|
|
|
delete m_protocols[i].protocol;
|
2013-06-17 08:48:44 -04:00
|
|
|
m_protocols.erase(m_protocols.begin()+(i-offset), m_protocols.begin()+(i-offset)+1);
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
}
|
2013-06-18 11:31:04 -04:00
|
|
|
printf("*** PROTOCOL MANAGER *** - A protocol has been terminated. There are %ld protocols running.\n", m_protocols.size());
|
2013-06-17 08:48:44 -04:00
|
|
|
}
|
2013-06-16 19:54:02 -04:00
|
|
|
|
|
|
|
void ProtocolManager::update()
|
|
|
|
{
|
|
|
|
// before updating, notice protocols that they have received information
|
|
|
|
int size = m_messagesToProcess.size();
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
uint8_t* data = m_messagesToProcess.back();
|
|
|
|
PROTOCOL_TYPE searchedProtocol = (PROTOCOL_TYPE)(data[0]);
|
|
|
|
for (unsigned int i = 0; i < m_protocols.size() ; i++)
|
|
|
|
{
|
2013-06-18 11:31:04 -04:00
|
|
|
if (m_protocols[i].protocol->getProtocolType() == searchedProtocol) // pass data to them even when paused
|
|
|
|
m_protocols[i].protocol->messageReceived(data+1);
|
2013-06-16 19:54:02 -04:00
|
|
|
}
|
|
|
|
m_messagesToProcess.pop_back();
|
|
|
|
}
|
|
|
|
// now update all protocols
|
|
|
|
for (unsigned int i = 0; i < m_protocols.size(); i++)
|
|
|
|
{
|
2013-06-18 18:25:03 -04:00
|
|
|
if (m_protocols[i].state == PROTOCOL_STATE_RUNNING)
|
2013-06-18 11:31:04 -04:00
|
|
|
m_protocols[i].protocol->update();
|
2013-06-16 19:54:02 -04:00
|
|
|
}
|
|
|
|
}
|
2013-06-17 08:48:44 -04:00
|
|
|
|
|
|
|
int ProtocolManager::runningProtocolsCount()
|
|
|
|
{
|
|
|
|
return m_protocols.size();
|
|
|
|
}
|
|
|
|
|
2013-06-18 18:25:03 -04:00
|
|
|
void ProtocolManager::assignProtocolId(ProtocolInfo& protocolInfo)
|
|
|
|
{
|
|
|
|
uint32_t newId;
|
|
|
|
bool exists;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
newId = (rand()<<16)+rand();
|
|
|
|
exists = false;
|
|
|
|
for (unsigned int i = 0; i < m_protocols.size(); i++)
|
|
|
|
{
|
|
|
|
if (m_protocols[i].id == newId)
|
|
|
|
exists = true;
|
|
|
|
}
|
|
|
|
} while (exists);
|
|
|
|
protocolInfo.id = newId;
|
|
|
|
}
|
|
|
|
|
|
|
|
|