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 */
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15 "Messages specific for this msg example");
19 static double parse_double(const char *string) {
23 value=strtod(string, &endptr);
25 THROW1(unknown_error, 0, "%s is not a double", string);
30 static void send(xbt_dynar_t action)
32 char *name = xbt_str_join(action, " ");
33 char *to = xbt_dynar_get_as(action, 2, char *);
34 char *size = xbt_dynar_get_as(action, 3, char *);
35 INFO2("Send: %s (size: %lg)", name, parse_double(size));
36 MSG_task_send(MSG_task_create(name, 0, parse_double(size), NULL), to);
37 INFO1("Sent %s", name);
41 static void recv(xbt_dynar_t action)
43 char *name = xbt_str_join(action, " ");
45 INFO1("Receiving: %s", name);
46 //FIXME: argument of action ignored so far; semantic not clear
47 //char *from=xbt_dynar_get_as(action,2,char*);
48 MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
49 INFO1("Received %s", MSG_task_get_name(task));
50 MSG_task_destroy(task);
54 static int spawned_recv(int argc, char *argv[])
57 char* name = (char *) MSG_process_get_data(MSG_process_self());
58 INFO1("Receiving on %s", name);
59 MSG_task_receive(&task, name);
60 INFO1("Received %s", MSG_task_get_name(task));
61 MSG_task_send(MSG_task_create("waiter",0,0,NULL),MSG_process_self()->name);
63 MSG_task_destroy(task);
67 static void Irecv(xbt_dynar_t action)
69 char *name = xbt_str_join(action, " ");
70 m_process_t comm_helper;
72 INFO1("Irecv on %s: spawn process ",
73 MSG_process_get_name(MSG_process_self()));
75 sprintf(name,"%s_wait",MSG_process_self()->name);
76 comm_helper = MSG_process_create(name,spawned_recv,
77 (void *) MSG_process_get_name(MSG_process_self()),
84 static void wait(xbt_dynar_t action)
86 char *name = xbt_str_join(action, " ");
90 INFO1("wait: %s", name);
91 sprintf(task_name,"%s_wait",MSG_process_self()->name);
92 MSG_task_receive(&task,task_name);
93 INFO1("waited: %s", name);
97 static void sleep(xbt_dynar_t action)
99 char *name = xbt_str_join(action, " ");
100 char *duration = xbt_dynar_get_as(action, 2, char *);
101 INFO1("sleep: %s", name);
102 MSG_process_sleep(parse_double(duration));
103 INFO1("sleept: %s", name);
107 static void compute(xbt_dynar_t action)
109 char *name = xbt_str_join(action, " ");
110 char *amout = xbt_dynar_get_as(action, 2, char *);
111 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
112 INFO1("computing: %s", name);
113 MSG_task_execute(task);
114 MSG_task_destroy(task);
115 INFO1("computed: %s", name);
120 int main(int argc, char *argv[])
122 MSG_error_t res = MSG_OK;
124 /* Check the given arguments */
125 MSG_global_init(&argc, argv);
127 printf("Usage: %s platform_file deployment_file action_files\n", argv[0]);
128 printf("example: %s msg_platform.xml msg_deployment.xml actions\n",
133 /* Simulation setting */
134 MSG_create_environment(argv[1]);
136 /* No need to register functions as in classical MSG programs: the actions get started anyway */
137 MSG_launch_application(argv[2]);
139 /* Action registration */
140 MSG_action_register("send", send);
141 MSG_action_register("recv", recv);
142 MSG_action_register("Irecv", Irecv);
143 MSG_action_register("wait", wait);
144 MSG_action_register("sleep", sleep);
145 MSG_action_register("compute", compute);
147 /* Actually do the simulation using MSG_action_trace_run */
148 res = MSG_action_trace_run(argv[3]);
150 INFO1("Simulation time %g", MSG_get_clock());