714a85b507
git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@12883 178a84e3-b1eb-0310-8ba1-8eac791a3b58
125 lines
3.4 KiB
C++
125 lines
3.4 KiB
C++
#include "protocol_manager.hpp"
|
|
|
|
#include "protocol.hpp"
|
|
|
|
#include <assert.h>
|
|
#include <stdio.h>
|
|
#include <cstdlib>
|
|
|
|
#define RAND_MAX 65536
|
|
|
|
ProtocolManager::ProtocolManager()
|
|
{
|
|
}
|
|
|
|
ProtocolManager::~ProtocolManager()
|
|
{
|
|
}
|
|
|
|
void ProtocolManager::messageReceived(uint8_t* data)
|
|
{
|
|
assert(data);
|
|
m_messagesToProcess.push_back(data);
|
|
}
|
|
|
|
int ProtocolManager::startProtocol(Protocol* protocol)
|
|
{
|
|
ProtocolInfo protocolInfo;
|
|
protocolInfo.state = PROTOCOL_STATE_RUNNING;
|
|
assignProtocolId(protocolInfo);
|
|
protocolInfo.protocol = protocol;
|
|
m_protocols.push_back(protocolInfo);
|
|
protocol->setListener(this);
|
|
protocol->setup();
|
|
protocol->start();
|
|
printf("*** PROTOCOL MANAGER *** - A new protocol has been started. There are %ld protocols running.\n", m_protocols.size());
|
|
return protocolInfo.id;
|
|
}
|
|
void ProtocolManager::stopProtocol(Protocol* protocol)
|
|
{
|
|
|
|
}
|
|
void ProtocolManager::pauseProtocol(Protocol* protocol)
|
|
{
|
|
for (unsigned int i = 0; i < m_protocols.size(); i++)
|
|
{
|
|
if (m_protocols[i].protocol == protocol && m_protocols[i].state == PROTOCOL_STATE_RUNNING)
|
|
{
|
|
m_protocols[i].state = PROTOCOL_STATE_PAUSED;
|
|
m_protocols[i].protocol->pause();
|
|
}
|
|
}
|
|
}
|
|
void ProtocolManager::unpauseProtocol(Protocol* protocol)
|
|
{
|
|
for (unsigned int i = 0; i < m_protocols.size(); i++)
|
|
{
|
|
if (m_protocols[i].protocol == protocol && m_protocols[i].state == PROTOCOL_STATE_PAUSED)
|
|
{
|
|
m_protocols[i].state = PROTOCOL_STATE_RUNNING;
|
|
m_protocols[i].protocol->unpause();
|
|
}
|
|
}
|
|
}
|
|
void ProtocolManager::protocolTerminated(Protocol* protocol)
|
|
{
|
|
int offset = 0;
|
|
for (unsigned int i = 0; i < m_protocols.size(); i++)
|
|
{
|
|
if (m_protocols[i-offset].protocol == protocol)
|
|
{
|
|
delete m_protocols[i].protocol;
|
|
m_protocols.erase(m_protocols.begin()+(i-offset), m_protocols.begin()+(i-offset)+1);
|
|
offset++;
|
|
}
|
|
}
|
|
printf("*** PROTOCOL MANAGER *** - A protocol has been terminated. There are %ld protocols running.\n", m_protocols.size());
|
|
}
|
|
|
|
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++)
|
|
{
|
|
if (m_protocols[i].protocol->getProtocolType() == searchedProtocol) // pass data to them even when paused
|
|
m_protocols[i].protocol->messageReceived(data+1);
|
|
}
|
|
m_messagesToProcess.pop_back();
|
|
}
|
|
// now update all protocols
|
|
for (unsigned int i = 0; i < m_protocols.size(); i++)
|
|
{
|
|
if (m_protocols[i].state == PROTOCOL_STATE_RUNNING)
|
|
m_protocols[i].protocol->update();
|
|
}
|
|
}
|
|
|
|
int ProtocolManager::runningProtocolsCount()
|
|
{
|
|
return m_protocols.size();
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
|