From b8f7e703e425b03794576432ebaa3351b26d0509 Mon Sep 17 00:00:00 2001 From: mquinson Date: Sun, 16 Jul 2006 17:45:15 +0000 Subject: [PATCH] Lesson 7: timers (plus some cleanups) git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@2593 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- doc/Doxyfile.in | 1 + doc/gtut-files/.cvsignore | 9 +++ doc/gtut-files/7-timers.c | 113 +++++++++++++++++++++++++++++++++ doc/gtut-files/7-timers.output | 57 +++++++++++++++++ doc/gtut-files/Makefile | 27 +++++++- doc/gtut-main.doc | 2 + doc/gtut-tour-6-logs.doc | 5 +- doc/gtut-tour-7-timers.doc | 92 +++++++++++++++++++++++++++ doc/gtut-tour.doc | 9 +-- 9 files changed, 309 insertions(+), 6 deletions(-) create mode 100644 doc/gtut-files/7-timers.c create mode 100644 doc/gtut-files/7-timers.output create mode 100644 doc/gtut-tour-7-timers.doc diff --git a/doc/Doxyfile.in b/doc/Doxyfile.in index f83edce07b..8edce6fb1e 100644 --- a/doc/Doxyfile.in +++ b/doc/Doxyfile.in @@ -415,6 +415,7 @@ INPUT = @srcdir@/index.doc \ @srcdir@/gtut-tour-4-callback.doc \ @srcdir@/gtut-tour-5-globals.doc \ @srcdir@/gtut-tour-6-logs.doc \ + @srcdir@/gtut-tour-7-timers.doc \ \ ./logcategories.doc \ \ diff --git a/doc/gtut-files/.cvsignore b/doc/gtut-files/.cvsignore index 6449498351..73be0370a8 100644 --- a/doc/gtut-files/.cvsignore +++ b/doc/gtut-files/.cvsignore @@ -51,3 +51,12 @@ _5-globals_simulator.c _6-logs_client.c _6-logs_server.c _6-logs_simulator.c + +7-timers.mk +7-timers.trace +7-timers_client +7-timers_server +7-timers_simulator +_7-timers_client.c +_7-timers_server.c +_7-timers_simulator.c diff --git a/doc/gtut-files/7-timers.c b/doc/gtut-files/7-timers.c new file mode 100644 index 0000000000..6622034297 --- /dev/null +++ b/doc/gtut-files/7-timers.c @@ -0,0 +1,113 @@ +#include + +XBT_LOG_NEW_DEFAULT_CATEGORY(test,"My little example"); + +/* *********************** Server *********************** */ +typedef struct { + int killed; +} server_data_t; + +int server_kill_cb(gras_msg_cb_ctx_t ctx, void *payload) { + gras_socket_t client = gras_msg_cb_ctx_from(ctx); + server_data_t *globals=(server_data_t*)gras_userdata_get(); + + CRITICAL2("Argh, killed by %s:%d! Bye folks...", + gras_socket_peer_name(client), gras_socket_peer_port(client)); + + globals->killed = 1; + + return 1; +} /* end_of_kill_callback */ + +int server_hello_cb(gras_msg_cb_ctx_t ctx, void *payload) { + gras_socket_t client = gras_msg_cb_ctx_from(ctx); + + INFO2("Cool, we received the message from %s:%d.", + gras_socket_peer_name(client), gras_socket_peer_port(client)); + + return 1; +} /* end_of_hello_callback */ + +int server(int argc, char *argv[]) { + gras_socket_t mysock; /* socket on which I listen */ + server_data_t *globals; + + gras_init(&argc,argv); + + globals=gras_userdata_new(server_data_t*); + globals->killed=0; + + gras_msgtype_declare("hello", NULL); + gras_msgtype_declare("kill", NULL); + mysock = gras_socket_server(atoi(argv[1])); + + gras_cb_register(gras_msgtype_by_name("hello"),&server_hello_cb); + gras_cb_register(gras_msgtype_by_name("kill"),&server_kill_cb); + + while (!globals->killed) { + gras_msg_handle(60); + } + + gras_exit(); + return 0; +} + +/* *********************** Client *********************** */ +/* client_data */ +typedef struct { + gras_socket_t toserver; + int done; +} client_data_t; + + +void client_do_hello(void) { + client_data_t *globals=(client_data_t*)gras_userdata_get(); + + gras_msg_send(globals->toserver,gras_msgtype_by_name("hello"), NULL); + INFO0("Hello sent to server"); +} /* end_of_client_do_hello */ + +void client_do_stop(void) { + client_data_t *globals=(client_data_t*)gras_userdata_get(); + + gras_msg_send(globals->toserver,gras_msgtype_by_name("kill"), NULL); + INFO0("Kill sent to server"); + + gras_timer_cancel_repeat(0.5,client_do_hello); + + globals->done = 1; + INFO0("Break the client's while loop"); +} /* end_of_client_do_stop */ + +int client(int argc, char *argv[]) { + gras_socket_t mysock; /* socket on which I listen */ + client_data_t *globals; + + gras_init(&argc,argv); + + gras_msgtype_declare("hello", NULL); + gras_msgtype_declare("kill", NULL); + mysock = gras_socket_server_range(1024, 10000, 0, 0); + + VERB1("Client ready; listening on %d", gras_socket_my_port(mysock)); + + globals=gras_userdata_new(server_data_t*); + globals->done = 0; + + gras_os_sleep(1.5); /* sleep 1 second and half */ + globals->toserver = gras_socket_client(argv[1], atoi(argv[2])); + + INFO0("Programming the repetitive action with a frequency of 0.5 sec"); + gras_timer_repeat(0.5,client_do_hello); + + INFO0("Programming the delayed action in 5 secs"); + gras_timer_delay(5,client_do_stop); + + while (!globals->done) { + gras_msg_handle(60); + } + + gras_exit(); + return 0; +} + diff --git a/doc/gtut-files/7-timers.output b/doc/gtut-files/7-timers.output new file mode 100644 index 0000000000..152b70ac87 --- /dev/null +++ b/doc/gtut-files/7-timers.output @@ -0,0 +1,57 @@ +$ ./test_server 12345 & ./test_client 127.0.0.1 12345 +[blaise:server:(23608) 0.000005] test.c:26: [test/INFO] Cool, we received the message from 127.0.0.1:1024. +[blaise:server:(23608) 0.500361] test.c:26: [test/INFO] Cool, we received the message from 127.0.0.1:1024. +[blaise:server:(23608) 1.003121] test.c:26: [test/INFO] Cool, we received the message from 127.0.0.1:1024. +[blaise:server:(23608) 1.507935] test.c:26: [test/INFO] Cool, we received the message from 127.0.0.1:1024. +[blaise:server:(23608) 2.010545] test.c:26: [test/INFO] Cool, we received the message from 127.0.0.1:1024. +[blaise:server:(23608) 2.516219] test.c:26: [test/INFO] Cool, we received the message from 127.0.0.1:1024. +[blaise:server:(23608) 3.018509] test.c:26: [test/INFO] Cool, we received the message from 127.0.0.1:1024. +[blaise:server:(23608) 3.518713] test.c:26: [test/INFO] Cool, we received the message from 127.0.0.1:1024. +[blaise:server:(23608) 4.022585] test.c:26: [test/INFO] Cool, we received the message from 127.0.0.1:1024. +[blaise:server:(23608) 4.482905] test.c:15: [test/CRITICAL] Argh, killed by 127.0.0.1:1024! Bye folks... +[blaise:server:(23608) 4.482930] gras/gras.c:79: [gras/INFO] Exiting GRAS +[blaise:client:(23611) 0.000005] test.c:99: [test/INFO] Programming the repetitive action with a frequency of 0.5 sec +[blaise:client:(23611) 0.000071] test.c:102: [test/INFO] Programming the delayed action in 5 secs +[blaise:client:(23611) 0.518553] test.c:66: [test/INFO] Hello sent to server +[blaise:client:(23611) 1.020515] test.c:66: [test/INFO] Hello sent to server +[blaise:client:(23611) 1.523454] test.c:66: [test/INFO] Hello sent to server +[blaise:client:(23611) 2.027895] test.c:66: [test/INFO] Hello sent to server +[blaise:client:(23611) 2.530948] test.c:66: [test/INFO] Hello sent to server +[blaise:client:(23611) 3.035530] test.c:66: [test/INFO] Hello sent to server +[blaise:client:(23611) 3.538912] test.c:66: [test/INFO] Hello sent to server +[blaise:client:(23611) 4.038965] test.c:66: [test/INFO] Hello sent to server +[blaise:client:(23611) 4.542989] test.c:66: [test/INFO] Hello sent to server +[blaise:client:(23611) 5.003026] test.c:73: [test/INFO] Kill sent to server +[blaise:client:(23611) 5.003056] test.c:78: [test/INFO] Break the client's while loop +[blaise:client:(23611) 5.003071] gras/gras.c:79: [gras/INFO] Exiting GRAS +$ +$ ./test_simulator platform.xml test.xml +[Boivin:client:(2) 0.000000] test.c:99: [test/INFO] Programming the repetitive action with a frequency of 0.5 sec +[Boivin:client:(2) 0.000000] test.c:102: [test/INFO] Programming the delayed action in 5 secs +[Boivin:client:(2) 0.500537] test.c:66: [test/INFO] Hello sent to server +[Jacquelin:server:(1) 0.500537] test.c:26: [test/INFO] Cool, we received the message from Boivin:1024. +[Boivin:client:(2) 1.000537] test.c:66: [test/INFO] Hello sent to server +[Jacquelin:server:(1) 1.000537] test.c:26: [test/INFO] Cool, we received the message from Boivin:1024. +[Boivin:client:(2) 1.500537] test.c:66: [test/INFO] Hello sent to server +[Jacquelin:server:(1) 1.500537] test.c:26: [test/INFO] Cool, we received the message from Boivin:1024. +[Boivin:client:(2) 2.000537] test.c:66: [test/INFO] Hello sent to server +[Jacquelin:server:(1) 2.000537] test.c:26: [test/INFO] Cool, we received the message from Boivin:1024. +[Boivin:client:(2) 2.500537] test.c:66: [test/INFO] Hello sent to server +[Jacquelin:server:(1) 2.500537] test.c:26: [test/INFO] Cool, we received the message from Boivin:1024. +[Boivin:client:(2) 3.000537] test.c:66: [test/INFO] Hello sent to server +[Jacquelin:server:(1) 3.000537] test.c:26: [test/INFO] Cool, we received the message from Boivin:1024. +[Boivin:client:(2) 3.500537] test.c:66: [test/INFO] Hello sent to server +[Jacquelin:server:(1) 3.500537] test.c:26: [test/INFO] Cool, we received the message from Boivin:1024. +[Boivin:client:(2) 4.000537] test.c:66: [test/INFO] Hello sent to server +[Jacquelin:server:(1) 4.000537] test.c:26: [test/INFO] Cool, we received the message from Boivin:1024. +[Boivin:client:(2) 4.500537] test.c:66: [test/INFO] Hello sent to server +[Jacquelin:server:(1) 4.500537] test.c:26: [test/INFO] Cool, we received the message from Boivin:1024. +[Boivin:client:(2) 5.000537] test.c:66: [test/INFO] Hello sent to server +[Jacquelin:server:(1) 5.000537] test.c:26: [test/INFO] Cool, we received the message from Boivin:1024. +[Boivin:client:(2) 5.001074] test.c:73: [test/INFO] Kill sent to server +[Boivin:client:(2) 5.001074] test.c:78: [test/INFO] Break the client's while loop +[Boivin:client:(2) 5.001074] gras/gras.c:79: [gras/INFO] Exiting GRAS +[Jacquelin:server:(1) 5.001074] test.c:15: [test/CRITICAL] Argh, killed by Boivin:1024! Bye folks... +[Jacquelin:server:(1) 5.001074] gras/gras.c:79: [gras/INFO] Exiting GRAS +[5.001074] msg/global.c:475: [msg_kernel/INFO] Congratulations ! Simulation terminated : all processes are over +$ diff --git a/doc/gtut-files/Makefile b/doc/gtut-files/Makefile index ca8f5e7bf3..1331e0db73 100644 --- a/doc/gtut-files/Makefile +++ b/doc/gtut-files/Makefile @@ -2,7 +2,7 @@ export LD_LIBRARY_PATH=$(GRAS_ROOT)/lib all: 1-bones.output 2-simple.output 3-args.output 4-callback.output \ - 5-globals.output 6-logs.output + 5-globals.output 6-logs.output 7-timers.output veryclean: clean rm *.output* @@ -167,3 +167,28 @@ clean:: if [ -e 6-logs.mk ] ; then make -f 6-logs.mk clean; fi rm -f _6-logs_client.c _6-logs_server.c _6-logs_simulator.c 6-logs.trace 6-logs.mk + +# Lesson 7: timers +######################################## + +7-timers.output: 7-timers_client 7-timers_server 7-timers_simulator + echo '$$ ./test_server 12345 & ./test_client 127.0.0.1 12345' > $@ + ./7-timers_server 12345 2>&1 |sed s/7-timers/test/ >> $@ 2>&1& + ./7-timers_client 127.0.0.1 12345 2>&1 |sed s/7-timers/test/ >> $@ 2>&1 + sleep 1 + echo '$$' >> $@ + echo '$$ ./test_simulator platform.xml test.xml' >> $@ + ./7-timers_simulator gtut-platform.xml 3-args.xml 2>&1 |sed s/7-timers/test/ >> $@ 2>&1 + echo '$$' >> $@ + killall 7-timers_server 7-timers_client 2>/dev/null || true + +7-timers_client 7-timers_server 7-timers_simulator: _7-timers_client.c _7-timers_server.c _7-timers_simulator.c + make -f 7-timers.mk + +_7-timers_client.c _7-timers_server.c _7-timers_simulator.c: 7-timers.c 3-args.xml + ../../tools/gras/gras_stub_generator 7-timers 3-args.xml >/dev/null + +clean:: + if [ -e 7-timers.mk ] ; then make -f 7-timers.mk clean; fi + rm -f _7-timers_client.c _7-timers_server.c _7-timers_simulator.c 7-timers.trace 7-timers.mk + diff --git a/doc/gtut-main.doc b/doc/gtut-main.doc index 6ffeaba07f..cf49a72b89 100644 --- a/doc/gtut-main.doc +++ b/doc/gtut-main.doc @@ -22,6 +22,8 @@ This section constitutes a tutorial to the GRAS programming environment. - \ref GRAS_tut_tour_callbacks - \ref GRAS_tut_tour_globals - \ref GRAS_tut_tour_logs + - \ref GRAS_tut_tour_timers + - \ref GRAS_tut_tour_exceptions \htmlonly \endhtmlonly - \ref GRAS_tut_tour_install @@ -23,6 +24,7 @@ all features available in GRAS. - \ref GRAS_tut_tour_callbacks - \ref GRAS_tut_tour_globals - \ref GRAS_tut_tour_logs + - \ref GRAS_tut_tour_timers
@@ -32,9 +34,8 @@ all features available in GRAS. Unfortunately, the tour is not terminated yet, and here are some ideas of missing missi^W lessons: -- Lesson 5: Globals (for a kill message) -- Lesson 6: Timers -- Lesson 7: Using logs +- Lesson 8: Timers +- Lesson 7: Exceptions - Lesson 8: Exchanging simple data through ping-pong - Lesson 9: More complex data description (automatic parsing, manual description) and example -- 2.20.1