Skip to content

Commit

Permalink
Remove the Messages feature (issue JSBSim-Team#666).
Browse files Browse the repository at this point in the history
The feature was almost unused in JSBSim and it was using `static` members which are difficult to manage in a multi-threaded environment.
  • Loading branch information
bcoconni committed Aug 20, 2022
1 parent 634c084 commit 7cffc10
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 262 deletions.
105 changes: 0 additions & 105 deletions src/FGJSBBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,117 +77,12 @@ CLASS IMPLEMENTATION
const string FGJSBBase::needed_cfg_version = "2.0";
const string FGJSBBase::JSBSim_version = JSBSIM_VERSION " " __DATE__ " " __TIME__ ;

queue <FGJSBBase::Message> FGJSBBase::Messages;
FGJSBBase::Message FGJSBBase::localMsg;
unsigned int FGJSBBase::messageId = 0;

int FGJSBBase::gaussian_random_number_phase = 0;

short FGJSBBase::debug_lvl = 1;

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void FGJSBBase::PutMessage(const Message& msg)
{
Messages.push(msg);
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void FGJSBBase::PutMessage(const string& text)
{
Message msg;
msg.text = text;
msg.messageId = messageId++;
msg.subsystem = "FDM";
msg.type = Message::eText;
Messages.push(msg);
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void FGJSBBase::PutMessage(const string& text, bool bVal)
{
Message msg;
msg.text = text;
msg.messageId = messageId++;
msg.subsystem = "FDM";
msg.type = Message::eBool;
msg.bVal = bVal;
Messages.push(msg);
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void FGJSBBase::PutMessage(const string& text, int iVal)
{
Message msg;
msg.text = text;
msg.messageId = messageId++;
msg.subsystem = "FDM";
msg.type = Message::eInteger;
msg.iVal = iVal;
Messages.push(msg);
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void FGJSBBase::PutMessage(const string& text, double dVal)
{
Message msg;
msg.text = text;
msg.messageId = messageId++;
msg.subsystem = "FDM";
msg.type = Message::eDouble;
msg.dVal = dVal;
Messages.push(msg);
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void FGJSBBase::ProcessMessage(void)
{
if (Messages.empty()) return;
localMsg = Messages.front();

while (SomeMessages()) {
switch (localMsg.type) {
case JSBSim::FGJSBBase::Message::eText:
cout << localMsg.messageId << ": " << localMsg.text << endl;
break;
case JSBSim::FGJSBBase::Message::eBool:
cout << localMsg.messageId << ": " << localMsg.text << " " << localMsg.bVal << endl;
break;
case JSBSim::FGJSBBase::Message::eInteger:
cout << localMsg.messageId << ": " << localMsg.text << " " << localMsg.iVal << endl;
break;
case JSBSim::FGJSBBase::Message::eDouble:
cout << localMsg.messageId << ": " << localMsg.text << " " << localMsg.dVal << endl;
break;
default:
cerr << "Unrecognized message type." << endl;
break;
}
Messages.pop();
if (SomeMessages()) localMsg = Messages.front();
else break;
}

}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

FGJSBBase::Message* FGJSBBase::ProcessNextMessage(void)
{
if (Messages.empty()) return NULL;
localMsg = Messages.front();

Messages.pop();
return &localMsg;
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void FGJSBBase::disableHighLighting(void)
{
highint[0]='\0';
Expand Down
55 changes: 0 additions & 55 deletions src/FGJSBBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,6 @@ class JSBSIM_API FGJSBBase {
/// Destructor for FGJSBBase.
virtual ~FGJSBBase() {};

/// JSBSim Message structure
struct Message {
unsigned int fdmId;
unsigned int messageId;
std::string text;
std::string subsystem;
enum mType {eText, eInteger, eDouble, eBool} type;
bool bVal;
int iVal;
double dVal;
};

/// First order, (low pass / lag) filter
class Filter {
double prev_in;
Expand Down Expand Up @@ -144,43 +132,6 @@ class JSBSIM_API FGJSBBase {
static char fgdef[6];
//@}

///@name JSBSim Messaging functions
//@{
/** Places a Message structure on the Message queue.
@param msg pointer to a Message structure
@return pointer to a Message structure */
void PutMessage(const Message& msg);
/** Creates a message with the given text and places it on the queue.
@param text message text
@return pointer to a Message structure */
void PutMessage(const std::string& text);
/** Creates a message with the given text and boolean value and places it on the queue.
@param text message text
@param bVal boolean value associated with the message
@return pointer to a Message structure */
void PutMessage(const std::string& text, bool bVal);
/** Creates a message with the given text and integer value and places it on the queue.
@param text message text
@param iVal integer value associated with the message
@return pointer to a Message structure */
void PutMessage(const std::string& text, int iVal);
/** Creates a message with the given text and double value and places it on the queue.
@param text message text
@param dVal double value associated with the message
@return pointer to a Message structure */
void PutMessage(const std::string& text, double dVal);
/** Reads the message on the queue (but does not delete it).
@return 1 if some messages */
int SomeMessages(void) const { return !Messages.empty(); }
/** Reads the message on the queue and removes it from the queue.
This function also prints out the message.*/
void ProcessMessage(void);
/** Reads the next message on the queue and removes it from the queue.
This function also prints out the message.
@return a pointer to the message, or NULL if there are no messages.*/
Message* ProcessNextMessage(void);
//@}

/** Returns the version number of JSBSim.
* @return The version number of JSBSim. */
static const std::string& GetVersion(void) {return JSBSim_version;}
Expand Down Expand Up @@ -340,12 +291,6 @@ class JSBSIM_API FGJSBBase {
static double GaussianRandomNumber(void);

protected:
static Message localMsg;

static std::queue <Message> Messages;

static unsigned int messageId;

static constexpr double radtodeg = 180. / M_PI;
static constexpr double degtorad = M_PI / 180.;
static constexpr double hptoftlbssec = 550.0;
Expand Down
15 changes: 6 additions & 9 deletions src/JSBSim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ int real_main(int argc, char* argv[])
LogDirectiveName.clear();
bool result = false, success;
bool was_paused = false;

double frame_duration;

double new_five_second_value = 0.0;
Expand Down Expand Up @@ -463,7 +463,7 @@ int real_main(int argc, char* argv[])

// Dump the simulation state (position, orientation, etc.)
FDMExec->GetPropagate()->DumpState();

// Perform trim if requested via the initialization file
JSBSim::TrimMode icTrimRequested = (JSBSim::TrimMode)FDMExec->GetIC()->TrimRequested();
if (icTrimRequested != JSBSim::TrimMode::tNone) {
Expand All @@ -480,7 +480,7 @@ int real_main(int argc, char* argv[])
exit(1);
}
}

cout << endl << JSBSim::FGFDMExec::fggreen << JSBSim::FGFDMExec::highint
<< "---- JSBSim Execution beginning ... --------------------------------------------"
<< JSBSim::FGFDMExec::reset << endl << endl;
Expand All @@ -506,18 +506,15 @@ int real_main(int argc, char* argv[])
if (realtime) sleep_nseconds = (long)(frame_duration*1e9);
else sleep_nseconds = (sleep_period )*1e9; // 0.01 seconds

tzset();
tzset();
current_seconds = initial_seconds = getcurrentseconds();

// *** CYCLIC EXECUTION LOOP, AND MESSAGE READING *** //
while (result && FDMExec->GetSimTime() <= end_time) {

FDMExec->ProcessMessage(); // Process messages, if any.

// Check if increment then hold is on and take appropriate actions if it is
// Iterate is not supported in realtime - only in batch and playnice modes
FDMExec->CheckIncrementalHold();

// if running realtime, throttle the execution, else just run flat-out fast
// unless "playing nice", in which case sleep for a while (0.01 seconds) each frame.
// If suspended, then don't increment cumulative realtime "stopwatch".
Expand Down Expand Up @@ -719,7 +716,7 @@ bool options(int count, char **arg)

XMLFile xmlFile;
SGPath path = SGPath::fromLocal8Bit(keyword.c_str());

if (xmlFile.IsScriptFile(path)) ScriptName = path;
else if (xmlFile.IsLogDirectiveFile(path)) LogDirectiveName.push_back(path);
else if (xmlFile.IsAircraftFile(SGPath("aircraft")/keyword/keyword)) AircraftName = keyword;
Expand Down
9 changes: 3 additions & 6 deletions src/models/FGLGear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,8 @@ void FGLGear::ReportTakeoffOrLanding(void)

if (lastWOW != WOW)
{
ostringstream buf;
buf << "GEAR_CONTACT: " << fdmex->GetSimTime() << " seconds: " << name;
PutMessage(buf.str(), WOW);
cout << "GEAR_CONTACT: " << fdmex->GetSimTime() << " seconds: " << name
<< " " << WOW << endl;
}
}

Expand All @@ -558,9 +557,7 @@ void FGLGear::CrashDetect(void)
GetMoments().Magnitude() > 5000000000.0 ||
SinkRate > 1.4666*30 ) && !fdmex->IntegrationSuspended())
{
ostringstream buf;
buf << "*CRASH DETECTED* " << fdmex->GetSimTime() << " seconds: " << name;
PutMessage(buf.str());
cout << "*CRASH DETECTED* " << fdmex->GetSimTime() << " seconds: " << name;
// fdmex->SuspendIntegration();
}
}
Expand Down
87 changes: 0 additions & 87 deletions tests/unit_tests/FGJSBBaseTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,93 +6,6 @@
class FGJSBBaseTest : public CxxTest::TestSuite, public JSBSim::FGJSBBase
{
public:
void testMessages() {
const std::string myMessage = "My message";
std::set<int> mesId;
// Check that the message queue is empty
TS_ASSERT(!SomeMessages());
TS_ASSERT(!ProcessNextMessage());
ProcessMessage();
// Process one text message
PutMessage(myMessage);
TS_ASSERT(SomeMessages());
Message* message = ProcessNextMessage();
TS_ASSERT(message);
mesId.insert(message->messageId);
TS_ASSERT_EQUALS(message->type, Message::eText);
TS_ASSERT_EQUALS(message->text, myMessage);
TS_ASSERT(!SomeMessages());
// Check that the message queue is empty again
TS_ASSERT(!SomeMessages());
TS_ASSERT(!ProcessNextMessage());
ProcessMessage();
// Process several messages
PutMessage(CreateIndexedPropertyName(myMessage,0), true);
PutMessage(CreateIndexedPropertyName(myMessage,1), -1);
PutMessage(CreateIndexedPropertyName(myMessage,2), 3.14159);
for (int i=0; i<3; i++) {
std::ostringstream os;
TS_ASSERT(SomeMessages());
message = ProcessNextMessage();
TS_ASSERT(message);
// Check the ID is a unique number
TS_ASSERT(!mesId.count(message->messageId));
mesId.insert(message->messageId);
os << myMessage << "[" << i << "]";
TS_ASSERT_EQUALS(message->text, os.str());
switch(i) {
case 0:
TS_ASSERT_EQUALS(message->type, Message::eBool);
TS_ASSERT(message->bVal);
break;
case 1:
TS_ASSERT_EQUALS(message->type, Message::eInteger);
TS_ASSERT_EQUALS(message->iVal, -1);
break;
case 2:
TS_ASSERT_EQUALS(message->type, Message::eDouble);
TS_ASSERT_EQUALS(message->dVal, 3.14159);
break;
}
}
// Check that the message queue is empty again
TS_ASSERT(!SomeMessages());
TS_ASSERT(!ProcessNextMessage());
ProcessMessage();
// Re-insert the last message in the queue
Message backup = *message;
PutMessage(*message);
TS_ASSERT(SomeMessages());
message = ProcessNextMessage();
TS_ASSERT(message);
TS_ASSERT_EQUALS(message->text, backup.text);
// Check that the message ID has not been altered
TS_ASSERT_EQUALS(message->messageId, backup.messageId);
TS_ASSERT(mesId.count(message->messageId));
TS_ASSERT_EQUALS(message->subsystem, backup.subsystem);
TS_ASSERT_EQUALS(message->type, Message::eDouble);
TS_ASSERT_EQUALS(message->dVal, 3.14159);
// Check that the message queue is empty again
TS_ASSERT(!SomeMessages());
TS_ASSERT(!ProcessNextMessage());
ProcessMessage();
// Process several messages
PutMessage(myMessage);
PutMessage(CreateIndexedPropertyName(myMessage,0), true);
PutMessage(CreateIndexedPropertyName(myMessage,1), -1);
PutMessage(CreateIndexedPropertyName(myMessage,2), 3.14159);
// Ugly hack to generate a malformed message
unsigned char* _type = (unsigned char*)(&backup.type);
*_type = 0xff;
PutMessage(backup);
TS_ASSERT(SomeMessages());
ProcessMessage();
// Check that the message queue is empty again
TS_ASSERT(!SomeMessages());
TS_ASSERT(!ProcessNextMessage());
ProcessMessage();
}

void testCASConversion() {
double p = 2116.228;
TS_ASSERT_EQUALS(VcalibratedFromMach(-0.1, p), 0.0);
Expand Down

0 comments on commit 7cffc10

Please sign in to comment.