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,"Messages specific for this msg example");
15 static void send(xbt_dynar_t action) {
16 char *name=xbt_str_join(action," ");
17 char *to = xbt_dynar_get_as(action,2,char*);
18 char *size=xbt_dynar_get_as(action,3,char*);
19 INFO1("Send: %s",name);
20 MSG_task_send(MSG_task_create(name, 0, atoi(size), NULL), to);
21 INFO1("Sent %s",name);
23 static void recv(xbt_dynar_t action) {
25 INFO1("Receiving: %s",xbt_str_join(action," "));
26 //FIXME: argument of action ignored so far; semantic not clear
27 //char *from=xbt_dynar_get_as(action,2,char*);
28 MSG_task_receive(&task,MSG_process_get_name(MSG_process_self()));
29 INFO1("Received %s",MSG_task_get_name(task));
30 MSG_task_destroy(task);
32 static void sleep(xbt_dynar_t action) {
33 char *duration=xbt_dynar_get_as(action,2,char*);
34 INFO1("sleep: %s",xbt_str_join(action," "));
35 MSG_process_sleep(atoi(duration));
36 INFO1("sleept: %s",xbt_str_join(action," "));
38 static void compute(xbt_dynar_t action) {
39 char *name=xbt_str_join(action," ");
40 char *amout=xbt_dynar_get_as(action,2,char*);
41 m_task_t task = MSG_task_create(name, atoi(amout), 0, NULL);
42 INFO1("computing: %s",name);
43 MSG_task_execute(task);
44 MSG_task_destroy(task);
45 INFO1("computed: %s",name);
49 int main(int argc, char *argv[]){
50 MSG_error_t res = MSG_OK;
52 /* Check the given arguments */
53 MSG_global_init(&argc,argv);
55 printf ("Usage: %s platform_file deployment_file action_files\n",argv[0]);
56 printf ("example: %s msg_platform.xml msg_deployment.xml actions\n",argv[0]);
60 /* Simulation setting */
61 MSG_create_environment(argv[1]);
63 /* No need to register functions as in classical MSG programs: the actions get started anyway */
64 MSG_launch_application(argv[2]);
66 /* Action registration */
67 MSG_action_register("send", send);
68 MSG_action_register("recv", recv);
69 MSG_action_register("sleep", sleep);
70 MSG_action_register("compute", compute);
72 /* Actually do the simulation using MSG_action_trace_run */
73 res = MSG_action_trace_run(argv[3]);
75 INFO1("Simulation time %g",MSG_get_clock());