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
|
#include <string.h>
#include "loopback.h"
#define BACKEND_NAME "loopback"
int init(){
backend loopback = {
.name = BACKEND_NAME,
.conf = backend_configure,
.create = backend_instance,
.conf_instance = backend_configure_instance,
.channel = backend_channel,
.handle = backend_set,
.process = backend_handle,
.start = backend_start,
.shutdown = backend_shutdown
};
//register backend
if(mm_backend_register(loopback)){
fprintf(stderr, "Failed to register loopback backend\n");
return 1;
}
return 0;
}
static int backend_configure(char* option, char* value){
//intentionally ignored
return 0;
}
static int backend_configure_instance(instance* inst, char* option, char* value){
//intentionally ignored
return 0;
}
static instance* backend_instance(){
instance* i = mm_instance();
if(!i){
return NULL;
}
i->impl = calloc(1, sizeof(loopback_instance));
if(!i->impl){
fprintf(stderr, "Failed to allocate memory\n");
return NULL;
}
return i;
}
static channel* backend_channel(instance* inst, char* spec){
size_t u;
loopback_instance* data = (loopback_instance*) inst->impl;
//find matching channel
for(u = 0; u < data->n; u++){
if(!strcmp(spec, data->name[u])){
break;
}
}
//allocate new channel
if(u == data->n){
data->name = realloc(data->name, (u + 1) * sizeof(char*));
if(!data->name){
fprintf(stderr, "Failed to allocate memory\n");
return NULL;
}
data->name[u] = strdup(spec);
if(!data->name[u]){
fprintf(stderr, "Failed to allocate memory\n");
return NULL;
}
data->n++;
}
return mm_channel(inst, u, 1);
}
static int backend_set(instance* inst, size_t num, channel** c, channel_value* v){
size_t n;
for(n = 0; n < num; n++){
mm_channel_event(c[n], v[n]);
}
return 0;
}
static int backend_handle(size_t num, managed_fd* fds){
//no events generated here
return 0;
}
static int backend_start(){
return 0;
}
static int backend_shutdown(){
size_t n, u, p;
instance** inst = NULL;
loopback_instance* data = NULL;
if(mm_backend_instances(BACKEND_NAME, &n, &inst)){
fprintf(stderr, "Failed to fetch instance list\n");
return 1;
}
for(u = 0; u < n; u++){
data = (loopback_instance*) inst[u]->impl;
for(p = 0; p < data->n; p++){
free(data->name[p]);
}
free(data->name);
free(inst[u]->impl);
}
free(inst);
return 0;
}
|