Skip to content

Commit

Permalink
Add an --interactivePort flag to force ZMQ to set a port (#6949)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoelund authored and adrpo committed Nov 16, 2020
1 parent 9124c0c commit 68245f4
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/Main/Main.mo
Expand Up @@ -642,7 +642,7 @@ protected
String str,replystr,suffix;
algorithm
suffix := Flags.getConfigString(Flags.ZEROMQ_FILE_SUFFIX);
zmqSocket := ZeroMQ.initialize(if suffix=="" then "" else ("."+suffix), Flags.isSet(Flags.ZMQ_LISTEN_TO_ALL));
zmqSocket := ZeroMQ.initialize(if suffix=="" then "" else ("."+suffix), Flags.isSet(Flags.ZMQ_LISTEN_TO_ALL), Flags.getConfigInt(Flags.INTERACTIVE_PORT));
false := valueEq(SOME(0), zmqSocket);
while true loop
str := ZeroMQ.handleRequest(zmqSocket);
Expand Down
16 changes: 10 additions & 6 deletions OMCompiler/Compiler/Util/Flags.mo
Expand Up @@ -1334,25 +1334,25 @@ constant ConfigFlag MAX_SIZE_ASSC = CONFIG_FLAG(133, "maxSizeASSC",
Gettext.gettext("Sets the maximum system size for the analytical to structural conversion algorithm (default 200)."));

constant ConfigFlag USE_ZEROMQ_IN_SIM = CONFIG_FLAG(134, "useZeroMQInSim",
NONE(), EXTERNAL(), BOOL_FLAG(false), NONE(),
NONE(), INTERNAL(), BOOL_FLAG(false), NONE(),
Gettext.gettext("Configures to use zeroMQ in simulation runtime to exchange information via ZeroMQ with other applications"));

constant ConfigFlag ZEROMQ_PUB_PORT = CONFIG_FLAG(135, "zeroMQPubPort",
NONE(), EXTERNAL(), INT_FLAG(3203), NONE(),
NONE(), INTERNAL(), INT_FLAG(3203), NONE(),
Gettext.gettext("Configures port number for simulation runtime to send information via ZeroMQ"));

constant ConfigFlag ZEROMQ_SUB_PORT = CONFIG_FLAG(136, "zeroMQSubPort",
NONE(), EXTERNAL(), INT_FLAG(3204), NONE(),
NONE(), INTERNAL(), INT_FLAG(3204), NONE(),
Gettext.gettext("Configures port number for simulation runtime to receive information via ZeroMQ"));

constant ConfigFlag ZEROMQ_JOB_ID = CONFIG_FLAG(137, "zeroMQJOBID",
NONE(), EXTERNAL(), STRING_FLAG("empty"), NONE(),
NONE(), INTERNAL(), STRING_FLAG("empty"), NONE(),
Gettext.gettext("Configures the ID with which the omc api call is labelled for zeroMQ communication."));
constant ConfigFlag ZEROMQ_SERVER_ID = CONFIG_FLAG(138, "zeroMQServerID",
NONE(), EXTERNAL(), STRING_FLAG("empty"), NONE(),
NONE(), INTERNAL(), STRING_FLAG("empty"), NONE(),
Gettext.gettext("Configures the ID with which server application is labelled for zeroMQ communication."));
constant ConfigFlag ZEROMQ_CLIENT_ID = CONFIG_FLAG(139, "zeroMQClientID",
NONE(), EXTERNAL(), STRING_FLAG("empty"), NONE(),
NONE(), INTERNAL(), STRING_FLAG("empty"), NONE(),
Gettext.gettext("Configures the ID with which the client application is labelled for zeroMQ communication."));

constant ConfigFlag FMI_VERSION = CONFIG_FLAG(140,
Expand Down Expand Up @@ -1385,6 +1385,10 @@ constant ConfigFlag NEW_BACKEND = CONFIG_FLAG(145, "newBackend",
NONE(), EXTERNAL(), BOOL_FLAG(false), NONE(),
Gettext.gettext("Activates experimental new backend for better array handling. This also activates the new frontend. [WIP]"));

constant ConfigFlag INTERACTIVE_PORT = CONFIG_FLAG(146, "interactivePort",
NONE(), INTERNAL(), INT_FLAG(0), NONE(),
Gettext.gettext("Sets the port used by the interactive server."));

function getFlags
"Loads the flags with getGlobalRoot. Assumes flags have been loaded."
input Boolean initialize = true;
Expand Down
3 changes: 2 additions & 1 deletion OMCompiler/Compiler/Util/FlagsUtil.mo
Expand Up @@ -404,7 +404,8 @@ constant list<Flags.ConfigFlag> allConfigFlags = {
Flags.FMI_FILTER,
Flags.FMI_SOURCES,
Flags.FMI_FLAGS,
Flags.NEW_BACKEND
Flags.NEW_BACKEND,
Flags.INTERACTIVE_PORT
};

public function new
Expand Down
3 changes: 2 additions & 1 deletion OMCompiler/Compiler/Util/ZeroMQ.mo
Expand Up @@ -42,9 +42,10 @@ encapsulated package ZeroMQ
public function initialize
input String fileSuffix;
input Boolean listenToAll;
input Integer port;
output Option<Integer> zmqSocket;

external "C" zmqSocket = ZeroMQ_initialize(fileSuffix, listenToAll) annotation(Library = "omcruntime");
external "C" zmqSocket = ZeroMQ_initialize(fileSuffix, listenToAll, port) annotation(Library = "omcruntime");
end initialize;

public function handleRequest
Expand Down
11 changes: 9 additions & 2 deletions OMCompiler/Compiler/runtime/zeromqimpl.c
Expand Up @@ -40,14 +40,21 @@

char* zeroMQFilePath = 0;

void* ZeroMQ_initialize(const char *zeroMQFileSuffix, int listenToAll)
void* ZeroMQ_initialize(const char *zeroMQFileSuffix, int listenToAll, int port)
{
// Create a pointer for storing the ZeroMQ socket
void *mmcZmqSocket = mmc_mk_some(0);
// Create the ZeroMQ context
void *context = zmq_ctx_new();
void *zmqSocket = zmq_socket(context, ZMQ_REP);
int rc = zmq_bind(zmqSocket, listenToAll ? "tcp://*:*" : "tcp://127.0.0.1:*");
const char *bindstr;
int rc;
if (port == 0) {
bindstr = listenToAll ? "tcp://*:*" : "tcp://127.0.0.1:*";
} else {
GC_asprintf(&bindstr, "tcp://%s:%d", listenToAll ? "*" : "127.0.0.1", port);
}
rc = zmq_bind(zmqSocket, bindstr);
if (rc != 0) {
printf("Error creating ZeroMQ Server. zmq_bind failed: %s\n", strerror(errno));
return mmcZmqSocket;
Expand Down

0 comments on commit 68245f4

Please sign in to comment.