/
platanos.c
144 lines (99 loc) · 3.46 KB
/
platanos.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
Copyright contributors as noted in the AUTHORS file.
This file is part of PLATANOS.
PLATANOS is free software; you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
PLATANOS 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include"../api/platanos.h"
#include"platanos_structs.h"
void
platanos_poll_init (platanos_poll_t ** poll, platanos_t * platanos)
{
*poll = malloc (sizeof (struct platanos_poll_t));
(*poll)->pollitem = (zmq_pollitem_t) {
platanos->dealer, 0, ZMQ_POLLIN};
(*poll)->next_time = -1;
}
int64_t
platanos_poll_before_poll (platanos_poll_t * poll)
{
return poll->next_time;
}
void
platanos_poll_pollitems (platanos_poll_t * poll,
zmq_pollitem_t ** pollitems, int *size)
{
*pollitems = &(poll->pollitem);
*size = 1;
}
void platanos_do (platanos_t * platanos);
platanos_poll_t *
platanos_return_poll (platanos_t * platanos)
{
return platanos->poll;
}
void
platanos_init (platanos_t ** platanos,
compute_t * compute, char *id, zctx_t * ctx)
{
*platanos = malloc (sizeof (platanos_t));
(*platanos)->router = zsocket_new (ctx, ZMQ_ROUTER);
(*platanos)->dealer = zsocket_new (ctx, ZMQ_DEALER);
zmq_setsockopt ((*platanos)->dealer, ZMQ_IDENTITY, id, strlen (id));
platanos_poll_init (&((*platanos)->poll), *platanos);
(*platanos)->compute = compute;
}
void
platanos_register (zhandle_t * zh, char *octopus, char *comp_name,
char *res_name, char *bind_point, oconfig_t * config)
{
char path[1000];
char bind_location[1000];
int port = oconfig_incr_port (config);
sprintf (bind_location, "tcp://%s:%d", bind_point, port);
sprintf (path, "/%s/computers/%s/worker_nodes/%s/bind_point", octopus,
comp_name, res_name);
int result = zoo_create (zh, path, bind_location,
strlen (bind_location) + 1, &ZOO_OPEN_ACL_UNSAFE,
0, NULL, 0);
assert (ZOK == result);
}
void platanos_send (platanos_t * platanos, zmsg_t * msg);
platanos_node_t *
platanos_connect (platanos_t * platanos, zmsg_t * msg)
{
char bind_point[50];
zframe_t *frame = zmsg_next (msg);
memcpy (bind_point, zframe_data (frame), zframe_size (frame));
zmsg_destroy (&msg);
int rc;
rc = zsocket_connect (platanos->router, "%s", bind_point);
assert (rc == 0);
platanos_node_t *node;
platanos_node_init (&node);
strcpy (node->bind_point, bind_point);
return node;
}
platanos_node_t *
platanos_bind (platanos_t * platanos, zmsg_t * msg)
{
char bind_point[50];
zframe_t *frame = zmsg_next (msg);
memcpy (bind_point, zframe_data (frame), zframe_size (frame));
zmsg_destroy (&msg);
int rc;
rc = zsocket_bind (platanos->dealer, "%s", bind_point);
assert (rc != -1);
platanos_node_t *node;
platanos_node_init (&node);
strcpy (node->bind_point, bind_point);
return node;
}