/Users/maurits/Documents/studie/afstuderen/biosphere/daemon/core_services.c

Go to the documentation of this file.
00001 /*
00002  * Author: MA Hartman
00003  * Date: mar 20, 2007
00004  * 
00005  * Function:
00006  * The core services are a group of basic platform services.
00007  * 
00008  * License information:
00009  * 
00010  * Copyright (c) 2006 Maurits Hartman
00011  *
00012  * Permission is hereby granted, free of charge, to any person
00013  * obtaining a copy of this software and associated documentation
00014  * files (the "Software"), to deal in the Software without
00015  * restriction, including without limitation the rights to use,
00016  * copy, modify, merge, publish, distribute, sublicense, and/or sell
00017  * copies of the Software, and to permit persons to whom the
00018  * Software is furnished to do so, subject to the following
00019  * conditions:
00020  * 
00021  * The above copyright notice and this permission notice shall be
00022  * included in all copies or substantial portions of the Software.
00023  * 
00024  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00026  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00028  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00029  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00030  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00031  * OTHER DEALINGS IN THE SOFTWARE.
00032  * 
00033  */
00034  
00035 #include "core_services.h"
00036 #include "module.h"
00037 #include "settings.h"
00038 #include "service.h"
00039 #include "thread.h"
00040 #include <io.h>
00041 #include <list.h>
00042 #include <type.h>
00043 #include <str.h>
00044 
00045 #include <apr_file_io.h>
00046 #include <apr_strings.h>
00047 #include <stdio.h>
00048 #include <stdlib.h>
00049 #include <string.h>
00050 #include <assert.h>
00051 
00052 
00053 static bs_service_response *core_service_create_status_response(
00054                 const bs_service_request *request, bs_status status,
00055                 apr_pool_t *mp)
00056 {
00057         bs_service_response *resp;
00058         bs_message_instance *output = NULL;
00059         bs_part_instance *part = NULL;
00060         bs_data_type *type = NULL;
00061         char *data;
00062         
00063         data = apr_psprintf(mp, "%u", status);
00064         type = new_bs_data_type(mp, "bs_uint32");
00065         if (!type) return NULL;
00066         part = new_bs_part_instance(mp, "status", type,
00067                         data, strlen(data));
00068         if (!part) return NULL;
00069         output = new_bs_message_instance(mp, "BSStatusMessage", 1);
00070         if (!output) return NULL;
00071         output->parts[0] = part;
00072         resp = new_bs_service_response(mp, request->uuid,
00073                         request->service, request->port, request->operation,
00074                         output, NULL);
00075         
00076         return resp;
00077 }
00078 
00079 
00080 static bs_status core_service_admin_shutdown(
00081                 const bs_service_request *request,
00082                 bs_service_response **response, apr_pool_t *mp)
00083 {
00084         bs_status rv;
00085         bs_service_response *resp;
00086         bs_uint32 timeout;
00087         
00088         /* Retrieve timeout and start shutdown process: */
00089         timeout = atoi(request->input->parts[0]->data);
00090         rv = stop_daemon(timeout * 1000000);
00091         
00092         /* Create return message: */
00093         resp = core_service_create_status_response(request, rv, mp);
00094         if (!resp) return BS_ERROR;
00095         *response = resp;
00096         return BS_OK;
00097 }
00098 
00099 
00100 static bs_status core_service_module_service_list(
00101                 const bs_service_request *request,
00102                 bs_service_response **response, apr_pool_t *mp)
00103 {
00104         bs_service_response *resp;
00105         bs_message_instance *output = NULL;
00106         bs_data_type *type;
00107         bs_part_instance *part = NULL, *part2 = NULL, *part3 = NULL, *part4 = NULL;
00108         bs_status rv;
00109         bs_list *names, *descr, *fnames, *versions;
00110         names = new_list(mp);
00111         descr = new_list(mp);
00112         fnames = new_list(mp);
00113         versions = new_list(mp);
00114         
00115         rv = get_module_descriptions(names, fnames, versions, descr);
00116         if (rv == BS_OK) {
00117                 unsigned i;
00118                 char *name_string = "", *descr_string = "";
00119                 char *fname_string = "", *version_string = "";
00120                 
00121                 /* Gather names and descriptions in two big strings: */
00122                 assert(list_size(names) == list_size(descr));
00123                 assert(list_size(names) == list_size(fnames));
00124                 assert(list_size(fnames) == list_size(versions));
00125                 for (i = 0; i < list_size(names); i++) {
00126                         name_string     = apr_pstrcat(mp, name_string, "#",
00127                                         list_index(names, i), NULL);
00128                         descr_string = apr_pstrcat(mp, descr_string, "#",
00129                                         list_index(descr, i), NULL);
00130                         fname_string = apr_pstrcat(mp, fname_string, "#",
00131                                         list_index(fnames, i), NULL);
00132                         version_string = apr_pstrcat(mp, version_string, "#",
00133                                         list_index(versions, i), NULL);
00134                 }
00135                 
00136                 /* Create response: */
00137                 type = new_bs_data_type(mp, "bs_string");
00138                 part = new_bs_part_instance(mp, "modules_names",
00139                                 type, name_string, strlen(name_string));
00140                 part2 = new_bs_part_instance(mp, "modules_full_names",
00141                                 type, fname_string, strlen(fname_string));
00142                 part3 = new_bs_part_instance(mp, "modules_versions",
00143                                 type, version_string, strlen(version_string));
00144                 part4 = new_bs_part_instance(mp, "modules_descriptions",
00145                                 type, descr_string, strlen(descr_string));
00146                 output = new_bs_message_instance(mp, "ModuleListMessage", 4);
00147                 output->parts[0] = part;
00148                 output->parts[1] = part2;
00149                 output->parts[2] = part3;
00150                 output->parts[3] = part4;
00151                 
00152                 resp = new_bs_service_response(mp, request->uuid,
00153                                 request->service, request->port,
00154                                 request->operation, output, NULL);
00155         }
00156         else {
00157                 /* TODO: create fault status message, for now, fail. */
00158                 return BS_NO_SERVICE;
00159         }
00160         
00161         *response = resp;
00162         return BS_OK;
00163 }
00164 
00165 
00166 static bs_status core_service_module_service_load(
00167                 const bs_service_request *request,
00168                 bs_service_response **response, apr_pool_t *mp)
00169 {
00170         char *modname;
00171         bs_status rv;
00172         
00173         /* Get the module name: */
00174         if (!request->input || request->input->num_parts != 1 ||
00175                         !request->input->parts[0]) return BS_ERROR;
00176         modname = request->input->parts[0]->data;
00177         
00178         /* Try to load the module: */
00179         rv = load_module(modname);
00180         
00181         /* Create the return message: */
00182         *response = core_service_create_status_response(request, rv, mp);
00183         return BS_OK;   
00184 }
00185 
00186 
00187 static bs_status core_service_module_service_unload(
00188                 const bs_service_request *request,
00189                 bs_service_response **response, apr_pool_t *mp)
00190 {
00191         char *modname;
00192         bs_status rv;
00193         
00194         /* Get the module name: */
00195         if (!request->input || request->input->num_parts != 1 ||
00196                         !request->input->parts[0]) return BS_ERROR;
00197         modname = request->input->parts[0]->data;
00198         
00199         /* Try to load the module: */
00200         rv = unload_module(modname);
00201         
00202         /* Create the return message: */
00203         *response = core_service_create_status_response(request, rv, mp);
00204         return BS_OK;           
00205 }
00206 
00207 
00208 static bs_status core_service_module_service(
00209                 const bs_service_request *request,
00210                 bs_service_response **response, apr_pool_t *mp)
00211 {
00212         if (streq(request->operation, "ListModules"))
00213                 return core_service_module_service_list(request, response, mp);
00214         else if (streq(request->operation, "LoadModule"))
00215                 return core_service_module_service_load(request, response, mp);
00216         else if (streq(request->operation, "UnloadModule"))
00217                 return core_service_module_service_unload(request, response, mp);
00218         else return BS_NO_SERVICE;
00219 }
00220 
00221 
00226 static bs_status core_service_daemon_admin(
00227                 const bs_service_request *request,
00228                 bs_service_response **response, apr_pool_t *mp)
00229 {
00230         if (streq(request->operation, "Shutdown"))
00231                 return core_service_admin_shutdown(request, response, mp);
00232         return BS_NOT_IMPLEMENTED;
00233 }
00234 
00235 
00239 static bs_status core_service_logging_service(
00240                 const bs_service_request *request,
00241                 bs_service_response **response, apr_pool_t *mp)
00242 {
00243         bs_service_response *resp = NULL;
00244         
00245         if (!streq(request->operation, "Log") || request->input->num_parts != 1)
00246                 return BS_ERROR;
00247         
00248         /* Print the message which is supposed to be safe and checked: */
00249         printf("%s\n", request->input->parts[0]->data);
00250         
00251         /* Create response: */
00252         resp = core_service_create_status_response(request, BS_OK, mp);
00253         
00254         if (!resp) return BS_ERROR;
00255         *response = resp;
00256         return BS_OK;
00257 }
00258 
00259 
00263 static bs_status core_service_composition_service(
00264                 const bs_service_request *request,
00265                 bs_service_response **response, apr_pool_t *mp)
00266 {
00267 
00268         return BS_NO_SERVICE;
00269 }
00270 
00271 
00272 bs_status core_service_request_handler (const bs_service_request *request,
00273                 bs_service_response **response, void *extra)
00274 {
00275         apr_pool_t *mp = (apr_pool_t *) extra;
00276         
00277         /* Check to see if really the core services are requested: */
00278         if (!streq(request->service, "BiosphereCoreServices"))
00279                 return BS_ERROR;
00280         
00281         /* Check which port is requested: */
00282         if (streq(request->port, "DaemonAdministration"))
00283                 return core_service_daemon_admin(request, response, mp);
00284         else if (streq(request->port, "LoggingService"))
00285                 return core_service_logging_service(request, response, mp);
00286         else if (streq(request->port, "ModuleService"))
00287                 return core_service_module_service(request, response, mp);
00288         else if (streq(request->port, "CompositionService"))
00289                 return core_service_composition_service(request, response, mp);
00290         else return BS_ERROR;
00291 }

Generated on Tue Jul 17 09:50:52 2007 for Bio-SPHERE by  doxygen 1.5.1