Cosmetic/coding style changes.

This commit is contained in:
hiker 2015-10-22 11:03:41 +11:00
parent 25ca5646ff
commit 6213959f31
5 changed files with 235 additions and 253 deletions

View File

@ -22,7 +22,7 @@
#include "network/network_manager.hpp" #include "network/network_manager.hpp"
#include "network/protocol_manager.hpp" #include "network/protocol_manager.hpp"
Protocol::Protocol(CallbackObject* callback_object, PROTOCOL_TYPE type) Protocol::Protocol(CallbackObject* callback_object, ProtocolType type)
{ {
m_callback_object = callback_object; m_callback_object = callback_object;
m_type = type; m_type = type;
@ -50,7 +50,7 @@ void Protocol::setListener(ProtocolManager* listener)
m_listener = listener; m_listener = listener;
} }
PROTOCOL_TYPE Protocol::getProtocolType() ProtocolType Protocol::getProtocolType()
{ {
return m_type; return m_type;
} }

View File

@ -30,11 +30,11 @@
class Event; class Event;
class ProtocolManager; class ProtocolManager;
/** \enum PROTOCOL_TYPE /** \enum ProtocolType
* \brief The types that protocols can have. This is used to select which protocol receives which event. * \brief The types that protocols can have. This is used to select which protocol receives which event.
* \ingroup network * \ingroup network
*/ */
enum PROTOCOL_TYPE enum ProtocolType
{ {
PROTOCOL_NONE = 0, //!< No protocol type assigned. PROTOCOL_NONE = 0, //!< No protocol type assigned.
PROTOCOL_CONNECTION = 1, //!< Protocol that deals with client-server connection. PROTOCOL_CONNECTION = 1, //!< Protocol that deals with client-server connection.
@ -65,7 +65,7 @@ class Protocol
* \param callback_object The callback object that will be used by the protocol. Protocols that do not use callback objects must set it to NULL. * \param callback_object The callback object that will be used by the protocol. Protocols that do not use callback objects must set it to NULL.
* \param type The type of the protocol. * \param type The type of the protocol.
*/ */
Protocol(CallbackObject* callback_object, PROTOCOL_TYPE type); Protocol(CallbackObject* callback_object, ProtocolType type);
/*! \brief Destructor /*! \brief Destructor
*/ */
virtual ~Protocol(); virtual ~Protocol();
@ -112,7 +112,7 @@ class Protocol
/*! \brief Method to get a protocol's type. /*! \brief Method to get a protocol's type.
* \return The protocol type. * \return The protocol type.
*/ */
PROTOCOL_TYPE getProtocolType(); ProtocolType getProtocolType();
/// functions to check incoming data easily /// functions to check incoming data easily
bool checkDataSizeAndToken(Event* event, int minimum_size); bool checkDataSizeAndToken(Event* event, int minimum_size);
@ -122,7 +122,7 @@ class Protocol
protected: protected:
ProtocolManager* m_listener; //!< The protocol listener ProtocolManager* m_listener; //!< The protocol listener
PROTOCOL_TYPE m_type; //!< The type of the protocol ProtocolType m_type; //!< The type of the protocol
CallbackObject* m_callback_object; //!< The callback object, if needed CallbackObject* m_callback_object; //!< The callback object, if needed
}; };

View File

