Skip to content

Commit

Permalink
Proxyfmu encapsulation
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhjp01 committed Feb 21, 2025
1 parent 5cce227 commit c57de0c
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 24 deletions.
5 changes: 2 additions & 3 deletions include/proxyfmu/fmi/slave.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <proxyfmu/fmi/model_description.hpp>
#include <proxyfmu/state.hpp>
#include <proxyfmu/thrift/defs_types.h>

#include <memory>
#include <vector>
Expand Down Expand Up @@ -47,8 +46,8 @@ class slave
virtual void save_state(state_index stateIndex) = 0;
virtual void restore_state(state_index stateIndex) = 0;
virtual void release_state(state_index stateIndex) = 0;
virtual void export_state(state_index stateIndex, proxyfmu::thrift::ExportedState& exportedState) const = 0;
virtual state_index import_state(const proxyfmu::thrift::ExportedState& exportedState) = 0;
virtual void export_state(state_index stateIndex, state::exported_state& exportedState) const = 0;
virtual state_index import_state(const state::exported_state& exportedState) = 0;

virtual ~slave() = default;
};
Expand Down
9 changes: 9 additions & 0 deletions include/proxyfmu/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ struct saved_state
bool simStarted = false;
};

struct exported_state
{
std::int32_t schemeVersion;
std::string uuid;
std::string fmuState;
bool setupComplete;
bool simStarted;
};

}


Expand Down
9 changes: 4 additions & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,10 @@ set_target_properties(fmilibwrapper PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_include_directories(fmilibwrapper
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>"
"$<BUILD_INTERFACE:${generatedSourcesDir}>"
PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}"
)
target_link_libraries(fmilibwrapper PRIVATE FMILIB::FMILIB PUBLIC thrift::thrift)
target_link_libraries(fmilibwrapper PRIVATE FMILIB::FMILIB)
if (UNIX)
target_link_libraries(fmilibwrapper INTERFACE stdc++fs PRIVATE dl)
endif ()
Expand Down Expand Up @@ -117,11 +116,13 @@ target_include_directories(proxyfmu-client
"$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>"
PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}"
"${generatedSourcesDir}"
)
target_link_libraries(proxyfmu-client
PUBLIC
fmilibwrapper
#PRIVATE # Temporarily disabled due to https://github.com/conan-io/conan/issues/13302
thrift::thrift
Boost::filesystem
)
if (UNIX)
Expand All @@ -142,8 +143,6 @@ install(
${PROXYFMU_INSTALL_DESTINATIONS}
)
install(
DIRECTORY
"${publicHeaderDir}/proxyfmu"
"${generatedSourcesDir}/proxyfmu"
DIRECTORY "${publicHeaderDir}/proxyfmu"
DESTINATION "${PROXYFMU_HEADER_INSTALL_DIR}"
)
22 changes: 18 additions & 4 deletions src/proxyfmu/client/proxy_slave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,28 @@ void proxy_slave::release_state(state_index stateIndex)
client_->release_state(stateIndex);
}

void proxy_slave::export_state(state_index stateIndex, proxyfmu::thrift::ExportedState& es) const
void proxy_slave::export_state(state_index stateIndex, state::exported_state& es) const
{
client_->export_state(es, stateIndex);
ExportedState es_;
es_.simStarted = es.simStarted;
es_.setupComplete = es.setupComplete;
es_.fmuState = es.fmuState;
es_.schemeVersion = es.schemeVersion;
es_.uuid = es.uuid;

client_->export_state(es_, stateIndex);
}

state_index proxy_slave::import_state(const proxyfmu::thrift::ExportedState& exportedState)
state_index proxy_slave::import_state(const state::exported_state& exportedState)
{
return client_->import_state(exportedState);
ExportedState es_;
es_.simStarted = exportedState.simStarted;
es_.setupComplete = exportedState.setupComplete;
es_.fmuState = exportedState.fmuState;
es_.schemeVersion = exportedState.schemeVersion;
es_.uuid = exportedState.uuid;

return client_->import_state(es_);
}

} // namespace proxyfmu::client
4 changes: 2 additions & 2 deletions src/proxyfmu/client/proxy_slave.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class proxy_slave : public fmi::slave
void save_state(state_index stateIndex) override;
void restore_state(state_index stateIndex) override;
void release_state(state_index stateIndex) override;
void export_state(state_index stateIndex, proxyfmu::thrift::ExportedState& exportedState) const override;
state_index import_state(const proxyfmu::thrift::ExportedState& exportedState) override;
void export_state(state_index stateIndex, state::exported_state& exportedState) const override;
state_index import_state(const state::exported_state& exportedState) override;

