Skip to content

Commit 579677b

Browse files
committed
Added ZeroMQ communication interface
1 parent e2bcaf3 commit 579677b

15 files changed

Lines changed: 284 additions & 8 deletions

Compiler/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ SET(SRCMO Absyn.mo AbsynDep.mo Algorithm.mo Builtin.mo
2121
ConnectUtil.mo Dependency.mo)
2222

2323
SET(ALLMO ${SRCMO} DAEEXT.mo DynLoad.mo Print.mo System.mo Parser.mo
24-
TaskGraphExt.mo Corba.mo Socket.mo ErrorExt.mo Settings.mo
24+
TaskGraphExt.mo Corba.mo Socket.mo ZeroMQ.mo ErrorExt.mo Settings.mo
2525
UnitParserExt.mo SimulationResults.mo Serializer.mo)
2626

2727
# RML

Compiler/Main/Main.mo

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import StackOverflow;
7979
import System;
8080
import TplMain;
8181
import Util;
82+
import ZeroMQ;
8283

8384
protected function serverLoop
8485
"This function is the main loop of the server listening
@@ -115,6 +116,40 @@ algorithm
115116
end match;
116117
end serverLoop;
117118

119+
protected function serverLoopZMQ
120+
"This function is the main loop of the ZeroMQ server listening
121+
to a port which recieves modelica expressions."
122+
input Boolean cont;
123+
input Option<Integer> inZMQSocket;
124+
input GlobalScript.SymbolTable inInteractiveSymbolTable;
125+
output GlobalScript.SymbolTable outInteractiveSymbolTable;
126+
algorithm
127+
outInteractiveSymbolTable := match (cont,inZMQSocket,inInteractiveSymbolTable)
128+
local
129+
Boolean b;
130+
String str,replystr;
131+
GlobalScript.SymbolTable newsymb,ressymb,isymb;
132+
Option<Integer> zmqSocket;
133+
case (false,_,isymb) then isymb;
134+
case (_,SOME(0),_) then fail();
135+
case (_,zmqSocket,isymb)
136+
equation
137+
str = ZeroMQ.handleRequest(zmqSocket);
138+
if Flags.isSet(Flags.INTERACTIVE_DUMP) then
139+
Debug.trace("------- Recieved Data from client -----\n");
140+
Debug.trace(str);
141+
Debug.trace("------- End recieved Data-----\n");
142+
end if;
143+
(b,replystr,newsymb) = handleCommand(str, isymb) "Print.clearErrorBuf &" ;
144+
replystr = if b then replystr else "quit requested, shutting server down\n";
145+
ZeroMQ.sendReply(zmqSocket, replystr);
146+
if not b then
147+
ZeroMQ.close(zmqSocket);
148+
end if;
149+
then serverLoopZMQ(b, zmqSocket, newsymb);
150+
end match;
151+
end serverLoopZMQ;
152+
118153
protected function makeDebugResult
119154
input Flags.DebugFlag inFlag;
120155
input String res;
@@ -655,6 +690,14 @@ algorithm
655690
end try;
656691
end interactivemodeCorba;
657692

693+
protected function interactivemodeZMQ
694+
"Initiate the interactive mode using ZMQ communication."
695+
input GlobalScript.SymbolTable symbolTable;
696+
algorithm
697+
print("Starting a ZeroMQ server on port " + intString(29500) + "\n");
698+
serverLoopZMQ(true, ZeroMQ.initialize(29500), symbolTable);
699+
end interactivemodeZMQ;
700+
658701
protected function serverLoopCorba
659702
"This function is the main loop of the server for a CORBA impl."
660703
input GlobalScript.SymbolTable inSettings;
@@ -866,6 +909,8 @@ algorithm
866909
interactivemode(readSettings(args));
867910
elseif Flags.isSet(Flags.INTERACTIVE_CORBA) then
868911
interactivemodeCorba(readSettings(args));
912+
elseif Flags.isSet(Flags.INTERACTIVE_ZMQ) then
913+
interactivemodeZMQ(readSettings(args));
869914
else // No interactive flag given, try to flatten the file.
870915
readSettings(args);
871916
FGraphStream.start();

