summaryrefslogtreecommitdiff
path: root/cesar/maximus/system/src/SystemManager.cpp
diff options
context:
space:
mode:
authorsave2008-04-07 14:17:42 +0000
committersave2008-04-07 14:17:42 +0000
commit3d58a62727346b7ac1a6cb36fed1a06ed72228dd (patch)
treed7788c3cf9f76426aef0286d0202e2097f0fa0eb /cesar/maximus/system/src/SystemManager.cpp
parent095dca4b0a8d4924093bab424f71f588fdd84613 (diff)
Moved the complete svn base into the cesar directory.
git-svn-id: svn+ssh://pessac/svn/cesar/trunk@1769 017c9cb6-072f-447c-8318-d5b54f68fe89
Diffstat (limited to 'cesar/maximus/system/src/SystemManager.cpp')
-rw-r--r--cesar/maximus/system/src/SystemManager.cpp546
1 files changed, 546 insertions, 0 deletions
diff --git a/cesar/maximus/system/src/SystemManager.cpp b/cesar/maximus/system/src/SystemManager.cpp
new file mode 100644
index 0000000000..4c40ae88ea
--- /dev/null
+++ b/cesar/maximus/system/src/SystemManager.cpp
@@ -0,0 +1,546 @@
+/************************************************************************
+ SystemManager.cpp - Copyright buret
+
+Here you can write a license for your code, some comments or any other
+information you want to have in your generated code. To to this simply
+configure the "headings" directory in uml to point to a directory
+where you have your heading files.
+
+or you can just replace the contents of this file with your own.
+If you want to do this, this file is located at
+
+/usr/share/apps/umbrello/headings/heading.cpp
+
+-->Code Generators searches for heading files based on the file extension
+ i.e. it will look for a file name ending in ".h" to include in C++ header
+ files, and for a file name ending in ".java" to include in all generated
+ java code.
+ If you name the file "heading.<extension>", Code Generator will always
+ choose this file even if there are other files with the same extension in the
+ directory. If you name the file something else, it must be the only one with that
+ extension in the directory to guarantee that Code Generator will choose it.
+
+you can use variables in your heading files which are replaced at generation
+time. possible variables are : author, date, time, filename and filepath.
+just write %variable_name%
+
+This file was generated on %date% at %time%
+The original location of this file is /home/buret/eclipse/maximus/system/src/SystemManager.cpp
+**************************************************************************/
+
+#include "SystemManager.h"
+
+#include "Station.h"
+#include "ISci.h"
+#include "SystemSciMsg.h"
+#include "INetworkClock.h"
+
+#include "sci_types.h"
+
+#include "Error.h"
+#include "Logger.h"
+
+#include <iomanip> // for 'setfill()' and 'setw()'
+#include <iostream> // for 'cout', 'cerr' and 'clog'
+using namespace std;
+
+
+// Constructors/Destructors
+//
+
+
+SystemManager::SystemManager ( ISci * p_sci_server ):
+mpSciServer(NULL),
+mpNetworkClock(NULL)
+{
+ logFunction();
+
+ if (NULL != p_sci_server)
+ {
+ mpSciServer = p_sci_server;
+ }
+ else
+ {
+ errno = EINVAL;
+ throw Error(__PRETTY_FUNCTION__, "SCI server pointer is NULL", errno);
+ }
+ initAttributes();
+}
+
+
+void SystemManager::initAttributes ( )
+{
+ logFunction();
+
+ if (NULL != mpSciServer)
+ {
+ if ( (!mpSciServer->registerSpecializedSciMsg(SCI_MSG_TYPE_SYSTEM, new SystemSciMsg(this)))
+ || (!mpSciServer->setStationsList(&mListOfStations)) )
+ {
+ throw Error(__PRETTY_FUNCTION__, "Error when initializing SCI server");
+ }
+ }
+ else
+ {
+ throw Error(__PRETTY_FUNCTION__, "SCI server pointer is NULL");
+ }
+}
+
+
+SystemManager::~SystemManager ( )
+{
+ logFunction();
+
+ removeAllStations();
+ if (NULL != mpSciServer)
+ {
+ mpSciServer = NULL;
+ }
+ if (NULL != mpNetworkClock)
+ {
+ mpNetworkClock = NULL;
+ }
+}
+
+
+//
+// Methods
+//
+
+
+// Other methods
+//
+
+
+// public methods
+//
+
+
+bool SystemManager::init ( const string & station_executable, const string & debugger )
+{
+ logFunction();
+
+ mDefaultStationExecutable = station_executable;
+ mDebugger = debugger;
+
+ return true;
+}
+
+
+Sci_Msg_Station_Id SystemManager::createStation ( const string & station_executable )
+{
+ logFunction();
+ Sci_Msg_Station_Id stationId = 0;
+
+ Station * createdStation = new Station(station_executable, getNetworkClock()->getCurrentTickValue());
+
+ if (NULL != createdStation)
+ {
+ mListOfStations.push_back(createdStation);
+ stationId = createdStation->getStationId();
+ createdStation = NULL;
+ }
+ clog << logger(LOG_COM) << "create station " \
+ << stationId << " (0x" << setfill('0') << setw(4) << uppercase << hex << stationId << ")" << dec << endl;
+
+ displayListOfStations();
+
+ return stationId;
+}
+
+
+bool SystemManager::receiveIdleMsg ( const SystemSciMsg & system_sci_msg ) const
+{
+ bool bReceive = false;
+
+ Station * pStation = findStation(system_sci_msg.getSciMsgStationId());
+ if (NULL != pStation)
+ {
+ pStation->decrementStationIdleCounter();
+ if ( (0 == pStation->getStationIdleCounter())
+ && (MAXIMUS_STATION_STATUS_DEACTIVATED != pStation->getStationStatus()) )
+ {
+ pStation->setStationStatus(MAXIMUS_STATION_STATUS_IDLE);
+ }
+ bReceive = true;
+ }
+ pStation = NULL;
+
+ return bReceive;
+}
+
+
+bool SystemManager::removeStation ( const Sci_Msg_Station_Id station_id )
+{
+ logFunction();
+ bool bRemove = false;
+
+ if (!mListOfStations.empty())
+ {
+ for (StationsList::iterator it = mListOfStations.begin(); it != mListOfStations.end(); ++it)
+ {
+ StationsList::iterator pos = it;
+ Station * pStation = *pos;
+ if ( (NULL != pStation) && (station_id == pStation->getStationId()) )
+ {
+ delete(pStation);
+ mListOfStations.erase(pos);
+ bRemove = true;
+ break;
+ }
+ }
+ }
+ displayListOfStations();
+
+ // Remove events that should be sent to the removed station
+ getNetworkClock()->removeEvts(station_id);
+
+ return bRemove;
+}
+
+
+bool SystemManager::isStationIdle ( Sci_Msg_Station_Id station_id ) const
+{
+ logFunction();
+ bool bIdle = false;
+
+ Station * pStation = findStation(station_id);
+ if ( (NULL != pStation) && (MAXIMUS_STATION_STATUS_IDLE == pStation->getStationStatus()) )
+ {
+ bIdle = true;
+ }
+ pStation = NULL;
+
+ return bIdle;
+}
+
+
+bool SystemManager::areAllActiveStationsIdle ( ) const
+{
+ logFunction();
+ bool bIdle = true;
+
+ if (!mListOfStations.empty())
+ {
+ for (StationsList::const_iterator it = mListOfStations.begin(); it != mListOfStations.end(); ++it)
+ {
+ if (NULL != *it)
+ {
+ if ( (MAXIMUS_STATION_STATUS_DEACTIVATED != (*it)->getStationStatus())
+ && (MAXIMUS_STATION_STATUS_IDLE != (*it)->getStationStatus()) )
+ {
+ bIdle = false;
+ it = mListOfStations.end();
+ --it;
+ }
+ }
+ else
+ {
+ throw Error(__PRETTY_FUNCTION__, "Station pointer is NULL");
+ }
+ }
+ }
+
+ return bIdle;
+}
+
+
+bool SystemManager::deactivateStation ( const Sci_Msg_Station_Id station_id )
+{
+ logFunction();
+ bool bDeactivate = false;
+
+ Station * pStation = findStation(station_id);
+ if (NULL != pStation)
+ {
+ bDeactivate = pStation->setStationStatus(MAXIMUS_STATION_STATUS_DEACTIVATED);
+ }
+ pStation = NULL;
+
+ return bDeactivate;
+}
+
+
+bool SystemManager::activateStation ( const Sci_Msg_Station_Id station_id )
+{
+ logFunction();
+ bool bActivate = false;
+
+ Station * pStation = findStation(station_id);
+ if (NULL != pStation)
+ {
+ bActivate = pStation->setStationStatus(MAXIMUS_STATION_STATUS_IDLE);
+ }
+ pStation = NULL;
+
+ return bActivate;
+}
+
+
+bool SystemManager::debugStation ( const Sci_Msg_Station_Id station_id )
+{
+ logFunction();
+ bool bDebug = false;
+
+ Station * pStation = findStation(station_id);
+ if (NULL != pStation)
+ {
+ bDebug = pStation->launchDebugger(getDebugger());
+ }
+ pStation = NULL;
+
+ return bDebug;
+}
+
+
+Station_Status SystemManager::getStationStatus ( const Sci_Msg_Station_Id station_id ) const
+{
+ logFunction();
+ Station_Status status = MAXIMUS_STATION_STATUS_NONE;
+
+ Station * pStation = findStation(station_id);
+ if (NULL != pStation)
+ {
+ status = pStation->getStationStatus();
+ }
+
+ return status;
+}
+
+
+bool SystemManager::updateStationStatus ( const Sci_Msg_Station_Id station_id, const Station_Status new_status )
+{
+ logFunction();
+ bool bSetIdle = false;
+
+ Station * pStation = findStation(station_id);
+ if (NULL != pStation)
+ {
+ bSetIdle = pStation->setStationStatus(new_status);
+ }
+
+ return bSetIdle;
+}
+
+
+bool SystemManager::removeAllStations ( )
+{
+ logFunction();
+ bool bRemove = false;
+ unsigned short int nbOfStations = 0;
+
+ if (!mListOfStations.empty())
+ {
+ for (StationsList::const_iterator it = mListOfStations.begin(); it != mListOfStations.end(); ++it)
+ {
+ if (NULL != *it)
+ {
+ delete (*it);
+ nbOfStations++;
+ }
+ }
+ mListOfStations.clear();
+ }
+ if (mListOfStations.empty())
+ {
+ bRemove = true;
+ clog << logger(LOG_COM) << "delete " << nbOfStations << " station(s)" << endl;
+ }
+
+ return bRemove;
+}
+
+
+bool SystemManager::setNetworkClock ( INetworkClock * p_network_clock )
+{
+ if (NULL == p_network_clock)
+ {
+ errno = EINVAL;
+ throw Error(__PRETTY_FUNCTION__, "Network Clock pointer is NULL", errno);
+ }
+ mpNetworkClock = p_network_clock;
+
+ return true;
+}
+
+
+const string & SystemManager::getDefaultStationExecutable ( ) const
+{
+ return mDefaultStationExecutable;
+}
+
+
+bool SystemManager::setStationName ( const Sci_Msg_Station_Id station_id, const std::string & station_name )
+{
+ logFunction();
+ bool bSetName = false;
+
+ Station * pStation = findStation(station_id);
+ if (NULL != pStation)
+ {
+ bSetName = pStation->setStationName(station_name);
+ }
+
+ /* Send a System SCI message to the station to configure its name. */
+
+ SystemSciMsg systemSciMsg(this);
+
+ // Fill specialized SCI msg data length and specialized SCI msg data
+ //
+ bSetName &= systemSciMsg.setSpecializedSciMsgDataLength(station_name.length() + 1);
+ bSetName &= systemSciMsg.setSpecializedSciMsgData((unsigned char *)station_name.c_str());
+
+ // Fill specialized SCI msg header
+ //
+ struct System_Header header = { SYSTEM_VERSION,
+ SYSTEM_TYPE_STATION_NAME,
+ 0x0000 }; // flags
+
+ // Set specialized SCI msg header
+ //
+ bSetName &= systemSciMsg.setSpecializedSciMsgHeader(header);
+
+ // Fill specialized SCI msg attributes:
+ // - header size
+ //
+ bSetName &= systemSciMsg.setSpecializedSciMsgHeaderSize(sizeof(struct System_Header));
+
+ // Fill SCI msg attributes:
+ // - type
+ // - station ID
+ //
+ bSetName &= systemSciMsg.setSciMsgType(SCI_MSG_TYPE_SYSTEM);
+ bSetName &= systemSciMsg.setSciMsgStationId(station_id);
+
+ if (bSetName)
+ {
+ if (NULL != mpSciServer)
+ {
+ bSetName = mpSciServer->fillSciMsg(systemSciMsg);
+ bSetName &= mpSciServer->sendSciMsg(systemSciMsg);
+ }
+ else
+ {
+ throw Error(__PRETTY_FUNCTION__, "SCI server pointer is NULL");
+ }
+ }
+ else
+ {
+ clog << logger(LOG_ERROR) << "system SCI message cannot be sent because it is not correctly filled in!" << endl;
+ }
+
+ return bSetName;
+}
+
+
+// private methods
+//
+
+
+void SystemManager::displayListOfStations ( ) const
+{
+ logFunction();
+ clog << logger(LOG_INFO) << "list of stations = " << endl;
+
+ if (!mListOfStations.empty())
+ {
+ for (StationsList::const_iterator it = mListOfStations.begin(); it != mListOfStations.end(); ++it)
+ {
+ if (NULL != *it)
+ {
+ const_cast<Station *>(*it)->displayStation();
+ }
+ }
+ }
+ else
+ {
+ clog << logger(LOG_INFO) << "\tempty!" << endl;
+ }
+}
+
+
+void SystemManager::registerSystemSciMsg ( )
+{
+ logFunction();
+
+ if (NULL != mpSciServer)
+ {
+ mpSciServer->registerSpecializedSciMsg(SCI_MSG_TYPE_SYSTEM, new SystemSciMsg(this));
+ }
+ else
+ {
+ throw Error(__PRETTY_FUNCTION__, "SCI server pointer is NULL");
+ }
+}
+
+
+// protected methods
+//
+
+
+// Accessor methods
+//
+
+
+// public attribute accessor methods
+//
+
+
+// private attribute accessor methods
+//
+
+
+StationsList * SystemManager::getListOfStations ( )
+{
+ return &mListOfStations;
+}
+
+
+Station * SystemManager::findStation ( const Sci_Msg_Station_Id station_id ) const
+{
+ logFunction();
+ Station * pStation = NULL;
+
+ if (!mListOfStations.empty())
+ {
+ for (StationsList::const_iterator it = mListOfStations.begin(); it != mListOfStations.end(); ++it)
+ {
+ if ( (NULL != *it) && (station_id == (*it)->getStationId()) )
+ {
+ pStation = *it;
+ it = mListOfStations.end();
+ --it;
+ }
+ }
+ }
+ if (NULL == pStation)
+ {
+ clog << logger(LOG_FATAL) << "station with id " << dec << station_id << " (0x" << setfill('0') << setw(4) << uppercase << hex << station_id << ") does not exist" << dec << endl;
+ errno = ENONET;
+ throw Error(__PRETTY_FUNCTION__, "Station does not exist", errno);
+ }
+
+ return pStation;
+}
+
+
+const string SystemManager::getDebugger ( ) const
+{
+ return mDebugger;
+}
+
+
+INetworkClock * SystemManager::getNetworkClock ( ) const
+{
+ if (NULL == mpNetworkClock)
+ {
+ throw Error(__PRETTY_FUNCTION__, "Network Clock pointer is NULL");
+ }
+
+ return mpNetworkClock;
+}
+
+
+// protected attribute accessor methods
+//
+