Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a510cd1ade1f10fc88c6663d13ff2538ad84f860
[simgrid.git] / testsuite / gras / gs_example_receive.c
1 /* gs_example_receive.c */
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <gras.h>
7
8 /* structs */
9 struct list {
10         int          v;
11         struct list *l;
12 };
13
14 struct s_pair {
15         int *pa;
16         int *pb;
17 };
18
19 struct s_mixed {
20         unsigned char c1;
21         unsigned long int l1;
22         unsigned char c2;
23         unsigned long int l2;
24 };
25
26 /* prototypes */
27 void disp_l(struct list *l);
28 long int
29 string_size_callback(void               *vars,
30                      struct s_gs_type   *p_type,
31                      void               *data);
32
33 /* local functions */
34 void
35 disp_l(struct list *l) {
36         if (l) {
37                 printf("%d ", l->v);
38                 disp_l(l->l);
39         }
40 }
41
42 long int
43 string_size_callback(void               *vars,
44                      struct s_gs_type   *p_type,
45                      void               *data) {
46         return 1+(long int)strlen(data);
47 }
48
49 /* main */
50 int
51 main(int argc, char **argv) {
52         struct list                     **pl            = NULL;
53         int                              *array         = NULL;
54         struct s_pair                    *p_pair        = NULL;
55         char                             *str           = NULL;
56         struct s_mixed                   *mixed         = NULL;
57
58         struct s_gs_type_driver          *t_driver      = NULL;
59         struct s_gs_net_driver           *n_driver      = NULL;
60         struct s_gs_type_bag             *bag           = NULL;
61         struct s_gs_connection           *cnx           = NULL;
62
63         struct s_gs_type_bag_ops         *bag_ops       = NULL;
64
65         struct s_gs_type                 *t_signed_int  = NULL;
66         struct s_gs_type                 *t_list        = NULL;
67         struct s_gs_type                 *t_ref_list    = NULL;
68
69         struct s_gs_type                 *t_array       = NULL;
70
71         struct s_gs_type                 *t_ref_sint    = NULL;
72         struct s_gs_type                 *t_pair        = NULL;
73
74         struct s_gs_type                 *t_char        = NULL;
75         struct s_gs_type                 *t_string      = NULL;
76
77         struct s_gs_type                *t_u_char     = NULL;
78         struct s_gs_type                *t_u_long_int = NULL;
79         struct s_gs_type                *t_mixed      = NULL;
80
81         struct s_gs_message_instance     *mi            = NULL;
82
83         gs_init(argc, argv);
84         gs_purge_cmd_line(&argc, argv);
85
86         t_driver = gs_type_driver_init("rl");
87         n_driver = gs_net_driver_init("fd");
88
89         bag     = gs_type_bag_alloc(t_driver);
90         bag_ops = bag->bag_ops;
91
92         {
93                 int fd = 0;
94                 cnx = gs_net_connection_accept(n_driver, &fd);
95         }
96
97         bag_ops->register_incoming_connection(bag, cnx);
98
99         /* sequence 1 */
100         t_signed_int    = bag_ops->get_type_by_name(bag, NULL, "signed int");
101         t_list          = gs_type_new_struct(bag, NULL, "list");
102         t_ref_list      = gs_type_new_ref(bag, NULL, "p_list", t_list);
103
104         gs_type_struct_append_field(t_list, "v", t_signed_int);
105         gs_type_struct_append_field(t_list, "l", t_ref_list);
106
107         /* sequence 2 */
108         t_array = gs_type_new_array(bag, NULL, "array", 5, t_signed_int);
109
110         /* sequence 3 */
111         t_ref_sint      = gs_type_new_ref(bag, NULL, "p_ref_sint", t_signed_int);
112         t_pair          = gs_type_new_struct(bag, NULL, "pair");
113
114         gs_type_struct_append_field(t_pair, "pa", t_ref_sint);
115         gs_type_struct_append_field(t_pair, "pb", t_ref_sint);
116
117         /* sequence 4 */
118         t_char  = bag_ops->get_type_by_name(bag, NULL, "signed char");
119         t_string = gs_type_new_array_with_callback(bag, NULL, "string", -1, t_char, string_size_callback, NULL);
120
121         /* sequence 5 */
122         t_u_char        = bag_ops->get_type_by_name(bag, NULL, "unsigned char");
123         t_u_long_int    = bag_ops->get_type_by_name(bag, NULL, "unsigned long int");
124         t_mixed         = gs_type_new_struct(bag, NULL, "mixed");
125    
126         gs_type_struct_append_field(t_mixed, "c1", t_u_char);
127         gs_type_struct_append_field(t_mixed, "l1", t_u_long_int);
128         gs_type_struct_append_field(t_mixed, "c2", t_u_char);
129         gs_type_struct_append_field(t_mixed, "l2", t_u_long_int);
130         
131         /* message receive */
132         mi      = gs_message_init_receive(bag, cnx);
133         fprintf(stderr, "\nreceiving sequence 1\n----------------\n");
134         pl      = gs_message_receive_next_sequence(mi);
135
136         printf("( ");
137         disp_l(*pl);
138         printf(")\n");
139
140         fprintf(stderr, "\nreceiving sequence 2\n----------------\n");
141         array   = gs_message_receive_next_sequence(mi);
142
143         {
144                 int i = 0;
145                 for (i = 0; i < 5; i++) {
146                         printf("array[%d] = %d\n", i, array[i]);
147                 }
148         }
149
150         fprintf(stderr, "\nreceiving sequence 3\n----------------\n");
151         p_pair  = gs_message_receive_next_sequence(mi);
152         printf("pair.pa = %p, *pair.pa = %d\n", (*p_pair).pa, *((*p_pair).pa));
153         printf("pair.pb = %p, *pair.pb = %d\n", (*p_pair).pb, *((*p_pair).pb));
154
155         fprintf(stderr, "\nreceiving sequence 4\n----------------\n");
156         str = gs_message_receive_next_sequence(mi);
157         printf("str = %s\n", str);
158
159         fprintf(stderr, "\nreceiving sequence 5\n----------------\n");
160         mixed = gs_message_receive_next_sequence(mi);
161         printf("c1=%c c2=%c; l1=%ld l2=%ld\n", 
162                mixed->c1, mixed->c2, mixed->l1, mixed->l2);
163
164         gs_exit();
165
166         return 0;
167 }