ANSI C11 interface
Daggy Core ANSI C11 interface

ANSI C functions

Core.h
1
#include "daggycore_export.h"
2
3
#include "Types.h"
4
#include "Errors.h"
5
6
#ifdef __cplusplus
7
extern "C" {
8
#endif
9
10
DAGGYCORE_EXPORT DaggyErrors libdaggy_core_create(const char* sources,
11
DaggySourcesTextTypes text_type,
12
DaggyCore* core);
13
DAGGYCORE_EXPORT DaggyErrors libdaggy_core_start(DaggyCore core);
14
DAGGYCORE_EXPORT DaggyErrors libdaggy_core_stop(DaggyCore core);
15
DAGGYCORE_EXPORT void libdaggy_core_destroy(DaggyCore* core);
16
17
DAGGYCORE_EXPORT DaggyErrors libdaggy_connect_aggregator(DaggyCore core,
18
libdaggy_on_daggy_state_changed on_daggy_state_changed,
19
libdaggy_on_provider_state_changed on_provider_state_changed,
20
libdaggy_on_provider_error on_provider_error,
21
libdaggy_on_command_state_changed on_command_state_changed,
22
libdaggy_on_command_stream on_command_stream,
23
libdaggy_on_command_error on_command_error);
24
25
26
DAGGYCORE_EXPORT void libdaggy_app_create(int argc, char** argv);
27
DAGGYCORE_EXPORT int libdaggy_app_exec();
28
DAGGYCORE_EXPORT void libdaggy_app_stop();
29
30
DAGGYCORE_EXPORT void libdaggy_run_in_thread(libdaggy_thread_function function, void* parameter);
31
32
#ifdef __cplusplus
33
}
34
#endif
35
Copied!

Usage Example

test.c
1
#include <stdio.h>
2
#ifdef _WIN32
3
#include <Windows.h>
4
#else
5
#include <unistd.h>
6
#endif
7
8
#include <DaggyCore/Core.h>
9
10
const char* json_data =
11
"{\
12
\"sources\": {\
13
\"localhost\" : {\
14
\"type\": \"local\",\
15
\"commands\": {\
16
\"ping1\": {\
17
\"exec\": \"ping 127.0.0.1\",\
18
\"extension\": \"log\"\
19
},\
20
\"ping2\": {\
21
\"exec\": \"ping 127.0.0.1\",\
22
\"extension\": \"log\"\
23
}\
24
}\
25
}\
26
}\
27
}"
28
;
29
30
void sleep_ms(int milliseconds)
31
{
32
#ifdef WIN32
33
Sleep(milliseconds);
34
#elif _POSIX_C_SOURCE >= 199309L
35
struct timespec ts;
36
ts.tv_sec = milliseconds / 1000;
37
ts.tv_nsec = (milliseconds % 1000) * 1000000;
38
nanosleep(&ts, NULL);
39
#else
40
usleep(milliseconds * 1000);
41
#endif
42
}
43
44
int quit_after_time(void* msec)
45
{
46
sleep_ms(*(int*)(msec));
47
libdaggy_app_stop();
48
return 0;
49
}
50
51
void on_daggy_state_changed(DaggyCore core, DaggyStates state);
52
53
void on_provider_state_changed(DaggyCore core, const char* provider_id, DaggyProviderStates state);
54
void on_provider_error(DaggyCore core, const char* provider_id, DaggyError error);
55
56
void on_command_state_changed(DaggyCore core, const char* provider_id, const char* command_id, DaggyCommandStates state, int exit_code);
57
void on_command_stream(DaggyCore core, const char* provider_id, const char* command_id, DaggyStream stream);
58
void on_command_error(DaggyCore core, const char* provider_id, const char* command_id, DaggyError error);
59
60
int main(int argc, char** argv)
61
{
62
DaggyCore core;
63
libdaggy_app_create(argc, argv);
64
libdaggy_core_create(json_data, Json, &core);
65
libdaggy_connect_aggregator(core,
66
on_daggy_state_changed,
67
on_provider_state_changed,
68
on_provider_error,
69
on_command_state_changed,
70
on_command_stream,
71
on_command_error);
72
libdaggy_core_start(core);
73
int time = 5000;
74
libdaggy_run_in_thread(quit_after_time, &time);
75
return libdaggy_app_exec();
76
}
77
78
void on_daggy_state_changed(DaggyCore core, DaggyStates state)
79
{
80
printf("Daggy state changed: %d\n", state);
81
}
82
83
void on_provider_state_changed(DaggyCore core, const char* provider_id, DaggyProviderStates state)
84
{
85
printf("Provider %s state changed: %d\n", provider_id, state);
86
}
87
88
void on_provider_error(DaggyCore core, const char* provider_id, DaggyError error)
89
{
90
printf("Provider %s error. Code: %d, Category: %s\n", provider_id, error.error, error.category);
91
}
92
93
void on_command_state_changed(DaggyCore core, const char* provider_id, const char* command_id, DaggyCommandStates state, int exit_code)
94
{
95
printf("Command %s in provider %s state changed: %d\n", command_id, provider_id, state);
96
}
97
98
void on_command_stream(DaggyCore core, const char* provider_id, const char* command_id, DaggyStream stream)
99
{
100
printf("Command %s in provider %s has stream from session %s: %li\n", command_id, provider_id, stream.session, stream.seq_num);
101
}
102
103
void on_command_error(DaggyCore core, const char* provider_id, const char* command_id, DaggyError error)
104
{
105
printf("Command %s in provider %s has error. Code: %d, Category: %s\n", command_id, provider_id, error.error, error.category);
106
}
107
Copied!
Last modified 19d ago
Copy link
Edit on GitHub