Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix the path to gras_stub_generator in the tutorial makefile
[simgrid.git] / doc / gtut-files / 09-simpledata.c
1 /* Copyright (c) 2006, 2007, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5   * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <gras.h>
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"My little example");
10
11 typedef struct {
12    int killed;
13 } server_data_t;
14    
15
16 int server_kill_cb(gras_msg_cb_ctx_t ctx, void *payload) {
17   double delay = *(double*)payload;
18   gras_socket_t client = gras_msg_cb_ctx_from(ctx);
19   server_data_t *globals=(server_data_t*)gras_userdata_get();
20    
21   CRITICAL3("Argh, %s:%d gave me %.2f seconds before suicide!",
22         gras_socket_peer_name(client), gras_socket_peer_port(client),delay);
23   gras_os_sleep(delay);
24   CRITICAL0("Bye folks...");
25    
26    
27   globals->killed = 1;
28    
29   return 0;
30 } /* end_of_kill_callback */
31
32 int server_hello_cb(gras_msg_cb_ctx_t ctx, void *payload) {
33   char *msg=*(char**)payload;
34   gras_socket_t client = gras_msg_cb_ctx_from(ctx);
35
36   INFO3("Cool, we received a message from %s:%d. Here it is: \"%s\"",
37         gras_socket_peer_name(client), gras_socket_peer_port(client),
38         msg);
39   
40   return 0;
41 } /* end_of_hello_callback */
42
43 void message_declaration(void) {
44   gras_msgtype_declare("kill", gras_datadesc_by_name("double"));
45   gras_msgtype_declare("hello", gras_datadesc_by_name("string"));
46 }
47
48
49 int server(int argc, char *argv[]) {
50   gras_socket_t mysock;   /* socket on which I listen */
51   server_data_t *globals;
52   
53   gras_init(&argc,argv);
54
55   globals=gras_userdata_new(server_data_t*);
56   globals->killed=0;
57
58   message_declaration();
59   mysock = gras_socket_server(atoi(argv[1]));
60    
61   gras_cb_register("hello",&server_hello_cb);   
62   gras_cb_register("kill",&server_kill_cb);
63
64   while (!globals->killed) {
65      gras_msg_handle(-1); /* blocking */
66   }
67     
68   gras_exit();
69   return 0;
70 }
71
72 int client(int argc, char *argv[]) {
73   gras_socket_t mysock;   /* socket on which I listen */
74   gras_socket_t toserver; /* socket used to write to the server */
75
76   gras_init(&argc,argv);
77
78   message_declaration();
79   mysock = gras_socket_server_range(1024, 10000, 0, 0);
80   
81   VERB1("Client ready; listening on %d", gras_socket_my_port(mysock));
82   
83   gras_os_sleep(1.5); /* sleep 1 second and half */
84   toserver = gras_socket_client(argv[1], atoi(argv[2]));
85   
86   char *hello_payload="Nice to meet you";
87   gras_msg_send(toserver,"hello", &hello_payload);
88   INFO1("we sent the hello to the server on %s.", gras_socket_peer_name(toserver));
89    
90   double kill_payload=0.5;
91   gras_msg_send(toserver,"kill", &kill_payload);
92   INFO0("Gave the server more 0.5 second to live");
93    
94   gras_exit();
95   return 0;
96 }