Skip to content

Commit

Permalink
Merge pull request #5 from openMSL/4-add-wait-time-as-fmi-parameter
Browse files Browse the repository at this point in the history
Add wait time as FMI parameter
  • Loading branch information
ClemensLinnhoff authored Jan 17, 2024
2 parents b6f5f76 + 7a744f4 commit e53ad0d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 21 additions & 5 deletions src/OSMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ fmi2Status OSMP::DoInit()
SetFmiPushPull(1);
SetFmiIp("127.0.0.1");
SetFmiPort("3456");
SetFmiWaitTime(5);

return fmi2OK;
}
Expand All @@ -154,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)
Expand All @@ -167,8 +167,13 @@ 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));
}
const int linger = 0;
zmq_setsockopt(socket_, ZMQ_LINGER, &linger, sizeof(linger));
socket_.bind(protocol);
}
else
Expand All @@ -181,8 +186,13 @@ 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));
}
const int linger = 0;
zmq_setsockopt(socket_, ZMQ_LINGER, &linger, sizeof(linger));
socket_.connect(protocol);
}

Expand Down Expand Up @@ -256,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;
}

Expand Down
13 changes: 11 additions & 2 deletions src/OSMP.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -125,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);

Expand Down Expand Up @@ -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;
}
};
3 changes: 3 additions & 0 deletions src/modelDescription.in.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<ScalarVariable name="port" valueReference="1" causality="parameter" variability="fixed" initial="exact">
<String start="3456"/>
</ScalarVariable>
<ScalarVariable name="wait_time_in_s" valueReference="6" causality="parameter" variability="fixed" initial="exact">
<Integer start="5"/>
</ScalarVariable>
</ModelVariables>
<ModelStructure>
<Outputs>
Expand Down

1 comment on commit e53ad0d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-tidy reports: 3 concern(s)
  • src/OSMP.cpp

    src/OSMP.cpp:50:16: warning: [cppcoreguidelines-avoid-non-const-global-variables]

    variable 'private_log_file' is non-const and globally accessible, consider making it const

    ofstream OSMP::private_log_file;
                   ^
    /home/runner/work/sl-5-3-osmp-network-proxy/sl-5-3-osmp-network-proxy/src/OSMP.cpp:139:20: warning: 5 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
        SetFmiWaitTime(5);
                       ^
  • src/OSMP.h

    src/OSMP.h:135:21: warning: [cppcoreguidelines-avoid-non-const-global-variables]

    variable 'private_log_file' is non-const and globally accessible, consider making it const

        static ofstream private_log_file;
                        ^

    src/OSMP.h:135:21: warning: [readability-identifier-naming]

    invalid case style for class member 'private_log_file'

        static ofstream private_log_file;
                        ^~~~~~~~~~~~~~~~
                        g_private_log_file
    /home/runner/work/sl-5-3-osmp-network-proxy/sl-5-3-osmp-network-proxy/src/OSMP.h:165:21: warning: 1024 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
            char buffer[1024];
                        ^
    /home/runner/work/sl-5-3-osmp-network-proxy/sl-5-3-osmp-network-proxy/src/OSMP.h:169:27: warning: 1024 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
            vsnprintf(buffer, 1024, format, arg);
                              ^

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.