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);
29 static double parse_double(const char *string)
34 value = strtod(string, &endptr);
36 THROW1(unknown_error, 0, "%s is not a double", string);
42 static void send(xbt_dynar_t action)
44 char *name = xbt_str_join(action, " ");
45 char *to = xbt_dynar_get_as(action, 2, char *);
46 char *size = xbt_dynar_get_as(action, 3, char *);
47 INFO2("Send: %s (size: %lg)", name, parse_double(size));
48 MSG_task_send(MSG_task_create(name, 0, parse_double(size), NULL), to);
49 INFO1("Sent %s", name);
53 static void recv(xbt_dynar_t action)
55 char *name = xbt_str_join(action, " ");
57 INFO1("Receiving: %s", name);
58 //FIXME: argument of action ignored so far; semantic not clear
59 //char *from=xbt_dynar_get_as(action,2,char*);
60 MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
61 INFO1("Received %s", MSG_task_get_name(task));
62 MSG_task_destroy(task);
66 static void sleep(xbt_dynar_t action)
68 char *name = xbt_str_join(action, " ");
69 char *duration = xbt_dynar_get_as(action, 2, char *);
70 INFO1("sleep: %s", name);
71 MSG_process_sleep(parse_double(duration));
72 INFO1("sleept: %s", name);
76 static void compute(xbt_dynar_t action)
78 char *name = xbt_str_join(action, " ");
79 char *amout = xbt_dynar_get_as(action, 2, char *);
80 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
81 INFO1("computing: %s", name);
82 MSG_task_execute(task);
83 MSG_task_destroy(task);
84 INFO1("computed: %s", name);
89 int main(int argc, char *argv[])
91 MSG_error_t res = MSG_OK;
93 /* Check the given arguments */
94 MSG_global_init(&argc, argv);
96 printf("Usage: %s platform_file deployment_file action_files\n", argv[0]);
97 printf("example: %s msg_platform.xml msg_deployment.xml actions\n",
102 /* Simulation setting */
103 MSG_create_environment(argv[1]);
105 /* No need to register functions as in classical MSG programs: the actions get started anyway */
106 MSG_launch_application(argv[2]);
108 /* Action registration */
109 MSG_action_register("send", send);
110 MSG_action_register("recv", recv);
111 MSG_action_register("sleep", sleep);
112 MSG_action_register("compute", compute);
114 /* Actually do the simulation using MSG_action_trace_run */
115 res = MSG_action_trace_run(argv[3]);
117 INFO1("Simulation time %g", MSG_get_clock());