Browse Source

Add basic agent implementaion and surgard template

Ilja Kartašov 1 month ago
parent
commit
5dd8cfd6b2
6 changed files with 267 additions and 35 deletions
  1. 2
    0
      project.mk
  2. 168
    9
      src/agent.c
  3. 34
    4
      src/agent.h
  4. 14
    18
      src/main.c
  5. 33
    3
      src/surgard.c
  6. 16
    1
      src/surgard.h

+ 2
- 0
project.mk View File

@@ -70,6 +70,8 @@ PROJECT_CFLAGS  = -D_POSIX_C_SOURCE=200809L
70 70
 PROJECT_CFLAGS += -D_ISOC99_SOURCE
71 71
 PROJECT_CFLAGS += -D_XOPEN_SOURCE=500
72 72
 PROJECT_CFLAGS += -D_GNU_SOURCE
73
+
74
+PROJECT_CFLAGS += -DARC_WITH_SURGARD
73 75
 # PROJECT_CFLAGS += -DDEBUG
74 76
 
75 77
 

+ 168
- 9
src/agent.c View File

@@ -12,33 +12,192 @@
12 12
  *
13 13
  * @see https://lowenware.com/
14 14
  */
15
-
15
+#include <stdbool.h>
16
+#include <stdlib.h>
17
+#include <string.h>
18
+#include <cStuff/string.h>
16 19
 #include "surgard.h"
20
+#include "log.h"
17 21
 #include "agent.h"
18 22
 
23
+#define LOG_PREFIX "agent: "
19 24
 
20
-int
21
-agent_init(void)
25
+
26
+static struct agent **m_agent = NULL;
27
+static int m_count = 0;
28
+static unsigned char m_buffer[ARC_AGENT_BUFFER_SIZE];
29
+
30
+
31
+static struct agent *
32
+agent__new(AgentType agent_type, const char *name, char *address, char *query)
22 33
 {
23
-	return 0;
34
+	struct agent *result = NULL;
35
+
36
+	switch (agent_type) {
37
+#ifdef ARC_WITH_SURGARD
38
+	case AGENT_TYPE_SURGARD:
39
+		result = surgard_new(agent_type, name, address, query);
40
+		break;
41
+#endif 
42
+	default:
43
+		break;
44
+	}
45
+	return result;
46
+}
47
+
48
+
49
+static void
50
+agent__free(struct agent *agent)
51
+{
52
+	switch (agent->agent_type) {
53
+#ifdef ARC_WITH_SURGARD
54
+	case AGENT_TYPE_SURGARD:
55
+		surgard_free(agent);
56
+		break;
57
+#endif
58
+	default:
59
+		break;
60
+	}
61
+
62
+	if (agent->name) {
63
+		free(agent->name);
64
+	}
65
+
66
+	free(agent);
67
+}
68
+
69
+
70
+static int
71
+agent__run(struct agent *agent, unsigned char *buffer, size_t buf_size)
72
+{
73
+	switch (agent->agent_type) {
74
+#ifdef ARC_WITH_SURGARD
75
+	case AGENT_TYPE_SURGARD:
76
+		return surgard_run(agent, m_buffer, sizeof (m_buffer));
77
+#endif 
78
+	default:
79
+		break;
80
+	}
81
+	return 1;
82
+}
83
+
84
+
85
+static AgentType
86
+agent_read_uri(char **p_uri, char **p_query)
87
+{
88
+	char *protocol = *p_uri, *address, *query;
89
+
90
+	if (!(address = strstr(protocol, "://"))) {
91
+		return AGENT_TYPE__UNKNOWN;
92
+	}
93
+
94
+	*address = 0;
95
+	address += 3;
96
+
97
+	if ((query = strchr(address, '?'))) {
98
+		*(address++) = 0;
99
+	}
100
+	*p_query = query;
101
+	*p_uri = address;
102
+
103
+#ifdef ARC_WITH_SURGARD 
104
+	if (surgard_supports(protocol)) {
105
+		return AGENT_TYPE_SURGARD;
106
+	}
107
+#endif
108
+
109
+	*p_uri = protocol;
110
+	return AGENT_TYPE__UNKNOWN;
111
+}
112
+
113
+
114
+struct agent *
115
+agent_alloc(const char *p_name, AgentType agent_type, size_t sz)
116
+{
117
+	struct agent *result;
118
+	char *name;
119
+
120
+	if (!cstuff_strcpy(&name, p_name)) {
121
+		if ((result = calloc(1, sz))) {
122
+			result->name = name;
123
+			result->agent_type = agent_type;
124
+			return result;
125
+		}
126
+		free(name);
127
+	} else {
128
+		result = NULL;
129
+	}
130
+	return result;
24 131
 }
