1 #include "config.h"
2
3 #include <razorback/event.h>
4 #include <razorback/log.h>
5 #include <razorback/block.h>
6 #include <razorback/block_id.h>
7 #include <razorback/ntlv.h>
8 #include <razorback/thread.h>
9 #include <razorback/judgment.h>
10
11 #include <time.h>
12 #include <string.h>
13
14 #ifdef _MSC_VER
15 #include "bobins.h"
16 #endif
17
18
19 SO_PUBLIC struct Judgment *
Judgment_Create(struct EventId * eventId,struct BlockId * blockId)20 Judgment_Create (struct EventId *eventId, struct BlockId *blockId)
21 {
22 struct Judgment *judgment = NULL;
23 struct timespec l_tsTime;
24 struct RazorbackContext *l_pContext;
25
26 l_pContext = Thread_GetCurrentContext ();
27
28 memset(&l_tsTime, 0, sizeof(struct timespec));
29 if (clock_gettime(CLOCK_REALTIME, &l_tsTime) == -1)
30 {
31 rzb_log(LOG_ERR, "%s: Failed to get time stamp", __func__);
32 return NULL;
33 }
34
35 if ((judgment = calloc(1, sizeof(struct Judgment))) == NULL)
36 {
37 rzb_log(LOG_ERR, "%s: Failed allocate judgment", __func__);
38 return NULL;
39 }
40
41 if ((judgment->pMetaDataList = NTLVList_Create()) == NULL)
42 {
43 rzb_log(LOG_ERR, "%s: Failed to allocate judgment meta data", __func__);
44 Judgment_Destroy(judgment);
45 return NULL;
46 }
47 if (eventId != NULL)
48 {
49 if ((judgment->pEventId = EventId_Clone(eventId)) == NULL)
50 {
51 rzb_log(LOG_ERR, "%s: Failed to allocate judgment eventId", __func__);
52 Judgment_Destroy(judgment);
53 return NULL;
54 }
55 }
56 if (blockId != NULL)
57 {
58 if ((judgment->pBlockId = BlockId_Clone(blockId)) == NULL)
59 {
60 rzb_log(LOG_ERR, "%s: Failed to allocate judgment blockId", __func__);
61 Judgment_Destroy(judgment);
62 return NULL;
63 }
64 }
65 judgment->iSeconds = l_tsTime.tv_sec;
66 judgment->iNanoSecs = l_tsTime.tv_nsec;
67 uuid_copy(judgment->uuidNuggetId, l_pContext->uuidNuggetId);
68 return judgment;
69 }
70
71 SO_PUBLIC void
Judgment_Destroy(struct Judgment * judgment)72 Judgment_Destroy (struct Judgment *judgment)
73 {
74
75 if (judgment->pEventId != NULL)
76 EventId_Destroy(judgment->pEventId);
77
78 if (judgment->pBlockId != NULL)
79 BlockId_Destroy(judgment->pBlockId);
80
81 if (judgment->pMetaDataList != NULL)
82 List_Destroy(judgment->pMetaDataList);
83 if (judgment->sMessage != NULL)
84 free(judgment->sMessage);
85
86 free(judgment);
87 }
88
89 SO_PUBLIC uint32_t
Judgment_BinaryLength(struct Judgment * judgment)90 Judgment_BinaryLength (struct Judgment *judgment)
91 {
92 uint32_t size = sizeof(judgment->uuidNuggetId)+
93 (sizeof(uint64_t)*2) +
94 sizeof (struct EventId) +
95 sizeof (uint8_t) +
96 NTLVList_Size(judgment->pMetaDataList) +
97 (sizeof (uint32_t) * 6) +
98 sizeof(uint8_t) +
99 BlockId_BinaryLength(judgment->pBlockId);
100
101 if (judgment->sMessage != NULL)
102 size += strlen((char *)judgment->sMessage) +1;
103
104 return size;
105 }
106
107