00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
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
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
00073 ti->start = apr_time_now();
00074 break;
00075 }
00076 }
00077 if (i >= list_size(timers)) {
00078
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
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
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
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
00125 if (i >= list_size(timers)) {
00126
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
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
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
00161 if (i >= list_size(timers)) {
00162
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