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/block.h>
8 #include <razorback/block_id.h>
9 #include <razorback/nugget.h>
10 #include <razorback/json_buffer.h>
11 
12 #include "messages/core.h"
13 
14 #include <string.h>
15 
16 static void OutputInspection_Destroy (struct Message *message);
17 static bool OutputInspection_Deserialize_Json(struct Message *message);
18 static bool OutputInspection_Deserialize(struct Message *message, int mode);
19 static bool OutputInspection_Serialize_Json(struct Message *message);
20 static bool OutputInspection_Serialize(struct Message *message, int mode);
21 
22 static struct MessageHandler handler = {
23     MESSAGE_TYPE_OUTPUT_INSPECTION,
24     OutputInspection_Serialize,
25     OutputInspection_Deserialize,
26     OutputInspection_Destroy
27 };
28 
29 //core.h
30 void
MessageOutputInspection_Init(void)31 MessageOutputInspection_Init(void)
32 {
33     Message_Register_Handler(&handler);
34 }
35 
36 SO_PUBLIC struct Message *
MessageOutputInspection_Initialize(struct Nugget * nugget,struct BlockId * blockId,uint8_t reason,bool final)37 MessageOutputInspection_Initialize (
38                                    struct Nugget *nugget,
39                                    struct BlockId *blockId, uint8_t reason, bool final)
40 {
41     struct Message *msg;
42     struct MessageOutputInspection *message;
43     struct timespec l_tsTime;
44     ASSERT (blockId != NULL);
45     ASSERT (nugget != NULL);
46     if (blockId == NULL)
47         return NULL;
48     if (nugget == NULL)
49         return NULL;
50 
51     if ((msg = Message_Create(MESSAGE_TYPE_OUTPUT_INSPECTION, MESSAGE_VERSION_1, sizeof(struct MessageOutputInspection))) == NULL)
52         return NULL;
53     message = msg->message;
54     message->blockId = BlockId_Clone(blockId);
55     message->nugget = nugget;
56     message->status = reason;
57     message->final = final;
58 
59     memset(&l_tsTime, 0, sizeof(struct timespec));
60     if (clock_gettime(CLOCK_REALTIME, &l_tsTime) == -1)
61     {
62         rzb_log(LOG_ERR, "%s: Failed to get time stamp", __func__);
63         return NULL;
64     }
65     message->seconds = l_tsTime.tv_sec;
66     message->nanosecs = l_tsTime.tv_nsec;
67 
68     msg->destroy = OutputInspection_Destroy;
69     msg->deserialize=OutputInspection_Deserialize;
70     msg->serialize=OutputInspection_Serialize;
71 
72     return msg;
73 }
74 
75 static void
OutputInspection_Destroy(struct Message * message)76 OutputInspection_Destroy (struct Message *message)
77 {
78     struct MessageOutputInspection *msg;
79 
80     ASSERT (message != NULL);
81     if (message == NULL)
82         return;
83     msg = message->message;
84 
85     // destroy any malloc'd components
86     if (msg->blockId != NULL)
87         BlockId_Destroy(msg->blockId);
88 
89     if (msg->nugget != NULL)
90         Nugget_Destroy(msg->nugget);
91 
92     Message_Destroy(message);
93 }
94 
95 static bool
OutputInspection_Deserialize_Json(struct Message * message)96 OutputInspection_Deserialize_Json(struct Message *message)
97 {
98     struct MessageOutputInspection *event;
99     json_object *msg;
100 
101     ASSERT(message != NULL);
102     if (message == NULL)
103         return false;
104 
105     if ((msg = json_tokener_parse((char *)message->serialized)) == NULL)
106         return false;
107 
108     event = message->message;
109 
110     if (!JsonBuffer_Get_Nugget(msg, "Nugget", &event->nugget))
111     {
112         json_object_put(msg);
113         return false;
114     }
115     if (!JsonBuffer_Get_BlockId(msg, "BlockId", &event->blockId))
116     {
117         json_object_put(msg);
118         return false;
119     }
120     if (!JsonBuffer_Get_uint8_t(msg, "Status", &event->status))
121     {
122         json_object_put(msg);
123         return false;
124     }
125     if (!JsonBuffer_Get_uint64_t(msg, "Seconds", &event->seconds))
126     {
127         json_object_put(msg);
128         return false;
129     }
130     if (!JsonBuffer_Get_uint64_t(msg, "NanoSecs", &event->nanosecs))
131     {
132         json_object_put(msg);
133         return false;
134     }
135     uint8_t final = 0;
136     if (!JsonBuffer_Get_uint8_t(msg, "Final", &final))
137     {
138         json_object_put(msg);
139         return false;
140     }
141     if (final == 0)
142         event->final = false;
143     else
144         event->final = true;
145 
146     json_object_put(msg);
147     return true;
148 }
149 
150 static bool
OutputInspection_Deserialize(struct Message * message,int mode)151 OutputInspection_Deserialize(struct Message *message, int mode)
152 {
153     ASSERT(message != NULL);
154     if ( message == NULL )
155         return false;
156 
157     if ((message->message = calloc(1,sizeof(struct MessageOutputInspection))) == NULL)
158         return false;
159 
160     switch (mode)
161     {
162     case MESSAGE_MODE_JSON:
163         return OutputInspection_Deserialize_Json(message);
164     default:
165         rzb_log(LOG_ERR, "%s: Invalid deserialization mode", __func__);
166         return false;
167     }
168     return false;
169 }
170 
171 
172 static bool
OutputInspection_Serialize_Json(struct Message * message)173 OutputInspection_Serialize_Json(struct Message *message)
174 {
175     struct MessageOutputInspection *event;
176     json_object *msg;
177     const char * wire;
178 
179     ASSERT(message != NULL);
180     if (message == NULL)
181         return false;
182 
183     event = message->message;
184 
185     if ((msg = json_object_new_object()) == NULL)
186         return false;
187 
188     if (!JsonBuffer_Put_Nugget(msg, "Nugget", event->nugget))
189     {
190         json_object_put(msg);
191         return false;
192     }
193     if (!JsonBuffer_Put_BlockId(msg, "BlockId", event->blockId))
194     {
195         json_object_put(msg);
196         return false;
197     }
198     if (!JsonBuffer_Put_uint8_t(msg, "Status", event->status))
199     {
200         json_object_put(msg);
201         return false;
202     }
203     if (!JsonBuffer_Put_uint64_t(msg, "Seconds", event->seconds))
204     {
205         json_object_put(msg);
206         return false;
207     }
208     if (!JsonBuffer_Put_uint64_t(msg, "NanoSecs", event->nanosecs))
209     {
210         json_object_put(msg);
211         return false;
212     }
213     if (!JsonBuffer_Put_uint8_t(msg, "Final", (event->final ? 1 : 0)))
214     {
215         json_object_put(msg);
216         return false;
217     }
218 
219     wire = json_object_to_json_string(msg);
220     message->length=strlen(wire);
221     if ((message->serialized = calloc(message->length+1, sizeof(char))) == NULL)
222     {
223         json_object_put(msg);
224         return false;
225     }
226     strcpy((char *)message->serialized, wire);
227     json_object_put(msg);
228 
229     return true;
230 }
231 
232 
233 static bool
OutputInspection_Serialize(struct Message * message,int mode)234 OutputInspection_Serialize(struct Message *message, int mode)
235 {
236     ASSERT(message != NULL);
237     if ( message == NULL )
238         return false;
239 
240     switch (mode)
241     {
242     case MESSAGE_MODE_JSON:
243         return OutputInspection_Serialize_Json(message);
244     default:
245         rzb_log(LOG_ERR, "%s: Invalid deserialization mode", __func__);
246         return false;
247     }
248     return false;
249 }
250