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. */
9 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
10 #include "xbt.h" /* calloc, printf */
12 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
13 "Messages specific for this msg example");
16 static void send(xbt_dynar_t action)
18 char *name = xbt_str_join(action, " ");
19 char *to = xbt_dynar_get_as(action, 2, char *);
20 char *size = xbt_dynar_get_as(action, 3, char *);
21 INFO1("Send: %s", name);
22 MSG_task_send(MSG_task_create(name, 0, atoi(size), NULL), to);
23 INFO1("Sent %s", name);
27 static void recv(xbt_dynar_t action)
29 char *name = xbt_str_join(action, " ");
31 INFO1("Receiving: %s", name);
32 //FIXME: argument of action ignored so far; semantic not clear
33 //char *from=xbt_dynar_get_as(action,2,char*);
34 MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
35 INFO1("Received %s", MSG_task_get_name(task));
36 MSG_task_destroy(task);
40 static void sleep(xbt_dynar_t action)
42 char *name = xbt_str_join(action, " ");
43 char *duration = xbt_dynar_get_as(action, 2, char *);
44 INFO1("sleep: %s", name);
45 MSG_process_sleep(atoi(duration));
46 INFO1("sleept: %s", name);
50 static void compute(xbt_dynar_t action)
52 char *name = xbt_str_join(action, " ");
53 char *amout = xbt_dynar_get_as(action, 2, char *);
54 m_task_t task = MSG_task_create(name, atoi(amout), 0, NULL);
55 INFO1("computing: %s", name);
56 MSG_task_execute(task);
57 MSG_task_destroy(task);
58 INFO1("computed: %s", name);
63 int main(int argc, char *argv[])
65 MSG_error_t res = MSG_OK;
67 /* Check the given arguments */
68 MSG_global_init(&argc, argv);
70 printf("Usage: %s platform_file deployment_file action_files\n", argv[0]);
71 printf("example: %s msg_platform.xml msg_deployment.xml actions\n",
76 /* Simulation setting */
77 MSG_create_environment(argv[1]);
79 /* No need to register functions as in classical MSG programs: the actions get started anyway */
80 MSG_launch_application(argv[2]);
82 /* Action registration */
83 MSG_action_register("send", send);
84 MSG_action_register("recv", recv);
85 MSG_action_register("sleep", sleep);
86 MSG_action_register("compute", compute);
88 /* Actually do the simulation using MSG_action_trace_run */
89 res = MSG_action_trace_run(argv[3]);
91 INFO1("Simulation time %g", MSG_get_clock());