Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Indent the rest of the code (examples, buildtools, doc...) except for examples/SMPI...
[simgrid.git] / doc / gtut-files / 04-callback.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 <stdio.h>
8 #include <gras.h>
9
10 int server_hello_cb(gras_msg_cb_ctx_t ctx, void *payload)
11 {
12   gras_socket_t client = gras_msg_cb_ctx_from(ctx);
13
14   fprintf(stderr, "Cool, we received the message from %s:%d.\n",
15           gras_socket_peer_name(client), gras_socket_peer_port(client));
16
17   return 0;
18 }                               /* end_of_callback */
19
20 int server(int argc, char *argv[])
21 {
22   gras_socket_t mysock;         /* socket on which I listen */
23
24   gras_init(&argc, argv);
25
26   gras_msgtype_declare("hello", NULL);
27   mysock = gras_socket_server(atoi(argv[1]));
28
29   gras_cb_register("hello", &server_hello_cb);
30   gras_msg_handle(60);
31
32   gras_exit();
33   return 0;
34 }
35
36 int client(int argc, char *argv[])
37 {
38   gras_socket_t mysock;         /* socket on which I listen */
39   gras_socket_t toserver;       /* socket used to write to the server */
40
41   gras_init(&argc, argv);
42
43   gras_msgtype_declare("hello", NULL);
44   mysock = gras_socket_server_range(1024, 10000, 0, 0);
45
46   fprintf(stderr, "Client ready; listening on %d\n",
47           gras_socket_my_port(mysock));
48
49   gras_os_sleep(1.5);           /* sleep 1 second and half */
50   toserver = gras_socket_client(argv[1], atoi(argv[2]));
51
52   gras_msg_send(toserver, "hello", NULL);
53   fprintf(stderr, "That's it, we sent the data to the server on %s\n",
54           gras_socket_peer_name(toserver));
55
56   gras_exit();
57   return 0;
58 }