Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 8ba253e

Browse files
adeas31OpenModelica-Hudson
authored andcommitted
Write zmq server connect string to file.
Let zmq choose the port automatically and write it to a file.
1 parent f2b55cc commit 8ba253e

File tree

4 files changed

+79
-10
lines changed

4 files changed

+79
-10
lines changed

Compiler/Main/Main.mo

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,8 +683,7 @@ protected function interactivemodeZMQ
683683
"Initiate the interactive mode using ZMQ communication."
684684
input GlobalScript.SymbolTable symbolTable;
685685
algorithm
686-
print("Starting a ZeroMQ server on port " + intString(29500) + "\n");
687-
serverLoopZMQ(true, ZeroMQ.initialize(29500), symbolTable);
686+
serverLoopZMQ(true, ZeroMQ.initialize(), symbolTable);
688687
end interactivemodeZMQ;
689688

690689
protected function serverLoopCorba

Compiler/Util/Flags.mo

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ encapsulated package Flags
5757
public import Util;
5858

5959
protected import Corba;
60+
protected import ZeroMQ;
6061
protected import Error;
6162
protected import ErrorExt;
6263
protected import Global;
@@ -1355,6 +1356,9 @@ constant ConfigFlag TEARING_STRICTNESS = CONFIG_FLAG(113, "tearingStrictness",
13551356
("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."))
13561357
})),
13571358
Util.gettext("Sets the strictness of the tearing method regarding the solvability restrictions."));
1359+
constant ConfigFlag ZEROMQ_FILE_SUFFIX = CONFIG_FLAG(114, "zeroMQFileSuffix",
1360+
SOME("z"), EXTERNAL(), STRING_FLAG(""), NONE(),
1361+
Util.gettext("Sets the file suffix for zeroMQ port file if -d=interactiveZMQ is used."));
13581362

13591363
protected
13601364
// This is a list of all configuration flags. A flag can not be used unless it's
@@ -1473,7 +1477,8 @@ constant list<ConfigFlag> allConfigFlags = {
14731477
CONDENSE_ARRAYS,
14741478
WFC_ADVANCED,
14751479
GRAPHICS_EXP_MODE,
1476-
TEARING_STRICTNESS
1480+
TEARING_STRICTNESS,
1481+
ZEROMQ_FILE_SUFFIX
14771482
};
14781483

14791484
public function new
@@ -2192,7 +2197,7 @@ algorithm
21922197
_ := matchcontinue(inFlag, inValue)
21932198
local
21942199
Boolean value;
2195-
String corba_name, corba_objid_path;
2200+
String corba_name, corba_objid_path, zeroMQFileSuffix;
21962201

21972202
// +showErrorMessages needs to be sent to the C runtime.
21982203
case (_, _)
@@ -2221,6 +2226,15 @@ algorithm
22212226
then
22222227
();
22232228

2229+
// The zeroMQ file suffix needs to be sent to the C runtime.
2230+
case (_, _)
2231+
equation
2232+
true = configFlagsIsEqualIndex(inFlag, ZEROMQ_FILE_SUFFIX);
2233+
STRING_FLAG(data = zeroMQFileSuffix) = inValue;
2234+
ZeroMQ.setFileSuffix(zeroMQFileSuffix);
2235+
then
2236+
();
2237+
22242238
else ();
22252239
end matchcontinue;
22262240
end applySideEffects;

Compiler/Util/ZeroMQ.mo

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,16 @@ encapsulated package ZeroMQ
3939
Used in interactive mode if omc is started with +d=interactiveZMQ
4040
Implemented in ./runtime/zeromqimpl.c"
4141

42+
public function setFileSuffix
43+
input String fileSuffix;
44+
45+
external "C" ZeroMQ_setFileSuffix(fileSuffix) annotation(Library = "omcruntime");
46+
end setFileSuffix;
47+
4248
public function initialize
43-
input Integer port;
4449
output Option<Integer> zmqSocket;
4550

46-
external "C" zmqSocket = ZeroMQ_initialize(port) annotation(Library = "omcruntime");
51+
external "C" zmqSocket = ZeroMQ_initialize() annotation(Library = "omcruntime");
4752
end initialize;
4853

4954
public function handleRequest

Compiler/runtime/zeromqimpl.c

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,66 @@
3535

3636
#include "modelica_string.h"
3737

