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

Fixes

parent afab8e82
package de.monticore.lang.monticar.generator.cpp.dynamic_interface;
import de.monticore.lang.monticar.generator.cpp.dynamic_interface.ProgramInterfaceResolver.SocketInfo;
import de.rwth.montisim.commons.dynamicinterface.*;
public class BinarySerializationGenerator extends SerializationGenerator {
......@@ -13,6 +14,45 @@ public class BinarySerializationGenerator extends SerializationGenerator {
return "Binary";
}
boolean isJson() {
return false;
}
void generateSocketSetter(PortInformation portInfo) {
SocketInfo sockInf = gen.interfaceResolver.getSocketInfo(portInfo);
int indent = BASE_INDENT+1;
// std::string ip = br.read_str();
b.a(indent, "auto ip = reader.read_str();");
b.a(indent, "auto id = get_socket_id(ip, %d);", sockInf.array_length);
b.a(indent, "if (id < 0) return;");
b.a(indent, "auto &target = program_instance.%s[id];", sockInf.input_name);
generateSetter(((SimplePacketType) portInfo.data_type).getPayloadType(), BASE_INDENT+1, 1, "target");
}
void generateSocketGetter(PortInformation portInfo) {
SocketInfo sockInf = gen.interfaceResolver.getSocketInfo(portInfo);
int indent = BASE_INDENT+1;
b.a(indent, "static int id = 0;");
b.a(indent, "if (id >= %d) {", sockInf.is_bc ? 1 : sockInf.array_length);
b.a(indent, " id = 0;");
b.a(indent, " return;");
b.a(indent, "}");
if (sockInf.is_bc) {
b.a(indent, "writer.write_str(N_TO_N_BROADCAST_ADDR); // Write address");
b.a(indent, "auto &target = program_instance.%s;", sockInf.output_name);
} else {
b.a(indent, "writer.write_str(N_TO_N_PREFIX + std::to_string(id+1)); // Write address");
b.a(indent, "auto &target = program_instance.%s[id];", sockInf.output_name);
}
generateGetter(((SimplePacketType) portInfo.data_type).getPayloadType(), BASE_INDENT+1, 1, "target");
b.a(indent, "++id;");
}
protected void generateSetter(DataType type, int indent, int depth, String varReference){
if (type instanceof BasicType){
......@@ -115,6 +155,7 @@ public class BinarySerializationGenerator extends SerializationGenerator {
case N:
case N1:
b.a(indent, "writer.write_u32(%s);", varReference);
break;
case BOOLEAN:
b.a(indent, "writer.write_u8(%s ? 1 : 0);", varReference);
break;
......
package de.monticore.lang.monticar.generator.cpp.dynamic_interface;
import de.monticore.lang.monticar.generator.cpp.dynamic_interface.ProgramInterfaceResolver.SocketInfo;
import de.rwth.montisim.commons.dynamicinterface.*;
public class JsonSerializationGenerator extends SerializationGenerator {
......@@ -12,6 +13,58 @@ public class JsonSerializationGenerator extends SerializationGenerator {
return "Json";
}
boolean isJson() {
return true;
}
void generateSocketSetter(PortInformation portInfo) {
SocketInfo sockInf = gen.interfaceResolver.getSocketInfo(portInfo);
int indent = BASE_INDENT+1;
b.a(indent, "auto as = reader.stream_array();");
b.a(indent, "auto it = as.begin();");
b.a(indent, "auto end = as.end();");
b.a(indent, "if (!(it != end)) {");
b.a(indent, " cerr << \"Expected IP entry in array\" << endl;");
b.a(indent, " break;");
b.a(indent, "}");
b.a(indent, "auto ip = reader.get_string().get_json_string();");
b.a(indent, "auto id = get_socket_id(ip, %d);", sockInf.array_length);
b.a(indent, "if (id < 0) return;");
b.a(indent, "auto &target = program_instance.%s[id];", sockInf.input_name);
b.a(indent, "++it;");
b.a(indent, "if (!(it != end)) {");
b.a(indent, " cerr << \"Expected IP entry in array\" << endl;");
b.a(indent, " break;");
b.a(indent, "}");
generateSetter(((SimplePacketType) portInfo.data_type).getPayloadType(), indent, 1, "target");
}
void generateSocketGetter(PortInformation portInfo) {
SocketInfo sockInf = gen.interfaceResolver.getSocketInfo(portInfo);
int indent = BASE_INDENT+1;
b.a(indent, "static int id = 0;");
b.a(indent, "if (id >= %d) {", sockInf.is_bc ? 1 : sockInf.array_length);
b.a(indent, " id = 0;");
b.a(indent, " return;");
b.a(indent, "}");
b.a(indent, "writer.start_array();");
if (sockInf.is_bc) {
b.a(indent, "writer.write_str(N_TO_N_BROADCAST_ADDR); // Write address");
b.a(indent, "auto &target = program_instance.%s;", sockInf.output_name);
} else {
b.a(indent, "writer.write_str(N_TO_N_PREFIX + std::to_string(id+1)); // Write address");
b.a(indent, "auto &target = program_instance.%s[id];", sockInf.output_name);
}
generateGetter(((SimplePacketType) portInfo.data_type).getPayloadType(), BASE_INDENT+1, 1, "target");
b.a(indent, "writer.end_array();");
b.a(indent, "++id;");
}
protected void generateSetter(DataType type, int indent, int depth, String varReference){
if (type instanceof BasicType){
......
......@@ -27,11 +27,12 @@ public abstract class SerializationGenerator {
int i = 0;
for (PortInformation portInfo : gen.interfaceResolver.programInterface.ports){
if (portInfo.direction == PortDirection.INPUT){
setPortCases.add(generateSetPortCase(i, portInfo));
} else {
if (portInfo.isOutput()) {
getPortCases.add(generateGetPortCase(i, portInfo));
}
if (portInfo.isInput()) {
setPortCases.add(generateSetPortCase(i, portInfo));
}
++i;
}
......@@ -46,18 +47,9 @@ public abstract class SerializationGenerator {
b.a(BASE_INDENT, "case %d: { // %s", id, portInfo.name);
if (portInfo.port_type == PortType.DATA) {
generateSetter(portInfo.data_type, BASE_INDENT+1, 1, "program_instance."+portInfo.name);
} else {
SocketInfo sockInf = gen.interfaceResolver.getSocketInfo(portInfo);
int indent = BASE_INDENT+1;
b.a(indent, "auto id = get_socket_id(br, %d);", sockInf.array_length);
b.a(indent, "if (id < 0) return;");
b.a(indent, "auto &target = program_instance.%s[id];", sockInf.input_name);
generateSetter(((SimplePacketType) portInfo.data_type).getPayloadType(), BASE_INDENT+1, 1, "target");
generateSocketSetter(portInfo);
}
b.a(BASE_INDENT, "} break;");
......@@ -70,42 +62,19 @@ public abstract class SerializationGenerator {
b.a(BASE_INDENT, "case %d: { // %s", id, portInfo.name);
if (portInfo.port_type == PortType.DATA) {
generateGetter(portInfo.data_type, BASE_INDENT+1, 1, "program_instance."+portInfo.name);
} else {
SocketInfo sockInf = gen.interfaceResolver.getSocketInfo(portInfo);
int indent = BASE_INDENT+1;
if (sockInf.is_bc) {
b.a(indent, "static bool sent = false;");
b.a(indent, "if (sent) {", sockInf.array_length);
b.a(indent, " sent = false;");
b.a(indent, " return;");
b.a(indent, "}", sockInf.array_length);
b.a(indent, "writer.write_str(N_TO_N_BROADCAST_ADDR); // Write address");
b.a(indent, "auto &target = program_instance.%s;", sockInf.output_name);
b.a(indent, "sent = true;");
} else {
b.a(indent, "static int id = 0;");
b.a(indent, "if (id >= %d) {", sockInf.array_length);
b.a(indent, " id = 0;");
b.a(indent, " return;");
b.a(indent, "}", sockInf.array_length);
b.a(indent, "writer.write_str(N_TO_N_PREFIX + std::to_string(id+1)); // Write address");
b.a(indent, "auto &target = program_instance.%s[id];", sockInf.output_name);
b.a(indent, "++id;");
}
generateGetter(((SimplePacketType) portInfo.data_type).getPayloadType(), BASE_INDENT+1, 1, "target");
generateSocketGetter(portInfo);
}
b.a(BASE_INDENT, "} break;");
return b.getContent();
}
protected abstract void generateSetter(DataType type, int indent, int depth, String varReference);
protected abstract void generateGetter(DataType type, int indent, int depth, String varReference);
abstract void generateSocketGetter(PortInformation portInfo);
abstract void generateSocketSetter(PortInformation portInfo);
abstract String getType();
abstract boolean isJson(); // Not ideal
}
Subproject commit 4e269938880649bf1af498d36178bc1812be1757
Subproject commit 5b49cc13ecccb22cbdd41a33787e7b7583781e8a
......@@ -28,7 +28,7 @@ EXPORT const char* DI__get_port(int i, int is_json) {
auto payload_size = bw.buffer.position() - 4;
bw.buffer.go_to(0);
bw.write_u32(payload_size);
return bw.buffer.buffer;
return bw.buffer.get_buffer();
} else {
JsonWriter writer {autopilot::buffer};
autopilot::get_port_json(i, writer);
......
......@@ -191,7 +191,7 @@ void SimulationSession::send_outputs() {
} else {
autopilot::get_port_binary(i, packet.bw);
}
if (!packet.has_payload()) break;
if (packet.buffer.position() == 5) break;
packet.send(socket);
if (!autopilot::IS_SOCKET[i]) break;
} while(true);
......
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