25 132
 
26 133
 
27 134
 void
28
-agent_release(void)
135
+agent_free(struct agent *agent)
136
+{
137
+	if (agent->name)
138
+		free(agent->name);
139
+	free(agent);
140
+}
141
+
142
+
143
+int
144
+agent_set(const char *name, char *uri)
29 145
 {
146
+	char *query;
147
+	AgentType agent_type;
148
+	struct agent **agent;
149
+
150
+	if (!(agent_type = agent_read_uri(&uri, &query))) {
151
+		LOG_ALERT(LOG_PREFIX "unsupported protocol `%s`", uri);
152
+		return -1;
153
+	}
30 154
 
155
+	/* uri = address */
156
+	if (!(agent = realloc(m_agent, sizeof (*agent) * (++m_count)))) {
157
+		LOG_ALERT(LOG_PREFIX "out of memory (%d ports)", m_count);
158
+		m_count--;
159
+		return -1;
160
+	}
161
+
162
+	m_agent = agent;
163
+
164
+	if (!(agent[m_count - 1] = agent__new(agent_type, name, uri, query))) {
165
+		return -1;
166
+	}
167
+
168
+	return 0;
31 169
 }
32 170
 
33 171
 
34 172
 int
35
-agent_run_cycle(void)
173
+agent_run(void)
36 174
 {
37
-	int result = 1, rc;
175
+	int result = 1, i, rc;
38 176
 
39
-	if (!(rc = surgard_run_cycle())) {
40
-		result = rc;
177
+	for (i = 0; i < m_count; i++) {
178
+		memset(m_buffer, 0, sizeof (m_buffer));
179
+		if (!(rc = agent__run(m_agent[i], m_buffer, sizeof (m_buffer)))) {
180
+			result = rc;
181
+		}
41 182
 	}
42 183
 
43 184
 	return result;
44 185
 }
186
+
187
+
188
+void
189
+agent_unset_all(void)
190
+{
191
+	int i;
192
+
193
+	for (i = 0; i < m_count; i++) {
194
+		agent__free(m_agent[i]);
195
+	}
196
+
197
+	if (m_agent) {
198
+		free(m_agent);
199
+		m_count = 0;
200
+		m_agent = NULL;
201
+	}
202
+}
203
+

+ 34
- 4
src/agent.h View File

@@ -16,16 +16,46 @@
16 16
 #ifndef AGENT_H_D0CDB429_AC49_4BC6_83FA_9163404D387E
17 17
 #define AGENT_H_D0CDB429_AC49_4BC6_83FA_9163404D387E
18 18
 
19
+#include <stdbool.h>
19 20
 
20
-int
21
-agent_init(void);
21
+
22
+#ifndef ARC_AGENT_BUFFER_SIZE
23
+#define ARC_AGENT_BUFFER_SIZE (4*1024)
24
+#endif
25
+
26
+
27
+typedef enum {
28
+	AGENT_TYPE__UNKNOWN
29
+#ifdef ARC_WITH_SURGARD
30
+	, AGENT_TYPE_SURGARD
31
+#endif
32
+} AgentType;
33
+
34
+
35
+struct agent {
36
+	char *name;
37
+	AgentType agent_type;
38
+};
39
+
40
+
41
+struct agent *
42
+agent_alloc(const char *name, AgentType agent_type, size_t sz);
22 43
 
23 44
 
24 45
 void
25
-agent_release(void);
46
+agent_free(struct agent *agent);
26 47
 
27 48
 
28 49
 int
29
-agent_run_cycle(void);
50
+agent_set(const char *name, char *uri);
51
+
52
+
53
+int
54
+agent_run(void);
55
+
56
+
57
+void
58
+agent_unset_all(void);
59
+
30 60
 
31 61
 #endif /* !AGENT_H */

+ 14
- 18
src/main.c View File

@@ -112,25 +112,21 @@ main(int argc, char **argv)
112 112
 	/* Initialize instance */
113 113
 	if ((aisl = aisl_new(&m_cfg)) != NULL) {
114 114
 		if (observer_init() == 0) {
115
-			if (agent_init() == 0) {
116
-				/* launch application loop */
117
-				fprintf(stdout, "Entering main loop");
118
-				for(;;) {
119
-					int rc, do_sleep = 1;
120
-
121
-					if ((rc = aisl_run_cycle(aisl)) == AISL_SUCCESS)
122
-						do_sleep = 0;
123
-
124
-					if ((rc = agent_run_cycle()) == 0)
125
-						do_sleep = 0;
126
-
127
-					if (!do_sleep)
128
-						continue;
129
-
130
-					aisl_sleep(aisl, 500);
131
-				}
132
-				agent_release();
115
+			/* launch application loop */
116
+			fprintf(stdout, "Entering main loop");
117
+			for(;;) {
118
+				int rc, do_sleep = 1;
119
+				if ((rc = aisl_run_cycle(aisl)) == AISL_SUCCESS)
120
+					do_sleep = 0;
121
+				if ((rc = agent_run()) == 0)
122
+					do_sleep = 0;
123
+
124
+				if (!do_sleep)
125
+					continue;
126
+
127
+				aisl_sleep(aisl, 500);
133 128
 			}
129
+			agent_unset_all();
134 130
 			observer_release();
135 131
 		}
136 132
 		aisl_free(aisl);

+ 33
- 3
src/surgard.c View File

@@ -13,13 +13,43 @@
13 13
  * @see https://lowenware.com/
14 14
  */
15 15
 
16
+#include <string.h>
16 17
 #include "surgard.h"
17 18
 
18 19
 
19
-int
20
-surgard_run_cycle(void)
20
+struct surgard {
21
+	struct agent parent;
22
+};
23
+
24
+
25
+bool
26
+surgard_supports(const char *protocol)
21 27
 {
22
-	return 1;
28
+	return !strcmp(protocol, "surgard");
29
+}
30
+
31
+
32
+struct agent *
33
+surgard_new(AgentType agent_type, const char *name, char *address, char *params)
34
+{
35
+	struct agent *agent;
36
+
37
+	if ((agent = agent_alloc(name, agent_type, sizeof (struct surgard)))) {
38
+
39
+	}
40
+	return agent;
41
+}
42
+
43
+
44
+void
45
+surgard_free(struct agent *agent)
46
+{
47
+	agent_free(agent);
23 48
 }
24 49
 
25 50
 
51
+int
52
+surgard_run(struct agent *agent, unsigned char *buffer, size_t buf_sz)
53
+{
54
+	return 1;
55
+}

+ 16
- 1
src/surgard.h View File

@@ -16,7 +16,22 @@
16 16
 #ifndef SURGARD_H_0C382373_4757_4FC1_A2DE_991F9D71CDBC
17 17
 #define SURGARD_H_0C382373_4757_4FC1_A2DE_991F9D71CDBC
18 18
 
19
+#include "agent.h"
20
+
21
+
22
+struct agent *
23
+surgard_new(AgentType agent_type, const char *name, char *address, char *params);
24
+
25
+
26
+void
27
+surgard_free(struct agent *agent);
28
+
29
+
19 30
 int
20
-surgard_run_cycle(void);
31
+surgard_run(struct agent *agent, unsigned char *buffer, size_t buf_sz);
32
+
33
+
34
+bool
35
+surgard_supports(const char *protocol);
21 36
 
22 37
 #endif /* !SURGARD_H */

Loading…
Cancel
Save