/Users/maurits/Documents/studie/afstuderen/biosphere/modules/stat/time_functions.c

Go to the documentation of this file.
00001 /*
00002  * Author: MA Hartman
00003  * Date: may 4, 2007
00004  * 
00005  * Function:
00006  * Timing related functions for the stat module.
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 "time_functions.h"
00036 #include "general.h"
00037 #include <list.h>
00038 #include <type.h>
00039 #include <str.h>
00040 #include <biosphere.h>
00041 
00042 #include <apr_time.h>
00043 #include <apr_strings.h>
00044 
00051 extern bs_list *timers;
00052 extern apr_pool_t *pool;
00053 
00054 
00055 static bs_status bsmod_stat_handle_start_timer(
00056                 const bs_service_request *request,
00057                 bs_service_response **response)
00058 {
00059         unsigned i;
00060         char *timer_id;
00061         
00062         /* Retrieve timer_id argument: */
00063         if (request->input->num_parts != 1 ||
00064                         request->input->parts[0] == NULL) return BS_ERROR;
00065         timer_id = request->input->parts[0]->data;
00066         
00067         /* Set (create) the timer: */
00068         lock_bsmod_stat_mux();
00069         for (i = 0; i < list_size(timers); i++) {
00070                 timer_info *ti = (timer_info *) list_index(timers, i);
00071                 if (streq(ti->timer_id, timer_id)) {
00072                         /* We're already keeping time for this timer_id */
00073                         ti->start = apr_time_now();
00074                         break;
00075                 }       
00076         }
00077         if (i >= list_size(timers)) {
00078                 /* Timer_id unknown: create: */
00079                 timer_info *ti;
00080                 ti = (timer_info *) apr_pcalloc(pool, sizeof(timer_info));
00081                 ti->start = apr_time_now();
00082                 ti->timer_id = apr_pstrdup(pool, timer_id);
00083                 list_append(timers, ti);
00084         }
00085         
00086         /* Create the response: */
00087         *response = create_void_message(request, pool);
00088         unlock_bsmod_stat_mux();
00089         return BS_OK;
00090 }
00091 
00092 
00093 static bs_status bsmod_stat_handle_stop_timer(
00094                 const bs_service_request *request,
00095                 bs_service_response **response)
00096 {
00097         unsigned i;
00098         char *timer_id;
00099         
00100         /* Retrieve timer_id argument: */
00101         if (request->input->num_parts != 1 ||
00102                         request->input->parts[0] == NULL) return BS_ERROR;
00103         timer_id = request->input->parts[0]->data;
00104         
00105         /* Stop (create) the timer: */
00106         lock_bsmod_stat_mux();
00107         for (i = 0; i < list_size(timers); i++) {
00108                 timer_info *ti = (timer_info *) list_index(timers, i);
00109                 if (streq(ti->timer_id, timer_id)) {
00110                         apr_time_t diff;
00111                         ti->stop = apr_time_now();
00112                         diff = ti->stop - ti->start;
00113                         if (!ti->gathered_times) {
00114                                 ti->gathered_times = apr_psprintf(pool, "%" BS_UINT64_FMT, diff);
00115                         }
00116                         else {
00117                                 ti->gathered_times = apr_psprintf(pool, "%s %" BS_UINT64_FMT,
00118                                                 ti->gathered_times, diff);
00119                         }
00120                         break;
00121                 }       
00122         }
00123         
00124         /* Create the response: */
00125         if (i >= list_size(timers)) {
00126                 /* Timer_id unknown: return fault message: */
00127                 *response = create_error_response(request, 1, 
00128                                 "The provided timer_id is unknown.", pool);
00129         }
00130         else {
00131                 *response = create_void_message(request, pool);
00132         }
00133         unlock_bsmod_stat_mux();
00134         return BS_OK;
00135 }
00136 
00137 
00138 static bs_status bsmod_stat_handle_gather_times(
00139                 const bs_service_request *request,
00140                 bs_service_response **response)
00141 {
00142         unsigned i;
00143         char *timer_id, *data = NULL;
00144         
00145         /* Retrieve timer_id argument: */
00146         if (request->input->num_parts != 1 ||
00147                         request->input->parts[0] == NULL) return BS_ERROR;
00148         timer_id = request->input->parts[0]->data;
00149         
00150         /* Stop (create) the timer: */
00151         lock_bsmod_stat_mux();
00152         for (i = 0; i < list_size(timers); i++) {
00153                 timer_info *ti = (timer_info *) list_index(timers, i);
00154                 if (streq(ti->timer_id, timer_id)) {
00155                         data = ti->gathered_times;
00156                         break;
00157                 }       
00158         }
00159         
00160         /* Create the response: */
00161         if (i >= list_size(timers)) {
00162                 /* Timer_id unknown: return fault message: */
00163                 *response = create_error_response(request, 1, 
00164                                 "The provided timer_id is unknown.", pool);
00165         }
00166         else if (data == NULL) {
00167                 *response = create_error_response(request, 2, 
00168                                 "No data yet for provided timer_id.", pool);
00169         }
00170         else {
00171                 bs_message_instance *output;
00172                 bs_part_instance *time_list;
00173                 bs_data_type *type;
00174                 
00175                 type = new_bs_data_type(pool, "bsmod_stat_times_list");
00176                 time_list = new_bs_part_instance(pool, "time_list", type,
00177                                 data, strlen(data));
00178                 output = new_bs_message_instance(pool, "BSModStatGatherTimeOutput", 1);
00179                 output->parts[0] = time_list;
00180                 *response = new_bs_service_response(pool, request->uuid,
00181                                 request->service, request->port, request->operation,
00182                                 output, NULL);
00183         }
00184         unlock_bsmod_stat_mux();
00185         return BS_OK;
00186 }
00187 
00188 
00189 bs_status bsmod_stat_handle_time_functions(
00190                 const bs_service_request *request,
00191                 bs_service_response **response)
00192 {
00193         if (streq(request->operation, "start_timer"))
00194                 return bsmod_stat_handle_start_timer(request, response);
00195         else if (streq(request->operation, "stop_timer"))
00196                 return bsmod_stat_handle_stop_timer(request, response);
00197         else if (streq(request->operation, "gather_times"))
00198                 return bsmod_stat_handle_gather_times(request, response);
00199         return BS_NO_SERVICE;
00200 }
00201 

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