Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9ec9bfe29131006dfa0d472dc04ec1d96241182d
[simgrid.git] / testsuite / gras / gs_example_send.c
1 /* gs_example_send */
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <gras.h>
7
8 #undef PARSING
9
10 /* structs */
11 struct list {
12         int          v;
13         struct list *l;
14 };
15
16 struct s_pair {
17         int *pa;
18         int *pb;
19 };
20
21 #ifdef PARSING
22 GRAS_DEFINE_TYPE(struct_s_mixed,
23   struct s_mixed {
24         unsigned char c1;
25         unsigned long int l1;
26         unsigned char c2;
27         unsigned long int l2;
28  }
29 );
30 #else
31   struct s_mixed {
32         unsigned char c1;
33         unsigned long int l1;
34         unsigned char c2;
35         unsigned long int l2;
36  };
37 #endif
38
39 /* prototypes */
40 struct list * cons(int v, struct list *l);
41 long int
42 string_size_callback(void               *vars,
43                      struct s_gs_type   *p_type,
44                      void               *data);
45
46 /* local functions */
47 struct list *
48 cons(int v, struct list *l) {
49         struct list *nl = malloc(sizeof (struct list));
50
51         nl->v = v;
52         nl->l = l;
53
54         return nl;
55 }
56
57 long int
58 string_size_callback(void               *vars,
59                      struct s_gs_type   *p_type,
60                      void               *data) {
61         return 1+(long int)strlen(data);
62 }
63
64 void parse_log_opt(int argc, char **argv,const char *deft);
65
66 /* main */
67 int
68 main(int argc, char **argv) {
69         struct list                  *l            = NULL;
70
71         struct s_pair                 pair;
72
73         int array[5] =
74                 {
75                         11, 12, 13, 14, 15,
76                 };
77         /*
78         struct s_mixed                mixed =
79                 {
80                         'a',1.0,'b',2.0
81                 };
82         */
83         struct s_gs_type_driver         *t_driver     = NULL;
84         struct s_gs_net_driver          *n_driver     = NULL;
85         struct s_gs_type_bag            *bag          = NULL;
86         struct s_gs_connection          *cnx          = NULL;
87
88         struct s_gs_type_bag_ops        *bag_ops      = NULL;
89
90         struct s_gs_type                *t_signed_int = NULL;
91         struct s_gs_type                *t_list       = NULL;
92         struct s_gs_type                *t_ref_list   = NULL;
93
94         struct s_gs_type                *t_array      = NULL;
95
96         struct s_gs_type                *t_ref_sint   = NULL;
97         struct s_gs_type                *t_pair       = NULL;
98
99         struct s_gs_type                *t_char       = NULL;
100         struct s_gs_type                *t_string     = NULL;
101
102 #ifndef PARSING
103         struct s_gs_type                *t_u_char     = NULL;
104         struct s_gs_type                *t_u_long_int = NULL;
105 #endif
106
107         struct s_gs_type                *t_mixed      = NULL;
108
109         struct s_gs_message             *m            = NULL;
110         struct s_gs_message_instance    *mi           = NULL;
111
112
113         parse_log_opt(argc,argv,"datadesc.thresh=debug");
114         gs_init(argc, argv);
115         gs_purge_cmd_line(&argc, argv);
116         gras_log_control_set("NDR.thresh=debug");
117
118         t_driver = gs_type_driver_init("rl");
119         n_driver = gs_net_driver_init("fd");
120
121         bag     = gs_type_bag_alloc(t_driver);
122         bag_ops = bag->bag_ops;
123
124         {
125                 int fd = 1;
126                 cnx = gs_net_connection_connect(n_driver, &fd);
127         }
128
129         bag_ops->register_outgoing_connection(bag, cnx);
130
131         /* sequence 1 */
132         t_signed_int    = bag_ops->get_type_by_name(bag, NULL, "signed int");
133         t_list          = gs_type_new_struct(bag, NULL, "list");
134         t_ref_list      = gs_type_new_ref(bag, NULL, "p_list", t_list);
135
136         gs_type_struct_append_field(t_list, "v", t_signed_int);
137         gs_type_struct_append_field(t_list, "l", t_ref_list);
138
139         /* sequence 2 */
140         t_array         = gs_type_new_array(bag, NULL, "array", 5, t_signed_int);
141
142         /* sequence 3 */
143         t_ref_sint      = gs_type_new_ref(bag, NULL, "p_ref_sint", t_signed_int);
144         t_pair          = gs_type_new_struct(bag, NULL, "pair");
145
146         gs_type_struct_append_field(t_pair, "pa", t_ref_sint);
147         gs_type_struct_append_field(t_pair, "pb", t_ref_sint);
148
149         /* sequence 4 */
150         t_char  = bag_ops->get_type_by_name(bag, NULL, "signed char");
151         t_string = gs_type_new_array_with_callback(bag, NULL, "string", -1, t_char, string_size_callback, NULL);
152
153         /* sequence 5 */
154 #ifdef PARSING
155         t_mixed         = gs_type_get_by_symbol(bag,struct_s_mixed);
156 #else
157         t_u_char        = bag_ops->get_type_by_name(bag, NULL, "unsigned char");
158         t_u_long_int    = bag_ops->get_type_by_name(bag, NULL, "unsigned long int");
159         t_mixed         = gs_type_new_struct(bag, NULL, "s_mixed");
160    
161         gs_type_struct_append_field(t_mixed, "c1", t_u_char);
162         gs_type_struct_append_field(t_mixed, "l1", t_u_long_int);
163         gs_type_struct_append_field(t_mixed, "c2", t_u_char);
164         gs_type_struct_append_field(t_mixed, "l2", t_u_long_int);
165 #endif
166
167         /* message declaration */
168         m = gs_message_new(bag, NULL, "my msg");
169         gs_message_append_new_sequence(m, t_ref_list);
170         gs_message_append_new_sequence(m, t_array);
171         gs_message_append_new_sequence(m, t_pair);
172         gs_message_append_new_sequence(m, t_string);
173         gs_message_append_new_sequence(m, t_mixed);
174
175         /* data setup */
176         l = cons (1, l);
177         l = cons (2, l);
178         l = cons (3, l);
179
180         pair.pa = malloc(sizeof(int));
181         pair.pb = pair.pa;
182         *(pair.pa) = 17;
183
184         /* message send */
185         mi = gs_message_init_send_by_name(bag, cnx, "my msg");
186         fprintf(stderr, "\nsending sequence 1\n----------------\n");
187         gs_message_send_next_sequence(mi, &l);
188         fprintf(stderr, "\nsending sequence 2\n----------------\n");
189         gs_message_send_next_sequence(mi, (void*)array);
190         fprintf(stderr, "\nsending sequence 3\n----------------\n");
191         gs_message_send_next_sequence(mi, &pair);
192         fprintf(stderr, "\nsending sequence 4\n----------------\n");
193         gs_message_send_next_sequence(mi, (void*)"Hello, World");
194         /*
195         fprintf(stderr, "\nsending sequence 5\n----------------\n");
196         gs_message_send_next_sequence(mi, (void*)&mixed);
197         */
198         gs_exit();
199
200         return 0;
201 }