src/server/config/serverconf.c

changeset 91
fac51f87def0
parent 88
73b3485e96f1
child 115
51d9a15eac98
equal deleted inserted replaced
90:279f343bbf6c 91:fac51f87def0
39 } 39 }
40 40
41 ServerConfig *conf = malloc(sizeof(ServerConfig)); 41 ServerConfig *conf = malloc(sizeof(ServerConfig));
42 conf->parser.parse = serverconf_parse; 42 conf->parser.parse = serverconf_parse;
43 conf->file = file; 43 conf->file = file;
44 conf->objects = ucx_map_new(16);
45 conf->obj = NULL; 44 conf->obj = NULL;
46 45
46 conf->objects = ucx_map_new(16);
47 int r = cfg_parse_basic_file((ConfigParser*)conf, in); 47 int r = cfg_parse_basic_file((ConfigParser*)conf, in);
48 cfg_map_destr(conf->parser.mp, conf->objects); 48 cfg_map_destr(conf->parser.mp->pool, conf->objects);
49 if(r != 0) { 49 if(r != 0) {
50 // TODO: free 50 // TODO: free
51 return NULL; 51 return NULL;
52 } 52 }
53 53
55 55
56 return conf; 56 return conf;
57 } 57 }
58 58
59 void free_server_config(ServerConfig *conf) { 59 void free_server_config(ServerConfig *conf) {
60 ucx_mempool_destroy(conf->parser.mp); 60 ucx_mempool_destroy(conf->parser.mp->pool);
61 free(conf); 61 free(conf);
62 } 62 }
63 63
64 int serverconf_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line){ 64 int serverconf_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line){
65 ServerConfig *conf = p; 65 ServerConfig *conf = p;
66 UcxMempool *mp = conf->parser.mp; 66 UcxAllocator *mp = conf->parser.mp;
67 67
68 begin->type = cfg_get_line_type(line); 68 begin->type = cfg_get_line_type(line);
69 switch(begin->type) { 69 switch(begin->type) {
70 case LINE_BEGIN_TAG: { 70 case LINE_BEGIN_TAG: {
71 ConfigTag *tag = cfg_parse_begin_tag(line, conf->parser.mp); 71 ConfigTag *tag = cfg_parse_begin_tag(line, conf->parser.mp);
79 obj->end = end; 79 obj->end = end;
80 obj->directives = NULL; 80 obj->directives = NULL;
81 81
82 // add object to server config 82 // add object to server config
83 UcxList *list = ucx_map_sstr_get(conf->objects, obj->type); 83 UcxList *list = ucx_map_sstr_get(conf->objects, obj->type);
84 list = cfg_list_append(mp, list, obj); 84 list = ucx_list_append_a(mp, list, obj);
85 ucx_map_sstr_put(conf->objects, obj->type, list); 85 ucx_map_sstr_put(conf->objects, obj->type, list);
86 conf->obj = obj; 86 conf->obj = obj;
87 87
88 break; 88 break;
89 } 89 }
107 ConfigDirective *d = cfg_parse_directive(line, conf->parser.mp); 107 ConfigDirective *d = cfg_parse_directive(line, conf->parser.mp);
108 d->begin = begin; 108 d->begin = begin;
109 d->end = end; 109 d->end = end;
110 110
111 //printf("%s.%s\n", conf->obj->type.ptr, d->directive_type.ptr); 111 //printf("%s.%s\n", conf->obj->type.ptr, d->directive_type.ptr);
112 conf->obj->directives = cfg_dlist_append(mp, conf->obj->directives, d); 112 conf->obj->directives = ucx_list_append_a(mp, conf->obj->directives, d);
113 } 113 }
114 } 114 }
115 return 0; 115 return 0;
116 } 116 }
117 117
118 118
119 UcxList* srvcfg_get_listeners(ServerConfig *cfg, UcxMempool *mp, int *error) { 119 UcxList* srvcfg_get_listeners(ServerConfig *cfg, UcxAllocator *mp, int *error) {
120 mp = mp ? mp : cfg->parser.mp; 120 mp = mp ? mp : cfg->parser.mp;
121 121
122 UcxList *list = ucx_map_sstr_get(cfg->objects, sstrn("Listener", 8)); 122 UcxList *list = ucx_map_sstr_get(cfg->objects, sstrn("Listener", 8));
123 UcxList *lslist = NULL; 123 UcxList *lslist = NULL;
124 UCX_FOREACH(UcxList*, list, elm) { 124 UCX_FOREACH(elm, list) {
125 ServerConfigObject *ls = elm->data; 125 ServerConfigObject *ls = elm->data;
126 sstr_t name = cfg_directivelist_get_str(ls->directives, sstr("Name")); 126 sstr_t name = cfg_directivelist_get_str(ls->directives, sstr("Name"));
127 sstr_t port = cfg_directivelist_get_str(ls->directives, sstr("Port")); 127 sstr_t port = cfg_directivelist_get_str(ls->directives, sstr("Port"));
128 sstr_t vs = cfg_directivelist_get_str( 128 sstr_t vs = cfg_directivelist_get_str(
129 ls->directives, 129 ls->directives,
130 sstr("DefaultVS")); 130 sstr("DefaultVS"));
131 sstr_t threadpool = cfg_directivelist_get_str( 131 sstr_t threadpool = cfg_directivelist_get_str(
132 ls->directives, 132 ls->directives,
133 sstr("Threadpool")); 133 sstr("Threadpool"));
134 134
135 CfgListener *listener = ucx_mempool_calloc(mp, 1, sizeof(CfgListener)); 135 CfgListener *listener = OBJ_NEW_N(mp, CfgListener);
136 // threadpool is optional, all other configs must be set 136 // threadpool is optional, all other configs must be set
137 if(!name.ptr || !port.ptr || !vs.ptr) { 137 if(!name.ptr || !port.ptr || !vs.ptr) {
138 // TODO: log error 138 // TODO: log error
139 *error = 1; 139 *error = 1;
140 listener->cfg_correct = 0; 140 listener->cfg_correct = 0;
141 } else { 141 } else {
142 listener->cfg_correct = 1; 142 listener->cfg_correct = 1;
143 } 143 }
144 144
145 if(name.ptr) { 145 if(name.ptr) {
146 listener->name = sstrdup_mp(mp, name); 146 listener->name = sstrdup_a(mp, name);
147 } 147 }
148 if(port.ptr) { 148 if(port.ptr) {
149 // don't expect that port is null terminated, sstrdup it to be sure 149 // don't expect that port is null terminated, sstrdup it to be sure
150 sstr_t portdp = sstrdup(port); 150 sstr_t portdp = sstrdup(port);
151 listener->port = atoi(portdp.ptr); 151 listener->port = atoi(portdp.ptr);
152 free(portdp.ptr); 152 free(portdp.ptr);
153 } 153 }
154 if(vs.ptr) { 154 if(vs.ptr) {
155 listener->vs = sstrdup_mp(mp, vs); 155 listener->vs = sstrdup_a(mp, vs);
156 } 156 }
157 if(threadpool.ptr) { 157 if(threadpool.ptr) {
158 listener->threadpool = sstrdup_mp(mp, threadpool); 158 listener->threadpool = sstrdup_a(mp, threadpool);
159 } 159 }
160 160
161 lslist = cfg_list_append(mp, lslist, listener); 161 lslist = ucx_list_append_a(mp, lslist, listener);
162 } 162 }
163 163
164 return lslist; 164 return lslist;
165 } 165 }

mercurial