summaryrefslogtreecommitdiff
path: root/qpid/extras/dispatch/src/agent.c
blob: a7475888b2d03f43bfb0ffcc682d01416ca6afaf (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include "dispatch_private.h"
#include <qpid/dispatch/error.h>
#include <qpid/dispatch/agent.h>
#include <qpid/dispatch/alloc.h>
#include <qpid/dispatch/ctools.h>
#include <qpid/dispatch/hash.h>
#include <qpid/dispatch/container.h>
#include <qpid/dispatch/message.h>
#include <qpid/dispatch/threading.h>
#include <qpid/dispatch/timer.h>
#include <qpid/dispatch/router.h>
#include <qpid/dispatch/log.h>
#include <qpid/dispatch/compose.h>
#include <qpid/dispatch/parse.h>
#include <qpid/dispatch/amqp.h>
#include <string.h>
#include <stdio.h>

struct dx_agent_t {
    dx_dispatch_t     *dx;
    dx_hash_t         *class_hash;
    dx_message_list_t  in_fifo;
    dx_message_list_t  out_fifo;
    sys_mutex_t       *lock;
    dx_timer_t        *timer;
    dx_address_t      *address;
};


struct dx_agent_class_t {
    char                 *fqname;
    void                 *context;
    dx_agent_schema_cb_t  schema_handler;
    dx_agent_query_cb_t   query_handler;  // 0 iff class is an event.
};


typedef struct {
    dx_agent_t          *agent;
    dx_composed_field_t *response;
} dx_agent_request_t;


static char *log_module = "AGENT";


static void dx_agent_process_get(dx_agent_t *agent, dx_parsed_field_t *map, dx_field_iterator_t *reply_to)
{
    dx_parsed_field_t *cls = dx_parse_value_by_key(map, "type");
    if (cls == 0)
        return;

    dx_field_iterator_t    *cls_string = dx_parse_raw(cls);
    const dx_agent_class_t *cls_record;
    dx_hash_retrieve_const(agent->class_hash, cls_string, (const void**) &cls_record);
    if (cls_record == 0)
        return;

    dx_log(log_module, LOG_TRACE, "Received GET request for type: %s", cls_record->fqname);

    //
    // Compose the header
    //
    dx_composed_field_t *field = dx_compose(DX_PERFORMATIVE_HEADER, 0);
    dx_compose_start_list(field);
    dx_compose_insert_bool(field, 0);     // durable
    dx_compose_end_list(field);

    //
    // Compose the Properties
    //
    field = dx_compose(DX_PERFORMATIVE_PROPERTIES, field);
    dx_compose_start_list(field);
    dx_compose_insert_null(field);                       // message-id
    dx_compose_insert_null(field);                       // user-id
    dx_compose_insert_string_iterator(field, reply_to);  // to
    dx_compose_insert_null(field);                       // subject
    dx_compose_insert_null(field);                       // reply-to
    dx_compose_insert_string(field, "1");                // correlation-id   // TODO - fix
    dx_compose_end_list(field);

    //
    // Compose the Application Properties
    //
    field = dx_compose(DX_PERFORMATIVE_APPLICATION_PROPERTIES, field);
    dx_compose_start_map(field);
    dx_compose_insert_string(field, "operation");
    dx_compose_insert_string(field, "GET");

    dx_compose_insert_string(field, "status-code");
    dx_compose_insert_uint(field, 200);

    dx_compose_insert_string(field, "status-descriptor");
    dx_compose_insert_string(field, "OK");
    dx_compose_end_map(field);

    //
    // Open the Body (AMQP Value) to be filled in by the handler.
    //
    field = dx_compose(DX_PERFORMATIVE_BODY_AMQP_VALUE, field);
    dx_compose_start_list(field);
    dx_compose_start_map(field);

    //
    // The request record is allocated locally because the entire processing of the request
    // will be done synchronously.
    //
    dx_agent_request_t request;
    request.agent    = agent;
    request.response = field;

    cls_record->query_handler(cls_record->context, 0, &request);

    //
    // The response is complete, close the list.
    //
    dx_compose_end_list(field);

    //
    // Create a message and send it.
    //
    dx_message_t *msg = dx_message();
    dx_message_compose_2(msg, field);
    dx_router_send(agent->dx, reply_to, msg);

    dx_message_free(msg);
    dx_compose_free(field);
}


static void dx_agent_process_request(dx_agent_t *agent, dx_message_t *msg)
{
    //
    // Parse the message through the body and exit if the message is not well formed.
    //
    if (!dx_message_check(msg, DX_DEPTH_BODY))
        return;

    //
    // Get an iterator for the application-properties.  Exit if the message has none.
    //
    dx_field_iterator_t *ap = dx_message_field_iterator(msg, DX_FIELD_APPLICATION_PROPERTIES);
    if (ap == 0)
        return;

    //
    // Get an iterator for the reply-to.  Exit if not found.
    //
    dx_field_iterator_t *reply_to = dx_message_field_iterator(msg, DX_FIELD_REPLY_TO);
    if (reply_to == 0)
        return;

    //
    // Try to get a map-view of the application-properties.
    //
    dx_parsed_field_t *map = dx_parse(ap);
    if (map == 0) {
        dx_field_iterator_free(ap);
        return;
    }

    //
    // Exit if there was a parsing error.
    //
    if (!dx_parse_ok(map)) {
        dx_log(log_module, LOG_TRACE, "Received unparsable App Properties: %s", dx_parse_error(map));
        dx_field_iterator_free(ap);
        dx_parse_free(map);
        return;
    }

    //
    // Exit if it is not a map.
    //
    if (!dx_parse_is_map(map)) {
        dx_field_iterator_free(ap);
        dx_parse_free(map);
        return;
    }

    //
    // Get an iterator for the "operation" field in the map.  Exit if the key is not found.
    //
    dx_parsed_field_t *operation = dx_parse_value_by_key(map, "operation");
    if (operation == 0) {
        dx_parse_free(map);
        dx_field_iterator_free(ap);
        return;
    }

    //
    // Dispatch the operation to the appropriate handler
    //
    dx_field_iterator_t *operation_string = dx_parse_raw(operation);
    if (dx_field_iterator_equal(operation_string, (unsigned char*) "GET"))
        dx_agent_process_get(agent, map, reply_to);

    dx_parse_free(map);
    dx_field_iterator_free(ap);
    dx_field_iterator_free(reply_to);
}


static void dx_agent_deferred_handler(void *context)
{
    dx_agent_t   *agent = (dx_agent_t*) context;
    dx_message_t *msg;

    do {
        sys_mutex_lock(agent->lock);
        msg = DEQ_HEAD(agent->in_fifo);
        if (msg)
            DEQ_REMOVE_HEAD(agent->in_fifo);
        sys_mutex_unlock(agent->lock);

        if (msg) {
            dx_agent_process_request(agent, msg);
            dx_message_free(msg);
        }
    } while (msg);
}


static void dx_agent_rx_handler(void *context, dx_message_t *msg, int unused_link_id)
{
    dx_agent_t   *agent = (dx_agent_t*) context;
    dx_message_t *copy  = dx_message_copy(msg);

    sys_mutex_lock(agent->lock);
    DEQ_INSERT_TAIL(agent->in_fifo, copy);
    sys_mutex_unlock(agent->lock);

    dx_timer_schedule(agent->timer, 0);
}


dx_agent_t *dx_agent(dx_dispatch_t *dx)
{
    dx_agent_t *agent = NEW(dx_agent_t);
    agent->dx         = dx;
    agent->class_hash = dx_hash(6, 10, 1);
    DEQ_INIT(agent->in_fifo);
    DEQ_INIT(agent->out_fifo);
    agent->lock    = sys_mutex();
    agent->timer   = dx_timer(dx, dx_agent_deferred_handler, agent);
    agent->address = dx_router_register_address(dx, "$management", dx_agent_rx_handler, agent);

    return agent;
}


void dx_agent_free(dx_agent_t *agent)
{
    dx_router_unregister_address(agent->address);
    sys_mutex_free(agent->lock);
    dx_timer_free(agent->timer);
    dx_hash_free(agent->class_hash);
    free(agent);
}


dx_agent_class_t *dx_agent_register_class(dx_dispatch_t        *dx,
                                          const char           *fqname,
                                          void                 *context,
                                          dx_agent_schema_cb_t  schema_handler,
                                          dx_agent_query_cb_t   query_handler)
{
    dx_agent_t *agent = dx->agent;

    dx_agent_class_t *cls = NEW(dx_agent_class_t);
    assert(cls);
    cls->fqname = (char*) malloc(strlen(fqname) + 1);
    strcpy(cls->fqname, fqname);
    cls->context        = context;
    cls->schema_handler = schema_handler;
    cls->query_handler  = query_handler;

    dx_field_iterator_t *iter = dx_field_iterator_string(fqname, ITER_VIEW_ALL);
    int result = dx_hash_insert_const(agent->class_hash, iter, cls, 0);
    dx_field_iterator_free(iter);
    if (result < 0)
        assert(false);

    dx_log(log_module, LOG_TRACE, "%s class registered: %s", query_handler ? "Object" : "Event", fqname);
    return cls;
}


dx_agent_class_t *dx_agent_register_event(dx_dispatch_t        *dx,
                                          const char           *fqname,
                                          void                 *context,
                                          dx_agent_schema_cb_t  schema_handler)
{
    return dx_agent_register_class(dx, fqname, context, schema_handler, 0);
}


void dx_agent_value_string(void *correlator, const char *key, const char *value)
{
    dx_agent_request_t *request = (dx_agent_request_t*) correlator;
    dx_compose_insert_string(request->response, key);
    dx_compose_insert_string(request->response, value);
}


void dx_agent_value_uint(void *correlator, const char *key, uint64_t value)
{
    dx_agent_request_t *request = (dx_agent_request_t*) correlator;
    dx_compose_insert_string(request->response, key);
    dx_compose_insert_uint(request->response, value);
}


void dx_agent_value_null(void *correlator, const char *key)
{
    dx_agent_request_t *request = (dx_agent_request_t*) correlator;
    dx_compose_insert_string(request->response, key);
    dx_compose_insert_null(request->response);
}


void dx_agent_value_boolean(void *correlator, const char *key, bool value)
{
    dx_agent_request_t *request = (dx_agent_request_t*) correlator;
    dx_compose_insert_string(request->response, key);
    dx_compose_insert_bool(request->response, value);
}


void dx_agent_value_binary(void *correlator, const char *key, const uint8_t *value, size_t len)
{
    dx_agent_request_t *request = (dx_agent_request_t*) correlator;
    dx_compose_insert_string(request->response, key);
    dx_compose_insert_binary(request->response, value, len);
}


void dx_agent_value_uuid(void *correlator, const char *key, const uint8_t *value)
{
    dx_agent_request_t *request = (dx_agent_request_t*) correlator;
    dx_compose_insert_string(request->response, key);
    dx_compose_insert_uuid(request->response, value);
}


void dx_agent_value_timestamp(void *correlator, const char *key, uint64_t value)
{
    dx_agent_request_t *request = (dx_agent_request_t*) correlator;
    dx_compose_insert_string(request->response, key);
    dx_compose_insert_timestamp(request->response, value);
}


void dx_agent_value_complete(void *correlator, bool more)
{
    dx_agent_request_t *request = (dx_agent_request_t*) correlator;
    dx_compose_end_map(request->response);
    if (more)
        dx_compose_start_map(request->response);
}


void *dx_agent_raise_event(dx_dispatch_t *dx, dx_agent_class_t *event)
{
    return 0;
}