event.c
Go to the documentation of this file.
1 /*!**********************************************************************
2 *
3 * @copyright Copyright (C) 2016 Siemens Aktiengesellschaft.\n
4 * All rights reserved.
5 *
6 *************************************************************************
7 *
8 * @file event.c
9 * @date Jul 27, 2016
10 * @brief Event module implementation file.
11 *
12 ************************************************************************/
13 
14 #include "definitions.h"
15 #include "event.h"
16 #include "log_util.h"
17 #include "memory.h"
18 #include "random.h"
19 #include "json_util.h"
20 #include "time_util.h"
21 #include "mcl/mcl_event.h"
22 
23 E_MCL_ERROR_CODE event_initialize(item_meta_t *meta, const char* type, const char *version, mcl_int32_t severity, const char* timestamp, event_t **event)
24 {
25  DEBUG_ENTRY("item_meta_t *meta = <%p>, const char *type = <%p>, const char *version = <%p>, mcl_int32_t severity = <%d>, const char *timestamp = <%p>, event_t **event = <%p>", meta, type, version, severity, timestamp, event)
26 
27  E_MCL_ERROR_CODE code;
28 
29  if (MCL_NULL == MCL_NEW(*event))
30  {
31  MCL_ERROR_RETURN(MCL_OUT_OF_MEMORY, "Memory couldn't be allocated for event.");
32  }
33 
34  (*event)->meta = meta;
35 
36  if (MCL_NULL == MCL_NEW((*event)->payload))
37  {
38  MCL_FREE(*event);
39  MCL_ERROR_RETURN(MCL_OUT_OF_MEMORY, "Memory couldn't be allocated for event payload.");
40  }
41 
42  // Event payload.
43  (*event)->payload->id = MCL_NULL;
44  (*event)->payload->correlation_id = MCL_NULL;
45  (*event)->payload->timestamp = MCL_NULL;
46  (*event)->payload->description = MCL_NULL;
47  (*event)->payload->type = MCL_NULL;
48  (*event)->payload->version = MCL_NULL;
49  (*event)->payload->details = MCL_NULL;
50 
51  // Set id (Unique identifier of the event).
52  code = random_generate_guid(&(*event)->payload->id);
53 
54  // Set type.
55  (MCL_OK == code) && (code = string_initialize_new(type, 0, &(*event)->payload->type));
56 
57  // Set version.
58  (MCL_OK == code) && (code = string_initialize_new(version, 0, &(*event)->payload->version));
59 
60  // Set severity.
61  (*event)->payload->severity = severity;
62 
63  // Set timestamp.
64  (MCL_OK == code) && (code = string_initialize_new(timestamp, 0, &(*event)->payload->timestamp));
65 
66  // error check:
67  if (MCL_OK != code)
68  {
69  MCL_DEBUG("payload initialize operation(s) has been failed!");
70  event_destroy(event);
71  }
72 
73  DEBUG_LEAVE("retVal = <%d>", code);
74  return code;
75 }
76 
78 {
79  DEBUG_ENTRY("mcl_event_t *event = <%p>, E_MCL_EVENT_OPTION option = <%d>, const void *value = <%p>", event, option, value)
80 
81  E_MCL_ERROR_CODE code;
82 
83  ASSERT_NOT_NULL(event);
84  ASSERT_NOT_NULL(value);
85 
86  switch (option)
87  {
89  string_destroy(&event->payload->correlation_id);
90  code = string_initialize_new((const char *)value, 0, &event->payload->correlation_id);
91  break;
93  string_destroy(&event->payload->description);
94  code = string_initialize_new((const char *)value, 0, &event->payload->description);
95  break;
97  json_util_destroy(&event->payload->details);
98  code = json_util_duplicate((const json_t *)value, MCL_TRUE, &event->payload->details);
99  break;
100  default:
101  code = MCL_INVALID_PARAMETER;
102  }
103 
104  DEBUG_LEAVE("retVal = <%d>", code);
105  return code;
106 }
107 
108 void event_destroy(event_t **event)
109 {
110  DEBUG_ENTRY("event_t **event = <%p>", event)
111 
112  if (MCL_NULL != *event)
113  {
114  // payload
115  string_destroy(&((*event)->payload->id));
116  string_destroy(&((*event)->payload->correlation_id));
117  string_destroy(&((*event)->payload->timestamp));
118  string_destroy(&((*event)->payload->description));
119  string_destroy(&((*event)->payload->type));
120  string_destroy(&((*event)->payload->version));
121  json_util_destroy(&((*event)->payload->details));
122  MCL_FREE((*event)->payload);
123  MCL_FREE(*event);
124  }
125 
126  DEBUG_LEAVE("retVal = void");
127 }
void string_destroy(string_t **string)
Destroys the allocated resources of the string.
Definition: string_type.c:326
Memory module header file.
#define DEBUG_LEAVE(...)
Definition: log_util.h:81
Json util module header file.
#define DEBUG_ENTRY(...)
Definition: log_util.h:80
#define MCL_NEW(p)
Definition: memory.h:121
E_MCL_ERROR_CODE string_initialize_new(const char *value, mcl_size_t value_length, string_t **string)
Initializes a new string_t object with the given value and length.
Definition: string_type.c:46
#define MCL_TRUE
Definition: mcl_common.h:54
#define MCL_DEBUG(...)
Definition: log_util.h:70
#define ASSERT_NOT_NULL(argument)
Definition: definitions.h:129
E_MCL_EVENT_OPTION
Optional parameters for event.
Definition: mcl_common.h:125
struct mcl_event_t mcl_event_t
This struct is used for building only one message of event.
Definition: mcl_event.h:27
Log utility module header file.
Description option.
Definition: mcl_common.h:128
E_MCL_ERROR_CODE
MCL Error code definitions. Every function returning an error code uses this enum values...
Definition: mcl_common.h:137
This struct is used for building item.meta structure.
Definition: data_types.h:84
void json_util_destroy(json_t **root)
This function destroys root.
Definition: json_util.c:863
#define MCL_FREE(p)
Definition: memory.h:125
Event module interface header file.
E_MCL_ERROR_CODE event_initialize(item_meta_t *meta, const char *type, const char *version, mcl_int32_t severity, const char *timestamp, event_t **event)
This function creates and initializes a data struct of event_t.
Definition: event.c:23
E_MCL_ERROR_CODE mcl_event_set_option(mcl_event_t *event, E_MCL_EVENT_OPTION option, const void *value)
This function is used to set optional payload fields of event.
Definition: event.c:77
General invalid parameter fail.
Definition: mcl_common.h:144
Correlation id option.
Definition: mcl_common.h:127
void event_destroy(event_t **event)
To destroy the event_t data struct.
Definition: event.c:108
E_MCL_ERROR_CODE random_generate_guid(string_t **guid)
Generates random guid.
Definition: random.c:86
Definitions module header file.
E_MCL_ERROR_CODE json_util_duplicate(const json_t *source_json, mcl_bool_t with_children, json_t **duplicated_json)
This function duplicates source_json as duplicated_json.
Definition: json_util.c:805
Success.
Definition: mcl_common.h:140
Event module header file.
Random module header file.
This struct is used for building a single event.
Definition: event.h:24
Memory allocation fail.
Definition: mcl_common.h:143
#define MCL_NULL
Definition: definitions.h:24
int32_t mcl_int32_t
Definition: mcl_common.h:41
Details option.
Definition: mcl_common.h:129
Time utility module header file.
This struct is used for json handling.
Definition: json_util.h:27
#define MCL_ERROR_RETURN(return_value,...)
Definition: definitions.h:69