-
Notifications
You must be signed in to change notification settings - Fork 298
/
zeromqimpl.c
128 lines (119 loc) · 4.43 KB
/
zeromqimpl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2010, Linköpings University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
* LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
* THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
* PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköpings University, either from the above address,
* from the URL: http://www.ida.liu.se/projects/OpenModelica
* and in the OpenModelica distribution.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "meta/meta_modelica.h"
#include "util/modelica_string.h"
#include "settingsimpl.h"
char* zeroMQFilePath = 0;
void* ZeroMQ_initialize(const char *zeroMQFileSuffix)
{
// 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, "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)
zeroMQFilePath = (char*)malloc(strlen(tempPath) + strlen("/openmodelica.port") + strlen(zeroMQFileSuffix) + 1);
sprintf(zeroMQFilePath, "%s/openmodelica.port%s", tempPath, zeroMQFileSuffix);
#else
char *tmp_user = getenv("USER");
zeroMQFilePath = (char*)malloc(strlen(tempPath) + strlen("/openmodelica..port.") + strlen(tmp_user ? tmp_user : "nobody") + strlen(zeroMQFileSuffix) + 1);
sprintf(zeroMQFilePath, "%s/openmodelica.%s.port%s", tempPath, tmp_user ? tmp_user : "nobody", zeroMQFileSuffix);
#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;
}
extern char* ZeroMQImpl_handleRequest(void *mmcZmqSocket)
{
// Convert the void* to ZeroMQ Socket
intptr_t zmqSocket = (intptr_t)MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(mmcZmqSocket),1));
// Create an empty ZeroMQ message to hold the message part
zmq_msg_t request;
int rc = zmq_msg_init(&request);
assert(rc == 0);
// Block until a message is available to be received from socket
int size = zmq_msg_recv(&request, (void*)zmqSocket, 0);
assert(size != -1);
// copy the zmq_msg_t to char*
char *requestStr = (char*)malloc(size + 1);
memcpy(requestStr, zmq_msg_data(&request), size);
// release the zmq_msg_t
zmq_msg_close(&request);
requestStr[size] = 0;
//fprintf(stdout, "Recieved message %s with size %d\n", requestStr, size);fflush(NULL);
return requestStr;
}
void ZeroMQ_sendReply(void *mmcZmqSocket, const char* reply)
{
// Convert the void* to ZeroMQ Socket
intptr_t zmqSocket = (intptr_t)MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(mmcZmqSocket),1));
// send the reply
//fprintf(stdout, "Sending message %s\n", reply);fflush(NULL);
// Create an empty ZeroMQ message to hold the message part
zmq_msg_t replyMsg;
zmq_msg_init_size(&replyMsg, strlen(reply));
// copy the char* to zmq_msg_t
memcpy(zmq_msg_data(&replyMsg), reply, strlen(reply));
// send the message
zmq_msg_send(&replyMsg, (void*)zmqSocket, 0);
// release the zmq_msg_t
zmq_msg_close(&replyMsg);
}
void ZeroMQ_close(void *mmcZmqSocket)
{
if (zeroMQFilePath) {
remove(zeroMQFilePath);
free(zeroMQFilePath);
}
// Convert the void* to ZeroMQ Socket
intptr_t zmqSocket = (intptr_t)MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(mmcZmqSocket),1));
// close the ZeroMQ socket
zmq_close((void*)zmqSocket);
}