Add typedefs for all structs and enums.
[libvscmgmt.git] / daemon / daemon.c
1 /*
2  * daemon.c: Daemon for Virtualized Super Computer Management
3  *
4  * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
19  */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23
24 #include <xmlrpc-c/base.h>
25 #include <xmlrpc-c/server.h>
26 #include <xmlrpc-c/server_abyss.h>
27
28 #include "../utils/utils.h"
29
30 #define _ERROR_TO_XMLRPC(error, env) \
31         xmlrpc_env_set_fault_formatted (env, XMLRPC_INTERNAL_ERROR, \
32                                         "libvscmgmt [%s] %s", \
33                                          vsc_mgmt_error_string ((error)->code), \
34                                          (error)->message != NULL ? (error)->message \
35                                                                   : "");
36
37
38 static xmlrpc_value *
39 xmlrpc_vsc_mgmt_get_version (xmlrpc_env *env,
40                              xmlrpc_value *param_list ATTR_UNUSED,
41                              void *server_info ATTR_UNUSED,
42                              void *channel_info ATTR_UNUSED)
43 {
44         int major, minor, patch;
45
46         vsc_mgmt_get_version (&major, &minor, &patch);
47
48         return xmlrpc_build_value (env, "{s:i,s:i,s:i}",
49                                    "major", (xmlrpc_int32) major,
50                                    "minor", (xmlrpc_int32) minor,
51                                    "patch", (xmlrpc_int32) patch);
52 }
53
54 static xmlrpc_value *
55 xmlrpc_vsc_mgmt_host_add (xmlrpc_env *env,
56                           xmlrpc_value *param_list,
57                           void *server_info ATTR_UNUSED,
58                           void *channel_info ATTR_UNUSED)
59 {
60         VscMgmtError error;
61         char* host_ipv4_string = NULL;
62         char* host_type_string = NULL;
63         VscMgmtHostInfo host_info;
64         xmlrpc_value *result = NULL;
65
66         host_info.host_username = NULL;
67         host_info.host_password = NULL;
68
69         xmlrpc_decompose_value (env, param_list, "({s:{s:s,s:s,s:s,s:s,*},*})",
70                                 "host_info",
71                                 "host_ipv4", &host_ipv4_string,
72                                 "host_type", &host_type_string,
73                                 "host_username", &host_info.host_username,
74                                 "host_password", &host_info.host_password);
75
76         if (env->fault_occurred) {
77                 goto cleanup;
78         }
79
80         printf ("host_ipv4_string %s\n", host_ipv4_string);
81         printf ("host_type %s\n", host_type_string);
82         printf ("host_username %s\n", host_info.host_username);
83         printf ("host_password %s\n", host_info.host_password);
84
85         vsc_mgmt_error_init (&error);
86
87         vsc_mgmt_ipv4_parse (&error, host_ipv4_string, &host_info.host_ipv4);
88
89         if (error.occured) {
90                 _ERROR_TO_XMLRPC (&error, env);
91                 goto cleanup;
92         }
93
94         host_info.host_type = vsc_mgmt_host_type_parse (&error, host_type_string);
95
96         if (error.occured) {
97                 _ERROR_TO_XMLRPC (&error, env);
98                 goto cleanup;
99         }
100
101         //vsc_mgmt_host_add
102
103         result = xmlrpc_build_value (env, "()");
104
105 cleanup:
106         free (host_ipv4_string);
107         free (host_type_string);
108         free (host_info.host_username);
109         free (host_info.host_password);
110
111         vsc_mgmt_error_cleanup (&error);
112
113         return result;
114 }
115
116 static struct xmlrpc_method_info3 method_info_list[] = {
117         {
118                 .methodName     = "vsc.mgmt.get_version",
119                 .methodFunction = &xmlrpc_vsc_mgmt_get_version,
120         }, {
121                 .methodName     = "vsc.mgmt.host.add",
122                 .methodFunction = &xmlrpc_vsc_mgmt_host_add,
123         }
124 };
125
126 int
127 main (int argc ATTR_UNUSED, const char **argv ATTR_UNUSED)
128 {
129         VscMgmtError error;
130         xmlrpc_env env;
131         xmlrpc_registry *registry;
132         xmlrpc_server_abyss_parms server_parms;
133
134         vsc_mgmt_error_init (&error);
135
136         vsc_mgmt_global_init (&error, "/tmp/libvscmgmt/backup");
137
138         if (error.occured) {
139                 vsc_mgmt_error_fprint (&error, stderr);
140                 vsc_mgmt_error_cleanup (&error);
141
142                 return -1;
143         }
144
145         xmlrpc_env_init (&env);
146
147         registry = xmlrpc_registry_new (&env);
148
149         xmlrpc_registry_add_method3 (&env, registry, &method_info_list[0]);
150         xmlrpc_registry_add_method3 (&env, registry, &method_info_list[1]);
151
152         server_parms.config_file_name = NULL;
153         server_parms.registryP = registry;
154         server_parms.port_number = 8080;
155         server_parms.log_file_name = "/tmp/libvscmgmt/xmlrpc-server-log";
156
157         printf("Running XML-RPC server...\n");
158
159         xmlrpc_server_abyss (&env, &server_parms, XMLRPC_APSIZE (log_file_name));
160
161         /*
162          * FIXME: The XML-RPC server can only be closed by an remove call to the
163          *        system.shutdown method. Need to add a callback for that to call
164          *        the vsc_mgmt_global_cleanup method.
165          */
166
167         return 0;
168 }