1 /* Copyright (c) 2006, 2007, 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
9 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "My little example");
16 int server_kill_cb(gras_msg_cb_ctx_t ctx, void *payload)
18 gras_socket_t client = gras_msg_cb_ctx_from(ctx);
19 server_data_t *globals = (server_data_t *) gras_userdata_get();
21 XBT_CRITICAL("Argh, killed by %s:%d! Bye folks...",
22 gras_socket_peer_name(client), gras_socket_peer_port(client));
27 } /* end_of_kill_callback */
29 int server_hello_cb(gras_msg_cb_ctx_t ctx, void *payload)
31 gras_socket_t client = gras_msg_cb_ctx_from(ctx);
33 XBT_INFO("Cool, we received the message from %s:%d.",
34 gras_socket_peer_name(client), gras_socket_peer_port(client));
37 } /* end_of_hello_callback */
39 int server(int argc, char *argv[])
41 gras_socket_t mysock; /* socket on which I listen */
42 server_data_t *globals;
44 gras_init(&argc, argv);
46 globals = gras_userdata_new(server_data_t *);
49 gras_msgtype_declare("hello", NULL);
50 gras_msgtype_declare("kill", NULL);
51 mysock = gras_socket_server(atoi(argv[1]));
53 gras_cb_register("hello", &server_hello_cb);
54 gras_cb_register("kill", &server_kill_cb);
56 while (!globals->killed) {
57 gras_msg_handle(-1); /* blocking */
64 int client(int argc, char *argv[])
66 gras_socket_t mysock; /* socket on which I listen */
67 gras_socket_t toserver; /* socket used to write to the server */
69 gras_init(&argc, argv);
71 gras_msgtype_declare("hello", NULL);
72 gras_msgtype_declare("kill", NULL);
73 mysock = gras_socket_server_range(1024, 10000, 0, 0);
75 XBT_VERB("Client ready; listening on %d", gras_socket_my_port(mysock));
77 gras_os_sleep(1.5); /* sleep 1 second and half */
78 toserver = gras_socket_client(argv[1], atoi(argv[2]));
80 gras_msg_send(toserver, "hello", NULL);
81 XBT_INFO("we sent the data to the server on %s. Let's do it again for fun",
82 gras_socket_peer_name(toserver));
83 gras_msg_send(toserver, "hello", NULL);
85 XBT_INFO("Ok. Enough. Have a rest, and then kill the server");
86 gras_os_sleep(5); /* sleep 1 second and half */
87 gras_msg_send(toserver, "kill", NULL);