2 * Copyright (c) 2018, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include "tr_mon_req.h"
42 // Monitoring request encoders
45 * Encode options array as a JSON array
49 * { "type": "first_type" },
50 * { "type": "second_type"},
54 * @param opts array of options
55 * @return reference to a JSON array of options
57 static json_t *tr_mon_opts_decode(GArray *opts)
59 json_t *array_json = json_array(); // the array of options
60 json_t *opt_json = NULL; // individual option JSON object
61 json_t *type_json = NULL;
65 if (array_json == NULL)
66 return NULL; // failed
68 /* Iterate over the options */
69 for (ii=0; ii < opts->len; ii++) {
70 this_opt = g_array_index(opts, TR_MON_OPT, ii);
72 /* Create the JSON object for this option */
73 opt_json = json_object();
74 if (opt_json == NULL) {
75 json_decref(array_json);
79 /* Add to the array, making opt_json a borrowed ref if we succeed */
80 if (json_array_append_new(array_json, opt_json) == -1) {
81 json_decref(array_json);
82 json_decref(opt_json); // handle ourselves because the set failed
85 /* Create the type string for this option */
86 type_json = json_string(opt_type_to_string(this_opt.type));
87 if (type_json == NULL) {
88 json_decref(array_json);
92 /* Add the type string to the JSON object, making type_json a borrowed ref */
93 if (json_object_set_new(opt_json, "type", type_json) == -1) {
94 json_decref(array_json);
95 json_decref(type_json); // must handle ourselves because the set failed
104 * Encode a request as a JSON object
106 * Caller must free the return value using json_decref()
110 * "command": "some_command",
111 * "options": [...see tr_mon_opts_to_json()...]
114 * @param req request to encode
115 * @return reference to a JSON object
117 json_t *tr_mon_req_encode(TR_MON_REQ *req)
119 json_t *req_json = NULL;
120 json_t *cmd_json = NULL;
121 json_t *opts_json = NULL;
123 /* Allocate the base JSON object */
124 req_json = json_object();
125 if (req_json == NULL)
128 /* Allocate the JSON string for the command */
129 cmd_json = json_string(cmd_to_string(req->command));
130 if (cmd_json == NULL) {
131 json_decref(req_json);
135 /* Add the command string to the base object. Steals the reference to
136 * the string if successful. */
137 if (json_object_set_new(req_json, "command", cmd_json) == -1) {
138 json_decref(cmd_json); // must clean this up ourselves because the set failed
139 json_decref(req_json);
143 /* If we have options, add them to the object */
144 if (req->options->len > 0) {
145 opts_json = tr_mon_opts_decode(req->options);
146 if (opts_json == NULL) {
147 json_decref(req_json);
151 if (json_object_set_new(req_json, "options", opts_json) == -1) {
152 json_decref(req_json);
153 json_decref(opts_json); // must clean this up ourselves because set failed
158 /* That's it, we succeeded */