@ -67,6 +67,8 @@ ProtocolManager::~ProtocolManager()
} // ~ProtocolManager } // ~ProtocolManager
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** \brief Stops the protocol manager.
*/
void ProtocolManager::abort() void ProtocolManager::abort()
{ {
pthread_mutex_unlock(&m_exit_mutex); // will stop the update function pthread_mutex_unlock(&m_exit_mutex); // will stop the update function
@ -77,9 +79,9 @@ void ProtocolManager::abort()
pthread_mutex_lock(&m_requests_mutex); pthread_mutex_lock(&m_requests_mutex);
pthread_mutex_lock(&m_id_mutex); pthread_mutex_lock(&m_id_mutex);
for (unsigned int i = 0; i < m_protocols.getData().size() ; i++) for (unsigned int i = 0; i < m_protocols.getData().size() ; i++)
delete m_protocols.getData()[i].protocol; delete m_protocols.getData()[i].m_protocol;
for (unsigned int i = 0; i < m_events_to_process.getData().size() ; i++) for (unsigned int i = 0; i < m_events_to_process.getData().size() ; i++)
delete m_events_to_process.getData()[i].event; delete m_events_to_process.getData()[i].m_event;
m_protocols.getData().clear(); m_protocols.getData().clear();
m_requests.clear(); m_requests.clear();
m_events_to_process.getData().clear(); m_events_to_process.getData().clear();
@ -96,17 +98,21 @@ void ProtocolManager::abort()
} // abort } // abort
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** \brief Function that processes incoming events.
* This function is called by the network manager each time there is an
* incoming packet.
*/
void ProtocolManager::propagateEvent(Event* event) void ProtocolManager::propagateEvent(Event* event)
{ {
m_events_to_process.lock(); m_events_to_process.lock();
// register protocols that will receive this event // register protocols that will receive this event
PROTOCOL_TYPE searched_protocol = PROTOCOL_NONE; ProtocolType searched_protocol = PROTOCOL_NONE;
if (event->getType() == EVENT_TYPE_MESSAGE) if (event->getType() == EVENT_TYPE_MESSAGE)
{ {
if (event->data().size() > 0) if (event->data().size() > 0)
{ {
searched_protocol = (PROTOCOL_TYPE)(event->data()[0]); searched_protocol = (ProtocolType)(event->data()[0]);
event->removeFront(1); event->removeFront(1);
} }
else else
@ -127,10 +133,10 @@ void ProtocolManager::propagateEvent(Event* event)
{ {
const ProtocolInfo &pi = m_protocols.getData()[i]; const ProtocolInfo &pi = m_protocols.getData()[i];
// Pass data to protocols even when paused // Pass data to protocols even when paused
if (pi.protocol->getProtocolType() == searched_protocol || if (pi.m_protocol->getProtocolType() == searched_protocol ||
event->getType() == EVENT_TYPE_DISCONNECTED) event->getType() == EVENT_TYPE_DISCONNECTED)
{ {
protocols_ids.push_back(pi.id); protocols_ids.push_back(pi.m_id);
} }
} // for i in m_protocols } // for i in m_protocols
m_protocols.unlock(); m_protocols.unlock();
@ -145,9 +151,9 @@ void ProtocolManager::propagateEvent(Event* event)
if (protocols_ids.size() != 0) if (protocols_ids.size() != 0)
{ {
EventProcessingInfo epi; EventProcessingInfo epi;
epi.arrival_time = (double)StkTime::getTimeSinceEpoch(); epi.m_arrival_time = (double)StkTime::getTimeSinceEpoch();
epi.event = event; epi.m_event = event;
epi.protocols_ids = protocols_ids; epi.m_protocols_ids = protocols_ids;
// Add the event to the queue. After the event is handled // Add the event to the queue. After the event is handled
// its memory will be freed. // its memory will be freed.
m_events_to_process.getData().push_back(epi); m_events_to_process.getData().push_back(epi);
@ -195,33 +201,42 @@ void ProtocolManager::sendMessageExcept(Protocol* sender, STKPeer* peer,
} // sendMessageExcept } // sendMessageExcept
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** \brief Asks the manager to start a protocol.
* This function will store the request, and process it at a time it is
* thread-safe.
* \param protocol : A pointer to the protocol to start
* \return The unique id of the protocol that is being started.
*/
uint32_t ProtocolManager::requestStart(Protocol* protocol) uint32_t ProtocolManager::requestStart(Protocol* protocol)
{ {
// create the request // create the request
ProtocolRequest req; ProtocolRequest req;
ProtocolInfo info; req.m_protocol_info.m_protocol = protocol;
info.protocol = protocol; req.m_protocol_info.m_state = PROTOCOL_STATE_INITIALISING;
info.state = PROTOCOL_STATE_RUNNING; assignProtocolId(&req.m_protocol_info); // assign a unique id to the protocol.
assignProtocolId(&info); // assign a unique id to the protocol. req.m_type = PROTOCOL_REQUEST_START;
req.protocol_info = info;
req.type = PROTOCOL_REQUEST_START;
// add it to the request stack // add it to the request stack
pthread_mutex_lock(&m_requests_mutex); pthread_mutex_lock(&m_requests_mutex);
m_requests.push_back(req); m_requests.push_back(req);
pthread_mutex_unlock(&m_requests_mutex); pthread_mutex_unlock(&m_requests_mutex);
return info.id; return req.m_protocol_info.m_id;
} // requestStart } // requestStart
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** \brief Asks the manager to stop a protocol.
* This function will store the request, and process it at a time it is
* thread-safe.
* \param protocol : A pointer to the protocol to stop
*/
void ProtocolManager::requestStop(Protocol* protocol) void ProtocolManager::requestStop(Protocol* protocol)
{ {
if (!protocol) if (!protocol)
return; return;
// create the request // create the request
ProtocolRequest req; ProtocolRequest req;
req.protocol_info.protocol = protocol; req.m_protocol_info.m_protocol = protocol;
req.type = PROTOCOL_REQUEST_STOP; req.m_type = PROTOCOL_REQUEST_STOP;
// add it to the request stack // add it to the request stack
pthread_mutex_lock(&m_requests_mutex); pthread_mutex_lock(&m_requests_mutex);
m_requests.push_back(req); m_requests.push_back(req);
@ -229,14 +244,19 @@ void ProtocolManager::requestStop(Protocol* protocol)
} // requestStop } // requestStop
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** \brief Asks the manager to pause a protocol.
* This function will store the request, and process it at a time it is
* thread-safe.
* \param protocol : A pointer to the protocol to pause
*/
void ProtocolManager::requestPause(Protocol* protocol) void ProtocolManager::requestPause(Protocol* protocol)
{ {
if (!protocol) if (!protocol)
return; return;
// create the request // create the request
ProtocolRequest req; ProtocolRequest req;
req.protocol_info.protocol = protocol; req.m_protocol_info.m_protocol = protocol;
req.type = PROTOCOL_REQUEST_PAUSE; req.m_type = PROTOCOL_REQUEST_PAUSE;
// add it to the request stack // add it to the request stack
pthread_mutex_lock(&m_requests_mutex); pthread_mutex_lock(&m_requests_mutex);
m_requests.push_back(req); m_requests.push_back(req);
@ -244,14 +264,19 @@ void ProtocolManager::requestPause(Protocol* protocol)
} // requestPause } // requestPause
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** \brief Asks the manager to unpause a protocol.
* This function will store the request, and process it at a time it is
* thread-safe.
* \param protocol : A pointer to the protocol to unpause
*/
void ProtocolManager::requestUnpause(Protocol* protocol) void ProtocolManager::requestUnpause(Protocol* protocol)
{ {
if (!protocol) if (!protocol)
return; return;
// create the request // create the request
ProtocolRequest req; ProtocolRequest req;
req.protocol_info.protocol = protocol; req.m_protocol_info.m_protocol = protocol;
req.type = PROTOCOL_REQUEST_UNPAUSE; req.m_type = PROTOCOL_REQUEST_UNPAUSE;
// add it to the request stack // add it to the request stack
pthread_mutex_lock(&m_requests_mutex); pthread_mutex_lock(&m_requests_mutex);
m_requests.push_back(req); m_requests.push_back(req);
@ -259,20 +284,25 @@ void ProtocolManager::requestUnpause(Protocol* protocol)
} // requestUnpause } // requestUnpause
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** \brief Notifies the manager that a protocol is terminated.
* This function will store the request, and process it at a time it is
* thread-safe.
* \param protocol : A pointer to the protocol that is finished
*/
void ProtocolManager::requestTerminate(Protocol* protocol) void ProtocolManager::requestTerminate(Protocol* protocol)
{ {
if (!protocol) if (!protocol)
return; return;
// create the request // create the request
ProtocolRequest req; ProtocolRequest req;
req.protocol_info.protocol = protocol; req.m_protocol_info.m_protocol = protocol;
req.type = PROTOCOL_REQUEST_TERMINATE; req.m_type = PROTOCOL_REQUEST_TERMINATE;
// add it to the request stack // add it to the request stack
pthread_mutex_lock(&m_requests_mutex); pthread_mutex_lock(&m_requests_mutex);
// check that the request does not already exist : // check that the request does not already exist :
for (unsigned int i = 0; i < m_requests.size(); i++) for (unsigned int i = 0; i < m_requests.size(); i++)
{ {
if (m_requests[i].protocol_info.protocol == protocol) if (m_requests[i].m_protocol_info.m_protocol == protocol)
{ {
pthread_mutex_unlock(&m_requests_mutex); pthread_mutex_unlock(&m_requests_mutex);
return; return;
@ -283,19 +313,25 @@ void ProtocolManager::requestTerminate(Protocol* protocol)
} // requestTerminate } // requestTerminate
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void ProtocolManager::startProtocol(ProtocolInfo protocol) /** \brief Starts a protocol.
* Add the protocol info to the m_protocols vector.
* \param protocol : ProtocolInfo to start.
*/
void ProtocolManager::startProtocol(ProtocolInfo &protocol)
{ {
assert(protocol.m_state == PROTOCOL_STATE_INITIALISING);
// add the protocol to the protocol vector so that it's updated // add the protocol to the protocol vector so that it's updated
m_protocols.lock(); m_protocols.lock();
pthread_mutex_lock(&m_asynchronous_protocols_mutex); pthread_mutex_lock(&m_asynchronous_protocols_mutex);
Log::info("ProtocolManager", Log::info("ProtocolManager",
"A %s protocol with id=%u has been started. There are %ld protocols running.", "A %s protocol with id=%u has been started. There are %ld protocols running.",
typeid(*protocol.protocol).name(), protocol.id, typeid(*protocol.m_protocol).name(), protocol.m_id,
m_protocols.getData().size()+1); m_protocols.getData().size()+1);
m_protocols.getData().push_back(protocol); m_protocols.getData().push_back(protocol);
// setup the protocol and notify it that it's started // setup the protocol and notify it that it's started
protocol.protocol->setListener(this); protocol.m_protocol->setListener(this);
protocol.protocol->setup(); protocol.m_protocol->setup();
protocol.m_state = PROTOCOL_STATE_RUNNING;
m_protocols.unlock(); m_protocols.unlock();
pthread_mutex_unlock(&m_asynchronous_protocols_mutex); pthread_mutex_unlock(&m_asynchronous_protocols_mutex);
} // startProtocol } // startProtocol
@ -311,12 +347,12 @@ void ProtocolManager::pauseProtocol(ProtocolInfo protocol)
// FIXME Does this need to be locked? // FIXME Does this need to be locked?
for (unsigned int i = 0; i < m_protocols.getData().size(); i++) for (unsigned int i = 0; i < m_protocols.getData().size(); i++)
{ {
ProtocolInfo &p = m_protocols.getData()[i]; ProtocolInfo &pi = m_protocols.getData()[i];
if (p.protocol == protocol.protocol && if (pi.m_protocol == protocol.m_protocol &&
p.state == PROTOCOL_STATE_RUNNING) pi.m_state == PROTOCOL_STATE_RUNNING)
{ {
p.state = PROTOCOL_STATE_PAUSED; pi.m_state = PROTOCOL_STATE_PAUSED;
p.protocol->pause(); pi.m_protocol->pause();
} }
} }
} // pauseProtocol } // pauseProtocol
@ -328,11 +364,11 @@ void ProtocolManager::unpauseProtocol(ProtocolInfo protocol)
for (unsigned int i = 0; i < m_protocols.getData().size(); i++) for (unsigned int i = 0; i < m_protocols.getData().size(); i++)
{ {
ProtocolInfo &p = m_protocols.getData()[i]; ProtocolInfo &p = m_protocols.getData()[i];
if (p.protocol == protocol.protocol && if (p.m_protocol == protocol.m_protocol &&
p.state == PROTOCOL_STATE_PAUSED) p.m_state == PROTOCOL_STATE_PAUSED)
{ {
p.state = PROTOCOL_STATE_RUNNING; p.m_state = PROTOCOL_STATE_RUNNING;
p.protocol->unpause(); p.m_protocol->unpause();
} }
} }
} // unpauseProtocol } // unpauseProtocol
@ -344,12 +380,12 @@ void ProtocolManager::protocolTerminated(ProtocolInfo protocol)
m_protocols.lock(); m_protocols.lock();
pthread_mutex_lock(&m_asynchronous_protocols_mutex); pthread_mutex_lock(&m_asynchronous_protocols_mutex);
int offset = 0; int offset = 0;
std::string protocol_type = typeid(*protocol.protocol).name(); std::string protocol_type = typeid(*protocol.m_protocol).name();
for (unsigned int i = 0; i < m_protocols.getData().size(); i++) for (unsigned int i = 0; i < m_protocols.getData().size(); i++)
{ {
if (m_protocols.getData()[i-offset].protocol == protocol.protocol) if (m_protocols.getData()[i-offset].m_protocol == protocol.m_protocol)
{ {
delete m_protocols.getData()[i].protocol; delete m_protocols.getData()[i].m_protocol;
m_protocols.getData().erase(m_protocols.getData().begin()+(i-offset), m_protocols.getData().erase(m_protocols.getData().begin()+(i-offset),
m_protocols.getData().begin()+(i-offset)+1); m_protocols.getData().begin()+(i-offset)+1);
offset++; offset++;
@ -371,33 +407,42 @@ bool ProtocolManager::sendEvent(EventProcessingInfo* event, bool synchronous)
int index = 0; int index = 0;
for (unsigned int i = 0; i < m_protocols.getData().size(); i++) for (unsigned int i = 0; i < m_protocols.getData().size(); i++)
{ {
if (event->protocols_ids[index] == m_protocols.getData()[i].id) if (event->m_protocols_ids[index] == m_protocols.getData()[i].m_id)
{ {
bool result = false; bool result = false;
if (synchronous) if (synchronous)
result = m_protocols.getData()[i].protocol result = m_protocols.getData()[i].m_protocol
->notifyEvent(event->event); ->notifyEvent(event->m_event);
else else
result = m_protocols.getData()[i].protocol result = m_protocols.getData()[i].m_protocol
->notifyEventAsynchronous(event->event); ->notifyEventAsynchronous(event->m_event);
if (result) if (result)
event->protocols_ids.pop_back(); event->m_protocols_ids.pop_back();
else else
index++; index++;
} }
} }
m_protocols.unlock(); m_protocols.unlock();
if (event->protocols_ids.size() == 0 || if (event->m_protocols_ids.size() == 0 ||
(StkTime::getTimeSinceEpoch()-event->arrival_time) >= TIME_TO_KEEP_EVENTS) (StkTime::getTimeSinceEpoch()-event->m_arrival_time) >= TIME_TO_KEEP_EVENTS)
{ {
delete event->event; delete event->m_event;
return true; return true;
} }
return false; return false;
} // sendEvent } // sendEvent
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** \brief Updates the manager.
*
* This function processes the events queue, notifies the concerned
* protocols that they have events to process. Then ask all protocols
* to update themselves. Finally processes stored requests about
* starting, stoping, pausing etc... protocols.
* This function is called by the main loop.
* This function IS FPS-dependant.
*/
void ProtocolManager::update() void ProtocolManager::update()
{ {
// before updating, notify protocols that they have received events // before updating, notify protocols that they have received events
@ -420,13 +465,21 @@ void ProtocolManager::update()
m_protocols.lock(); m_protocols.lock();
for (unsigned int i = 0; i < m_protocols.getData().size(); i++) for (unsigned int i = 0; i < m_protocols.getData().size(); i++)
{ {
if (m_protocols.getData()[i].state == PROTOCOL_STATE_RUNNING) if (m_protocols.getData()[i].m_state == PROTOCOL_STATE_RUNNING)
m_protocols.getData()[i].protocol->update(); m_protocols.getData()[i].m_protocol->update();
} }
m_protocols.unlock(); m_protocols.unlock();
} // update } // update
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** \brief Updates the manager.
* This function processes the events queue, notifies the concerned
* protocols that they have events to process. Then ask all protocols
* to update themselves. Finally processes stored requests about
* starting, stoping, pausing etc... protocols.
* This function is called in a thread.
* This function IS NOT FPS-dependant.
*/
void ProtocolManager::asynchronousUpdate() void ProtocolManager::asynchronousUpdate()
{ {
// before updating, notice protocols that they have received information // before updating, notice protocols that they have received information
@ -451,8 +504,8 @@ void ProtocolManager::asynchronousUpdate()
// FIXME: does m_protocols need to be locked??? // FIXME: does m_protocols need to be locked???
for (unsigned int i = 0; i < m_protocols.getData().size(); i++) for (unsigned int i = 0; i < m_protocols.getData().size(); i++)
{ {
if (m_protocols.getData()[i].state == PROTOCOL_STATE_RUNNING) if (m_protocols.getData()[i].m_state == PROTOCOL_STATE_RUNNING)
m_protocols.getData()[i].protocol->asynchronousUpdate(); m_protocols.getData()[i].m_protocol->asynchronousUpdate();
} }
pthread_mutex_unlock(&m_asynchronous_protocols_mutex); pthread_mutex_unlock(&m_asynchronous_protocols_mutex);
@ -461,22 +514,22 @@ void ProtocolManager::asynchronousUpdate()
pthread_mutex_lock(&m_requests_mutex); pthread_mutex_lock(&m_requests_mutex);
for (unsigned int i = 0; i < m_requests.size(); i++) for (unsigned int i = 0; i < m_requests.size(); i++)
{ {
switch (m_requests[i].type) switch (m_requests[i].m_type)
{ {
case PROTOCOL_REQUEST_START: case PROTOCOL_REQUEST_START:
startProtocol(m_requests[i].protocol_info); startProtocol(m_requests[i].m_protocol_info);
break; break;
case PROTOCOL_REQUEST_STOP: case PROTOCOL_REQUEST_STOP:
stopProtocol(m_requests[i].protocol_info); stopProtocol(m_requests[i].m_protocol_info);
break; break;
case PROTOCOL_REQUEST_PAUSE: case PROTOCOL_REQUEST_PAUSE:
pauseProtocol(m_requests[i].protocol_info); pauseProtocol(m_requests[i].m_protocol_info);
break; break;
case PROTOCOL_REQUEST_UNPAUSE: case PROTOCOL_REQUEST_UNPAUSE:
unpauseProtocol(m_requests[i].protocol_info); unpauseProtocol(m_requests[i].m_protocol_info);
break; break;
case PROTOCOL_REQUEST_TERMINATE: case PROTOCOL_REQUEST_TERMINATE:
protocolTerminated(m_requests[i].protocol_info); protocolTerminated(m_requests[i].m_protocol_info);
break; break;
} }
} }
@ -485,38 +538,46 @@ void ProtocolManager::asynchronousUpdate()
} // asynchronousUpdate } // asynchronousUpdate
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
PROTOCOL_STATE ProtocolManager::getProtocolState(uint32_t id) /** \brief Get the state of a protocol using its id.
* \param id : The id of the protocol you seek the state.
* \return The state of the protocol.
*/
ProtocolState ProtocolManager::getProtocolState(uint32_t id)
{ {
//FIXME that actually need a lock, but it also can be called from //FIXME that actually need a lock, but it also can be called from
// a locked section anyway // a locked section anyway
for (unsigned int i = 0; i < m_protocols.getData().size(); i++) for (unsigned int i = 0; i < m_protocols.getData().size(); i++)
{ {
if (m_protocols.getData()[i].id == id) // we know a protocol with that id if (m_protocols.getData()[i].m_id == id) // we know a protocol with that id
return m_protocols.getData()[i].state; return m_protocols.getData()[i].m_state;
} }
// the protocol isn't running right now // the protocol isn't running right now
for (unsigned int i = 0; i < m_requests.size(); i++) for (unsigned int i = 0; i < m_requests.size(); i++)
{ {
// the protocol is going to be started // the protocol is going to be started
if (m_requests[i].protocol_info.id == id) if (m_requests[i].m_protocol_info.m_id == id)
return PROTOCOL_STATE_RUNNING; // we can say it's running return PROTOCOL_STATE_RUNNING; // we can say it's running
} }
return PROTOCOL_STATE_TERMINATED; // else, it's already finished return PROTOCOL_STATE_TERMINATED; // else, it's already finished
} // getProtocolState } // getProtocolState
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
PROTOCOL_STATE ProtocolManager::getProtocolState(Protocol* protocol) /** \brief Get the state of a protocol using a pointer on it.
* \param protocol : A pointer to the protocol you seek the state.
* \return The state of the protocol.
*/
ProtocolState ProtocolManager::getProtocolState(Protocol* protocol)
{ {
// FIXME Does this need to be locked? // FIXME Does this need to be locked?
for (unsigned int i = 0; i < m_protocols.getData().size(); i++) for (unsigned int i = 0; i < m_protocols.getData().size(); i++)
{ {
if (m_protocols.getData()[i].protocol == protocol) // the protocol is known if (m_protocols.getData()[i].m_protocol == protocol) // the protocol is known
return m_protocols.getData()[i].state; return m_protocols.getData()[i].m_state;
} }
for (unsigned int i = 0; i < m_requests.size(); i++) for (unsigned int i = 0; i < m_requests.size(); i++)
{ {
// the protocol is going to be started // the protocol is going to be started
if (m_requests[i].protocol_info.protocol == protocol) if (m_requests[i].m_protocol_info.m_protocol == protocol)
return PROTOCOL_STATE_RUNNING; // we can say it's running return PROTOCOL_STATE_RUNNING; // we can say it's running
} }
// we don't know this protocol at all, it's finished // we don't know this protocol at all, it's finished
@ -524,48 +585,65 @@ PROTOCOL_STATE ProtocolManager::getProtocolState(Protocol* protocol)
} // getProtocolState } // getProtocolState
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** \brief Get the id of a protocol.
* \param protocol : A pointer to the protocol you seek the id.
* \return The id of the protocol pointed by the protocol parameter.
*/
uint32_t ProtocolManager::getProtocolID(Protocol* protocol) uint32_t ProtocolManager::getProtocolID(Protocol* protocol)
{ {
// FIXME: Does this need to be locked? // FIXME: Does this need to be locked?
for (unsigned int i = 0; i < m_protocols.getData().size(); i++) for (unsigned int i = 0; i < m_protocols.getData().size(); i++)
{ {
if (m_protocols.getData()[i].protocol == protocol) if (m_protocols.getData()[i].m_protocol == protocol)
return m_protocols.getData()[i].id; return m_protocols.getData()[i].m_id;
} }
return 0; return 0;
} // getProtocolID } // getProtocolID
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** \brief Get a protocol using its id.
* \param id : Unique ID of the seek protocol.
* \return The protocol that has the ID id.
*/
Protocol* ProtocolManager::getProtocol(uint32_t id) Protocol* ProtocolManager::getProtocol(uint32_t id)
{ {
// FIXME: does m_protocols need to be locked?? // FIXME: does m_protocols need to be locked??
for (unsigned int i = 0; i < m_protocols.getData().size(); i++) for (unsigned int i = 0; i < m_protocols.getData().size(); i++)
{ {
if (m_protocols.getData()[i].id == id) if (m_protocols.getData()[i].m_id == id)
return m_protocols.getData()[i].protocol; return m_protocols.getData()[i].m_protocol;
} }
return NULL; return NULL;
} // getProtocol } // getProtocol
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
Protocol* ProtocolManager::getProtocol(PROTOCOL_TYPE type) /** \brief Get a protocol using its type.
* \param type : The type of the protocol.
* \return The protocol that matches the given type.
*/
Protocol* ProtocolManager::getProtocol(ProtocolType type)
{ {
// FIXME: Does m_protocols need to be locked? // FIXME: Does m_protocols need to be locked?
for (unsigned int i = 0; i < m_protocols.getData().size(); i++) for (unsigned int i = 0; i < m_protocols.getData().size(); i++)
{ {
if (m_protocols.getData()[i].protocol->getProtocolType() == type) if (m_protocols.getData()[i].m_protocol->getProtocolType() == type)
return m_protocols.getData()[i].protocol; return m_protocols.getData()[i].m_protocol;
} }
return NULL; return NULL;
} // getProtocol } // getProtocol
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** \brief Know whether the app is a server.
* \return True if this application is in server mode, false elseway.
*/
bool ProtocolManager::isServer() bool ProtocolManager::isServer()
{ {
return NetworkManager::getInstance()->isServer(); return NetworkManager::getInstance()->isServer();
} // isServer } // isServer
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/*! \brief Tells if we need to stop the update thread.
*/
int ProtocolManager::exit() int ProtocolManager::exit()
{ {
switch(pthread_mutex_trylock(&m_exit_mutex)) { switch(pthread_mutex_trylock(&m_exit_mutex)) {
@ -582,7 +660,7 @@ int ProtocolManager::exit()
void ProtocolManager::assignProtocolId(ProtocolInfo* protocol_info) void ProtocolManager::assignProtocolId(ProtocolInfo* protocol_info)
{ {
pthread_mutex_lock(&m_id_mutex); pthread_mutex_lock(&m_id_mutex);
protocol_info->id = m_next_protocol_id; protocol_info->m_id = m_next_protocol_id;
m_next_protocol_id++; m_next_protocol_id++;
pthread_mutex_unlock(&m_id_mutex); pthread_mutex_unlock(&m_id_mutex);
} // assignProtocolId } // assignProtocolId

View File

@ -37,75 +37,79 @@ class STKPeer;
#define TIME_TO_KEEP_EVENTS 1.0 #define TIME_TO_KEEP_EVENTS 1.0
/*! /** \enum PROTOCOL_STATE
* \enum PROTOCOL_STATE * \brief Defines the three states that a protocol can have.
* \brief Defines the three states that a protocol can have.
*/ */
enum PROTOCOL_STATE enum ProtocolState
{ {
PROTOCOL_STATE_RUNNING, //!< The protocol is being updated everytime. PROTOCOL_STATE_INITIALISING, //!< The protocol is waiting to be started
PROTOCOL_STATE_PAUSED, //!< The protocol is paused. PROTOCOL_STATE_RUNNING, //!< The protocol is being updated everytime.
PROTOCOL_STATE_TERMINATED //!< The protocol is terminated/does not exist. PROTOCOL_STATE_PAUSED, //!< The protocol is paused.
}; PROTOCOL_STATE_TERMINATED //!< The protocol is terminated/does not exist.
}; // ProtocolState
/*! // ----------------------------------------------------------------------------
* \enum PROTOCOL_REQUEST_TYPE /** \enum ProtocolRequestType
* \brief Defines actions that can be done about protocols. * \brief Defines actions that can be done about protocols.
* This enum is used essentially to keep the manager thread-safe and * This enum is used essentially to keep the manager thread-safe and
* to avoid protocols modifying directly their state. * to avoid protocols modifying directly their state.
*/ */
enum PROTOCOL_REQUEST_TYPE enum ProtocolRequestType
{ {
PROTOCOL_REQUEST_START, //!< Start a protocol PROTOCOL_REQUEST_START, //!< Start a protocol
PROTOCOL_REQUEST_STOP, //!< Stop a protocol PROTOCOL_REQUEST_STOP, //!< Stop a protocol
PROTOCOL_REQUEST_PAUSE, //!< Pause a protocol PROTOCOL_REQUEST_PAUSE, //!< Pause a protocol
PROTOCOL_REQUEST_UNPAUSE, //!< Unpause a protocol PROTOCOL_REQUEST_UNPAUSE, //!< Unpause a protocol
PROTOCOL_REQUEST_TERMINATE //!< Terminate a protocol PROTOCOL_REQUEST_TERMINATE //!< Terminate a protocol
}; }; // ProtocolRequestType
/*! // ----------------------------------------------------------------------------
* \struct ProtocolInfo /** \struct ProtocolInfo
* \brief Stores the information needed to manage protocols * \brief Stores the information needed to manage protocols
*/ */
typedef struct ProtocolInfo typedef struct ProtocolInfo
{ {
PROTOCOL_STATE state; //!< The state of the protocol ProtocolState m_state; //!< The state of the protocol
Protocol* protocol; //!< A pointer to the protocol Protocol* m_protocol; //!< A pointer to the protocol
uint32_t id; //!< The unique id of the protocol uint32_t m_id; //!< The unique id of the protocol
} ProtocolInfo; } ProtocolInfo;
/*! // ----------------------------------------------------------------------------
* \struct ProtocolRequest /** \struct ProtocolRequest
* \brief Represents a request to do an action about a protocol. * \brief Represents a request to do an action about a protocol.
*/ */
typedef struct ProtocolRequest typedef struct ProtocolRequest
{ {
PROTOCOL_REQUEST_TYPE type; //!< The type of request /** The type of request. */
ProtocolInfo protocol_info; //!< The concerned protocol information ProtocolRequestType m_type;
/** The concerned protocol information. */
ProtocolInfo m_protocol_info;
} ProtocolRequest; } ProtocolRequest;
/*! \struct ProtocolRequest // ----------------------------------------------------------------------------
/** \struct ProtocolRequest
* \brief Used to pass the event to protocols that need it * \brief Used to pass the event to protocols that need it
*/ */
typedef struct EventProcessingInfo typedef struct EventProcessingInfo
{ {
Event* event; Event* m_event;
double arrival_time; double m_arrival_time;
std::vector<unsigned int> protocols_ids; std::vector<unsigned int> m_protocols_ids;
} EventProcessingInfo; } EventProcessingInfo;
/*! // ----------------------------------------------------------------------------
* \class ProtocolManager /** \class ProtocolManager
* \brief Manages the protocols at runtime. * \brief Manages the protocols at runtime.
* *
* This class is in charge of storing and managing protocols. * This class is in charge of storing and managing protocols.
* It is a singleton as there can be only one protocol manager per game * It is a singleton as there can be only one protocol manager per game
* instance. Any game object that wants to start a protocol must create a * instance. Any game object that wants to start a protocol must create a
* protocol and give it to this singleton. The protocols are updated in a * protocol and give it to this singleton. The protocols are updated in a
* special thread, to ensure that they are processed independently from the * special thread, to ensure that they are processed independently from the
* frames per second. Then, the management of protocols is thread-safe: any * frames per second. Then, the management of protocols is thread-safe: any
* object can start/pause/stop protocols whithout problems. * object can start/pause/stop protocols whithout problems.
*/ */
class ProtocolManager : public AbstractSingleton<ProtocolManager>, class ProtocolManager : public AbstractSingleton<ProtocolManager>,
public NoCopy public NoCopy
{ {
@ -113,126 +117,31 @@ class ProtocolManager : public AbstractSingleton<ProtocolManager>,
static void* mainLoop(void *data); static void* mainLoop(void *data);
public: public:
/*! \brief Stops the protocol manager. */ virtual void abort();
virtual void abort(); virtual void propagateEvent(Event* event);
/*! virtual void sendMessage(Protocol* sender,
* \brief Function that processes incoming events. const NetworkString& message,
* This function is called by the network manager each time there is an bool reliable = true);
* incoming packet. virtual void sendMessage(Protocol* sender, STKPeer* peer,
*/ const NetworkString& message,
virtual void propagateEvent(Event* event); bool reliable = true);
/*! virtual void sendMessageExcept(Protocol* sender, STKPeer* peer,
* \brief WILL BE COMMENTED LATER const NetworkString& message,
*/ bool reliable = true);
virtual void sendMessage(Protocol* sender, const NetworkString& message, bool reliable = true); virtual uint32_t requestStart(Protocol* protocol);
/*! virtual void requestStop(Protocol* protocol);
* \brief WILL BE COMMENTED LATER virtual void requestPause(Protocol* protocol);
*/ virtual void requestUnpause(Protocol* protocol);
virtual void sendMessage(Protocol* sender, STKPeer* peer, const NetworkString& message, bool reliable = true); virtual void requestTerminate(Protocol* protocol);
/*! virtual void update();
* \brief WILL BE COMMENTED LATER virtual void asynchronousUpdate();
*/ virtual ProtocolState getProtocolState(uint32_t id);
virtual void sendMessageExcept(Protocol* sender, STKPeer* peer, const NetworkString& message, bool reliable = true); virtual ProtocolState getProtocolState(Protocol* protocol);
virtual uint32_t getProtocolID(Protocol* protocol);
/*! virtual Protocol* getProtocol(uint32_t id);
* \brief Asks the manager to start a protocol. virtual Protocol* getProtocol(ProtocolType type);
* This function will store the request, and process it at a time it is bool isServer();
* thread-safe. int exit();
* \param protocol : A pointer to the protocol to start
* \return The unique id of the protocol that is being started.
*/
virtual uint32_t requestStart(Protocol* protocol);
/*!
* \brief Asks the manager to stop a protocol.
* This function will store the request, and process it at a time it is
* thread-safe.
* \param protocol : A pointer to the protocol to stop
*/
virtual void requestStop(Protocol* protocol);
/*!
* \brief Asks the manager to pause a protocol.
* This function will store the request, and process it at a time it is
* thread-safe.
* \param protocol : A pointer to the protocol to pause
*/
virtual void requestPause(Protocol* protocol);
/*!
* \brief Asks the manager to unpause a protocol.
* This function will store the request, and process it at a time it is
* thread-safe.
* \param protocol : A pointer to the protocol to unpause
*/
virtual void requestUnpause(Protocol* protocol);
/*!
* \brief Notifies the manager that a protocol is terminated.
* This function will store the request, and process it at a time it is
* thread-safe.
* \param protocol : A pointer to the protocol that is finished
*/
virtual void requestTerminate(Protocol* protocol);
/*!
* \brief Updates the manager.
*
* This function processes the events queue, notifies the concerned
* protocols that they have events to process. Then ask all protocols
* to update themselves. Finally processes stored requests about
* starting, stoping, pausing etc... protocols.
* This function is called by the main loop.
* This function IS FPS-dependant.
*/
virtual void update();
/*!
* \brief Updates the manager.
*
* This function processes the events queue, notifies the concerned
* protocols that they have events to process. Then ask all protocols
* to update themselves. Finally processes stored requests about
* starting, stoping, pausing etc... protocols.
* This function is called in a thread.
* This function IS NOT FPS-dependant.
*/
virtual void asynchronousUpdate();
/*!
* \brief Get the state of a protocol using its id.
* \param id : The id of the protocol you seek the state.
* \return The state of the protocol.
*/
virtual PROTOCOL_STATE getProtocolState(uint32_t id);
/*!
* \brief Get the state of a protocol using a pointer on it.
* \param protocol : A pointer to the protocol you seek the state.
* \return The state of the protocol.
*/
virtual PROTOCOL_STATE getProtocolState(Protocol* protocol);
/*!
* \brief Get the id of a protocol.
* \param protocol : A pointer to the protocol you seek the id.
* \return The id of the protocol pointed by the protocol parameter.
*/
virtual uint32_t getProtocolID(Protocol* protocol);
/*!
* \brief Get a protocol using its id.
* \param id : Unique ID of the seek protocol.
* \return The protocol that has the ID id.
*/
virtual Protocol* getProtocol(uint32_t id);
/*!
* \brief Get a protocol using its type.
* \param type : The type of the protocol.
* \return The protocol that matches the given type.
*/
virtual Protocol* getProtocol(PROTOCOL_TYPE type);
/*! \brief Know whether the app is a server.
* \return True if this application is in server mode, false elseway.
*/
bool isServer();
/*! \brief Tells if we need to stop the update thread. */
int exit();
protected: protected:
// protected functions // protected functions
@ -253,12 +162,7 @@ class ProtocolManager : public AbstractSingleton<ProtocolManager>,
*/ */
void assignProtocolId(ProtocolInfo* protocol_info); void assignProtocolId(ProtocolInfo* protocol_info);
/*! virtual void startProtocol(ProtocolInfo &protocol);
* \brief Starts a protocol.
* Add the protocol info to the m_protocols vector.
* \param protocol : ProtocolInfo to start.
*/
virtual void startProtocol(ProtocolInfo protocol);
/*! /*!
* \brief Stops a protocol. * \brief Stops a protocol.
* Coes nothing. Noone can stop running protocols for now. * Coes nothing. Noone can stop running protocols for now.

View File

@ -111,7 +111,7 @@ void ServerNetworkManager::run()
Log::info("ServerNetworkManager", "Host initialized."); Log::info("ServerNetworkManager", "Host initialized.");
// listen keyboard console input // listen keyboard console input
m_thread_keyboard = (pthread_t*)(malloc(sizeof(pthread_t))); m_thread_keyboard = new pthread_t;
pthread_create(m_thread_keyboard, NULL, waitInput2, NULL); pthread_create(m_thread_keyboard, NULL, waitInput2, NULL);
NetworkManager::run(); NetworkManager::run();