~proxy_slave() override;
};
Expand Down
4 changes: 2 additions & 2 deletions src/proxyfmu/fmi/fmi1/fmi1_slave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,12 @@ void fmi1_slave::release_state(state_index)
throw std::runtime_error("State saving API is not supported for FMI 1.0");
}

void fmi1_slave::export_state(state_index, proxyfmu::thrift::ExportedState&) const
void fmi1_slave::export_state(state_index, state::exported_state&) const
{
throw std::runtime_error("State saving API is not supported for FMI 1.0");
}

state_index fmi1_slave::import_state(const proxyfmu::thrift::ExportedState&)
state_index fmi1_slave::import_state(const state::exported_state&)
{
throw std::runtime_error("State saving API is not supported for FMI 1.0");
}
Expand Down
4 changes: 2 additions & 2 deletions src/proxyfmu/fmi/fmi1/fmi1_slave.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class fmi1_slave : public slave
void save_state(state_index stateIndex) override;
void restore_state(state_index stateIndex) override;
void release_state(state_index stateIndex) override;
void export_state(state_index stateIndex, proxyfmu::thrift::ExportedState& es) const override;
state_index import_state(const proxyfmu::thrift::ExportedState& exportedState) override;
void export_state(state_index stateIndex, state::exported_state& es) const override;
state_index import_state(const state::exported_state& exportedState) override;

~fmi1_slave() override;
};
Expand Down
5 changes: 3 additions & 2 deletions src/proxyfmu/fmi/fmi2/fmi2_slave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <fmilib.h>

#include <cassert>
#include <exception>
#include <memory>

Expand Down Expand Up @@ -251,7 +252,7 @@ void fmi2_slave::release_state(state_index state)

}

void fmi2_slave::export_state(state_index stateIndex, proxyfmu::thrift::ExportedState& es) const
void fmi2_slave::export_state(state_index stateIndex, state::exported_state& es) const
{
const auto& savedState = savedStates_.at(stateIndex);

Expand Down Expand Up @@ -283,7 +284,7 @@ void fmi2_slave::export_state(state_index stateIndex, proxyfmu::thrift::Exported
es.simStarted = savedState.simStarted;
}

state_index fmi2_slave::import_state(const proxyfmu::thrift::ExportedState& exportedState)
state_index fmi2_slave::import_state(const state::exported_state& exportedState)
{
saved_state savedState;
// First some sanity checks
Expand Down
4 changes: 2 additions & 2 deletions src/proxyfmu/fmi/fmi2/fmi2_slave.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ class fmi2_slave : public slave
void save_state(state_index stateIndex) override;
void restore_state(state_index stateIndex) override;
void release_state(state_index stateIndex) override;
void export_state(state_index stateIndex, proxyfmu::thrift::ExportedState& es) const override;
state_index import_state(const proxyfmu::thrift::ExportedState& exportedState) override;
void export_state(state_index stateIndex, state::exported_state& es) const override;
state_index import_state(const state::exported_state& exportedState) override;

~fmi2_slave() override;
};
Expand Down
20 changes: 18 additions & 2 deletions tool/handlers/fmu_service_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,26 @@ void fmu_service_handler::release_state(const StateIndex idx)

void fmu_service_handler::export_state(ExportedState& _return, const StateIndex idx)
{
slave_->export_state(idx, _return);
proxyfmu::state::exported_state es;

slave_->export_state(idx, es);

_return.schemeVersion = es.schemeVersion;
_return.fmuState = es.fmuState;
_return.setupComplete = es.setupComplete;
_return.simStarted = es.simStarted;
_return.uuid = es.uuid;
}

StateIndex fmu_service_handler::import_state(const ExportedState& esin)
{
return slave_->import_state(esin);
proxyfmu::state::exported_state es;

es.schemeVersion = esin.schemeVersion;
es.fmuState = esin.fmuState;
es.setupComplete = esin.setupComplete;
es.simStarted = esin.simStarted;
es.uuid = esin.uuid;

return slave_->import_state(es);
}

0 comments on commit c57de0c

Please sign in to comment.