aboutsummaryrefslogtreecommitdiff
path: root/src/im-zmq.c
blob: ece806626503bf8d7fed95794282bc22aba465e2 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * zmq.c
 *
 * ZMQ data interface
 *
 * Copyright © 2017-2021 Deutsches Elektronen-Synchrotron DESY,
 *                       a research centre of the Helmholtz Association.
 *
 * Authors:
 *   2018-2021 Thomas White <taw@physics.org>
 *   2014      Valerio Mariani
 *   2017      Stijn de Graaf
 *
 * This file is part of CrystFEL.
 *
 * CrystFEL is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CrystFEL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CrystFEL.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <unistd.h>
#include <zmq.h>

#include <image.h>
#include <utils.h>

#include "im-zmq.h"

#include "datatemplate_priv.h"


struct im_zmq
{
	void *ctx;
	void *socket;
	zmq_msg_t msg;
	const char *request_str;
	int request_sent;
};


struct im_zmq *im_zmq_connect(struct im_zmq_params *params)
{
	struct im_zmq *z;

	z = malloc(sizeof(struct im_zmq));
	if ( z == NULL ) return NULL;

	z->ctx = zmq_ctx_new();
	if ( z->ctx == NULL ) {
		free(z);
		return NULL;
	}

	if ( params->request == NULL ) {
		STATUS("Connecting ZMQ subscriber to '%s'\n", params->addr);
		z->socket = zmq_socket(z->ctx, ZMQ_SUB);
	} else {
		STATUS("Connecting ZMQ requester to '%s'\n", params->addr);
		z->socket = zmq_socket(z->ctx, ZMQ_REQ);
	}
	if ( z->socket == NULL ) {
		free(z);
		return NULL;
	}

	if ( zmq_connect(z->socket, params->addr) == -1 ) {
		ERROR("ZMQ connection failed: %s\n", zmq_strerror(errno));
		free(z);
		return NULL;
	}

	int timeout = 3000;
	zmq_setsockopt(z->socket, ZMQ_RCVTIMEO, &timeout, sizeof(timeout));
	int linger = 0;;
	zmq_setsockopt(z->socket, ZMQ_LINGER, &linger, sizeof(linger));

	if ( params->request == NULL ) {

		int i;

		/* SUB mode */
		if ( params->n_subscriptions == 0 ) {
			ERROR("WARNING: No ZeroMQ subscriptions.  You should "
			      "probably try again with --zmq-subscribe.\n");
		}
		for ( i=0; i<params->n_subscriptions; i++ ) {
			STATUS("Subscribing to '%s'\n", params->subscriptions[i]);
			if ( zmq_setsockopt(z->socket, ZMQ_SUBSCRIBE,
			                    params->subscriptions[i],
			                    strlen(params->subscriptions[i])) )
			{
				ERROR("ZMQ subscription failed: %s\n",
				      zmq_strerror(errno));
				return NULL;
			}
		}

		z->request_str = NULL;

	} else {

		/* REQ mode */
		z->request_str = params->request;
		z->request_sent = 0;

	}

	return z;
}


void *im_zmq_fetch(struct im_zmq *z, size_t *pdata_size)
{
	int msg_size;
	void *data_copy;

	if ( (z->request_str != NULL) && !z->request_sent ) {

		/* Send the request */
		if ( zmq_send(z->socket, z->request_str,
		              strlen(z->request_str), 0) == -1 )
		{
			ERROR("ZMQ message send failed: %s\n",
			      zmq_strerror(errno));
			return NULL;
		}

		z->request_sent = 1;
	}

	/* Receive message */
	zmq_msg_init(&z->msg);
	msg_size = zmq_msg_recv(&z->msg, z->socket, 0);
	if ( msg_size == -1 ) {
		if ( errno != EAGAIN ) {
			ERROR("ZMQ recieve failed: %s\n", zmq_strerror(errno));
		}
		zmq_msg_close(&z->msg);
		return NULL;
	}

	/* Reply received.  OK to send request again */
	z->request_sent = 0;

	data_copy = malloc(msg_size);
	if ( data_copy == NULL ) return NULL;
	memcpy(data_copy, zmq_msg_data(&z->msg), msg_size);

	zmq_msg_close(&z->msg);
	*pdata_size = msg_size;
	return data_copy;
}


void im_zmq_shutdown(struct im_zmq *z)
{
	if ( z == NULL ) return;
	zmq_close(z->socket);
	zmq_ctx_destroy(z->ctx);
}