summaryrefslogtreecommitdiff
path: root/infra/module_manager/module_manager.c
blob: 2574267e019b2fa7d26ef5ac6417ee1d5032b143 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include "module_manager_interna.h"

#include "stellar/utils.h"
#include <dlfcn.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>


/*******************************************
 * module manager  API *
 *******************************************/

#include "toml/toml.h"

struct module_manager *module_manager_new(const char *module_spec_toml_path, int max_thread_num, struct mq_schema *mq_schema, struct logger *logger)
{
	
	struct module_manager *mod_mgr = CALLOC(struct module_manager, 1);
	mod_mgr->schema.max_thread_num=max_thread_num;
	mod_mgr->schema.mq_schema=mq_schema;
	mod_mgr->schema.logger=logger;
	if(module_spec_toml_path==NULL)return mod_mgr;
    FILE *fp = fopen(module_spec_toml_path, "r");
    if (fp == NULL)return mod_mgr;
    mod_mgr->module_spec_toml_path = strdup(module_spec_toml_path);

    int mod_num = 0;
	toml_table_t *conf = toml_parse_file(fp, NULL, 0);
    toml_array_t* mod_array = toml_array_in(conf, "module");

	if(mod_array==NULL)goto MODULE_SPEC_LOAD_END;

    mod_num = toml_array_nelem(mod_array);
    mod_mgr->module_specs = CALLOC(struct module_spec_load, mod_num);

    // TODO: store module specific in hash
    for (int i = 0; i < mod_num; i++) {
        toml_table_t* toml_mod = toml_table_at(mod_array, i);

        const char *path_raw = toml_raw_in(toml_mod, "path");
        const char *init_func_name_raw = toml_raw_in(toml_mod, "init");
        const char *exit_func_name_raw = toml_raw_in(toml_mod, "exit");
		const char *thread_init_func_name_raw = toml_raw_in(toml_mod, "thread_init");
		const char *thread_exit_func_name_raw = toml_raw_in(toml_mod, "thread_exit");

		toml_rtos(path_raw, &mod_mgr->module_specs[i].path);
		toml_rtos(init_func_name_raw, &mod_mgr->module_specs[i].instance_init_cb_name);
		toml_rtos(exit_func_name_raw, &mod_mgr->module_specs[i].instance_exit_cb_name);
		toml_rtos(thread_init_func_name_raw, &mod_mgr->module_specs[i].thread_init_cb_name);
		toml_rtos(thread_exit_func_name_raw, &mod_mgr->module_specs[i].thread_exit_cb_name);

        void* handle = dlopen(mod_mgr->module_specs[i].path, RTLD_NOW|RTLD_LAZY|RTLD_GLOBAL);
        if (!handle) {
            fprintf(stderr, "Error loading module %s: %s\n", mod_mgr->module_specs[i].path, dlerror());
            break;
        }
        if (mod_mgr->module_specs[i].instance_init_cb_name)
        {
            mod_mgr->module_specs[i].on_instance_init_cb =
                (module_on_instance_init_func *)dlsym(handle, mod_mgr->module_specs[i].instance_init_cb_name);
            if (mod_mgr->module_specs[i].on_instance_init_cb)
            {
                mod_mgr->module_specs[i].mod = mod_mgr->module_specs[i].on_instance_init_cb(mod_mgr);
                if (module_manager_get_module(mod_mgr, mod_mgr->module_specs[i].mod->name) == NULL)
                {
                    mod_mgr->module_specs[i].init_succ = true;
                }
                else
                {
                    fprintf(stderr, "Module %s already exists\n", mod_mgr->module_specs[i].mod->name);
                    if (mod_mgr->module_specs[i].on_instance_exit_cb)
                        mod_mgr->module_specs[i].on_instance_exit_cb(mod_mgr, mod_mgr->module_specs[i].mod);
                }
            }
            else
            {
                fprintf(stderr, "Could not load init function %s: %s\n", mod_mgr->module_specs[i].instance_init_cb_name, dlerror());
            }
        }
        if (mod_mgr->module_specs[i].instance_exit_cb_name)
        {
            mod_mgr->module_specs[i].on_instance_exit_cb =
                (module_on_instance_exit_func *)dlsym(handle, mod_mgr->module_specs[i].instance_exit_cb_name);
            if (!mod_mgr->module_specs[i].on_instance_exit_cb)
            {
                fprintf(stderr, "Could not load exit function %s: %s\n", mod_mgr->module_specs[i].instance_exit_cb_name, dlerror());
            }
        }

        if (mod_mgr->module_specs[i].thread_init_cb_name)
        {
            mod_mgr->module_specs[i].on_thread_init_cb =
                (module_on_thread_init_func *)dlsym(handle, mod_mgr->module_specs[i].thread_init_cb_name);
            if (!mod_mgr->module_specs[i].on_thread_init_cb)
            {
                fprintf(stderr, "Could not load init function %s: %s\n", mod_mgr->module_specs[i].thread_init_cb_name, dlerror());
            }
        }


        if (mod_mgr->module_specs[i].thread_exit_cb_name)
        {
            mod_mgr->module_specs[i].on_thread_exit_cb =
                (module_on_thread_exit_func *)dlsym(handle, mod_mgr->module_specs[i].thread_exit_cb_name);
            if (!mod_mgr->module_specs[i].on_thread_exit_cb)
            {
                fprintf(stderr, "Could not load exit function %s: %s\n", mod_mgr->module_specs[i].thread_exit_cb_name, dlerror());
            }
        }
		mod_mgr->load_module_num+=1;
    }

MODULE_SPEC_LOAD_END:

