Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7a62512ad8999ee4700a2503fa15670f360ec2ae
[simgrid.git] / src / amok / base.c
1 /* $Id$ */
2
3 /* base - several addons to do specific stuff not in GRAS itself     */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2003 the OURAGAN project.                                  */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <strings.h>
14
15 #include "xbt/error.h"
16 #include "gras/datadesc.h"
17 #include "amok/base.h"
18
19 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(amok,GRAS_LOG_ROOT_CAT,"All AMOK categories");
20
21 amok_remoterr_t *amok_remoterr_new(gras_error_t param_errcode, 
22                                   const char* format,...) {
23    
24   amok_remoterr_t *res;
25    
26   va_list ap;
27   va_start(ap,format);
28   res = amok_remoterr_new_va(param_errcode,format,ap);
29   va_end(ap);
30   return res;
31 }
32
33 amok_remoterr_t *amok_remoterr_new_va(gras_error_t param_errcode, 
34                                       const char* format,va_list ap) {
35   amok_remoterr_t *res=gras_new(amok_remoterr_t,1);
36   res->code=param_errcode;
37   if (format) {
38      res->msg=(char*)gras_malloc(1024);
39      vsnprintf(res->msg,1024,format,ap);
40   } else {
41      res->msg = NULL;
42   }
43    
44   return res;   
45 }
46
47 void amok_remoterr_free(amok_remoterr_t*err) {
48    if (err) {
49       if (err->msg) gras_free(err->msg);
50       gras_free(err);
51    }
52 }
53
54
55 void
56 amok_repport_error (gras_socket_t *sock, gras_msgtype_t *msgtype,
57                     gras_error_t param_errcode, const char* format,...) {
58   amok_remoterr_t *error;
59   gras_error_t errcode;
60   va_list ap;
61
62   error=gras_new(amok_remoterr_t,1);
63   error->code=param_errcode;
64   error->msg=(char*)gras_malloc(1024); /* FIXME */
65   va_start(ap,format);
66   vsnprintf(error->msg,1024,format,ap);
67   va_end(ap);
68
69   errcode = gras_msg_send(sock,msgtype,error);
70   if (errcode != no_error) {
71      CRITICAL4("Error '%s' while reporting error '%s' to %s:%d",
72                gras_error_name(errcode),error->msg,
73                gras_socket_peer_name(sock),gras_socket_peer_port(sock) );
74   }
75 }
76
77 void amok_base_init(void) {
78   gras_datadesc_type_t *host_desc, *remoterr_desc;
79      
80   /* Build the datatype descriptions */
81   host_desc = gras_datadesc_struct("gras_host_t");
82   gras_datadesc_struct_append(host_desc,"name",gras_datadesc_by_name("string"));
83   gras_datadesc_struct_append(host_desc,"exp_size",gras_datadesc_by_name("int"));
84   gras_datadesc_struct_close(host_desc);
85   host_desc = gras_datadesc_ref("gras_host_t*",host_desc);
86    
87   remoterr_desc = gras_datadesc_struct("amok_remoterr_t");
88   gras_datadesc_struct_append(remoterr_desc,"msg",gras_datadesc_by_name("string"));
89   gras_datadesc_struct_append(remoterr_desc,"code",gras_datadesc_by_name("unsigned int"));
90   gras_datadesc_struct_close(remoterr_desc);
91   remoterr_desc = gras_datadesc_ref("amok_remoterr_t*",remoterr_desc);
92 }
93
94 void amok_base_exit(void) {
95    /* No real module mecanism in GRAS so far, nothing to do. */
96 }
97