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

Commit

Permalink
Improved the ZeroMQ server receive method
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 authored and OpenModelica-Hudson committed Jun 13, 2017
1 parent 7a88dc8 commit b70932d
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions Compiler/runtime/zeromqimpl.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2010, Linköpings University,
* Copyright (c) 1998-2010, Linköpings University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
Expand All @@ -14,7 +14,7 @@
*
* 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 Linköpings University, either from the above address,
* from the URL: http://www.ida.liu.se/projects/OpenModelica
* and in the OpenModelica distribution.
*
Expand Down Expand Up @@ -46,7 +46,7 @@ void* ZeroMQ_initialize(int port)
sprintf(hostname, "tcp://*:%d", port);
int rc = zmq_bind(zmqSocket, hostname);
if (rc != 0) {
printf("Error creating ZeroMQ Server\n");
printf("Error creating ZeroMQ Server. zmq_bind failed: %s\n", strerror(errno));
return mmcZmqSocket;
}
mmcZmqSocket = mmc_mk_some(zmqSocket);
Expand All @@ -55,21 +55,31 @@ void* ZeroMQ_initialize(int port)

extern char* ZeroMQImpl_handleRequest(void *mmcZmqSocket)
{
int bufferSize = 4000;
char *buffer = (char*)malloc(bufferSize + 1);
// Convert the void* to ZeroMQ Socket
intptr_t zmqSocket = (intptr_t)MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(mmcZmqSocket),1));
zmq_recv((void*)zmqSocket, buffer, bufferSize, 0);
fprintf(stdout, "Recieved message %s\n", buffer);fflush(NULL);
return buffer;
// 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);
//fprintf(stdout, "Sending message %s\n", reply);fflush(NULL);
zmq_send((void*)zmqSocket, reply, strlen(reply) + 1, 0);
}

Expand Down

0 comments on commit b70932d

Please sign in to comment.