~ubuntu-branches/ubuntu/raring/sip-tester/raring

« back to all changes in this revision

Viewing changes to stat.hpp

  • Committer: Bazaar Package Importer
  • Author(s): ARAKI Yasuhiro
  • Date: 2005-04-11 11:53:53 UTC
  • Revision ID: james.westby@ubuntu.com-20050411115353-o33qn3noyjwjgnpd
Tags: upstream-1.1rc1
ImportĀ upstreamĀ versionĀ 1.1rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  This program is free software; you can redistribute it and/or modify
 
3
 *  it under the terms of the GNU General Public License as published by
 
4
 *  the Free Software Foundation; either version 2 of the License, or
 
5
 *  (at your option) any later version.
 
6
 *
 
7
 *  This program is distributed in the hope that it will be useful,
 
8
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
 *  GNU General Public License for more details.
 
11
 *
 
12
 *  You should have received a copy of the GNU General Public License
 
13
 *  along with this program; if not, write to the Free Software
 
14
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
 *
 
16
 *  Authors : Benjamin GAUTHIER - 24 Mar 2004
 
17
 *            Joseph BANINO
 
18
 *            Olivier JACQUES
 
19
 *            Richard GAYRAUD
 
20
 *            From Hewlett Packard Company.
 
21
 *           
 
22
 */
 
23
 
 
24
#ifndef __STAT_H__
 
25
#define __STAT_H__
 
26
 
 
27
 
 
28
#define TIME_LENGTH 32
 
29
#define DEFAULT_FILE_NAME (char*)"dumpFile"
 
30
#define DEFAULT_EXTENSION (char*)".csv"
 
31
 
 
32
#define MAX_REPARTITION_HEADER_LENGTH 1024
 
33
#define MAX_REPARTITION_INFO_LENGTH   1024
 
34
#define MAX_CHAR_BUFFER_SIZE          256
 
35
 
 
36
#include <ctime> 
 
37
#include <sys/time.h> 
 
38
#include <time.h> 
 
39
#include <iostream>
 
40
#include <fstream>
 
41
#include <stdio.h>
 
42
 
 
43
using namespace std;
 
44
 
 
45
/*
 
46
__________________________________________________________________________
 
47
 
 
48
              C S t a t    C L A S S
 
49
__________________________________________________________________________
 
50
*/
 
51
 
 
52
  /**
 
53
   * This class provides some means to compute and display statistics.
 
54
   * This is a singleton class.
 
55
   */
 
