Commit f56b4c00 authored by Jean Meurice's avatar Jean Meurice
Browse files

Using err_out for autopilots

parent 5348234c
Pipeline #493098 passed with stage
in 5 minutes and 39 seconds
...@@ -70,12 +70,12 @@ public class AdapterGenerator { ...@@ -70,12 +70,12 @@ public class AdapterGenerator {
addFileDependencySharedCpp("json.cpp"); addFileDependencySharedCpp("json.cpp");
addFileDependencySharedCpp("printf.h"); addFileDependencySharedCpp("printf.h");
addFileDependencySharedCpp("printf.cpp"); addFileDependencySharedCpp("printf.cpp");
addFileDependencySharedCpp("err_out/err_out.h"); addFileDependencySharedCpp("err_out.h");
addFileDependencySharedCpp("err_out/standalone_err_out.cpp"); addFileDependencySharedCpp("err_out.cpp");
// TODO "intermediate target" with serialization code ? // TODO "intermediate target" with serialization code ?
cmake.addCMakeCommandEnd("# A static library around the root EMA component with JSON/Binary serialization functions."); cmake.addCMakeCommandEnd("# A static library around the root EMA component with JSON/Binary serialization functions.");
cmake.addCMakeCommandEnd("add_library("+targetNameWithSerialization+" STATIC program.cpp buffer.cpp json.cpp printf.cpp err_out/standalone_err_out.cpp)"); cmake.addCMakeCommandEnd("add_library("+targetNameWithSerialization+" STATIC program.cpp buffer.cpp json.cpp printf.cpp err_out.cpp)");
cmake.addCMakeCommandEnd("target_include_directories("+targetNameWithSerialization+" PUBLIC ${INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR})"); cmake.addCMakeCommandEnd("target_include_directories("+targetNameWithSerialization+" PUBLIC ${INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR})");
//cmake.addCMakeCommandEnd("target_link_libraries("+targetNameWithSerialization+" PUBLIC ${LIBS} -static-libgcc -static-libstdc++)"); //cmake.addCMakeCommandEnd("target_link_libraries("+targetNameWithSerialization+" PUBLIC ${LIBS} -static-libgcc -static-libstdc++)");
cmake.addCMakeCommandEnd("target_link_libraries("+targetNameWithSerialization+" PRIVATE ${LIBS})"); cmake.addCMakeCommandEnd("target_link_libraries("+targetNameWithSerialization+" PRIVATE ${LIBS})");
...@@ -107,11 +107,13 @@ public class AdapterGenerator { ...@@ -107,11 +107,13 @@ public class AdapterGenerator {
addFileDependencySharedCpp("network.cpp"); addFileDependencySharedCpp("network.cpp");
addFileDependencySharedCpp("tcp_protocol.h"); addFileDependencySharedCpp("tcp_protocol.h");
addFileDependencyOwnCpp("server_adapter.h"); addFileDependencyOwnCpp("server_adapter.h");
addFileDependencySharedCpp("standard_err_out.h");
addFileDependencySharedCpp("standard_err_out.cpp");
addFileDependencyOwnCpp("server_adapter.cpp"); addFileDependencyOwnCpp("server_adapter.cpp");
// create adapter executable // create adapter executable
cmake.addCMakeCommandEnd("# ServerAdapter for the EMA component"); cmake.addCMakeCommandEnd("# ServerAdapter for the EMA component");
cmake.addCMakeCommandEnd("add_executable("+targetNameServer+" server_adapter.cpp network.cpp)"); cmake.addCMakeCommandEnd("add_executable("+targetNameServer+" server_adapter.cpp network.cpp standard_err_out.cpp)");
cmake.addCMakeCommandEnd("target_link_libraries("+targetNameServer+" PUBLIC "+targetNameWithSerialization+")"); cmake.addCMakeCommandEnd("target_link_libraries("+targetNameServer+" PUBLIC "+targetNameWithSerialization+")");
cmake.addCMakeCommandEnd("if (CMAKE_HOST_WIN32)"); cmake.addCMakeCommandEnd("if (CMAKE_HOST_WIN32)");
cmake.addCMakeCommandEnd(" target_link_libraries("+targetNameServer+" PUBLIC ws2_32)"); cmake.addCMakeCommandEnd(" target_link_libraries("+targetNameServer+" PUBLIC ws2_32)");
......
...@@ -25,7 +25,7 @@ public class JsonSerializationGenerator extends SerializationGenerator { ...@@ -25,7 +25,7 @@ public class JsonSerializationGenerator extends SerializationGenerator {
b.a(indent, "auto it = as.begin();"); b.a(indent, "auto it = as.begin();");
b.a(indent, "auto end = as.end();"); b.a(indent, "auto end = as.end();");
b.a(indent, "if (!(it != end)) {"); b.a(indent, "if (!(it != end)) {");
b.a(indent, " throw_error(\"ParsingError\", \"Expected IP entry in array\");"); b.a(indent, " throw_error(\"PortIO\", \"Expected IP entry in array\");");
b.a(indent, " break;"); b.a(indent, " break;");
b.a(indent, "}"); b.a(indent, "}");
b.a(indent, "auto ip = reader.get_string().get_json_string();"); b.a(indent, "auto ip = reader.get_string().get_json_string();");
...@@ -34,7 +34,7 @@ public class JsonSerializationGenerator extends SerializationGenerator { ...@@ -34,7 +34,7 @@ public class JsonSerializationGenerator extends SerializationGenerator {
b.a(indent, "auto &target = program_instance.%s[id];", sockInf.input_name); b.a(indent, "auto &target = program_instance.%s[id];", sockInf.input_name);
b.a(indent, "++it;"); b.a(indent, "++it;");
b.a(indent, "if (!(it != end)) {"); b.a(indent, "if (!(it != end)) {");
b.a(indent, " throw_error(\"ParsingError\", \"Expected IP entry in array\");"); b.a(indent, " throw_error(\"PortIO\", \"Expected IP entry in array\");");
b.a(indent, " break;"); b.a(indent, " break;");
b.a(indent, "}"); b.a(indent, "}");
generateSetter(((SimplePacketType) portInfo.data_type).getPayloadType(), indent, 1, "target"); generateSetter(((SimplePacketType) portInfo.data_type).getPayloadType(), indent, 1, "target");
...@@ -79,13 +79,13 @@ public class JsonSerializationGenerator extends SerializationGenerator { ...@@ -79,13 +79,13 @@ public class JsonSerializationGenerator extends SerializationGenerator {
case N: { case N: {
String varName = String.format("res%d", depth); String varName = String.format("res%d", depth);
b.a(indent, "auto %s = reader.get_long();", varName); b.a(indent, "auto %s = reader.get_long();", varName);
b.a(indent, "if (%s < 0) { %s = 0; throw_error(\"ParsingError\", \"Received negative natural number (N).\"); }", varName, varName); b.a(indent, "if (%s < 0) { %s = 0; throw_error(\"PortIO\", \"Received negative natural number (N): '%%\" PRIi64 \"'.\", %s); }", varName, varName, varName);
b.a(indent, "%s = %s;", varReference, varName); b.a(indent, "%s = %s;", varReference, varName);
} break; } break;
case N1: { case N1: {
String varName = String.format("res%d", depth); String varName = String.format("res%d", depth);
b.a(indent, "auto %s = reader.get_long();", varName); b.a(indent, "auto %s = reader.get_long();", varName);
b.a(indent, "if (%s < 1) { %s = 0; throw_error(\"ParsingError\", \"Received natural number <= 0 (N1).\");}", varName, varName); b.a(indent, "if (%s < 1) { %s = 0; throw_error(\"PortIO\", \"Received natural number <= 0 (N1): '%%\" PRIi64 \"'.\", %s);}", varName, varName, varName);
b.a(indent, "%s = %s;", varReference, varName); b.a(indent, "%s = %s;", varReference, varName);
} break; } break;
case C: { case C: {
...@@ -93,10 +93,10 @@ public class JsonSerializationGenerator extends SerializationGenerator { ...@@ -93,10 +93,10 @@ public class JsonSerializationGenerator extends SerializationGenerator {
b.a(indent, "int %s = 0;", i); b.a(indent, "int %s = 0;", i);
b.a(indent, "for (auto t : reader.stream_array()) {" ); b.a(indent, "for (auto t : reader.stream_array()) {" );
b.a(indent, " if (%s == 0) %s = reader.get_double();", i, varReference); b.a(indent, " if (%s == 0) %s = reader.get_double();", i, varReference);
b.a(indent, " else if (%s == 1) {throw_error(\"ParsingError\", \"Unimplemented complex number\");}\n", i); // TODO handle correct Complex native type b.a(indent, " else if (%s == 1) {throw_error(\"PortIO\", \"Unimplemented complex number\");}\n", i); // TODO handle correct Complex native type
b.a(indent, " else { throw_error(\"ParsingError\", \"Too much entries in the received complex number JSON serialization.\"); }" ); b.a(indent, " else { throw_error(\"PortIO\", \"Too much entries in the received complex number JSON serialization.\"); }" );
b.a(indent, " ++%s;", i); b.a(indent, " ++%s;", i);
b.a(indent, "} if (%s < 2) { throw_error(\"ParsingError\", \"Missing entries in the received complex number JSON serialization.\"); }", i); b.a(indent, "} if (%s < 2) { throw_error(\"PortIO\", \"Missing entries in the received complex number JSON serialization.\"); }", i);
} break; } break;
case BOOLEAN: case BOOLEAN:
b.a(indent, "%s = reader.get_bool();", varReference); b.a(indent, "%s = reader.get_bool();", varReference);
...@@ -108,18 +108,18 @@ public class JsonSerializationGenerator extends SerializationGenerator { ...@@ -108,18 +108,18 @@ public class JsonSerializationGenerator extends SerializationGenerator {
b.a(indent, "int %s = 0;", i); b.a(indent, "int %s = 0;", i);
b.a(indent, "for (auto t : reader.stream_array()) {" ); b.a(indent, "for (auto t : reader.stream_array()) {" );
b.a(indent, " if (%s < 2) %s(%s) = reader.get_double();", i, varReference, i); b.a(indent, " if (%s < 2) %s(%s) = reader.get_double();", i, varReference, i);
b.a(indent, " else { throw_error(\"ParsingError\", \"Too much entries in the received VEC2 JSON serialization.\"); }"); b.a(indent, " else { throw_error(\"PortIO\", \"Too much entries in the received VEC2 JSON serialization.\"); }");
b.a(indent, " ++%s;", i); b.a(indent, " ++%s;", i);
b.a(indent, "} if (%s < 2) { throw_error(\"ParsingError\", \"Missing entries in the received VEC2 JSON serialization.\"); }", i); b.a(indent, "} if (%s < 2) { throw_error(\"PortIO\", \"Missing entries in the received VEC2 JSON serialization.\"); }", i);
} break; } break;
case VEC3: { case VEC3: {
String i = String.format("i%d", depth); String i = String.format("i%d", depth);
b.a(indent, "int %s = 0;", i); b.a(indent, "int %s = 0;", i);
b.a(indent, "for (auto t : reader.stream_array()) {" ); b.a(indent, "for (auto t : reader.stream_array()) {" );
b.a(indent, " if (%s < 3) %s(%s) = reader.get_double();", i, varReference, i); b.a(indent, " if (%s < 3) %s(%s) = reader.get_double();", i, varReference, i);
b.a(indent, " else { throw_error(\"ParsingError\", \"Too much entries in the received VEC3 JSON serialization.\"); }"); b.a(indent, " else { throw_error(\"PortIO\", \"Too much entries in the received VEC3 JSON serialization.\"); }");
b.a(indent, " ++%s;", i); b.a(indent, " ++%s;", i);
b.a(indent, "} if (%s < 3) { throw_error(\"ParsingError\", \"Missing entries in the received VEC3 JSON serialization.\"); }",i); b.a(indent, "} if (%s < 3) { throw_error(\"PortIO\", \"Missing entries in the received VEC3 JSON serialization.\"); }",i);
} break; } break;
default: default:
throw new IllegalArgumentException("Missing case"); throw new IllegalArgumentException("Missing case");
...@@ -137,13 +137,14 @@ public class JsonSerializationGenerator extends SerializationGenerator { ...@@ -137,13 +137,14 @@ public class JsonSerializationGenerator extends SerializationGenerator {
b.a(indent, "for (auto t : reader.stream_array()) {" ); b.a(indent, "for (auto t : reader.stream_array()) {" );
b.a(indent, " if (%s) {", first); b.a(indent, " if (%s) {", first);
b.a(indent, " %s = false;", first); b.a(indent, " %s = false;", first);
b.a(indent, " if (reader.get_long() != %s) { throw_error(\"ParsingError\", \"Size given in serialized VectorType array does not match the expected vector length.\"); break; }", size); b.a(indent, " auto size = reader.get_long();");
b.a(indent, " if (size != %s) { throw_error(\"PortIO\", \"Size given in serialized VectorType array does not match the expected vector length (got %%\" PRIi64 \"but expected %s).\", size); break; }", size, size);
b.a(indent, " continue;"); b.a(indent, " continue;");
b.a(indent, " } else if (%s >= %d) { throw_error(\"ParsingError\", \"Too much entries in the received Vector JSON serialization.\"); break; }", i, size); b.a(indent, " } else if (%s >= %d) { throw_error(\"PortIO\", \"Too much entries in the received Vector JSON serialization (expected %s).\"); break; }", i, size, size);
b.a(indent, " auto &%s = %s(%s);", ref, varReference, i); b.a(indent, " auto &%s = %s(%s);", ref, varReference, i);
generateSetter(vt.getBaseType(), newIndent, depth+1, ref); generateSetter(vt.getBaseType(), newIndent, depth+1, ref);
b.a(indent, " ++%s;", i); b.a(indent, " ++%s;", i);
b.a(indent, "} if (%s < %d) { throw_error(\"ParsingError\", \"Missing entries in the received Vector JSON serialization.\"); }", i, size); b.a(indent, "} if (%s < %d) { throw_error(\"PortIO\", \"Missing entries in the received Vector JSON serialization (expected %s).\"); }", i, size, size);
} else if (type instanceof MatrixType) { } else if (type instanceof MatrixType) {
MatrixType vt = (MatrixType)type; MatrixType vt = (MatrixType)type;
int newIndent = indent + 2; int newIndent = indent + 2;
...@@ -156,16 +157,16 @@ public class JsonSerializationGenerator extends SerializationGenerator { ...@@ -156,16 +157,16 @@ public class JsonSerializationGenerator extends SerializationGenerator {
b.a(indent, "int %s = 0;", i); b.a(indent, "int %s = 0;", i);
b.a(indent, "for (auto t : reader.stream_array()) {" ); b.a(indent, "for (auto t : reader.stream_array()) {" );
b.a(indent, " if (%s >= %d) { throw_error(\"ParsingError\", \"Too much rows in the received Matrix JSON serialization.\"); break; }", i, rows); b.a(indent, " if (%s >= %d) { throw_error(\"PortIO\", \"Too much rows in the received Matrix JSON serialization.\"); break; }", i, rows);
b.a(indent, " int %s = 0;", j); b.a(indent, " int %s = 0;", j);
b.a(indent, " for (auto u : reader.stream_array()) {" ); b.a(indent, " for (auto u : reader.stream_array()) {" );
b.a(indent, " if (%s >= %d) { throw_error(\"ParsingError\", \"Too much columns in the received Matrix JSON serialization.\"); break; }", j, cols); b.a(indent, " if (%s >= %d) { throw_error(\"PortIO\", \"Too much columns in the received Matrix JSON serialization.\"); break; }", j, cols);
b.a(indent, " auto &%s = %s(%s, %s);", ref, varReference, i, j); b.a(indent, " auto &%s = %s(%s, %s);", ref, varReference, i, j);
generateSetter(vt.getBaseType(), newIndent, depth+1, ref); generateSetter(vt.getBaseType(), newIndent, depth+1, ref);
b.a(indent, " ++%s;", j); b.a(indent, " ++%s;", j);
b.a(indent, " } if (%s < %d) { throw_error(\"ParsingError\", \"Missing entries in a column of the received Matrix JSON serialization.\"); } ", j, cols); b.a(indent, " } if (%s < %d) { throw_error(\"PortIO\", \"Missing entries in a column of the received Matrix JSON serialization.\"); } ", j, cols);
b.a(indent, " ++%s;", i); b.a(indent, " ++%s;", i);
b.a(indent, "} if (%s < %d) {throw_error(\"ParsingError\", \"Missing rows in the received Matrix JSON serialization.\");}", i, cols); b.a(indent, "} if (%s < %d) {throw_error(\"PortIO\", \"Missing rows in the received Matrix JSON serialization.\");}", i, cols);
} else if (type instanceof DynVectorType) { } else if (type instanceof DynVectorType) {
System.out.println("Unimplemented: DynVectorType set_port case."); System.out.println("Unimplemented: DynVectorType set_port case.");
...@@ -189,10 +190,10 @@ public class JsonSerializationGenerator extends SerializationGenerator { ...@@ -189,10 +190,10 @@ public class JsonSerializationGenerator extends SerializationGenerator {
b.a(indent, "if (%s != %s) {", it, end); b.a(indent, "if (%s != %s) {", it, end);
generateSetter(st.getFieldType(i), newIndent, depth+1, String.format("%s.%s", varReference, fieldName)); generateSetter(st.getFieldType(i), newIndent, depth+1, String.format("%s.%s", varReference, fieldName));
b.a(indent, " ++%s;", it); b.a(indent, " ++%s;", it);
b.a(indent, "} else { throw_error(\"ParsingError\", \"Missing entries in the received Struct JSON serialization.\"); }" ); b.a(indent, "} else { throw_error(\"PortIO\", \"Missing entries in the received Struct JSON serialization.\"); }" );
} }
b.a(indent, "if (%s != %s) { throw_error(\"ParsingError\", \"Too much entries in the received Struct JSON serialization.\"); }", it, end); b.a(indent, "if (%s != %s) { throw_error(\"PortIO\", \"Too much entries in the received Struct JSON serialization.\"); }", it, end);
} else if (type instanceof EnumType) { } else if (type instanceof EnumType) {
EnumType et = (EnumType)type; EnumType et = (EnumType)type;
...@@ -213,7 +214,7 @@ public class JsonSerializationGenerator extends SerializationGenerator { ...@@ -213,7 +214,7 @@ public class JsonSerializationGenerator extends SerializationGenerator {
elseS = "else "; elseS = "else ";
} }
b.a(indent, "else { throw_error(\"ParsingError\", \"Unknown ENUM variant received in the JSON serialization.\"); }"); b.a(indent, "else { throw_error(\"PortIO\", \"Unknown ENUM variant '%%s' received. (Type %s)\", %s); }", et.getName(), variant);
} else { } else {
throw new IllegalArgumentException("Missing case"); throw new IllegalArgumentException("Missing case");
......
Subproject commit 2ed81f25480fd57e6075781a180af9d4b41595b1 Subproject commit fa83ab6811e27429d9d6e9cfc7405aced539d227
#include "program.h" #include "program.h"
#include <inttypes.h>
#include "./${mainModelName}.h" #include "./${mainModelName}.h"
#include <exception> #include "err_out.h"
#include "err_out/err_out.h"
static constexpr auto N_TO_N_BROADCAST_ADDR = "ff02::2"; static constexpr auto N_TO_N_BROADCAST_ADDR = "ff02::2";
static constexpr auto N_TO_N_PREFIX = "2::"; static constexpr auto N_TO_N_PREFIX = "2::";
...@@ -12,38 +12,34 @@ DynamicBuffer buffer; ...@@ -12,38 +12,34 @@ DynamicBuffer buffer;
${mainModelName} program_instance; ${mainModelName} program_instance;
void set_port_json(int i, const char* data) { void set_port_json(int32_t i, const char* data) {
JsonTraverser reader{data}; JsonTraverser reader{data};
switch(i){ switch(i){
<#list setPortJsonCases as i>${i}</#list> <#list setPortJsonCases as i>${i}</#list>
default: default:
cerr << "Invalid output port ID: '"<< i << "'" << endl; throw_error("PortIO", "[set_port_json()] Invalid input port ID: '%PRIi32'");
throw std::exception();
} }
} }
void get_port_json(int i, JsonWriter &writer) { void get_port_json(int32_t i, JsonWriter &writer) {
switch (i){ switch (i){
<#list getPortJsonCases as i>${i}</#list> <#list getPortJsonCases as i>${i}</#list>
default: default:
cerr << "Invalid output port ID: '"<< i << "'" << endl; throw_error("PortIO", "[get_port_json()] Invalid output port ID: '%PRIi32'");
throw std::exception();
} }
} }
void set_port_binary(int i, BinaryReader &reader) { void set_port_binary(int32_t i, BinaryReader &reader) {
switch (i){ switch (i){
<#list setPortBinaryCases as i>${i}</#list> <#list setPortBinaryCases as i>${i}</#list>
default: default:
cerr << "Invalid output port ID: '"<< i << "'" << endl; throw_error("PortIO", "[set_port_binary()] Invalid output port ID: '%PRIi32'");
throw std::exception();
} }
} }
void get_port_binary(int i, BinaryWriter &writer) { void get_port_binary(int32_t i, BinaryWriter &writer) {
switch (i){ switch (i){
<#list getPortBinaryCases as i>${i}</#list> <#list getPortBinaryCases as i>${i}</#list>
default: default:
cerr << "Invalid output port ID: '"<< i << "'" << endl; throw_error("PortIO", "[get_port_binary()] Invalid output port ID: '%PRIi32'");
throw std::exception();
} }
} }
...@@ -61,7 +57,7 @@ void execute(double delta_sec) { ...@@ -61,7 +57,7 @@ void execute(double delta_sec) {
program_instance.execute(); program_instance.execute();
} }
const int PORT_COUNT = ${portCount}; const int32_t PORT_COUNT = ${portCount};
const bool IS_SOCKET[] = { const bool IS_SOCKET[] = {
${isSocket} ${isSocket}
......
...@@ -9,10 +9,10 @@ namespace autopilot { ...@@ -9,10 +9,10 @@ namespace autopilot {
extern DynamicBuffer buffer; // Buffer used by all "writers" (json & binary) & Socket input buffer extern DynamicBuffer buffer; // Buffer used by all "writers" (json & binary) & Socket input buffer
void set_port_json(int i, const char* data); void set_port_json(int32_t i, const char* data);
void get_port_json(int i, JsonWriter &writer); void get_port_json(int32_t i, JsonWriter &writer);
void set_port_binary(int i, BinaryReader &br); void set_port_binary(int32_t i, BinaryReader &br);
void get_port_binary(int i, BinaryWriter &bw); void get_port_binary(int32_t i, BinaryWriter &bw);
// Methods // Methods
void init(); void init();
...@@ -20,7 +20,7 @@ void execute(double delta_sec); ...@@ -20,7 +20,7 @@ void execute(double delta_sec);
extern const bool IS_SOCKET[]; extern const bool IS_SOCKET[];
extern const bool IS_OUTPUT[]; extern const bool IS_OUTPUT[];
extern const int PORT_COUNT; extern const int32_t PORT_COUNT;
extern const char* PROGRAM_INTERFACE; extern const char* PROGRAM_INTERFACE;
} }
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#include "json.h" #include "json.h"
#include "program.h" #include "program.h"
#include "err_out.h"
#include "standard_err_out.h"
#if defined _WIN32 || defined _WIN64 #if defined _WIN32 || defined _WIN64
#define IS_WIN #define IS_WIN
...@@ -38,6 +40,9 @@ int main(int argc, char** argv) { ...@@ -38,6 +40,9 @@ int main(int argc, char** argv) {
} }
#endif #endif
// Use the "standard_err_out.h" functions for errors and messages from the autopilot
ERR_OUT_set_functions(ERR_OUT_standard_throw_error, ERR_OUT_standard_print_cout, ERR_OUT_standard_print_cerr);
if (argc == 2) { if (argc == 2) {
session_server(argv[1], simulation_session, running); session_server(argv[1], simulation_session, running);
return 0; return 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment