From ec5e847b768b16ea26ee410278c711f9a790f937 Mon Sep 17 00:00:00 2001 From: ClemensLinnhoff Date: Wed, 17 Jan 2024 08:52:10 +0100 Subject: [PATCH 1/3] Add wait time as FMI parameter Signed-off-by: ClemensLinnhoff --- README.md | 15 ++++++++------- src/OSMP.cpp | 15 +++++++++++---- src/OSMP.h | 11 ++++++++++- src/modelDescription.in.xml | 3 +++ 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index fa6b347..8a3585d 100644 --- a/README.md +++ b/README.md @@ -12,13 +12,14 @@ The following FMI parameters can be set. Either `sender` or `receiver` have to be set to _true_. Otherwise, the FMU will return with an error. -| Type | Parameter | Default | Description | -|---------|------------|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| Boolean | `sender` | _true_ | Set if Proxy shall send data via TCP/IP | -| Boolean | `receiver` | _false_ | Set if Proxy shall receive data via TCP/IP | -| Boolean | `pushpull` | _false_ | If true, use [Push/Pull](https://learning-0mq-with-pyzmq.readthedocs.io/en/latest/pyzmq/patterns/pushpull.html) ZeroMQ configuration, if false, use [Client/Server](https://learning-0mq-with-pyzmq.readthedocs.io/en/latest/pyzmq/patterns/client_server.html) config. | -| String | `ip` | _127.0.0.1_ | IP address of TCP connection | -| String | `port` | _3456_ | Port of TCP connection | +| Type | Parameter | Default | Description | +|---------|------------------|-------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| Boolean | `sender` | _true_ | Set if Proxy shall send data via TCP/IP | +| Boolean | `receiver` | _false_ | Set if Proxy shall receive data via TCP/IP | +| Boolean | `pushpull` | _false_ | If true, use [Push/Pull](https://learning-0mq-with-pyzmq.readthedocs.io/en/latest/pyzmq/patterns/pushpull.html) ZeroMQ configuration, if false, use [Client/Server](https://learning-0mq-with-pyzmq.readthedocs.io/en/latest/pyzmq/patterns/client_server.html) config. | +| String | `ip` | _127.0.0.1_ | IP address of TCP connection | +| String | `port` | _3456_ | Port of TCP connection | +| Integer | `wait_time_in_s` | _5_ | In Client/Server configuration: wait time to receive message after request. When the network proxy does not receive a message in this wait time, it will throw an error. If this is set to _0_, the network proxy will wait indefinitely. | ## Interface diff --git a/src/OSMP.cpp b/src/OSMP.cpp index 80316c3..483c35f 100644 --- a/src/OSMP.cpp +++ b/src/OSMP.cpp @@ -136,6 +136,7 @@ fmi2Status OSMP::DoInit() SetFmiPushPull(1); SetFmiIp("127.0.0.1"); SetFmiPort("3456"); + SetFmiWaitTime(5); return fmi2OK; } @@ -167,8 +168,11 @@ fmi2Status OSMP::DoExitInitializationMode() { socket_ = zmq::socket_t(context_, ZMQ_REP); } - const int wait_time_ms = 5000; - zmq_setsockopt(socket_, ZMQ_SNDTIMEO, &wait_time_ms, sizeof(wait_time_ms)); + if (FmiWaitTime() > 0) + { + const int wait_time_ms = FmiWaitTime() * 1000; + zmq_setsockopt(socket_, ZMQ_SNDTIMEO, &wait_time_ms, sizeof(wait_time_ms)); + } socket_.bind(protocol); } else @@ -181,8 +185,11 @@ fmi2Status OSMP::DoExitInitializationMode() { socket_ = zmq::socket_t(context_, ZMQ_REQ); } - const int wait_time_ms = 5000; - zmq_setsockopt(socket_, ZMQ_RCVTIMEO, &wait_time_ms, sizeof(wait_time_ms)); + if (FmiWaitTime() > 0) + { + const int wait_time_ms = FmiWaitTime() * 1000; + zmq_setsockopt(socket_, ZMQ_RCVTIMEO, &wait_time_ms, sizeof(wait_time_ms)); + } socket_.connect(protocol); } diff --git a/src/OSMP.h b/src/OSMP.h index b90dc39..dab6c0c 100644 --- a/src/OSMP.h +++ b/src/OSMP.h @@ -54,7 +54,8 @@ #define FMI_INTEGER_OSI_OUT_BASELO_IDX 3 #define FMI_INTEGER_OSI_OUT_BASEHI_IDX 4 #define FMI_INTEGER_OSI_OUT_SIZE_IDX 5 -#define FMI_INTEGER_LAST_IDX FMI_INTEGER_OSI_OUT_SIZE_IDX +#define FMI_INTEGER_WAIT_TIME_IDX 6 +#define FMI_INTEGER_LAST_IDX FMI_INTEGER_WAIT_TIME_IDX #define FMI_INTEGER_VARS (FMI_INTEGER_LAST_IDX + 1) /* Real Variables */ @@ -277,4 +278,12 @@ class OSMP { string_vars_[FMI_STRING_PORT_IDX] = value; } + int FmiWaitTime() + { + return integer_vars_[FMI_INTEGER_WAIT_TIME_IDX]; + } + void SetFmiWaitTime(fmi2Integer value) + { + integer_vars_[FMI_INTEGER_WAIT_TIME_IDX] = value; + } }; diff --git a/src/modelDescription.in.xml b/src/modelDescription.in.xml index 3df024c..a5e5393 100644 --- a/src/modelDescription.in.xml +++ b/src/modelDescription.in.xml @@ -60,6 +60,9 @@ + + + From 2f2216e72783683513f26edbf4c9512c6a309ce7 Mon Sep 17 00:00:00 2001 From: ClemensLinnhoff Date: Wed, 17 Jan 2024 11:22:38 +0100 Subject: [PATCH 2/3] Set linger time to 0 to properly close the sockets during termination of the FMU. Signed-off-by: ClemensLinnhoff --- src/OSMP.cpp | 11 ++++++++++- src/OSMP.h | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/OSMP.cpp b/src/OSMP.cpp index 483c35f..44a890f 100644 --- a/src/OSMP.cpp +++ b/src/OSMP.cpp @@ -155,7 +155,6 @@ fmi2Status OSMP::DoExitInitializationMode() { string address = "tcp://" + FmiIp() + ":" + FmiPort(); - std::cout << address << std::endl; const char* protocol = address.c_str(); if (FmiSender() != 0) @@ -173,6 +172,8 @@ fmi2Status OSMP::DoExitInitializationMode() const int wait_time_ms = FmiWaitTime() * 1000; zmq_setsockopt(socket_, ZMQ_SNDTIMEO, &wait_time_ms, sizeof(wait_time_ms)); } + const int linger = 0; + zmq_setsockopt(socket_, ZMQ_LINGER, &linger, sizeof (linger)); socket_.bind(protocol); } else @@ -190,6 +191,8 @@ fmi2Status OSMP::DoExitInitializationMode() const int wait_time_ms = FmiWaitTime() * 1000; zmq_setsockopt(socket_, ZMQ_RCVTIMEO, &wait_time_ms, sizeof(wait_time_ms)); } + const int linger = 0; + zmq_setsockopt(socket_, ZMQ_LINGER, &linger, sizeof (linger)); socket_.connect(protocol); } @@ -263,6 +266,12 @@ fmi2Status OSMP::DoCalc(fmi2Real current_communication_point, fmi2Real communica fmi2Status OSMP::DoTerm() { + string address = "tcp://" + FmiIp() + ":" + FmiPort(); + const char* protocol = address.c_str(); + socket_.unbind(protocol); + socket_.close(); + context_.shutdown(); + context_.close(); return fmi2OK; } diff --git a/src/OSMP.h b/src/OSMP.h index dab6c0c..b623149 100644 --- a/src/OSMP.h +++ b/src/OSMP.h @@ -126,7 +126,7 @@ class OSMP static fmi2Status DoEnterInitializationMode(); fmi2Status DoExitInitializationMode(); fmi2Status DoCalc(fmi2Real current_communication_point, fmi2Real communication_step_size, fmi2Boolean no_set_fmu_state_prior_to_current_pointfmi_2_component); - static fmi2Status DoTerm(); + fmi2Status DoTerm(); void DoFree(); void ProcessMessage(zmq::message_t& message); From 7a744f42f0442c15fe920362404d82d8bf727da8 Mon Sep 17 00:00:00 2001 From: ClemensLinnhoff Date: Wed, 17 Jan 2024 11:31:22 +0100 Subject: [PATCH 3/3] Fix formatting Signed-off-by: ClemensLinnhoff --- src/OSMP.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OSMP.cpp b/src/OSMP.cpp index 44a890f..2316b42 100644 --- a/src/OSMP.cpp +++ b/src/OSMP.cpp @@ -173,7 +173,7 @@ fmi2Status OSMP::DoExitInitializationMode() zmq_setsockopt(socket_, ZMQ_SNDTIMEO, &wait_time_ms, sizeof(wait_time_ms)); } const int linger = 0; - zmq_setsockopt(socket_, ZMQ_LINGER, &linger, sizeof (linger)); + zmq_setsockopt(socket_, ZMQ_LINGER, &linger, sizeof(linger)); socket_.bind(protocol); } else @@ -192,7 +192,7 @@ fmi2Status OSMP::DoExitInitializationMode() zmq_setsockopt(socket_, ZMQ_RCVTIMEO, &wait_time_ms, sizeof(wait_time_ms)); } const int linger = 0; - zmq_setsockopt(socket_, ZMQ_LINGER, &linger, sizeof (linger)); + zmq_setsockopt(socket_, ZMQ_LINGER, &linger, sizeof(linger)); socket_.connect(protocol); }