38-
void* ZeroMQ_initialize(int port)
38+
#include "settingsimpl.h"
39+
40+
char* zeroMQFilePath = 0;
41+
char* zeroMQFileSuffix = 0;
42+
43+
void ZeroMQ_setFileSuffix(const char *fileSuffix)
44+
{
45+
if (strlen(fileSuffix) == 0) return;
46+
if (zeroMQFileSuffix) free(zeroMQFileSuffix);
47+
48+
if (*fileSuffix) {
49+
zeroMQFileSuffix = strdup(fileSuffix);
50+
} else {
51+
zeroMQFileSuffix = NULL;
52+
}
53+
}
54+
55+
void* ZeroMQ_initialize()
3956
{
4057
// Create a pointer for storing the ZeroMQ socket
4158
void *mmcZmqSocket = mmc_mk_some(0);
4259
// Create the ZeroMQ context
4360
void *context = zmq_ctx_new();
4461
void *zmqSocket = zmq_socket(context, ZMQ_REP);
45-
char hostname[20];
46-
sprintf(hostname, "tcp://*:%d", port);
47-
int rc = zmq_bind(zmqSocket, hostname);
62+
int rc = zmq_bind(zmqSocket, "tcp://127.0.0.1:*");
4863
if (rc != 0) {
4964
printf("Error creating ZeroMQ Server. zmq_bind failed: %s\n", strerror(errno));
5065
return mmcZmqSocket;
5166
}
67+
// get the port number
68+
const size_t endPointBufSize = 30;
69+
char endPointBuf[endPointBufSize];
70+
zmq_getsockopt(zmqSocket, ZMQ_LAST_ENDPOINT, &endPointBuf, (size_t *)&endPointBufSize);
71+
// create the file path
72+
const char* tempPath = SettingsImpl__getTempDirectoryPath();
73+
#if defined(__MINGW32__) || defined(_MSC_VER)
74+
if (zeroMQFileSuffix != NULL) {
75+
zeroMQFilePath = (char*)malloc(strlen(tempPath) + strlen("/openmodelica.port.") + strlen(zeroMQFileSuffix) + 1);
76+
sprintf(zeroMQFilePath, "%s/openmodelica.port.%s", tempPath, zeroMQFileSuffix);
77+
} else {
78+
zeroMQFilePath = (char*)malloc(strlen(tempPath) + strlen("/openmodelica.port") + 1);
79+
sprintf(zeroMQFilePath, "%s/openmodelica.port", tempPath);
80+
}
81+
#else
82+
char *tmp_user = getenv("USER");
83+
if (zeroMQFileSuffix != NULL) {
84+
zeroMQFilePath = (char*)malloc(strlen(tempPath) + strlen("/openmodelica.") + tmp_user ? strlen(tmp_user) : strlen("nobody") + strlen(zeroMQFileSuffix) + 1);
85+
sprintf(zeroMQFilePath, "%s/openmodelica.%s.port.%s", tempPath, tmp_user, zeroMQFileSuffix);
86+
} else {
87+
zeroMQFilePath = (char*)malloc(strlen(tempPath) + strlen("/openmodelica.") + tmp_user ? strlen(tmp_user) : strlen("nobody") + 1);
88+
sprintf(zeroMQFilePath, "%s/openmodelica.%s.port", tempPath, tmp_user);
89+
}
90+
#endif
91+
// Create the file with port number
92+
FILE *fp;
93+
fp = fopen(zeroMQFilePath, "w");
94+
fputs(endPointBuf, fp);
95+
fclose(fp);
96+
printf("Created ZeroMQ Server.\nDumped server port in file: %s", zeroMQFilePath);fflush(NULL);
97+
5298
mmcZmqSocket = mmc_mk_some(zmqSocket);
5399
return mmcZmqSocket;
54100
}
@@ -93,6 +139,11 @@ void ZeroMQ_sendReply(void *mmcZmqSocket, const char* reply)
93139

94140
void ZeroMQ_close(void *mmcZmqSocket)
95141
{
142+
if (zeroMQFilePath) {
143+
remove(zeroMQFilePath);
144+
free(zeroMQFilePath);
145+
}
146+
if (zeroMQFileSuffix) free(zeroMQFileSuffix);
96147
// Convert the void* to ZeroMQ Socket
97148
intptr_t zmqSocket = (intptr_t)MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(mmcZmqSocket),1));
98149
// close the ZeroMQ socket

0 commit comments

Comments
 (0)