Compiler/Util/Flags.mo

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,8 @@ constant DebugFlag DISABLE_COLORING = DEBUG_FLAG(169, "disableColoring", false,
513513
Util.gettext("Disables coloring algorithm while spasity detection."));
514514
constant DebugFlag MERGE_ALGORITHM_SECTIONS = DEBUG_FLAG(170, "mergeAlgSections", false,
515515
Util.gettext("Disables coloring algorithm while spasity detection."));
516+
constant DebugFlag INTERACTIVE_ZMQ = DEBUG_FLAG(171, "interactiveZMQ", false,
517+
Util.gettext("Starts omc as a ZeroMQ server listening on the socket interface."));
516518

517519

518520
// This is a list of all debug flags, to keep track of which flags are used. A
@@ -690,7 +692,8 @@ constant list<DebugFlag> allDebugFlags = {
690692
EVAL_PARAM_DUMP,
691693
NF_UNITCHECK,
692694
DISABLE_COLORING,
693-
MERGE_ALGORITHM_SECTIONS
695+
MERGE_ALGORITHM_SECTIONS,
696+
INTERACTIVE_ZMQ
694697
};
695698

696699
public

Compiler/Util/ZeroMQ.mo

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* This file is part of OpenModelica.
3+
*
4+
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
5+
* c/o Linköpings universitet, Department of Computer and Information Science,
6+
* SE-58183 Linköping, Sweden.
7+
*
8+
* All rights reserved.
9+
*
10+
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
11+
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
12+
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13+
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
14+
* ACCORDING TO RECIPIENTS CHOICE.
15+
*
16+
* The OpenModelica software and the Open Source Modelica
17+
* Consortium (OSMC) Public License (OSMC-PL) are obtained
18+
* from OSMC, either from the above address,
19+
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
20+
* http://www.openmodelica.org, and in the OpenModelica distribution.
21+
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
22+
*
23+
* This program is distributed WITHOUT ANY WARRANTY; without
24+
* even the implied warranty of MERCHANTABILITY or FITNESS
25+
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
26+
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
27+
*
28+
* See the full OSMC Public License conditions for more details.
29+
*
30+
*/
31+
32+
encapsulated package ZeroMQ
33+
" file: ZeroMQ.mo
34+
package: ZeroMQ
35+
description: ZeroMQ communication module
36+
37+
38+
This is the ZeroMQ connection module of the compiler
39+
Used in interactive mode if omc is started with +d=interactiveZMQ
40+
Implemented in ./runtime/zeromqimpl.c"
41+
42+
public function initialize
43+
input Integer port;
44+
output Option<Integer> zmqSocket;
45+
46+
external "C" zmqSocket = ZeroMQ_initialize(port) annotation(Library = "omcruntime");
47+
end initialize;
48+
49+
public function handleRequest
50+
input Option<Integer> zmqSocket;
51+
output String request;
52+
53+
external "C" request = ZeroMQ_handleRequest(zmqSocket) annotation(Library = "omcruntime");
54+
end handleRequest;
55+
56+
public function sendReply
57+
input Option<Integer> zmqSocket;
58+
input String reply;
59+
60+
external "C" ZeroMQ_sendReply(zmqSocket, reply) annotation(Library = "omcruntime");
61+
end sendReply;
62+
63+
public function close
64+
input Option<Integer> zmqSocket;
65+
66+
external "C" ZeroMQ_close(zmqSocket) annotation(Library = "omcruntime");
67+
end close;
68+
69+
annotation(__OpenModelica_Interface="util");
70+
end ZeroMQ;

