3 /* Copyright (c) 2009. The SimGrid team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
11 #include "xbt.h" /* calloc, printf */
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
14 "Messages specific for this msg example");
17 static double parse_double(const char *string) {
21 value=strtod(string, &endptr);
23 THROW1(unknown_error, 0, "%s is not a double", string);
28 static void send(xbt_dynar_t action)
30 char *name = xbt_str_join(action, " ");
31 char *to = xbt_dynar_get_as(action, 2, char *);
32 char *size = xbt_dynar_get_as(action, 3, char *);
33 INFO2("Send: %s (size: %lg)", name, parse_double(size));
34 MSG_task_send(MSG_task_create(name, 0, parse_double(size), NULL), to);
35 INFO1("Sent %s", name);
39 static void recv(xbt_dynar_t action)
41 char *name = xbt_str_join(action, " ");
43 INFO1("Receiving: %s", name);
44 //FIXME: argument of action ignored so far; semantic not clear
45 //char *from=xbt_dynar_get_as(action,2,char*);
46 MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
47 INFO1("Received %s", MSG_task_get_name(task));
48 MSG_task_destroy(task);
52 static void sleep(xbt_dynar_t action)
54 char *name = xbt_str_join(action, " ");
55 char *duration = xbt_dynar_get_as(action, 2, char *);
56 INFO1("sleep: %s", name);
57 MSG_process_sleep(parse_double(duration));
58 INFO1("sleept: %s", name);
62 static void compute(xbt_dynar_t action)
64 char *name = xbt_str_join(action, " ");
65 char *amout = xbt_dynar_get_as(action, 2, char *);
66 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
67 INFO1("computing: %s", name);
68 MSG_task_execute(task);
69 MSG_task_destroy(task);
70 INFO1("computed: %s", name);
75 int main(int argc, char *argv[])
77 MSG_error_t res = MSG_OK;
79 /* Check the given arguments */
80 MSG_global_init(&argc, argv);
82 printf("Usage: %s platform_file deployment_file action_files\n", argv[0]);
83 printf("example: %s msg_platform.xml msg_deployment.xml actions\n",
88 /* Simulation setting */
89 MSG_create_environment(argv[1]);
91 /* No need to register functions as in classical MSG programs: the actions get started anyway */
92 MSG_launch_application(argv[2]);
94 /* Action registration */
95 MSG_action_register("send", send);
96 MSG_action_register("recv", recv);
97 MSG_action_register("sleep", sleep);
98 MSG_action_register("compute", compute);
100 /* Actually do the simulation using MSG_action_trace_run */
101 res = MSG_action_trace_run(argv[3]);
103 INFO1("Simulation time %g", MSG_get_clock());