56
 
 
57
class CStat {
 
58
public:
 
59
 
 
60
  /* 
 
61
   * This struct is used for repartition table
 
62
   * border max is the max value allow for this range
 
63
   * nbInThisBorder is the counter of value in this range
 
64
   */
 
65
  typedef struct _T_dynamicalRepartition
 
66
  {
 
67
    unsigned int  borderMax;
 
68
    unsigned long nbInThisBorder; 
 
69
  } T_dynamicalRepartition; 
 
70
 
 
71
  /**
 
72
   * Actions on counters
 
73
   */
 
74
  enum E_Action
 
75
  {
 
76
    E_CREATE_OUTGOING_CALL,
 
77
    E_CREATE_INCOMING_CALL,
 
78
    E_CALL_FAILED, 
 
79
    E_CALL_SUCCESSFULLY_ENDED,
 
80
    E_RESET_PD_COUNTERS,
 
81
    E_RESET_PL_COUNTERS,
 
82
    E_ADD_CALL_DURATION,
 
83
    E_ADD_RESPONSE_TIME_DURATION,
 
84
    E_FAILED_CANNOT_SEND_MSG,
 
85
    E_FAILED_MAX_UDP_RETRANS,
 
86
    E_FAILED_UNEXPECTED_MSG,
 
87
    E_FAILED_CALL_REJECTED,
 
88
    E_FAILED_CMD_NOT_SENT,
 
89
    E_FAILED_REGEXP_DOESNT_MATCH,
 
90
    E_FAILED_REGEXP_HDR_NOT_FOUND,
 
91
    E_OUT_OF_CALL_MSGS,
 
92
    E_AUTO_ANSWERED
 
93
  };
 
94
  /**
 
95
   * Counters management
 
96
   */
 
97
  enum E_CounterName
 
98
  {
 
99
  // Cumulative counters
 
100
  CPT_C_IncomingCallCreated,
 
101
  CPT_C_OutgoingCallCreated,
 
102
  CPT_C_SuccessfulCall,
 
103
  CPT_C_FailedCall,
 
104
  CPT_C_CurrentCall,
 
105
  CPT_C_NbOfCallUsedForAverageCallLength,
 
106
  CPT_C_AverageCallLength,
 
107
  CPT_C_NbOfCallUsedForAverageResponseTime,
 
108
  CPT_C_AverageResponseTime,
 
109
  CPT_C_FailedCallCannotSendMessage,
 
110
  CPT_C_FailedCallMaxUdpRetrans,
 
111
  CPT_C_FailedCallUnexpectedMessage,
 
112
  CPT_C_FailedCallCallRejected,
 
113
  CPT_C_FailedCallCmdNotSent,
 
114
  CPT_C_FailedCallRegexpDoesntMatch,
 
115
  CPT_C_FailedCallRegexpHdrNotFound,
 
116
  CPT_C_OutOfCallMsgs,
 
117
  CPT_C_AutoAnswered,
 
118
 
 
119
  // Periodic Display counter
 
120
  CPT_PD_IncomingCallCreated, // must be first (RESET_PD_COUNTER macro)
 
121
  CPT_PD_OutgoingCallCreated,
 
122
  CPT_PD_SuccessfulCall,
 
123
  CPT_PD_FailedCall,
 
124
  CPT_PD_NbOfCallUsedForAverageCallLength,
 
125
  CPT_PD_AverageCallLength,
 
126
  CPT_PD_NbOfCallUsedForAverageResponseTime,
 
127
  CPT_PD_AverageResponseTime,
 
128
  CPT_PD_FailedCallCannotSendMessage,
 
129
  CPT_PD_FailedCallMaxUdpRetrans,
 
130
  CPT_PD_FailedCallUnexpectedMessage,
 
131
  CPT_PD_FailedCallCallRejected,
 
132
  CPT_PD_FailedCallCmdNotSent,
 
133
  CPT_PD_FailedCallRegexpDoesntMatch,
 
134
  CPT_PD_FailedCallRegexpHdrNotFound,
 
135
  CPT_PD_OutOfCallMsgs,
 
136
  CPT_PD_AutoAnswered, // must be last (RESET_PD_COUNTER)
 
137
 
 
138
  // Periodic logging counter
 
139
  CPT_PL_IncomingCallCreated, // must be first (RESET_PL_COUNTER macro)
 
140
  CPT_PL_OutgoingCallCreated,
 
141
  CPT_PL_SuccessfulCall,
 
142
  CPT_PL_FailedCall,
 
143
  CPT_PL_NbOfCallUsedForAverageCallLength,
 
144
  CPT_PL_AverageCallLength,
 
145
  CPT_PL_NbOfCallUsedForAverageResponseTime,
 
146
  CPT_PL_AverageResponseTime,
 
147
  CPT_PL_FailedCallCannotSendMessage,
 
148
  CPT_PL_FailedCallMaxUdpRetrans,
 
149
  CPT_PL_FailedCallUnexpectedMessage,
 
150
  CPT_PL_FailedCallCallRejected,
 
151
  CPT_PL_FailedCallCmdNotSent,
 
152
  CPT_PL_FailedCallRegexpDoesntMatch,
 
153
  CPT_PL_FailedCallRegexpHdrNotFound,
 
154
  CPT_PL_OutOfCallMsgs,
 
155
  CPT_PL_AutoAnswered, // must be last (RESET_PL_COUNTER)
 
156
 
 
157
  E_NB_COUNTER
 
158
  };
 
159
 
 
160
  /*
 
161
  ** exported methods
 
162
  */
 
163
  /**
 
164
   * Get the single instance of the class.
 
165
   *
 
166
   * Only one instance of CStat exists in the component. This
 
167
   * instance is automatically created the first time the instance()
 
168
   * method is called.
 
169
   *
 
170
   * @return the single instance of the CStat class.
 
171
   */
 
172
  static CStat* instance (); 
 
173
 
 
174
  /**
 
175
   * Delete the single instance of the class.
 
176
   *
 
177
   * Only one instance of CStat exists in the component. This
 
178
   * instance is deleted when the close method is called.
 
179
   */
 
180
  void close (); 
 
181
 
 
182
  /**
 
183
   * ComputeStat Methods are used to modify counter value
 
184
   * It's the main interface to update counter
 
185
   *
 
186
   * @return 0 if the action is known
 
187
   *        -1 else
 
188
   */
 
189
  int computeStat (E_Action P_action);
 
190
  int computeStat (E_Action P_action, unsigned long P_value);
 
191
 
 
192
  /**
 
193
   * GetStat Method is used to retrieve a counter value
 
194
   *
 
195
   * @return the counter value
 
196
   **/
 
197
  unsigned long  GetStat (E_CounterName P_counter);
 
198
  
 
199
  /**
 
200
   * formatTime.
 
201
   *
 
202
   * This method converts a struct timeval parameter into a printable string
 
203
   * in the format given in parameter.
 
204
   *
 
205
   * @param P_tv.
 
206
   * @return a pointer on a static string containing formated time
 
207
   */
 
208
  char* formatTime (struct timeval* P_tv);
 
209
 
 
210
  /**
 
211
   * setRepartitionCallLength 
 
212
   * - set the unsigned int table passed in parameter as the repartition table 
 
213
   *   for call length. This is done by calling the initRepartition methode on 
 
214
   *   the M_CallLengthRepartition variable.
 
215
   * - set the char* list of int (must be separeted with coma as the 
 
216
   *   repartition table for call length
 
217
   *   This is done by calling the createIntegerTable to transform the char* 
 
218
   *   list into unsigned int list. Then the initRepartition methode is 
 
219
   *   call with the created unsigned int list and the M_CallLengthRepartition 
 
220
   *   variable
 
221
   *
 
222
   * setRepartitionResponseTime
 
223
   *   Same than setRepartitionCallLength with the variable
 
224
   *  M_ResponseTimeRepartition variableinstead of M_CallLengthRepartition 
 
225
   *  variable
 
226
   */
 
227
  void setRepartitionCallLength   (unsigned int* repartition, int nombre);
 
228
  void setRepartitionCallLength   (char * liste);
 
229
  void setRepartitionResponseTime (unsigned int* repartition, int nombre);
 
230
  void setRepartitionResponseTime (char * liste);
 
231
 
 
232
  /* define the file name to use to dump statistic in file */
 
233
  void setFileName                (char * name);
 
234
  void setFileName                (char * name, char * extension);
 
235
 
 
236
  /**
 
237
   * Display data periodically updated on screen.
 
238
   */
 
239
  void displayData (FILE *f);
 
240
  void displayStat(FILE *f);
 
241
  void displayRepartition(FILE *f);
 
242
 
 
243
 
 
244
  /**
 
245
   * Dump data periodically in the file M_FileName
 
246
   */
 
247
  void dumpData ();
 
248
 
 
249
  /**
 
250
   * initialize the class variable member
 
251
   */
 
252
  int init();
 
253
 
 
254
private:
 
255
   
 
256
  /** 
 
257
   * Constructor.
 
258
   *
 
259
   * Made private because this is a singleton class.
 
260
   */
 
261
  CStat ();
 
262
 
 
263
  /** 
 
264
   * Destructor.
 
265
   *
 
266
   * Made private because this is a singleton class.
 
267
   */
 
268
  ~CStat ();
 
269
 
 
270
  static CStat*            M_instance;
 
271
  unsigned long            M_counters[E_NB_COUNTER];
 
272
  T_dynamicalRepartition*  M_ResponseTimeRepartition;
 
273
  T_dynamicalRepartition*  M_CallLengthRepartition;
 
274
  int                      M_SizeOfResponseTimeRepartition;
 
275
  int                      M_SizeOfCallLengthRepartition;
 
276
  struct timeval           M_startTime;
 
277
  struct timeval           M_pdStartTime;
 
278
  struct timeval           M_plStartTime;
 
279
 
 
280
  bool                     M_headerAlreadyDisplayed;
 
281
  char*                    M_fileName;
 
282
  ofstream*                M_outputStream;
 
283
 
 
284
  unsigned long long       M_C_sumCallLength;
 
285
  unsigned long long       M_C_sumResponseTime;
 
286
  unsigned long long       M_PD_sumCallLength;
 
287
  unsigned long long       M_PD_sumResponseTime;
 
288
  unsigned long long       M_PL_sumCallLength;
 
289
  unsigned long long       M_PL_sumResponseTime;
 
290
 
 
291
  /**
 
292
   * initRepartition
 
293
   * This methode is used to create the repartition table with a table of 
 
294
   * unsigned int the reparition is created like following, with Vi the given 
 
295
   * value in the table
 
296
   * 0    <= x <  V1  
 
297
   * V1   <= x <  V2 
 
298
   *  ...
 
299
   * Vn-1 <= x <  Vn
 
300
   *         x >= Vn
 
301
   * So the repartition table have the size n+1 if the given table has a size 
 
302
   * of n */
 
303
  void  initRepartition(unsigned int* repartition, int nombre,
 
304
                        T_dynamicalRepartition ** tabRepartition, int* nbTab);
 
305
  
 
306
  /**
 
307
   * createIntegerTable
 
308
   * this method try to create a table of unsigned int with the list of char* 
 
309
   * passed in parameters
 
310
   * if it succed, it's return true (1)
 
311
   * else it's return false (0)
 
312
   */
 
313
  int  createIntegerTable(char * P_listeStr, 
 
314
                          unsigned int ** listeInteger, 
 
315
                          int * sizeOfList);
 
316
 
 
317
  /**
 
318
   * isWellFormed
 
319
   * this method check if the char* passed in parameter in really a list of  
 
320
   * integer separated with comma.
 
321
   * if yes, it's return true (1)
 
322
   * else, it's return false (0)
 
323
   */
 
324
  int  isWellFormed(char * P_listeStr, int * nombre);
 
325
 
 
326
  /**
 
327
   * updateRepartition
 
328
   * The methode look for the place to set the value passed in parameter
 
329
   * Once found, the associeted counter is incremented
 
330
   */
 
331
  void  updateRepartition( T_dynamicalRepartition* tabRepart, 
 
332
                           int sizeOfTab, 
 
333
                           unsigned long value);
 
334
 
 
335
  /**
 
336
   * displayRepartition
 
337
   * Display the repartition passed in parameter at the screen
 
338
   */
 
339
  void  displayRepartition(FILE *f,
 
340
                           T_dynamicalRepartition * tabRepartition, 
 
341
                           int sizeOfTab);
 
342
 
 
343
  /**
 
344
   * sRepartitionHeader
 
345
   * return a string with the range description of the given repartition
 
346
   */
 
347
  char* sRepartitionHeader(T_dynamicalRepartition * tabRepartition, 
 
348
                           int sizeOfTab,
 
349
                           char* P_repartitionName);
 
350
 
 
351
  /**
 
352
   * sRepartitionInfo
 
353
   * return a string with the number of value in the differente range of the 
 
354
   * given repartition
 
355
   */
 
356
  char* sRepartitionInfo(T_dynamicalRepartition * tabRepartition, 
 
357
                         int sizeOfTab);
 
358
 
 
359
  /**
 
360
   * UpdateAverageCounter
 
361
   * This methode compute the real moyenne with the passed value on the given 
 
362
   * counter
 
363
   */
 
364
  void updateAverageCounter(E_CounterName P_AverageCounter, 
 
365
                            E_CounterName P_NbOfCallUsed,
 
366
                            unsigned long long* P_sum, 
 
367
                            unsigned long P_value);
 
368
 
 
369
  /**
 
370
   * computeDiffTimeInMs.
 
371
   *
 
372
   * This method calculates elaped time in ms
 
373
   *
 
374
   * @param tf = final date
 
375
   * @param ti = initial date
 
376
   * 
 
377
   * @return number of ms between the 2 dates
 
378
   */
 
379
  long computeDiffTimeInMs (struct timeval* tf, struct timeval* ti);
 
380
  
 
381
  /**
 
382
   * msToHHMMSS.
 
383
   *
 
384
   * This converts an unsigned long containing a number of ms
 
385
   * into a string expressing the same value in format HH:MM:SS.
 
386
   *
 
387
   * @param P_ms.
 
388
   * 
 
389
   * @return a pointer on a static string containing formated time
 
390
   */
 
391
  char* msToHHMMSS (unsigned long P_ms);
 
392
  
 
393
  /**
 
394
   * msToHHMMSSmm.
 
395
   *
 
396
   * This converts an unsigned long containing a number of ms
 
397
   * into a string expressing the same value in format HH:MM:SS:mmm.
 
398
   *
 
399
   * @param P_ms.
 
400
   * 
 
401
   * @return a pointer on a static string containing formated time
 
402
   */
 
403
  char* msToHHMMSSmmm (unsigned long P_ms);
 
404
  
 
405
  /**
 
406
   * Effective C++
 
407
   *
 
408
   * To prevent public copy ctor usage: no implementation
 
409
   */
 
410
  CStat (const CStat&);
 
411
  
 
412
  /**
 
413
   * Effective C++
 
414
   *
 
415
   * To prevent public operator= usage: no implementation
 
416
   */
 
417
  CStat& operator=(const CStat&);
 
418
};
 
419
 
 
420
#endif // __STAT_H__