Compiler/boot/LoadCompilerSources.mos

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ if true then /* Suppress output */
226226
"../Util/Socket.mo",
227227
"../Util/System.mo",
228228
"../Util/Util.mo",
229-
"../Util/VarTransform.mo"
229+
"../Util/VarTransform.mo",
230+
"../Util/ZeroMQ.mo"
230231
};
231232
backendfiles := if OpenModelica.Scripting.getEnvironmentVar("OPENMODELICA_BACKEND_STUBS")<>"" then
232233
{

Compiler/boot/Makefile.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ EXE_EXT=@EXE@
1212
EXE_SUFFIX=
1313

1414
LIB_OMC=lib/@host_short@/omc
15-
LDFLAGS=-L. -L$(GEN_DIR) -L"$(OMHOME)/$(LIB_OMC)" $(LOMPARSE) $(LCOMPILERRUNTIME) -lOpenModelicaRuntimeC -lModelicaExternalC -lomantlr3 $(CORBALIBS) $(FMILIB_OR_BOOT) $(GSLIB) @RT_LDFLAGS@ @LIBSOCKET@ @LIBLPSOLVE55@ @OMC_LIBS@ @GRAPHLIB@ @LD_LAPACK@
15+
LDFLAGS=-L. -L$(GEN_DIR) -L"$(OMHOME)/$(LIB_OMC)" $(LOMPARSE) $(LCOMPILERRUNTIME) -lOpenModelicaRuntimeC -lModelicaExternalC -lomantlr3 $(CORBALIBS) $(FMILIB_OR_BOOT) $(GSLIB) @RT_LDFLAGS@ @LIBSOCKET@ @LIBLPSOLVE55@ @OMC_LIBS@ @GRAPHLIB@ @LD_LAPACK@ -lzmq
1616
LDFLAGS_SHARED_MAIN=-L"$(OMHOME)/$(LIB_OMC)" @RT_LDFLAGS_SHARED@
1717
ifeq (@WITH_FMIL@,yes)
1818
FMILIB = -L$(TOP_DIR)/3rdParty/FMIL/install/lib -lfmilib

Compiler/boot/Makefile.omdev.mingw

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ LDFLAGS=-L./ $(LOMPARSE) $(LCOMPILERRUNTIME) -L"$(OMHOME)/lib/omc" \
1919
-Wl,--enable-stdcall-fixup -lstdc++ -static-libgcc \
2020
-L../../3rdParty/lpsolve/build/lib \
2121
-lgfortran -ltre -lomniORB420_rt -lomnithread40_rt \
22+
-lzmq \
2223
$(EXTRA_LD_FLAGS)
2324

2425
FMILIB = -L$(TOP_DIR)/3rdParty/FMIL/install/lib -lfmilib

Compiler/runtime/Makefile.common

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ OMC_OBJ_SHARED = Dynload_omc$(OBJEXT) Error_omc$(OBJEXT) \
2424
ErrorMessage$(OBJEXT) systemimplmisc.o System_omc$(OBJEXT) \
2525
Lapack_omc.o Settings_omc$(OBJEXT) \
2626
UnitParserExt_omc.o unitparser.o \
27-
IOStreamExt_omc.o Socket_omc.o getMemorySize.o \
27+
IOStreamExt_omc.o Socket_omc.o ZeroMQ_omc.o getMemorySize.o \
2828
is_utf8.o
2929

3030
OMC_OBJ_STUBS = corbaimpl_stub_omc.o
@@ -98,6 +98,7 @@ IOStreamExt_omc.o : IOStreamExt.c
9898
ErrorMessage.o : ErrorMessage.cpp ErrorMessage.hpp errorext.h
9999
serializer.o: serializer.cpp
100100
Socket_omc.o : socketimpl.c
101+
ZeroMQ_omc.o : zeromqimpl.c
101102
UnitParserExt_omc.o : unitparserext.cpp unitparser.h
102103
BackendDAEEXT_omc.o : BackendDAEEXT.cpp $(RML_COMPAT) matching.c matchmaker.h matching_cheap.c
103104

Compiler/runtime/Makefile.omdev.mingw

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ CJSONINCLUDE = ../../3rdParty/cJSON/
2727
OMBUILDDIR = ../../../build
2828
LPSOLVEINCLUDE = ../../3rdParty/lpsolve/build/include/
2929
SQLITE3INCLUDE = ../../3rdParty/sqlite3/build/include/
30+
ZMQINCLUDE = ../../3rdParty/libzmq/include
3031
OMC=$(OMBUILDDIR)/bin/omc
3132
SHREXT=.a
3233
OMPCC = gcc -fopenmp
@@ -46,7 +47,7 @@ OMCCORBASRC = omc_communication.o omc_communication_impl.o Corba_omc.o
4647
SHELL = /bin/sh
4748
CC = gcc
4849
CXX = g++
49-
override CFLAGS += -I. $(USE_CORBA) $(USE_METIS) -Wall -Wno-unused-variable -I../../ -I$(top_builddir) -I$(top_builddir)/SimulationRuntime/c -I$(top_builddir)/SimulationRuntime/c/simulation/results -I$(top_builddir)/SimulationRuntime/c/util -I$(top_builddir)/SimulationRuntime/c/meta -I$(top_builddir)/SimulationRuntime/c/meta/gc $(CORBAINCL) -I$(FMIINCLUDE) -I../../3rdParty/gc/include -I$(GRAPHSTREAMINCLUDE) -I$(CJSONINCLUDE) -I$(GRAPHINCLUDE) -I$(LPSOLVEINCLUDE) -I$(SQLITE3INCLUDE)
50+
override CFLAGS += -I. $(USE_CORBA) $(USE_METIS) -Wall -Wno-unused-variable -I../../ -I$(top_builddir) -I$(top_builddir)/SimulationRuntime/c -I$(top_builddir)/SimulationRuntime/c/simulation/results -I$(top_builddir)/SimulationRuntime/c/util -I$(top_builddir)/SimulationRuntime/c/meta -I$(top_builddir)/SimulationRuntime/c/meta/gc $(CORBAINCL) -I$(FMIINCLUDE) -I../../3rdParty/gc/include -I$(GRAPHSTREAMINCLUDE) -I$(CJSONINCLUDE) -I$(GRAPHINCLUDE) -I$(LPSOLVEINCLUDE) -I$(SQLITE3INCLUDE) -I$(ZMQINCLUDE)
5051
CXXFLAGS = $(CFLAGS)
5152

5253
include Makefile.common

Compiler/runtime/ZeroMQ_omc.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This file is part of OpenModelica.
3+
*
4+
* Copyright (c) 1998-2010, Linköpings University,
5+
* Department of Computer and Information Science,
6+
* SE-58183 Linköping, Sweden.
7+
*
8+
* All rights reserved.
9+
*
10+
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
11+
* LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
12+
* THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
13+
* PUBLIC LICENSE.
14+
*
15+
* The OpenModelica software and the Open Source Modelica
16+
* Consortium (OSMC) Public License (OSMC-PL) are obtained
17+
* from Linköpings University, either from the above address,
18+
* from the URL: http://www.ida.liu.se/projects/OpenModelica
19+
* and in the OpenModelica distribution.
20+
*
21+
* This program is distributed WITHOUT ANY WARRANTY; without
22+
* even the implied warranty of MERCHANTABILITY or FITNESS
23+
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
24+
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
25+
* OF OSMC-PL.
26+
*
27+
* See the full OSMC Public License conditions for more details.
28+
*
29+
*/
30+
31+
#if defined(_MSC_VER) || defined(__MINGW32__)
32+
#define WIN32_LEAN_AND_MEAN
33+
#include <windows.h>
34+
#endif
35+
36+
#include <stdio.h>
37+
#include <meta_modelica.h>
38+
39+
#include "zeromqimpl.c"
40+
#include "meta_modelica.h"
41+
#include "ModelicaUtilities.h"
42+
43+
extern const char* ZeroMQ_handleRequest(void* mmcZmqSocket)
44+
{
45+
char *str = ZeroMQImpl_handleRequest(mmcZmqSocket);
46+
char *res = strcpy(ModelicaAllocateString(strlen(str)),str);
47+
free(str);
48+
return res;
49+
}

0 commit comments

Comments
 (0)