	assert(mod_mgr->load_module_num==mod_num);

	if(conf	)toml_free(conf);
	if(fp)fclose(fp);
	return mod_mgr;
}


void module_manager_free(struct module_manager *mod_mgr)
{
	if(mod_mgr==NULL)return;
	if(mod_mgr->module_spec_toml_path)FREE(mod_mgr->module_spec_toml_path);
    if (mod_mgr->module_specs)
    {
        for (int i = 0; i < mod_mgr->load_module_num; i++)
        {
            if (mod_mgr->module_specs[i].on_instance_exit_cb != NULL &&
                mod_mgr->module_specs[i].init_succ)
            {
                mod_mgr->module_specs[i].on_instance_exit_cb(mod_mgr, mod_mgr->module_specs[i].mod);
            }
			if(mod_mgr->module_specs[i].path)FREE(mod_mgr->module_specs[i].path);
			if(mod_mgr->module_specs[i].instance_init_cb_name)FREE(mod_mgr->module_specs[i].instance_init_cb_name);
			if(mod_mgr->module_specs[i].instance_exit_cb_name)FREE(mod_mgr->module_specs[i].instance_exit_cb_name);
			if(mod_mgr->module_specs[i].thread_init_cb_name)FREE(mod_mgr->module_specs[i].thread_init_cb_name);
			if(mod_mgr->module_specs[i].thread_exit_cb_name)FREE(mod_mgr->module_specs[i].thread_exit_cb_name);
        }
		FREE(mod_mgr->module_specs);
    }
    FREE(mod_mgr);
	return;
}

int module_manager_get_max_thread_num(struct module_manager*mod_mgr)
{
	if(mod_mgr==NULL)return -1;
	return mod_mgr->schema.max_thread_num;
}

struct mq_schema *module_manager_get_mq_schema(struct module_manager *mod_mgr)
{
	if(mod_mgr==NULL)return NULL;
	return mod_mgr->schema.mq_schema;
}

struct logger *module_manager_get_logger(struct module_manager *mod_mgr)
{
	if(mod_mgr==NULL)return NULL;
	return mod_mgr->schema.logger;
}

const char *module_manager_get_toml_path(struct module_manager *mod_mgr)
{
	if(mod_mgr==NULL)return NULL;
	return mod_mgr->module_spec_toml_path;
}

__thread int local_thread_id=-1;
__thread struct mq_runtime *local_mq_rt=NULL;

int module_manager_get_thread_id(struct module_manager* mod_mgr __unused)
{
	return local_thread_id;
}

struct mq_runtime *module_manager_get_mq_runtime(struct module_manager *mod_mgr __unused)
{
	return local_mq_rt;
}

void module_manager_register_thread(struct module_manager* mod_mgr, int thread_id, struct mq_runtime *mq_rt)
{
	local_thread_id=thread_id;
	local_mq_rt=mq_rt;

	for(int i=0; i<mod_mgr->load_module_num; i++)
	{
		if(mod_mgr->module_specs[i].mod == NULL)break;
		if(mod_mgr->module_specs[i].on_thread_init_cb && mod_mgr->module_specs[i].init_succ)
		{
			mod_mgr->module_specs[i].on_thread_init_cb(mod_mgr, thread_id, mod_mgr->module_specs[i].mod);
		}
	}
	return;
}

