From 9ba32e9e4cbb147b495d1c59711463c0b28c8a79 Mon Sep 17 00:00:00 2001 From: coldpeace Date: Wed, 11 Aug 2010 13:01:53 +0000 Subject: [PATCH] add gras console example, illustrating the use of lua gras generator to setup the project and lua console to bypass the parser git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@8156 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- buildtools/Cmake/MakeExeLib.cmake | 3 + examples/gras/console/CMakeLists.txt | 33 ++++++ .../gras/console/gras_platform_script.lua | 49 ++++++++ examples/gras/console/ping.h | 21 ++++ examples/gras/console/ping_client.c | 91 +++++++++++++++ examples/gras/console/ping_common.c | 18 +++ examples/gras/console/ping_generator.lua | 9 ++ examples/gras/console/ping_server.c | 105 ++++++++++++++++++ 8 files changed, 329 insertions(+) create mode 100644 examples/gras/console/CMakeLists.txt create mode 100644 examples/gras/console/gras_platform_script.lua create mode 100644 examples/gras/console/ping.h create mode 100644 examples/gras/console/ping_client.c create mode 100644 examples/gras/console/ping_common.c create mode 100644 examples/gras/console/ping_generator.lua create mode 100644 examples/gras/console/ping_server.c diff --git a/buildtools/Cmake/MakeExeLib.cmake b/buildtools/Cmake/MakeExeLib.cmake index e6393a808c..9bd1c38d2c 100644 --- a/buildtools/Cmake/MakeExeLib.cmake +++ b/buildtools/Cmake/MakeExeLib.cmake @@ -53,9 +53,11 @@ if(HAVE_LUA) OUTPUT ${PROJECT_DIRECTORY}/examples/lua/simgrid.so OUTPUT ${PROJECT_DIRECTORY}/examples/msg/masterslave/simgrid.so OUTPUT ${PROJECT_DIRECTORY}/examples/simdag/simgrid.so + OUTPUT ${PROJECT_DIRECTOTY}/examples/gras/console/simgrid.so COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_BINARY_DIR}/lib/libsimgrid.so ${PROJECT_DIRECTORY}/examples/lua/simgrid.so COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_BINARY_DIR}/lib/libsimgrid.so ${PROJECT_DIRECTORY}/examples/msg/masterslave/simgrid.so COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_BINARY_DIR}/lib/libsimgrid.so ${PROJECT_DIRECTORY}/examples/simdag/simgrid.so + COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_BINARY_DIR}/lib/libsimgrid.so ${PROJECT_DIRECTORY}/examples/gras/console/simgrid.so COMMENT "Generating libsimgrid.so link for binding lua..." ) @@ -135,6 +137,7 @@ add_subdirectory(${PROJECT_DIRECTORY}/examples/gras/all2all) add_subdirectory(${PROJECT_DIRECTORY}/examples/gras/pmm) add_subdirectory(${PROJECT_DIRECTORY}/examples/gras/synchro) add_subdirectory(${PROJECT_DIRECTORY}/examples/gras/properties) +add_subdirectory(${PROJECT_DIRECTORY}/examples/gras/console) add_subdirectory(${PROJECT_DIRECTORY}/examples/msg/properties) add_subdirectory(${PROJECT_DIRECTORY}/examples/msg/actions) diff --git a/examples/gras/console/CMakeLists.txt b/examples/gras/console/CMakeLists.txt new file mode 100644 index 0000000000..fe00ce24fd --- /dev/null +++ b/examples/gras/console/CMakeLists.txt @@ -0,0 +1,33 @@ +if(HAVE_LUA) +cmake_minimum_required(VERSION 2.6) + +set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/_ping_simulator.c + ${CMAKE_CURRENT_BINARY_DIR}/_ping_client.c + ${CMAKE_CURRENT_BINARY_DIR}/_ping_server.c + PROPERTIES GENERATED true) + +set(EXECUTABLE_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}") + +add_executable(ping_simulator_console ${CMAKE_CURRENT_BINARY_DIR}/_ping_simulator.c + ${CMAKE_CURRENT_SOURCE_DIR}/ping_server.c + ${CMAKE_CURRENT_SOURCE_DIR}/ping_client.c + ${CMAKE_CURRENT_SOURCE_DIR}/ping_common.c) +add_executable(ping_client_console ${CMAKE_CURRENT_BINARY_DIR}/_ping_client.c + ${CMAKE_CURRENT_SOURCE_DIR}/ping_client.c + ${CMAKE_CURRENT_SOURCE_DIR}/ping_common.c) +add_executable(ping_server_console ${CMAKE_CURRENT_BINARY_DIR}/_ping_server.c + ${CMAKE_CURRENT_SOURCE_DIR}/ping_server.c + ${CMAKE_CURRENT_SOURCE_DIR}/ping_common.c) + +add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/_ping_simulator.c + ${CMAKE_CURRENT_BINARY_DIR}/_ping_client.c + ${CMAKE_CURRENT_BINARY_DIR}/_ping_server.c + DEPENDS lua ${CMAKE_CURRENT_SOURCE_DIR}/ping_generator.lua + COMMAND lua ${CMAKE_CURRENT_SOURCE_DIR}/ping_generator.lua + ) +### Add definitions for compile +target_link_libraries(ping_simulator_console simgrid pthread m ) +target_link_libraries(ping_client_console gras pthread m ) +target_link_libraries(ping_server_console gras pthread m ) +endif(HAVE_LUA) diff --git a/examples/gras/console/gras_platform_script.lua b/examples/gras/console/gras_platform_script.lua new file mode 100644 index 0000000000..bf295f10d5 --- /dev/null +++ b/examples/gras/console/gras_platform_script.lua @@ -0,0 +1,49 @@ +require "simgrid" + + simgrid.Host.new{id="Tremblay",power=98095000}; + simgrid.Host.new{id="Jupiter",power=76296000}; + simgrid.Host.new{id="Fafard",power=76296000}; + simgrid.Host.new{id="Ginette",power=48492000}; + simgrid.Host.new{id="Bourassa",power=48492000}; + + -- create Links + for i=10,0,-1 do + simgrid.Link.new{id=i,bandwidth=252750+ i*768,latency=0.000270544+i*0.087}; + end + -- simgrid.Route.new(src_id,des_id,links_nb,links_list) + simgrid.Route.new("Tremblay","Jupiter",{"1"}); + simgrid.Route.new("Tremblay","Fafard",{"0","1","2","3","4","8"}); + simgrid.Route.new("Tremblay","Ginette",{"3","4","5"}); + simgrid.Route.new("Tremblay","Bourassa",{"0","1","3","2","4","6","7"}); + + simgrid.Route.new("Jupiter","Tremblay",{"1"}); + simgrid.Route.new("Jupiter","Fafard",{"0","1","2","3","4","8","9"}); + simgrid.Route.new("Jupiter","Ginette",{"3","4","5","9"}); + simgrid.Route.new("Jupiter","Bourassa",{"0","1","2","3","4","6","7","9"}); + + simgrid.Route.new("Fafard","Tremblay",{"0","1","2","3","4","8"}); + simgrid.Route.new("Fafard","Jupiter",{"0","1","2","3","4","8","9"}); + simgrid.Route.new("Fafard","Ginette",{"0","1","2","5","8"}); + simgrid.Route.new("Fafard","Bourassa",{"6","7","8"}); + + simgrid.Route.new("Ginette","Tremblay",{"3","4","5"}); + simgrid.Route.new("Ginette","Jupiter",{"3","4","5","9"}); + simgrid.Route.new("Ginette","Fafard",{"0","1","2","5","8"}); + simgrid.Route.new("Ginette","Bourassa",{"0","1","2","5","6","7"}); + + simgrid.Route.new("Bourassa","Tremblay",{"0","1","3","2","4","6","7"}); + simgrid.Route.new("Bourassa","Jupiter",{"0","1","2","3","4","6","7","9"}); + simgrid.Route.new("Bourassa","Fafard",{"6","7","8"}); + simgrid.Route.new("Bourassa","Ginette",{"0","1","2","5","6","7"}); + + --Save Platform + simgrid.gras_register_platform(); + + --Set Application + simgrid.Host.setFunction("Tremblay","server",{"4000"}); + simgrid.Host.setFunction("Fafard","client",{"Tremblay","4000"}); + + --Save Application + simgrid.gras_register_application(); + + diff --git a/examples/gras/console/ping.h b/examples/gras/console/ping.h new file mode 100644 index 0000000000..ffd57cf893 --- /dev/null +++ b/examples/gras/console/ping.h @@ -0,0 +1,21 @@ +/* ping - ping/pong demo of GRAS features */ + +/* Copyright (c) 2006, 2009, 2010. The SimGrid Team. + * All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#ifndef PING_COMMON_H +#define PING_COMMON_H + +#include "gras.h" + +/* register messages which may be sent (common to client and server) */ +void ping_register_messages(void); + +/* Function prototypes */ +int server(int argc, char *argv[]); +int client(int argc, char *argv[]); + +#endif diff --git a/examples/gras/console/ping_client.c b/examples/gras/console/ping_client.c new file mode 100644 index 0000000000..701499f621 --- /dev/null +++ b/examples/gras/console/ping_client.c @@ -0,0 +1,91 @@ +/* ping - ping/pong demo of GRAS features */ + +/* Copyright (c) 2006, 2007, 2009, 2010. The SimGrid Team. + * All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "ping.h" +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(Ping); + +int client(int argc, char *argv[]) +{ + xbt_ex_t e; + gras_socket_t toserver = NULL; /* peer */ + int connected = 0; + + gras_socket_t from; + int ping, pong; + + const char *host = "127.0.0.1"; + int port = 4000; + + /* 1. Init the GRAS's infrastructure */ + gras_init(&argc, argv); + + /* 2. Get the server's address. The command line override defaults when specified */ + if (argc == 3) { + host = argv[1]; + port = atoi(argv[2]); + } + + INFO2("Launch client (server on %s:%d)", host, port); + + /* 3. Create a socket to speak to the server */ + while (!connected) { + TRY { + toserver = gras_socket_client(host, port); + connected = 1; + } + CATCH(e) { + if (e.category != system_error) + /* dunno what happened, let the exception go through */ + RETHROW0("Unable to connect to the server: %s"); + xbt_ex_free(e); + gras_os_sleep(0.05); + } + } + + INFO2("Connected to %s:%d.", host, port); + + /* 4. Register the messages. + See, it doesn't have to be done completely at the beginning, only before use */ + ping_register_messages(); + + /* 5. Keep the user informed of what's going on */ + INFO2(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<", + gras_socket_peer_name(toserver), gras_socket_peer_port(toserver)); + + /* 6. Prepare and send the ping message to the server */ + ping = 1234; + TRY { + gras_msg_send(toserver, "ping", &ping); + } + CATCH(e) { + gras_socket_close(toserver); + RETHROW0("Failed to send PING to server: %s"); + } + INFO3(">>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<", + ping, + gras_socket_peer_name(toserver), gras_socket_peer_port(toserver)); + + /* 7. Wait for the answer from the server, and deal with issues */ + TRY { + gras_msg_wait(6000, "pong", &from, &pong); + } + CATCH(e) { + gras_socket_close(toserver); + RETHROW0("Why can't I get my PONG message like everyone else: %s"); + } + + /* 8. Keep the user informed of what's going on, again */ + INFO3(">>>>>>>> Got PONG(%d) from %s:%d <<<<<<<<", + pong, gras_socket_peer_name(from), gras_socket_peer_port(from)); + + /* 9. Free the allocated resources, and shut GRAS down */ + gras_socket_close(toserver); + INFO0("Done."); + gras_exit(); + return 0; +} /* end_of_client */ diff --git a/examples/gras/console/ping_common.c b/examples/gras/console/ping_common.c new file mode 100644 index 0000000000..046f81e0f6 --- /dev/null +++ b/examples/gras/console/ping_common.c @@ -0,0 +1,18 @@ +/* ping - ping/pong demo of GRAS features */ + +/* Copyright (c) 2006, 2009, 2010. The SimGrid Team. + * All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "ping.h" +XBT_LOG_NEW_DEFAULT_CATEGORY(Ping, "Messages specific to this example"); + +/* register messages which may be sent (common to client and server) */ +void ping_register_messages(void) +{ + gras_msgtype_declare("ping", gras_datadesc_by_name("int")); + gras_msgtype_declare("pong", gras_datadesc_by_name("int")); + INFO0("Messages registered"); +} diff --git a/examples/gras/console/ping_generator.lua b/examples/gras/console/ping_generator.lua new file mode 100644 index 0000000000..edca5a9ca0 --- /dev/null +++ b/examples/gras/console/ping_generator.lua @@ -0,0 +1,9 @@ +require "simgrid" + + --Set Application + simgrid.gras_set_process_function("Fafard","client",{"Tremblay","4000"}); + simgrid.gras_set_process_function("Tremblay","server",{"4000"}); + --Save Application + simgrid.gras_generate("ping"); + + diff --git a/examples/gras/console/ping_server.c b/examples/gras/console/ping_server.c new file mode 100644 index 0000000000..11a9cccdbe --- /dev/null +++ b/examples/gras/console/ping_server.c @@ -0,0 +1,105 @@ +/* ping - ping/pong demo of GRAS features */ + +/* Copyright (c) 2006, 2007, 2009, 2010. The SimGrid Team. + * All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "ping.h" + +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(Ping); + +/* Global private data */ +typedef struct { + gras_socket_t sock; + int endcondition; +} server_data_t; + + +static int server_cb_ping_handler(gras_msg_cb_ctx_t ctx, void *payload) +{ + + xbt_ex_t e; + /* 1. Get the payload into the msg variable, and retrieve my caller */ + int msg = *(int *) payload; + gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx); + + /* 2. Retrieve the server's state (globals) */ + + server_data_t *globals = (server_data_t *) gras_userdata_get(); + globals->endcondition = 0; + + /* 3. Log which client connected */ + INFO3(">>>>>>>> Got message PING(%d) from %s:%d <<<<<<<<", + msg, + gras_socket_peer_name(expeditor), gras_socket_peer_port(expeditor)); + + /* 4. Change the value of the msg variable */ + msg = 4321; + /* 5. Send it back as payload of a pong message to the expeditor */ + TRY { + gras_msg_send(expeditor, "pong", &msg); + + /* 6. Deal with errors: add some details to the exception */ + } CATCH(e) { + gras_socket_close(globals->sock); + RETHROW0("Unable answer with PONG: %s"); + } + + INFO0(">>>>>>>> Answered with PONG(4321) <<<<<<<<"); + + /* 7. Set the endcondition boolean to true (and make sure the server stops after receiving it). */ + globals->endcondition = 1; + + /* 8. Tell GRAS that we consummed this message */ + return 0; +} /* end_of_server_cb_ping_handler */ + +int server(int argc, char *argv[]) +{ + server_data_t *globals; + + int port = 4000; + + /* 1. Init the GRAS infrastructure and declare my globals */ + gras_init(&argc, argv); + globals = gras_userdata_new(server_data_t); + + /* 2. Get the port I should listen on from the command line, if specified */ + if (argc == 2) { + port = atoi(argv[1]); + } + + INFO1("Launch server (port=%d)", port); + + /* 3. Create my master socket */ + globals->sock = gras_socket_server(port); + + /* 4. Register the known messages. This function is called twice here, but it's because + this file also acts as regression test, no need to do so yourself of course. */ + ping_register_messages(); + ping_register_messages(); /* just to make sure it works ;) */ + + /* 5. Register my callback */ + gras_cb_register("ping", &server_cb_ping_handler); + + INFO1(">>>>>>>> Listening on port %d <<<<<<<<", + gras_socket_my_port(globals->sock)); + globals->endcondition = 0; + + /* 6. Wait up to 20 minutes for an incomming message to handle */ + gras_msg_handle(20.0); + + /* 7. Housekeeping */ + if (!globals->endcondition) + WARN0("An error occured, the endcondition was not set by the callback"); + + /* 8. Free the allocated resources, and shut GRAS down */ + gras_socket_close(globals->sock); + free(globals); + INFO0("Done."); + gras_exit(); + + return 0; +} /* end_of_server */ -- 2.20.1