1 #include "config.h"
2
3 #include <razorback/debug.h>
4 #include <razorback/messages.h>
5 #include <razorback/log.h>
6 #include <razorback/block_id.h>
7 #include <razorback/json_buffer.h>
8
9
10 #include "messages/core.h"
11 #include "binary_buffer.h"
12
13 #include <string.h>
14
15 static void CacheResp_Destroy (struct Message *message);
16 static bool CacheResp_Deserialize_Binary(struct Message *message);
17 static bool CacheResp_Deserialize_Json(struct Message *message);
18 static bool CacheResp_Deserialize(struct Message *message, int mode);
19 static bool CacheResp_Serialize_Binary(struct Message *message);
20 static bool CacheResp_Serialize_Json(struct Message *message);
21 static bool CacheResp_Serialize(struct Message *message, int mode);
22
23 static struct MessageHandler handler = {
24 MESSAGE_TYPE_RESP,
25 CacheResp_Serialize,
26 CacheResp_Deserialize,
27 CacheResp_Destroy
28 };
29
30 //core.h
31 void
MessageCacheResp_Init(void)32 MessageCacheResp_Init(void)
33 {
34 Message_Register_Handler(&handler);
35 }
36
37 SO_PUBLIC struct Message *
MessageCacheResp_Initialize(const struct BlockId * p_pBlockId,uint32_t p_iSfFlags,uint32_t p_iEntFlags)38 MessageCacheResp_Initialize (
39 const struct BlockId *p_pBlockId,
40 uint32_t p_iSfFlags, uint32_t p_iEntFlags)
41 {
42 struct Message *msg;
43 struct MessageCacheResp *message;
44
45 ASSERT (p_pBlockId != NULL);
46 if (p_pBlockId == NULL)
47 return NULL;
48
49 if ((msg = Message_Create(MESSAGE_TYPE_RESP, MESSAGE_VERSION_1, sizeof(struct MessageCacheResp))) == NULL)
50 return NULL;
51
52 message = msg->message;
53
54 // fill in rest of message
55 if ((message->pId = BlockId_Clone (p_pBlockId)) == NULL)
56 {
57 rzb_log (LOG_ERR,
58 "%s: failed due to failure of BlockId_Clone", __func__);
59 CacheResp_Destroy(msg);
60 return NULL;
61 }
62 message->iSfFlags = p_iSfFlags;
63 message->iEntFlags = p_iEntFlags;
64 msg->destroy=CacheResp_Destroy;
65 msg->deserialize=CacheResp_Deserialize;
66 msg->serialize=CacheResp_Serialize;
67
68 return msg;
69 }
70
71 static void
CacheResp_Destroy(struct Message * message)72 CacheResp_Destroy (struct Message *message)
73 {
74 struct MessageCacheResp *msg;
75 ASSERT (message != NULL);
76 if (message == NULL)
77 return;
78
79 msg = message->message;
80
81 // destroy any malloc'd components
82 if (msg->pId != NULL)
83 BlockId_Destroy (msg->pId);
84
85 Message_Destroy(message);
86 }
87
88 static bool
CacheResp_Deserialize_Binary(struct Message * message)89 CacheResp_Deserialize_Binary(struct Message *message)
90 {
91 struct BinaryBuffer *buffer;
92 struct MessageCacheResp *submit;
93
94 ASSERT(message != NULL);
95 if (message == NULL)
96 return false;
97
98 if ((buffer = BinaryBuffer_CreateFromMessage(message)) == NULL)
99 return false;
100
101 submit = message->message;
102
103 if (!BinaryBuffer_Get_BlockId (buffer, &submit->pId))
104 {
105 buffer->pBuffer = NULL;
106 BinaryBuffer_Destroy (buffer);
107 rzb_log (LOG_ERR,
108 "%s: failed due to failure of BinaryBuffer_Get_BlockId", __func__);
109 return false;
110 }
111
112 if (!BinaryBuffer_Get_uint32_t (buffer, &submit->iSfFlags))
113 {
114 buffer->pBuffer = NULL;
115 BinaryBuffer_Destroy (buffer);
116 rzb_log (LOG_ERR,
117 "%s: failed due to failure of BinaryBuffer_Get_uint32_t", __func__);
118 return false;
119 }
120 if (!BinaryBuffer_Get_uint32_t (buffer, &submit->iEntFlags))
121 {
122 buffer->pBuffer = NULL;
123 BinaryBuffer_Destroy (buffer);
124 rzb_log (LOG_ERR,
125
126 "%s: failed due to failure of BinaryBuffer_Get_uint32_t", __func__);
127 return false;
128 }
129
130 buffer->pBuffer = NULL;
131 BinaryBuffer_Destroy (buffer);
132
133 return true;
134 }
135
136 static bool
CacheResp_Deserialize_Json(struct Message * message)137 CacheResp_Deserialize_Json(struct Message *message)
138 {
139 struct MessageCacheResp *submit;
140 json_object *msg;
141
142 ASSERT(message != NULL);
143 if (message == NULL)
144 return false;
145
146 if ((msg = json_tokener_parse((char *)message->serialized)) == NULL)
147 return false;
148
149 submit = message->message;
150
151 if (!JsonBuffer_Get_BlockId (msg, "Block_ID", &submit->pId))
152 {
153 json_object_put(msg);
154 rzb_log (LOG_ERR,
155 "%s: failed due to failure of JsonBuffer_Get_BlockId", __func__);
156 return false;
157 }
158
159 if (!JsonBuffer_Get_uint32_t (msg, "SF_Flags", &submit->iSfFlags))
160 {
161 json_object_put(msg);
162 rzb_log (LOG_ERR,
163 "%s: failed due to failure of JsonBuffer_Get_uint32_t", __func__);
164 return false;
165 }
166 if (!JsonBuffer_Get_uint32_t (msg, "Ent_Flags", &submit->iEntFlags))
167 {
168 json_object_put(msg);
169 rzb_log (LOG_ERR,
170
171 "%s: failed due to failure of JsonBuffer_Get_uint32_t", __func__);
172 return false;
173 }
174 json_object_put(msg);
175 return true;
176 }
177
178 static bool
CacheResp_Deserialize(struct Message * message,int mode)179 CacheResp_Deserialize(struct Message *message, int mode)
180 {
181 ASSERT(message != NULL);
182 if ( message == NULL )
183 return false;
184
185 if ((message->message = calloc(1,sizeof(struct MessageCacheResp))) == NULL)
186 return false;
187
188 switch (mode)
189 {
190 case MESSAGE_MODE_BIN:
191 return CacheResp_Deserialize_Binary(message);
192 case MESSAGE_MODE_JSON:
193 return CacheResp_Deserialize_Json(message);
194 default:
195 rzb_log(LOG_ERR, "%s: Invalid deserialization mode", __func__);
196 return false;
197 }
198 return false;
199 }
200
201
202 static bool
CacheResp_Serialize_Binary(struct Message * message)203 CacheResp_Serialize_Binary(struct Message *message)
204 {
205 struct MessageCacheResp *submit;
206 struct BinaryBuffer *buffer;
207
208 ASSERT(message != NULL);
209 if (message == NULL)
210 return false;
211
212 submit = message->message;
213
214 message->length = BlockId_BinaryLength (submit->pId) +
215 (sizeof(uint32_t)*2);
216
217 if ((buffer = BinaryBuffer_Create(message->length)) == NULL)
218 return false;
219
220 if (!BinaryBuffer_Put_BlockId (buffer, submit->pId))
221 {
222 BinaryBuffer_Destroy (buffer);
223 rzb_log (LOG_ERR,
224 "%s: failed due to failure of BinaryBuffer_Put_BlockId", __func__);
225 return false;
226 }
227 if (!BinaryBuffer_Put_uint32_t (buffer, submit->iSfFlags))
228 {
229 BinaryBuffer_Destroy (buffer);
230 rzb_log (LOG_ERR,
231 "%s: failed due to failure of BinaryBuffer_Put_uint32_t", __func__);
232 return false;
233 }
234 if (!BinaryBuffer_Put_uint32_t (buffer, submit->iEntFlags))
235 {
236 BinaryBuffer_Destroy (buffer);
237 rzb_log (LOG_ERR,
238 "%s: failed due to failure of BinaryBuffer_Put_uint32_t", __func__);
239 return false;
240 }
241
242 message->serialized = buffer->pBuffer;
243 buffer->pBuffer = NULL;
244 BinaryBuffer_Destroy(buffer);
245 return true;
246 }
247
248 static bool
CacheResp_Serialize_Json(struct Message * message)249 CacheResp_Serialize_Json(struct Message *message)
250 {
251 struct MessageCacheResp *submit;
252 json_object *msg;
253 const char * wire;
254
255 ASSERT(message != NULL);
256 if (message == NULL)
257 return false;
258
259 if ((msg = json_object_new_object()) == NULL)
260 return false;
261
262 submit = message->message;
263
264 if (!JsonBuffer_Put_BlockId (msg, "Block_ID", submit->pId))
265 {
266 rzb_log (LOG_ERR,
267 "%s: failed due to failure of JsonBuffer_Put_BlockId", __func__);
268 return false;
269 }
270 if (!JsonBuffer_Put_uint32_t (msg, "SF_Flags", submit->iSfFlags))
271 {
272 rzb_log (LOG_ERR,
273 "%s: failed due to failure of JsonBuffer_Put_uint32_t", __func__);
274 return false;
275 }
276 if (!JsonBuffer_Put_uint32_t (msg, "Ent_Flags", submit->iEntFlags))
277 {
278 rzb_log (LOG_ERR,
279 "%s: failed due to failure of JsonBuffer_Put_uint32_t", __func__);
280 return false;
281 }
282 wire = json_object_to_json_string(msg);
283 message->length=strlen(wire);
284 if ((message->serialized = calloc(message->length+1, sizeof(char))) == NULL)
285 {
286 json_object_put(msg);
287 return false;
288 }
289 strcpy((char *)message->serialized, wire);
290 json_object_put(msg);
291
292 return true;
293 }
294
295 static bool
CacheResp_Serialize(struct Message * message,int mode)296 CacheResp_Serialize(struct Message *message, int mode)
297 {
298 ASSERT(message != NULL);
299 if ( message == NULL )
300 return false;
301
302 switch (mode)
303 {
304 case MESSAGE_MODE_BIN:
305 return CacheResp_Serialize_Binary(message);
306 case MESSAGE_MODE_JSON:
307 return CacheResp_Serialize_Json(message);
308 default:
309 rzb_log(LOG_ERR, "%s: Invalid deserialization mode", __func__);
310 return false;
311 }
312 return false;
313 }
314