void module_manager_unregister_thread(struct module_manager *mod_mgr, int thread_id)
{
	assert(local_thread_id==thread_id);
	for(int i=0; i<mod_mgr->load_module_num; i++)
	{
		if(mod_mgr->module_specs[i].mod == NULL)break;
		if(mod_mgr->module_specs[i].on_thread_exit_cb && mod_mgr->module_specs[i].init_succ)
		{
			mod_mgr->module_specs[i].on_thread_exit_cb(mod_mgr, thread_id, mod_mgr->module_specs[i].mod);
		}
	}
	local_thread_id=-1;
	local_mq_rt=NULL;
	return;
}

struct module *module_manager_get_module(struct module_manager *mod_mgr, const char *module_name)
{
	if(mod_mgr==NULL || module_name == NULL)return NULL;
	if (mod_mgr->module_specs)
	{
		for(int i=0; i<mod_mgr->load_module_num; i++)
		{
			if(mod_mgr->module_specs[i].mod == NULL)break;
			if(strcmp(mod_mgr->module_specs[i].mod->name, module_name)==0 && mod_mgr->module_specs[i].init_succ)
			{
				return mod_mgr->module_specs[i].mod;
			}
		}
	}
	return NULL;
} 

/*******************************************
 * stellar module API *
 *******************************************/


struct module *module_new(const char *name, void *ctx)
{
	struct module *mod = CALLOC(struct module, 1);
	memcpy(mod->name, name, MIN(NAME_MAX, strlen(name)));
	mod->module_ctx=ctx;
	return mod;
}

void module_free(struct module *mod)
{
	if(mod==NULL)return;
	FREE(mod);
	return;
}

void * module_get_ctx(struct module *mod)
{
	if(mod==NULL)return NULL;
	return mod->module_ctx;
}

void module_set_ctx(struct module *mod, void *ctx)
{
	if(mod==NULL)return;
	mod->module_ctx=ctx;
	return;
}

const char *module_get_name(struct module* mod)
{
	if(mod==NULL)return NULL;
	return mod->name;
}

void module_set_name(struct module* mod, const char *name)
{
	if(mod==NULL)return;
	memcpy(mod->name, name, MIN(NAME_MAX, strlen(name)));
	return;
}

/*******************************************
 * polling API *
 *******************************************/

 #define TOPIC_POLLING "polling"

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
static void on_polling_dispatch(int topic_id __unused, 
									 void *msg __unused,  
									on_msg_cb_func* on_msg_cb, 
									 void *on_msg_cb_arg, 
									 void *dispatch_arg)
{
	struct module_manager *mod_mgr=(struct module_manager *)dispatch_arg;
	module_on_polling_func *polling = (module_on_polling_func *)on_msg_cb;
	polling(mod_mgr, on_msg_cb_arg);
}

int module_manager_polling_subscribe(struct module_manager	*mod_mgr,  module_on_polling_func on_polling, void *polling_arg)
{
	if(mod_mgr == NULL)return -1;
	mod_mgr->topic_polling_id=mq_schema_get_topic_id(module_manager_get_mq_schema(mod_mgr), TOPIC_POLLING);
	if(mod_mgr->topic_polling_id<0)
	{
		mod_mgr->topic_polling_id=mq_schema_create_topic(mod_mgr->schema.mq_schema, TOPIC_POLLING, on_polling_dispatch, mod_mgr, NULL, NULL);
	}
	return mq_schema_subscribe(mod_mgr->schema.mq_schema, mod_mgr->topic_polling_id, (on_msg_cb_func *)on_polling, polling_arg);
}

#pragma GCC diagnostic pop 

void module_manager_polling_active(struct module_manager *mod_mgr)
{
	if(mod_mgr == NULL)return;
	mq_runtime_publish_message(local_mq_rt, mod_mgr->topic_polling_id, NULL);
}


void stellar_polling_dispatch(struct module_manager *mod_mgr)
{
	if(mod_mgr==NULL)return;
	module_manager_polling_active(mod_mgr);
	mq_runtime_dispatch(local_mq_rt);
	return;
}