1 #include "config.h"
2 
3 #include <razorback/debug.h>
4 #include <razorback/messages.h>
5 #include <razorback/log.h>
6 #include <razorback/event.h>
7 #include <razorback/json_buffer.h>
8 
9 #include "messages/core.h"
10 #include "binary_buffer.h"
11 
12 #include <string.h>
13 
14 static void BlockSubmission_Destroy (struct Message *message);
15 static bool BlockSubmission_Deserialize_Binary(struct Message *message);
16 static bool BlockSubmission_Deserialize_Json(struct Message *message);
17 static bool BlockSubmission_Deserialize(struct Message *message, int mode);
18 static bool BlockSubmission_Serialize_Binary(struct Message *message);
19 static bool BlockSubmission_Serialize_Json(struct Message *message);
20 static bool BlockSubmission_Serialize(struct Message *message, int mode);
21 
22 static struct MessageHandler handler = {
23     MESSAGE_TYPE_BLOCK,
24     BlockSubmission_Serialize,
25     BlockSubmission_Deserialize,
26     BlockSubmission_Destroy
27 };
28 
29 // core.h
30 void
MessageBlockSubmission_Init(void)31 MessageBlockSubmission_Init(void)
32 {
33     Message_Register_Handler(&handler);
34 }
35 
36 SO_PUBLIC struct Message *
MessageBlockSubmission_Initialize(struct Event * p_pEvent,uint32_t p_iReason,uint8_t locality)37 MessageBlockSubmission_Initialize (
38                                    struct Event *p_pEvent,
39                                    uint32_t p_iReason, uint8_t locality)
40 {
41     struct Message *msg;
42     struct MessageBlockSubmission *message;
43     ASSERT (p_pEvent != NULL);
44     if (p_pEvent == NULL)
45         return NULL;
46 
47     if ((msg = Message_Create(MESSAGE_TYPE_BLOCK, MESSAGE_VERSION_1, sizeof(struct MessageBlockSubmission))) == NULL)
48         return NULL;
49 
50     message = msg->message;
51 
52     message->pEvent = p_pEvent;
53     message->iReason = p_iReason;
54     message->storedLocality = locality;
55 
56     msg->destroy = BlockSubmission_Destroy;
57     msg->deserialize=BlockSubmission_Deserialize;
58     msg->serialize=BlockSubmission_Serialize;
59 
60     return msg;
61 }
62 
63 static void
BlockSubmission_Destroy(struct Message * message)64 BlockSubmission_Destroy (struct Message *message)
65 {
66     struct MessageBlockSubmission *msg;
67 
68     ASSERT (message != NULL);
69     if (message == NULL)
70         return;
71     msg = message->message;
72 
73     // destroy any malloc'd components
74     if (msg->pEvent != NULL)
75         Event_Destroy (msg->pEvent);
76 
77     Message_Destroy(message);
78 }
79 
80 static bool
BlockSubmission_Deserialize_Binary(struct Message * message)81 BlockSubmission_Deserialize_Binary(struct Message *message)
82 {
83     struct BinaryBuffer *buffer;
84     struct MessageBlockSubmission *submit;
85 
86     ASSERT(message != NULL);
87     if (message == NULL)
88         return false;
89 
90     if ((buffer = BinaryBuffer_CreateFromMessage(message)) == NULL)
91         return false;
92 
93     submit = message->message;
94 
95     if (!BinaryBuffer_Get_uint32_t (buffer, &submit->iReason))
96     {
97         buffer->pBuffer = NULL;
98         BinaryBuffer_Destroy (buffer);
99         return false;
100     }
101     if (!BinaryBuffer_Get_Event (buffer, &submit->pEvent))
102     {
103         buffer->pBuffer = NULL;
104         BinaryBuffer_Destroy (buffer);
105         return false;
106     }
107     if (!BinaryBuffer_Get_uint8_t (buffer, &submit->storedLocality))
108     {
109         buffer->pBuffer = NULL;
110         BinaryBuffer_Destroy (buffer);
111         return false;
112     }
113 
114     buffer->pBuffer = NULL;
115     BinaryBuffer_Destroy (buffer);
116 
117     return true;
118 }
119 
120 static bool
BlockSubmission_Deserialize_Json(struct Message * message)121 BlockSubmission_Deserialize_Json(struct Message *message)
122 {
123     struct MessageBlockSubmission *submit;
124     json_object *msg;
125 
126     ASSERT(message != NULL);
127     if (message == NULL)
128         return false;
129 
130     if ((msg = json_tokener_parse((char *)message->serialized)) == NULL)
131         return false;
132 
133     submit = message->message;
134 
135     if (!JsonBuffer_Get_uint32_t (msg, "Reason", &submit->iReason))
136     {
137         json_object_put(msg);
138         return false;
139     }
140     if (!JsonBuffer_Get_Event (msg, "Event", &submit->pEvent))
141     {
142         json_object_put(msg);
143         return false;
144     }
145     if (!JsonBuffer_Get_uint8_t (msg, "Stored_Locality", &submit->storedLocality))
146     {
147         json_object_put(msg);
148         return false;
149     }
150 
151     json_object_put(msg);
152     return true;
153 }
154 
155 static bool
BlockSubmission_Deserialize(struct Message * message,int mode)156 BlockSubmission_Deserialize(struct Message *message, int mode)
157 {
158     ASSERT(message != NULL);
159     if ( message == NULL )
160         return false;
161 
162     if ((message->message = calloc(1,sizeof(struct MessageBlockSubmission))) == NULL)
163         return false;
164 
165     switch (mode)
166     {
167     case MESSAGE_MODE_BIN:
168         return BlockSubmission_Deserialize_Binary(message);
169     case MESSAGE_MODE_JSON:
170         return BlockSubmission_Deserialize_Json(message);
171     default:
172         rzb_log(LOG_ERR, "%s: Invalid deserialization mode", __func__);
173         return false;
174     }
175     return false;
176 }
177 
178 
179 static bool
BlockSubmission_Serialize_Binary(struct Message * message)180 BlockSubmission_Serialize_Binary(struct Message *message)
181 {
182     struct MessageBlockSubmission *submit;
183     struct BinaryBuffer *buffer;
184 
185     ASSERT(message != NULL);
186     if (message == NULL)
187         return false;
188 
189     submit = message->message;
190 
191     message->length = Event_BinaryLength (submit->pEvent) +
192          sizeof(uint8_t) + // XXX: Whats this bad boy?
193          (uint32_t) sizeof (submit->iReason) + sizeof(uint8_t);
194 
195     if ((buffer = BinaryBuffer_Create(message->length)) == NULL)
196         return false;
197 
198     if (!BinaryBuffer_Put_uint32_t (buffer, submit->iReason))
199     {
200         BinaryBuffer_Destroy (buffer);
201         return false;
202     }
203 
204     if (!BinaryBuffer_Put_Event (buffer, submit->pEvent))
205     {
206         BinaryBuffer_Destroy (buffer);
207         return false;
208     }
209     if (!BinaryBuffer_Put_uint8_t (buffer, submit->storedLocality))
210     {
211         BinaryBuffer_Destroy (buffer);
212         return false;
213     }
214 
215     message->serialized = buffer->pBuffer;
216     buffer->pBuffer = NULL;
217     BinaryBuffer_Destroy(buffer);
218     return true;
219 }
220 
221 static bool
BlockSubmission_Serialize_Json(struct Message * message)222 BlockSubmission_Serialize_Json(struct Message *message)
223 {
224     struct MessageBlockSubmission *submit;
225     json_object *msg;
226     const char * wire;
227 
228     ASSERT(message != NULL);
229     if (message == NULL)
230         return false;
231 
232     submit = message->message;
233 
234     if ((msg = json_object_new_object()) == NULL)
235         return false;
236 
237     if (!JsonBuffer_Put_uint32_t (msg, "Reason", submit->iReason))
238     {
239         json_object_put(msg);
240         return false;
241     }
242 
243     if (!JsonBuffer_Put_Event (msg, "Event", submit->pEvent))
244     {
245         json_object_put(msg);
246         return false;
247     }
248 
249     if (!JsonBuffer_Put_uint8_t (msg, "Stored_Locality", submit->storedLocality))
250     {
251         json_object_put(msg);
252         return false;
253     }
254 
255     wire = json_object_to_json_string(msg);
256     message->length=strlen(wire);
257     if ((message->serialized = calloc(message->length+1, sizeof(char))) == NULL)
258     {
259         json_object_put(msg);
260         return false;
261     }
262     strcpy((char *)message->serialized, wire);
263     json_object_put(msg);
264 
265     return true;
266 }
267 
268 static bool
BlockSubmission_Serialize(struct Message * message,int mode)269 BlockSubmission_Serialize(struct Message *message, int mode)
270 {
271     ASSERT(message != NULL);
272     if ( message == NULL )
273         return false;
274 
275     switch (mode)
276     {
277     case MESSAGE_MODE_BIN:
278         return BlockSubmission_Serialize_Binary(message);
279     case MESSAGE_MODE_JSON:
280         return BlockSubmission_Serialize_Json(message);
281     default:
282         rzb_log(LOG_ERR, "%s: Invalid deserialization mode", __func__);
283         return false;
284     }
285     return false;
286 }
287