Skip to content

Commit

Permalink
Write zmq server connect string to file.
Browse files Browse the repository at this point in the history
Let zmq choose the port automatically and write it to a file.
  • Loading branch information
adeas31 authored and OpenModelica-Hudson committed Jun 15, 2017
1 parent f2b55cc commit 8ba253e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 10 deletions.
3 changes: 1 addition & 2 deletions Compiler/Main/Main.mo
Expand Up @@ -683,8 +683,7 @@ protected function interactivemodeZMQ
"Initiate the interactive mode using ZMQ communication."
input GlobalScript.SymbolTable symbolTable;
algorithm
print("Starting a ZeroMQ server on port " + intString(29500) + "\n");
serverLoopZMQ(true, ZeroMQ.initialize(29500), symbolTable);
serverLoopZMQ(true, ZeroMQ.initialize(), symbolTable);
end interactivemodeZMQ;

protected function serverLoopCorba
Expand Down
18 changes: 16 additions & 2 deletions Compiler/Util/Flags.mo
Expand Up @@ -57,6 +57,7 @@ encapsulated package Flags
public import Util;

protected import Corba;
protected import ZeroMQ;
protected import Error;
protected import ErrorExt;
protected import Global;
Expand Down Expand Up @@ -1355,6 +1356,9 @@ constant ConfigFlag TEARING_STRICTNESS = CONFIG_FLAG(113, "tearingStrictness",
("veryStrict", Util.gettext("Very strict tearing rules that do not allow to divide by any parameter. Use this if you aim at overriding parameters after compilation with values equal to or close to zero."))
})),
Util.gettext("Sets the strictness of the tearing method regarding the solvability restrictions."));
constant ConfigFlag ZEROMQ_FILE_SUFFIX = CONFIG_FLAG(114, "zeroMQFileSuffix",
SOME("z"), EXTERNAL(), STRING_FLAG(""), NONE(),
Util.gettext("Sets the file suffix for zeroMQ port file if -d=interactiveZMQ is used."));

protected
// This is a list of all configuration flags. A flag can not be used unless it's
Expand Down Expand Up @@ -1473,7 +1477,8 @@ constant list<ConfigFlag> allConfigFlags = {
CONDENSE_ARRAYS,
WFC_ADVANCED,
GRAPHICS_EXP_MODE,
TEARING_STRICTNESS
TEARING_STRICTNESS,
ZEROMQ_FILE_SUFFIX
};

public function new
Expand Down Expand Up @@ -2192,7 +2197,7 @@ algorithm
_ := matchcontinue(inFlag, inValue)
local
Boolean value;
String corba_name, corba_objid_path;
String corba_name, corba_objid_path, zeroMQFileSuffix;

// +showErrorMessages needs to be sent to the C runtime.
case (_, _)
Expand Down Expand Up @@ -2221,6 +2226,15 @@ algorithm
then
();

// The zeroMQ file suffix needs to be sent to the C runtime.
case (_, _)
equation
true = configFlagsIsEqualIndex(inFlag, ZEROMQ_FILE_SUFFIX);
STRING_FLAG(data = zeroMQFileSuffix) = inValue;
ZeroMQ.setFileSuffix(zeroMQFileSuffix);
then
();

else ();
end matchcontinue;
end applySideEffects;
Expand Down
9 changes: 7 additions & 2 deletions Compiler/Util/ZeroMQ.mo
Expand Up @@ -39,11 +39,16 @@ encapsulated package ZeroMQ
Used in interactive mode if omc is started with +d=interactiveZMQ
Implemented in ./runtime/zeromqimpl.c"

public function setFileSuffix
input String fileSuffix;

external "C" ZeroMQ_setFileSuffix(fileSuffix) annotation(Library = "omcruntime");
end setFileSuffix;

public function initialize
input Integer port;
output Option<Integer> zmqSocket;

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

