]> git.mxchange.org Git - flightgear.git/blobdiff - src/Network/generic.cxx
NavDisplay - custom symbol support enabled.
[flightgear.git] / src / Network / generic.cxx
index 242bd881c58765cc73e88e0afcb3b5f6faad2b15..dca36308739490aa765cb33c9a828e73fd7fb840 100644 (file)
@@ -42,7 +42,7 @@
 #include <Main/util.hxx>
 #include "generic.hxx"
 
-FGGeneric::FGGeneric(vector<string> tokens) : exitOnError(false)
+FGGeneric::FGGeneric(vector<string> tokens) : exitOnError(false), initOk(false)
 {
     size_t configToken;
     if (tokens[1] == "socket") {
@@ -53,9 +53,9 @@ FGGeneric::FGGeneric(vector<string> tokens) : exitOnError(false)
         configToken = 6; 
     }
 
-    if (configToken >= tokens.size()) {
+    if ((configToken >= tokens.size())||(tokens[ configToken ] == "")) {
        SG_LOG(SG_NETWORK, SG_ALERT,
-              "Not enough tokens passed for generic protocol");
+              "Not enough tokens passed for generic '" << tokens[1] << "' protocol. ");
        return;
     }
 
@@ -66,6 +66,7 @@ FGGeneric::FGGeneric(vector<string> tokens) : exitOnError(false)
     if (direction != "in" && direction != "out" && direction != "bi") {
         SG_LOG(SG_NETWORK, SG_ALERT, "Unsuported protocol direction: "
                << direction);
+        return;
     }
 
     reinit();
@@ -339,7 +340,7 @@ bool FGGeneric::parse_message_binary(int length) {
 }
 
 bool FGGeneric::parse_message_ascii(int length) {
-    char *p2, *p1 = buf;
+    char *p1 = buf;
     int i = -1;
     int chunks = _in_message.size();
     int line_separator_size = line_separator.size();
@@ -353,11 +354,17 @@ bool FGGeneric::parse_message_ascii(int length) {
         buf[length - line_separator_size] = 0;
     }
 
+    size_t varsep_len = var_separator.length();
     while ((++i < chunks) && p1) {
-        p2 = strstr(p1, var_separator.c_str());
-        if (p2) {
-            *p2 = 0;
-            p2 += var_separator.length();
+        char* p2 = NULL;
+
+        if (varsep_len > 0)
+        {
+            p2 = strstr(p1, var_separator.c_str());
+            if (p2) {
+                *p2 = 0;
+                p2 += varsep_len;
+            }
         }
 
         switch (_in_message[i].type) {
@@ -389,7 +396,7 @@ bool FGGeneric::parse_message_ascii(int length) {
     return true;
 }
 
-bool FGGeneric::parse_message(int length) {
+bool FGGeneric::parse_message_len(int length) {
     if (binary_mode) {
         return parse_message_binary(length);
     } else {
@@ -447,7 +454,7 @@ bool FGGeneric::process() {
             if (!binary_mode) {
                 length = io->readline( buf, FG_MAX_MSG_SIZE );
                 if ( length > 0 ) {
-                    parse_message( length );
+                    parse_message_len( length );
                 } else {
                     SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
                     return false;
@@ -455,7 +462,7 @@ bool FGGeneric::process() {
             } else {
                 length = io->read( buf, binary_record_length );
                 if ( length == binary_record_length ) {
-                    parse_message( length );
+                    parse_message_len( length );
                 } else {
                     SG_LOG( SG_IO, SG_ALERT,
                             "Generic protocol: Received binary "
@@ -467,12 +474,12 @@ bool FGGeneric::process() {
         } else {
             if (!binary_mode) {
                 while ((length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
-                    parse_message( length );
+                    parse_message_len( length );
                 }
             } else {
                 while ((length = io->read( buf, binary_record_length )) 
                           == binary_record_length ) {
-                    parse_message( length );
+                    parse_message_len( length );
                 }
 
                 if ( length > 0 ) {
@@ -541,13 +548,21 @@ FGGeneric::reinit()
         SGPropertyNode *output = root.getNode("generic/output");
         if (output) {
             _out_message.clear();
-            read_config(output, _out_message);
+            if (!read_config(output, _out_message))
+            {
+                // bad configuration
+                return;
+            }
         }
     } else if (direction == "in") {
         SGPropertyNode *input = root.getNode("generic/input");
         if (input) {
             _in_message.clear();
-            read_config(input, _in_message);
+            if (!read_config(input, _in_message))
+            {
+                // bad configuration
+                return;
+            }
             if (!binary_mode && (line_separator.size() == 0 ||
                 *line_separator.rbegin() != '\n')) {
 
@@ -557,10 +572,12 @@ FGGeneric::reinit()
             }
         }
     }
+
+    initOk = true;
 }
 
 
-void
+bool
 FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
 {
     binary_mode = root->getBoolValue("binary_mode");
@@ -660,6 +677,7 @@ FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
 
     int record_length = 0; // Only used for binary protocols.
     vector<SGPropertyNode_ptr> chunks = root->getChildren("chunk");
+
     for (unsigned int i = 0; i < chunks.size(); i++) {
 
         _serial_prot chunk;
@@ -702,7 +720,19 @@ FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
 
     }
 
-    if( binary_mode ) {
+    if( !binary_mode )
+    {
+        if ((chunks.size() > 1)&&(var_sep_string.length() == 0))
+        {
+            // ASCII protocols really need a separator when there is more than one chunk per line
+            SG_LOG(SG_IO, SG_ALERT,
+                   "generic protocol: Invalid configuration. "
+                   "'var_separator' must not be empty for protocols which have more than one chunk per line.");
+            return false;
+        }
+    }
+    else
+    {
         if (binary_record_length == -1) {
             binary_record_length = record_length;
         } else if (binary_record_length < record_length) {
@@ -712,6 +742,8 @@ FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
             binary_record_length = record_length;
         }
     }
+
+    return true;
 }
 
 void FGGeneric::updateValue(FGGeneric::_serial_prot& prot, bool val)