Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
54ff8a6c86e4f5f1f6040a0e1fa2c79dad4295ec
[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 //#define 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 /* main */
65 int
66 main(int argc, char **argv) {
67         struct list                  *l            = NULL;
68
69         struct s_pair                 pair;
70
71         int array[5] =
72                 {
73                         11, 12, 13, 14, 15,
74                 };
75    
76         struct s_mixed                mixed =
77                 {
78                         'a',1.0,'b',2.0
79                 };
80
81         struct s_gs_type_driver         *t_driver     = NULL;
82         struct s_gs_net_driver          *n_driver     = NULL;
83         struct s_gs_type_bag            *bag          = NULL;
84         struct s_gs_connection          *cnx          = NULL;
85
86         struct s_gs_type_bag_ops        *bag_ops      = NULL;
87
88         struct s_gs_type                *t_signed_int = NULL;
89         struct s_gs_type                *t_list       = NULL;
90         struct s_gs_type                *t_ref_list   = NULL;
91
92         struct s_gs_type                *t_array      = NULL;
93
94         struct s_gs_type                *t_ref_sint   = NULL;
95         struct s_gs_type                *t_pair       = NULL;
96
97         struct s_gs_type                *t_char       = NULL;
98         struct s_gs_type                *t_string     = NULL;
99
100 #ifndef PARSING
101         struct s_gs_type                *t_u_char     = NULL;
102         struct s_gs_type                *t_u_long_int = NULL;
103 #endif
104
105         struct s_gs_type                *t_mixed      = NULL;
106
107         struct s_gs_message             *m            = NULL;
108         struct s_gs_message_instance    *mi           = NULL;
109
110
111         gs_init(argc, argv);
112         gs_purge_cmd_line(&argc, argv);
113         gras_log_control_set("NDR.thresh=debug");
114
115         t_driver = gs_type_driver_init("rl");
116         n_driver = gs_net_driver_init("fd");
117
118         bag     = gs_type_bag_alloc(t_driver);
119         bag_ops = bag->bag_ops;
120
121         {
122                 int fd = 1;
123                 cnx = gs_net_connection_connect(n_driver, &fd);
124         }
125
126         bag_ops->register_outgoing_connection(bag, cnx);
127
128         /* sequence 1 */
129         t_signed_int    = bag_ops->get_type_by_name(bag, NULL, "signed int");
130         t_list          = gs_type_new_struct(bag, NULL, "list");
131         t_ref_list      = gs_type_new_ref(bag, NULL, "p_list", t_list);
132
133         gs_type_struct_append_field(t_list, "v", t_signed_int);
134         gs_type_struct_append_field(t_list, "l", t_ref_list);
135
136         /* sequence 2 */
137         t_array         = gs_type_new_array(bag, NULL, "array", 5, t_signed_int);
138
139         /* sequence 3 */
140         t_ref_sint      = gs_type_new_ref(bag, NULL, "p_ref_sint", t_signed_int);
141         t_pair          = gs_type_new_struct(bag, NULL, "pair");
142
143         gs_type_struct_append_field(t_pair, "pa", t_ref_sint);
144         gs_type_struct_append_field(t_pair, "pb", t_ref_sint);
145
146         /* sequence 4 */
147         t_char  = bag_ops->get_type_by_name(bag, NULL, "signed char");
148         t_string = gs_type_new_array_with_callback(bag, NULL, "string", -1, t_char, string_size_callback, NULL);
149
150         /* sequence 5 */
151 #ifdef PARSING
152         t_mixed         = gs_type_get_by_symbol(bag,struct_s_mixed);
153 #else
154         t_u_char        = bag_ops->get_type_by_name(bag, NULL, "unsigned char");
155         t_u_long_int    = bag_ops->get_type_by_name(bag, NULL, "unsigned long int");
156         t_mixed         = gs_type_new_struct(bag, NULL, "s_mixed");
157    
158         gs_type_struct_append_field(t_mixed, "c1", t_u_char);
159         gs_type_struct_append_field(t_mixed, "l1", t_u_long_int);
160         gs_type_struct_append_field(t_mixed, "c2", t_u_char);
161         gs_type_struct_append_field(t_mixed, "l2", t_u_long_int);
162 #endif
163
164         /* message declaration */
165         m = gs_message_new(bag, NULL, "my msg");
166         gs_message_append_new_sequence(m, t_ref_list);
167         gs_message_append_new_sequence(m, t_array);
168         gs_message_append_new_sequence(m, t_pair);
169         gs_message_append_new_sequence(m, t_string);
170         gs_message_append_new_sequence(m, t_mixed);
171
172         /* data setup */
173         l = cons (1, l);
174         l = cons (2, l);
175         l = cons (3, l);
176
177         pair.pa = malloc(sizeof(int));
178         pair.pb = pair.pa;
179         *(pair.pa) = 17;
180
181         /* message send */
182         mi = gs_message_init_send_by_name(bag, cnx, "my msg");
183         fprintf(stderr, "\nsending sequence 1\n----------------\n");
184         gs_message_send_next_sequence(mi, &l);
185         fprintf(stderr, "\nsending sequence 2\n----------------\n");
186         gs_message_send_next_sequence(mi, (void*)array);
187         fprintf(stderr, "\nsending sequence 3\n----------------\n");
188         gs_message_send_next_sequence(mi, &pair);
189         fprintf(stderr, "\nsending sequence 4\n----------------\n");
190         gs_message_send_next_sequence(mi, (void*)"Hello, World");
191         fprintf(stderr, "\nsending sequence 5\n----------------\n");
192         gs_message_send_next_sequence(mi, (void*)&mixed);
193
194         gs_exit();
195
196         return 0;
197 }