This source file includes following definitions.
- pcmk__new_message_as
- pcmk__new_reply_as
- pcmk__register_handlers
- pcmk__process_request
- pcmk__reset_request
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <stdio.h>
13 #include <time.h>
14 #include <sys/types.h>
15
16 #include <glib.h>
17 #include <libxml/tree.h>
18
19 #include <crm/common/xml.h>
20 #include <crm/common/xml_internal.h>
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 xmlNode *
58 pcmk__new_message_as(const char *origin, enum pcmk_ipc_server server,
59 const char *reply_to, const char *sender_system,
60 const char *recipient_node, const char *recipient_system,
61 const char *task, xmlNode *data)
62 {
63 static unsigned int message_counter = 0U;
64
65 xmlNode *message = NULL;
66 char *message_id = NULL;
67 const char *subtype = PCMK__VALUE_RESPONSE;
68
69 CRM_CHECK(!pcmk__str_empty(origin)
70 && !pcmk__str_empty(sender_system)
71 && !pcmk__str_empty(task),
72 return NULL);
73
74 if (reply_to == NULL) {
75 subtype = PCMK__VALUE_REQUEST;
76 message_id = crm_strdup_printf("%s-%s-%llu-%u", task, sender_system,
77 (unsigned long long) time(NULL),
78 message_counter++);
79 reply_to = message_id;
80 }
81
82 message = pcmk__xe_create(NULL, PCMK__XE_MESSAGE);
83 pcmk__xe_set_props(message,
84 PCMK_XA_ORIGIN, origin,
85 PCMK__XA_T, pcmk__server_message_type(server),
86 PCMK__XA_SUBT, subtype,
87 PCMK_XA_VERSION, CRM_FEATURE_SET,
88 PCMK_XA_REFERENCE, reply_to,
89 PCMK__XA_CRM_SYS_FROM, sender_system,
90 PCMK__XA_CRM_HOST_TO, recipient_node,
91 PCMK__XA_CRM_SYS_TO, recipient_system,
92 PCMK__XA_CRM_TASK, task,
93 NULL);
94 if (data != NULL) {
95 xmlNode *wrapper = pcmk__xe_create(message, PCMK__XE_CRM_XML);
96
97 pcmk__xml_copy(wrapper, data);
98 }
99 free(message_id);
100 return message;
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 xmlNode *
123 pcmk__new_reply_as(const char *origin, const xmlNode *original_request,
124 xmlNode *data)
125 {
126 const char *message_type = crm_element_value(original_request, PCMK__XA_T);
127 const char *host_from = crm_element_value(original_request, PCMK__XA_SRC);
128 const char *sys_from = crm_element_value(original_request,
129 PCMK__XA_CRM_SYS_FROM);
130 const char *sys_to = crm_element_value(original_request,
131 PCMK__XA_CRM_SYS_TO);
132 const char *type = crm_element_value(original_request, PCMK__XA_SUBT);
133 const char *operation = crm_element_value(original_request,
134 PCMK__XA_CRM_TASK);
135 const char *crm_msg_reference = crm_element_value(original_request,
136 PCMK_XA_REFERENCE);
137 enum pcmk_ipc_server server = pcmk__parse_server(message_type);
138
139 if (server == pcmk_ipc_unknown) {
140
141
142
143
144
145
146
147 server = pcmk_ipc_controld;
148 }
149
150 if (type == NULL) {
151 crm_warn("Cannot reply to invalid message: No message type specified");
152 return NULL;
153 }
154
155 if (strcmp(type, PCMK__VALUE_REQUEST) != 0) {
156
157
158
159 crm_trace("Creating a reply for a non-request original message");
160 }
161
162
163 return pcmk__new_message_as(origin, server, crm_msg_reference, sys_to,
164 host_from, sys_from, operation, data);
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180 GHashTable *
181 pcmk__register_handlers(const pcmk__server_command_t handlers[])
182 {
183 GHashTable *commands = g_hash_table_new(g_str_hash, g_str_equal);
184
185 if (handlers != NULL) {
186 int i;
187
188 for (i = 0; handlers[i].command != NULL; ++i) {
189 g_hash_table_insert(commands, (gpointer) handlers[i].command,
190 handlers[i].handler);
191 }
192 if (handlers[i].handler != NULL) {
193
194 g_hash_table_insert(commands, (gpointer) "", handlers[i].handler);
195 }
196 }
197 return commands;
198 }
199
200
201
202
203
204
205
206
207
208
209 xmlNode *
210 pcmk__process_request(pcmk__request_t *request, GHashTable *handlers)
211 {
212 xmlNode *(*handler)(pcmk__request_t *request) = NULL;
213
214 CRM_CHECK((request != NULL) && (request->op != NULL) && (handlers != NULL),
215 return NULL);
216
217 if (pcmk_is_set(request->flags, pcmk__request_sync)
218 && (request->ipc_client != NULL)) {
219 CRM_CHECK(request->ipc_client->request_id == request->ipc_id,
220 return NULL);
221 }
222
223 handler = g_hash_table_lookup(handlers, request->op);
224 if (handler == NULL) {
225 handler = g_hash_table_lookup(handlers, "");
226 if (handler == NULL) {
227 crm_info("Ignoring %s request from %s %s with no handler",
228 request->op, pcmk__request_origin_type(request),
229 pcmk__request_origin(request));
230 return NULL;
231 }
232 }
233
234 return (*handler)(request);
235 }
236
237
238
239
240
241
242
243 void
244 pcmk__reset_request(pcmk__request_t *request)
245 {
246 free(request->op);
247 request->op = NULL;
248
249 pcmk__reset_result(&(request->result));
250 }