public function handleRequest
Expand Down
59 changes: 55 additions & 4 deletions Compiler/runtime/zeromqimpl.c
Expand Up @@ -35,20 +35,66 @@

#include "modelica_string.h"

void* ZeroMQ_initialize(int port)
#include "settingsimpl.h"

char* zeroMQFilePath = 0;
char* zeroMQFileSuffix = 0;

void ZeroMQ_setFileSuffix(const char *fileSuffix)
{
if (strlen(fileSuffix) == 0) return;
if (zeroMQFileSuffix) free(zeroMQFileSuffix);

if (*fileSuffix) {
zeroMQFileSuffix = strdup(fileSuffix);
} else {
zeroMQFileSuffix = NULL;
}
}

void* ZeroMQ_initialize()
{
// 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);
char hostname[20];
sprintf(hostname, "tcp://*:%d", port);
int rc = zmq_bind(zmqSocket, hostname);
int rc = zmq_bind(zmqSocket, "tcp://127.0.0.1:*");
if (rc != 0) {
printf("Error creating ZeroMQ Server. zmq_bind failed: %s\n", strerror(errno));
return mmcZmqSocket;
}
// get the port number
const size_t endPointBufSize = 30;
char endPointBuf[endPointBufSize];
zmq_getsockopt(zmqSocket, ZMQ_LAST_ENDPOINT, &endPointBuf, (size_t *)&endPointBufSize);
// create the file path
const char* tempPath = SettingsImpl__getTempDirectoryPath();
#if defined(__MINGW32__) || defined(_MSC_VER)
if (zeroMQFileSuffix != NULL) {
zeroMQFilePath = (char*)malloc(strlen(tempPath) + strlen("/openmodelica.port.") + strlen(zeroMQFileSuffix) + 1);
sprintf(zeroMQFilePath, "%s/openmodelica.port.%s", tempPath, zeroMQFileSuffix);
} else {
zeroMQFilePath = (char*)malloc(strlen(tempPath) + strlen("/openmodelica.port") + 1);
sprintf(zeroMQFilePath, "%s/openmodelica.port", tempPath);
}
#else
char *tmp_user = getenv("USER");
if (zeroMQFileSuffix != NULL) {
zeroMQFilePath = (char*)malloc(strlen(tempPath) + strlen("/openmodelica.") + tmp_user ? strlen(tmp_user) : strlen("nobody") + strlen(zeroMQFileSuffix) + 1);
sprintf(zeroMQFilePath, "%s/openmodelica.%s.port.%s", tempPath, tmp_user, zeroMQFileSuffix);
} else {
zeroMQFilePath = (char*)malloc(strlen(tempPath) + strlen("/openmodelica.") + tmp_user ? strlen(tmp_user) : strlen("nobody") + 1);
sprintf(zeroMQFilePath, "%s/openmodelica.%s.port", tempPath, tmp_user);
}
#endif
// Create the file with port number
FILE *fp;
fp = fopen(zeroMQFilePath, "w");
fputs(endPointBuf, fp);
fclose(fp);
printf("Created ZeroMQ Server.\nDumped server port in file: %s", zeroMQFilePath);fflush(NULL);

mmcZmqSocket = mmc_mk_some(zmqSocket);
return mmcZmqSocket;
}
Expand Down Expand Up @@ -93,6 +139,11 @@ void ZeroMQ_sendReply(void *mmcZmqSocket, const char* reply)

void ZeroMQ_close(void *mmcZmqSocket)
{
if (zeroMQFilePath) {
remove(zeroMQFilePath);
free(zeroMQFilePath);
}
if (zeroMQFileSuffix) free(zeroMQFileSuffix);
// Convert the void* to ZeroMQ Socket
intptr_t zmqSocket = (intptr_t)MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(mmcZmqSocket),1));
// close the ZeroMQ socket
Expand Down

0 comments on commit 8ba253e

Please sign in to comment.