Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rewrite the platform_script.lua used by the test for lua console.
[simgrid.git] / doc / gtut-files / 05-globals.c
1 /* Copyright (c) 2006, 2007, 2009, 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 <stdio.h>
8 #include <gras.h>
9
10 typedef struct {
11   int killed;
12 } server_data_t;
13
14
15 int server_kill_cb(gras_msg_cb_ctx_t ctx, void *payload)
16 {
17   gras_socket_t client = gras_msg_cb_ctx_from(ctx);
18   server_data_t *globals = (server_data_t *) gras_userdata_get();
19
20   fprintf(stderr, "Argh, killed by %s:%d! Bye folks...\n",
21           gras_socket_peer_name(client), gras_socket_peer_port(client));
22
23   globals->killed = 1;
24
25   return 0;
26 }                               /* end_of_kill_callback */
27
28 int server_hello_cb(gras_msg_cb_ctx_t ctx, void *payload)
29 {
30   gras_socket_t client = gras_msg_cb_ctx_from(ctx);
31
32   fprintf(stderr, "Cool, we received the message from %s:%d.\n",
33           gras_socket_peer_name(client), gras_socket_peer_port(client));
34
35   return 0;
36 }                               /* end_of_hello_callback */
37
38 int server(int argc, char *argv[])
39 {
40   gras_socket_t mysock;         /* socket on which I listen */
41   server_data_t *globals;
42
43   gras_init(&argc, argv);
44
45   globals = gras_userdata_new(server_data_t);
46   globals->killed = 0;
47
48   gras_msgtype_declare("hello", NULL);
49   gras_msgtype_declare("kill", NULL);
50   mysock = gras_socket_server(atoi(argv[1]));
51
52   gras_cb_register("hello", &server_hello_cb);
53   gras_cb_register("kill", &server_kill_cb);
54
55   while (!globals->killed) {
56     gras_msg_handle(-1);        /* blocking */
57   }
58
59   gras_exit();
60   return 0;
61 }
62
63 int client(int argc, char *argv[])
64 {
65   gras_socket_t mysock;         /* socket on which I listen */
66   gras_socket_t toserver;       /* socket used to write to the server */
67
68   gras_init(&argc, argv);
69
70   gras_msgtype_declare("hello", NULL);
71   gras_msgtype_declare("kill", NULL);
72   mysock = gras_socket_server_range(1024, 10000, 0, 0);
73
74   fprintf(stderr, "Client ready; listening on %d\n",
75           gras_socket_my_port(mysock));
76
77   gras_os_sleep(1.5);           /* sleep 1 second and half */
78   toserver = gras_socket_client(argv[1], atoi(argv[2]));
79
80   gras_msg_send(toserver, "hello", NULL);
81   fprintf(stderr,
82           "we sent the data to the server on %s. Let's do it again for fun\n",
83           gras_socket_peer_name(toserver));
84   gras_msg_send(toserver, "hello", NULL);
85
86   fprintf(stderr, "Ok. Enough. Have a rest, and then kill the server\n");
87   gras_os_sleep(5);             /* sleep 1 second and half */
88   gras_msg_send(toserver, "kill", NULL);
89
90   gras_exit();
91   return 0;
92 }