Skip to content

Commit

Permalink
Enable use of client/server config with ZeroMQ
Browse files Browse the repository at this point in the history
Signed-off-by: ClemensLinnhoff <clemens.linnhoff@partner.bmw.de>
  • Loading branch information
ClemensLinnhoff committed Oct 10, 2023
1 parent 28cdee8 commit d8f55eb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ 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 |
| 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 |

## Interface

Expand Down
38 changes: 32 additions & 6 deletions src/OSMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,10 @@ fmi2Status OSMP::DoInit()
// Set default values
SetFmiSender(1);
SetFmiReceiver(0);
SetFmiPushPull(1);
SetFmiIp("127.0.0.1");
SetFmiPort("3456");

// todo: set default values

return fmi2OK;
}

Expand All @@ -156,22 +155,36 @@ fmi2Status OSMP::DoExitInitializationMode()
string address = "tcp://" + FmiIp() + ":" + FmiPort();
std::cout << address << std::endl;
const char* protocol = address.c_str();

if (FmiSender() != 0)
{
socket_ = zmq::socket_t(context_, ZMQ_PUSH);
if (FmiPushPull()) // Push/Pull configuration

Check warning on line 161 in src/OSMP.cpp

View workflow job for this annotation

GitHub Actions / CL 1 / C++ Linter / C++ Linter

/src/OSMP.cpp:161:13 [readability-implicit-bool-conversion]

implicit conversion 'fmi2Boolean' (aka 'int') -> bool
{
socket_ = zmq::socket_t(context_, ZMQ_PUSH);
}
else // Server/Client configuration
{
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));
socket_.bind(protocol);
std::cout << "push" << std::endl;
}
else
{
socket_ = zmq::socket_t(context_, ZMQ_PULL);
if (FmiPushPull()) // Push/Pull configuration

Check warning on line 175 in src/OSMP.cpp

View workflow job for this annotation

GitHub Actions / CL 1 / C++ Linter / C++ Linter

/src/OSMP.cpp:175:13 [readability-implicit-bool-conversion]

implicit conversion 'fmi2Boolean' (aka 'int') -> bool
{
socket_ = zmq::socket_t(context_, ZMQ_PULL);
}
else // Server/Client configuration
{
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));
socket_.connect(protocol);
std::cout << "pull" << std::endl;
}

return fmi2OK;
}

Expand All @@ -193,6 +206,11 @@ fmi2Status OSMP::DoCalc(fmi2Real current_communication_point, fmi2Real communica

if (FmiSender() != 0)
{
if (!FmiPushPull()) // Server/Client configuration

Check warning on line 209 in src/OSMP.cpp

View workflow job for this annotation

GitHub Actions / CL 1 / C++ Linter / C++ Linter

/src/OSMP.cpp:209:14 [readability-implicit-bool-conversion]

implicit conversion 'fmi2Boolean' (aka 'int') -> bool
{
zmq::message_t request_message;
socket_.recv(request_message, zmq::recv_flags::none);
}
zmq::message_t send_message(buffer, buffer_size, nullptr);
auto success = socket_.send(send_message, zmq::send_flags::none);
if (success.has_value())
Expand All @@ -207,6 +225,13 @@ fmi2Status OSMP::DoCalc(fmi2Real current_communication_point, fmi2Real communica
}
else if (FmiReceiver() != 0)
{
if (!FmiPushPull()) // Server/Client configuration

Check warning on line 228 in src/OSMP.cpp

View workflow job for this annotation

GitHub Actions / CL 1 / C++ Linter / C++ Linter

/src/OSMP.cpp:228:14 [readability-implicit-bool-conversion]

implicit conversion 'fmi2Boolean' (aka 'int') -> bool
{
std::stringstream s;
s << "ready";
zmq::message_t send_message(s.str().data(), s.str().size());
socket_.send(send_message, zmq::send_flags::none);
}
zmq::message_t rec_message;
auto success = socket_.recv(rec_message, zmq::recv_flags::none);
if (success.has_value())
Expand All @@ -224,6 +249,7 @@ fmi2Status OSMP::DoCalc(fmi2Real current_communication_point, fmi2Real communica
NormalLog("OSMP", "Either sender or receiver has to be set to true.");
output_status = fmi2Error;
}

return output_status;
}

Expand Down
5 changes: 4 additions & 1 deletion src/OSMP.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
#define FMI_BOOLEAN_VALID_IDX 0
#define FMI_BOOLEAN_SENDER_IDX 1
#define FMI_BOOLEAN_RECEIVER_IDX 2
#define FMI_BOOLEAN_LAST_IDX FMI_BOOLEAN_RECEIVER_IDX
#define FMI_BOOLEAN_PUSHPULL_IDX 3
#define FMI_BOOLEAN_LAST_IDX FMI_BOOLEAN_PUSHPULL_IDX
#define FMI_BOOLEAN_VARS (FMI_BOOLEAN_LAST_IDX + 1)

/* Integer Variables */
Expand Down Expand Up @@ -233,6 +234,8 @@ class OSMP
void SetFmiReceiver(fmi2Boolean value) { boolean_vars_[FMI_BOOLEAN_RECEIVER_IDX] = value; }
fmi2Boolean FmiSender() { return boolean_vars_[FMI_BOOLEAN_SENDER_IDX]; }
void SetFmiSender(fmi2Boolean value) { boolean_vars_[FMI_BOOLEAN_SENDER_IDX] = value; }
fmi2Boolean FmiPushPull() { return boolean_vars_[FMI_BOOLEAN_PUSHPULL_IDX]; }
void SetFmiPushPull(fmi2Boolean value) { boolean_vars_[FMI_BOOLEAN_PUSHPULL_IDX] = value; }
string FmiIp() { return string_vars_[FMI_STRING_IP_IDX]; }
void SetFmiIp(fmi2String value) { string_vars_[FMI_STRING_IP_IDX] = value; }
string FmiPort() { return string_vars_[FMI_STRING_PORT_IDX]; }
Expand Down
3 changes: 3 additions & 0 deletions src/modelDescription.in.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
<ScalarVariable name="receiver" valueReference="2" causality="parameter" variability="fixed" initial="exact">
<Boolean start="false"/>
</ScalarVariable>
<ScalarVariable name="pushpull" valueReference="3" causality="parameter" variability="fixed" initial="exact">
<Boolean start="true"/>
</ScalarVariable>
<ScalarVariable name="ip" valueReference="0" causality="parameter" variability="fixed" initial="exact">
<String start="127.0.0.1"/>
</ScalarVariable>
Expand Down

0 comments on commit d8f55eb

Please sign in to comment.