Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

event.h

Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 2004 Russell Leighton
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 /*
00020 Changes:
00021 
00022 3/30/02 Russell Leighton
00023 Added xactionid for transaction based control
00024 
00025 */
00026 
00027 #ifndef __AWEVENT__
00028 #define __AWEVENT__
00029 
00030 
00036 #include <sys/bitypes.h>
00037 #include <time.h>
00038 #include <sys/time.h>
00039 #include <unistd.h>
00040 #include <string.h>
00041 
00042 #include "util.h"
00043 #include "address.h"
00044 
00045 /* Careful with this! It should change when you change the network format */
00046 #define AW_EVENT_NET_FMTVERSION 2
00047 
00048 /* largest message we will accept */
00049 #define AW_EVENT_NET_MAXSIZE 0xffff
00050 
00054 typedef int64_t  aw_timestamp_t;
00055 #define AW_TIMESTAMP_PER_SEC ((aw_timestamp_t)1000000)
00056 
00061 inline static aw_timestamp_t aw_gettimestamp(void)
00062 {
00063   aw_timestamp_t
00064     stamp;
00065 
00066   /* is there a faster way? */
00067   struct timeval 
00068     tval;
00069   gettimeofday(&tval, NULL);
00070   stamp = (tval.tv_sec * AW_TIMESTAMP_PER_SEC) + tval.tv_usec ;
00071 
00072   return stamp ;
00073 }
00074 
00078 typedef enum {
00079 
00080   AW_EVENT_CONTEXT_CONTROL,     
00082   AW_EVENT_CONTEXT_ALARM,       
00084   AW_EVENT_CONTEXT_HANDLER,     
00086   AW_EVENT_CONTEXT_EXTERNAL,    
00088 } aw_event_context_t;
00089 
00093 typedef enum {
00094 
00095   AW_EVENT_TYPE_UNKNOWN,  /* must be 1st! */
00096 
00097   /* for internal use only */
00098   AW_EVENT_TYPE_MON_ADD_ALARM,
00099   AW_EVENT_TYPE_MON_REMOVE_ALARM,
00100   AW_EVENT_TYPE_MON_ADD_HANDLER_FDCALLBACK,
00101   AW_EVENT_TYPE_MON_REMOVE_HANDLER_FDCALLBACK,
00102   AW_EVENT_TYPE_MON_ADD_HANDLER_IRECALLBACK,
00103   AW_EVENT_TYPE_MON_REMOVE_HANDLER_IRECALLBACK,
00104   AW_EVENT_TYPE_MON_ADD_HANDLER,
00105   AW_EVENT_TYPE_MON_REMOVE_HANDLER,
00106 
00107   /* generic */
00108   AW_EVENT_TYPE_INT32,
00109   AW_EVENT_TYPE_INT64,
00110   AW_EVENT_TYPE_TIMESTAMP,
00111   AW_EVENT_TYPE_FLOAT32,
00112   AW_EVENT_TYPE_FLOAT64,
00113   AW_EVENT_TYPE_STRING,
00114   AW_EVENT_TYPE_PTR,
00115 
00116   AW_EVENT_NTYPES         /* must be last ! */
00117 
00118 }  aw_event_payload_t;
00119 
00120 
00121 #define AW_EVENT_SIGNAME_SIZE 64
00122 
00126 typedef struct {
00127 
00128   /* descriptive name of event */
00129   byte_t
00130    name[AW_EVENT_SIGNAME_SIZE];
00131 
00132   /* descriptive name of event source */
00133   byte_t
00134    source[AW_EVENT_SIGNAME_SIZE];
00135 
00136   /* priority */
00137   int32_t
00138    priority;
00139 
00140   /* address for handlers to match */
00141   aw_address_t
00142    id;
00143 
00144 } aw_event_sig_t;
00145 
00149 typedef struct 
00150 {
00151 
00152   int32_t
00153    xactionid;               /* id of particular xaction  */
00154 
00155 } aw_event_xactioninfo_t;
00156 
00160 typedef struct aw_event_s 
00161 {
00162 
00163   /* list */
00164   struct 
00165    aw_event_s *next;
00166 
00167   /* where this was created */
00168   aw_event_context_t
00169    context;
00170 
00171   /* time stamp for this event */
00172   aw_timestamp_t 
00173    tstamp;
00174 
00175   /* event signature */
00176   aw_event_sig_t
00177    sig;
00178 
00179   /* transaction info, used to track events when alarms/handlers use alarmsync */
00180   aw_event_xactioninfo_t
00181    xactioninfo;
00182 
00183   /* type for payload */
00184   aw_event_payload_t
00185    payload_type;
00186 
00187   /* data */
00188   union {
00189 
00190     int32_t
00191      int32_value;
00192 
00193     int64_t
00194      int64_value;
00195 
00196     float32_t
00197      float32_value;
00198 
00199     float64_t
00200      float64_value;
00201 
00202     aw_timestamp_t
00203      tstamp;
00204 
00205     byte_t
00206      *string;                         /* ptr to null terminated string */
00207 
00208     void 
00209      *valueptr ;                      /* ptr to some data, use event's 'id', 'source' and 'status' to dispatch */
00210 
00211   } payload;
00212 
00213 
00214   /* called when dup()ing an event of type AW_EVENT_TYPE_PTR, may return a copy of the data
00215      or the orginal ptr, you are on your own to manage this memory. It is suggested you
00216      not pass fancy structures.
00217   */
00218   void * (*copy_valueptr)(void *valueptr);
00219 
00220   /* called before freeing event structure, use this to free resources an event references */
00221   void (*finalize)(struct aw_event_s *e);
00222 
00223   /* called to write payload as text in 's' with max chars 'size' , set to default in aw_create_event() */
00224   void (*tostring)(struct aw_event_s *e, byte_t *s, u_int32_t size);
00225 
00226   /* memory management */
00227   aw_cache_allocator_t
00228    *factory;
00229 
00230 } aw_event_t;
00231 
00235 typedef struct 
00236 {
00237 
00238   /* how many in q */
00239   u_int32_t count;
00240 
00241   /* first in q */
00242   aw_event_t *first;
00243 
00244   /* last in q */
00245   aw_event_t *last;
00246 
00247 } aw_event_q_t;
00248 
00249 
00250 /* #################### exported functions ######################## */
00251 
00252 /* event.c */
00253 
00254 /*** q management ***/
00255 
00262 inline static void aw_append_q(aw_event_t *e, aw_event_q_t *q)
00263 {
00264   u_int32_t
00265     count = q->count;
00266 
00267   if ( count == 0 ) {
00268 
00269     q->first = e;
00270   
00271   } else {
00272 
00273     q->last->next = e;
00274 
00275   } /* end else */
00276 
00277   q->last = e;
00278   q->count = count + 1;
00279   e->next = NULL;
00280 }
00281 
00287 inline static void aw_push_q(aw_event_t *e, aw_event_q_t *q)
00288 {
00289   u_int32_t
00290     count = q->count;
00291 
00292   if ( count == 0 ) q->last = e;
00293 
00294   q->count = count + 1;
00295   e->next = q->first;
00296   q->first = e;
00297 
00298 }
00299 
00305 inline static aw_event_t * aw_dq_q(aw_event_q_t *q)
00306 {
00307   aw_event_t
00308     *last, *e;
00309   u_int32_t
00310     count = q->count;
00311   
00312   if (count != 0) {
00313 
00314     q->count = count - 1;
00315     e = q->last;
00316     last = e->next;
00317     e->next = NULL ;
00318     q->last = last;
00319     if ( last == NULL ) q->first = last;
00320 
00321   } else {
00322 
00323     e = NULL ;
00324 
00325   }/* end else */
00326 
00327   return e ;
00328 }
00329 
00330 
00336 inline static aw_event_t * aw_pop_q(aw_event_q_t *q)
00337 {
00338   aw_event_t
00339     *first, *e;
00340   u_int32_t
00341     count = q->count;
00342   
00343   if (count != 0) {
00344 
00345     q->count = count - 1;
00346     e = q->first;
00347     first = e->next;
00348     e->next = NULL;
00349     q->first = first;
00350     if ( first == NULL ) q->last = first;
00351 
00352   } else {
00353 
00354     e = NULL ;
00355 
00356   }/* end else */
00357 
00358   return e ;
00359 }
00360 
00361 
00367 inline static void aw_append_qs(aw_event_q_t *q1, aw_event_q_t *q2)
00368 {
00369   u_int32_t
00370     count = q1->count;
00371 
00372   /* don't bother */
00373   if ( q2->count == 0 ) goto done;
00374 
00375   /* just set if 0 */
00376   if (count == 0) {
00377 
00378     /* copy */
00379     memcpy( q1, q2, sizeof(aw_event_q_t));
00380 
00381   } else {
00382 
00383     count += q2->count;
00384 
00385     q1->count = count;
00386     q1->last->next = q2->first;
00387     q1->last = q2->last;
00388 
00389   }/* end else */
00390 
00391   /* clear q2 */
00392   memset(q2, 0, sizeof(aw_event_q_t));
00393 
00394  done:;
00395 
00396 }
00397 
00398 
00405 const aw_event_t *aw_findin_q(const aw_event_t *e, const aw_event_q_t *q);
00406 
00411 void aw_clear_q(aw_event_q_t *q);
00412 
00413 /*** event management ***/
00414 
00421 aw_event_t * aw_create_event(aw_event_context_t context, aw_cache_allocator_t *f);
00422 
00427 void aw_destroy_event(aw_event_t *e);
00428 
00435 aw_event_t *aw_dup_event(const aw_event_t *e,   aw_cache_allocator_t  *f);
00436 
00444 void aw_event_format(aw_event_t *e, byte_t *format, byte_t *s, u_int32_t size);
00445 
00446 
00454 int32_t aw_event2n(aw_event_t *e, int32_t size, byte_t *buffer);
00455 
00456 
00463 int32_t aw_n2event(aw_event_t *e, const byte_t *buffer);
00464 
00465 #endif
Aware 0.11.1 Copyright (C) 1998-2005 Russell Leighton (russ@elegant-software.com)