1
0
Fork 0

Improved error indication from FGIO::parse_port_config() and FGIO::add_channel().

E.g. for multiplay, we return (FGProtocol*) nullptr on success.
This commit is contained in:
Julian Smith 2020-12-29 17:43:17 +00:00
parent c478a80c5c
commit 8cead7e59b
2 changed files with 37 additions and 16 deletions

View file

@ -77,7 +77,7 @@ using std::to_string;
// configure a port based on the config string // configure a port based on the config string
FGProtocol* FGProtocol*
FGIO::parse_port_config( const string& config ) FGIO::parse_port_config( const string& config, bool& o_ok )
{ {
SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config ); SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
string_list tokens = simgear::strutils::split( config, "," ); string_list tokens = simgear::strutils::split( config, "," );
@ -85,15 +85,17 @@ FGIO::parse_port_config( const string& config )
{ {
SG_LOG( SG_IO, SG_ALERT, SG_LOG( SG_IO, SG_ALERT,
"Port configuration error: empty config string" ); "Port configuration error: empty config string" );
o_ok = false;
return nullptr; return nullptr;
} }
return parse_port_config(tokens); return parse_port_config(tokens, o_ok);
} }
FGProtocol* FGProtocol*
FGIO::parse_port_config( const string_list& tokens ) FGIO::parse_port_config( const string_list& tokens, bool& o_ok )
{ {
o_ok = false;
const string protocol = tokens[0]; const string protocol = tokens[0];
SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol ); SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol );
@ -116,6 +118,7 @@ FGIO::parse_port_config( const string_list& tokens )
fgSetBool( "/input/atcsim/ignore-pedal-controls", false ); fgSetBool( "/input/atcsim/ignore-pedal-controls", false );
} }
atcsim->set_path_names(tokens[2], tokens[3], tokens[4], tokens[5]); atcsim->set_path_names(tokens[2], tokens[3], tokens[4], tokens[5]);
o_ok = true;
return atcsim; return atcsim;
} else if ( protocol == "atlas" ) { } else if ( protocol == "atlas" ) {
io = new FGAtlas; io = new FGAtlas;
@ -151,6 +154,7 @@ FGIO::parse_port_config( const string_list& tokens )
io = new FGNMEA(); io = new FGNMEA();
} else if ( protocol == "props" || protocol == "telnet" ) { } else if ( protocol == "props" || protocol == "telnet" ) {
io = new FGProps( tokens ); io = new FGProps( tokens );
o_ok = true;
return io; return io;
} else if ( protocol == "pve" ) { } else if ( protocol == "pve" ) {
io = new FGPVE; io = new FGPVE;
@ -189,11 +193,12 @@ FGIO::parse_port_config( const string_list& tokens )
fgSetInt("/sim/multiplay/txport", port); fgSetInt("/sim/multiplay/txport", port);
fgSetString("/sim/multiplay/txhost", host.c_str()); fgSetString("/sim/multiplay/txhost", host.c_str());
} }
o_ok = true;
return NULL; return NULL;
} }
#if FG_HAVE_HLA #if FG_HAVE_HLA
else if ( protocol == "hla" ) { else if ( protocol == "hla" ) {
o_ok = true;
return new FGHLA(tokens); return new FGHLA(tokens);
} }
else if ( protocol == "hla-local" ) { else if ( protocol == "hla-local" ) {
@ -208,6 +213,7 @@ FGIO::parse_port_config( const string_list& tokens )
HLA_tokens.insert(HLA_tokens.begin(), "60"); HLA_tokens.insert(HLA_tokens.begin(), "60");
HLA_tokens.insert(HLA_tokens.begin(), "bi"); HLA_tokens.insert(HLA_tokens.begin(), "bi");
HLA_tokens.push_back("fg-local.xml"); HLA_tokens.push_back("fg-local.xml");
o_ok = true;
return new FGHLA(HLA_tokens); return new FGHLA(HLA_tokens);
} }
#endif #endif
@ -326,7 +332,7 @@ FGIO::parse_port_config( const string_list& tokens )
delete io; delete io;
return nullptr; return nullptr;
} }
if (io) o_ok = true;
return io; return io;
} }
@ -346,9 +352,16 @@ FGIO::init()
// original, which closes the port and frees up the fd ... doh!!! // original, which closes the port and frees up the fd ... doh!!!
for (const auto& config : *(globals->get_channel_options_list())) { for (const auto& config : *(globals->get_channel_options_list())) {
FGProtocol* p = add_channel(config); bool ok;
if (p) { FGProtocol* p = add_channel(config, ok);
addToPropertyTree(p->get_name(), config); SG_LOG( SG_IO, SG_DEBUG, "add_channel() with config=" << config << " => ok=" << ok << " p=" << p);
if (ok) {
if (p) {
addToPropertyTree(p->get_name(), config);
}
}
else {
SG_LOG( SG_IO, SG_ALERT, "add_channel() failed. config=" << config);
} }
} // of channel options iteration } // of channel options iteration
@ -358,13 +371,17 @@ FGIO::init()
} }
// add another I/O channel // add another I/O channel
FGProtocol* FGIO::add_channel(const string& config) FGProtocol* FGIO::add_channel(const string& config, bool& o_ok)
{ {
// parse the configuration string and store the results in the // parse the configuration string and store the results in the
// appropriate FGIOChannel structure // appropriate FGIOChannel structure
FGProtocol *p = parse_port_config( config ); FGProtocol *p = parse_port_config( config, o_ok );
if (!p) if (!o_ok)
{ {
SG_LOG(SG_IO, SG_ALERT, "Failed to parse config=" << config);
return nullptr;
}
if (!p) {
return nullptr; return nullptr;
} }
@ -480,11 +497,15 @@ bool FGIO::commandAddChannel(const SGPropertyNode * arg, SGPropertyNode * root)
string name = arg->getStringValue("name"); string name = arg->getStringValue("name");
const string config = arg->getStringValue("config"); const string config = arg->getStringValue("config");
auto protocol = add_channel(config); bool ok;
if (!protocol) { auto protocol = add_channel(config, ok);
if (!ok) {
SG_LOG(SG_NETWORK, SG_WARN, "add-io-channel: adding channel failed"); SG_LOG(SG_NETWORK, SG_WARN, "add-io-channel: adding channel failed");
return false; return false;
} }
if (!protocol) {
return true;
}
if (!name.empty()) { if (!name.empty()) {
const string validName = simgear::strutils::makeStringSafeForPropertyName(name); const string validName = simgear::strutils::makeStringSafeForPropertyName(name);

View file

@ -59,10 +59,10 @@ public:
static bool isMultiplayerRequested(); static bool isMultiplayerRequested();
private: private:
FGProtocol* add_channel(const std::string& config); FGProtocol* add_channel(const std::string& config, bool& o_ok);
FGProtocol* parse_port_config( const std::string& cfgstr ); FGProtocol* parse_port_config( const std::string& cfgstr, bool& o_ok );
FGProtocol* parse_port_config( const string_list& tokens ); FGProtocol* parse_port_config( const string_list& tokens, bool& o_ok );
void addToPropertyTree(const string name, const string config); void addToPropertyTree(const string name, const string config);
void removeFromPropertyTree(const string name); void removeFromPropertyTree(const string name);