1 /*  =========================================================================
2     zhttp_server_options - class description
3 
4     Copyright (c) the Contributors as noted in the AUTHORS file.
5 
6     This Source Code Form is subject to the terms of the Mozilla Public
7     License, v. 2.0. If a copy of the MPL was not distributed with this
8     file, You can obtain one at http://mozilla.org/MPL/2.0/.
9     =========================================================================
10 */
11 
12 /*
13 @header
14     zhttp_server_options -
15 @discuss
16 @end
17 */
18 
19 #include "czmq_classes.h"
20 
21 //  Structure of our class
22 
23 struct _zhttp_server_options_t {
24     int port;
25     char *backend_address;
26 };
27 
28 
29 //  --------------------------------------------------------------------------
30 //  Create a new zhttp_server_options
31 
32 zhttp_server_options_t *
zhttp_server_options_new(void)33 zhttp_server_options_new (void)
34 {
35     zhttp_server_options_t *self = (zhttp_server_options_t *) zmalloc (sizeof (zhttp_server_options_t));
36 
37     self->port = 8080;
38     self->backend_address =
39             zsys_sprintf ("inproc://zhttp_server-%04x-%04x", randof (0x10000), randof (0x10000));
40 
41     assert (self);
42     //  Initialize class properties here
43     return self;
44 }
45 
46 
47 //  --------------------------------------------------------------------------
48 //  Destroy the zhttp_server_options
49 
50 void
zhttp_server_options_destroy(zhttp_server_options_t ** self_p)51 zhttp_server_options_destroy (zhttp_server_options_t **self_p)
52 {
53     assert (self_p);
54     if (*self_p) {
55         zhttp_server_options_t *self = *self_p;
56         zstr_free (&self->backend_address);
57         //  Free object itself
58         free (self);
59         *self_p = NULL;
60     }
61 }
62 
63 
64 zhttp_server_options_t *
zhttp_server_options_from_config(zconfig_t * config)65 zhttp_server_options_from_config (zconfig_t *config) {
66     assert (config);
67     zhttp_server_options_t *self = zhttp_server_options_new ();
68 
69     const char* backend_address = zconfig_get (config, "http_server/backend_address", NULL);
70     const char* port_str = zconfig_get (config, "http_server/port", NULL);
71 
72     if (backend_address)
73         self->backend_address = strdup (backend_address);
74 
75     if (port_str)
76         self->port = atoi (port_str);
77 
78     return self;
79 }
80 
81 int
zhttp_server_options_port(zhttp_server_options_t * self)82 zhttp_server_options_port (zhttp_server_options_t *self) {
83     assert (self);
84 
85     return self->port;
86 }
87 
88 void
zhttp_server_options_set_port(zhttp_server_options_t * self,int port)89 zhttp_server_options_set_port (zhttp_server_options_t *self, int port) {
90     self->port = port;
91 }
92 
93 const char *
zhttp_server_options_backend_address(zhttp_server_options_t * self)94 zhttp_server_options_backend_address (zhttp_server_options_t *self) {
95     assert (self);
96 
97     return self->backend_address;
98 }
99 
100 void
zhttp_server_options_set_backend_address(zhttp_server_options_t * self,const char * address)101 zhttp_server_options_set_backend_address (zhttp_server_options_t *self, const char *address) {
102     assert (self);
103     self->backend_address = strdup (address);
104 }
105 
106 void
zhttp_server_options_test(bool verbose)107 zhttp_server_options_test (bool verbose) {
108 
109 }