Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reindent everything (possibly breaking all branches, but for the last time)
[simgrid.git] / examples / msg / actions / actions.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2009. The SimGrid team. All rights reserved.               */
4
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. */
7
8 #include <stdio.h>
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 */
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
13                              "Messages specific for this msg example");
14
15 /* My actions */
16 static void send(xbt_dynar_t action)
17 {
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);
24   free(name);
25 }
26
27 static void recv(xbt_dynar_t action)
28 {
29   char *name = xbt_str_join(action, " ");
30   m_task_t task = NULL;
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);
37   free(name);
38 }
39
40 static void sleep(xbt_dynar_t action)
41 {
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);
47   free(name);
48 }
49
50 static void compute(xbt_dynar_t action)
51 {
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);
59   free(name);
60 }
61
62 /** Main function */
63 int main(int argc, char *argv[])
64 {
65   MSG_error_t res = MSG_OK;
66
67   /* Check the given arguments */
68   MSG_global_init(&argc, argv);
69   if (argc < 4) {
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",
72            argv[0]);
73     exit(1);
74   }
75
76   /*  Simulation setting */
77   MSG_create_environment(argv[1]);
78
79   /* No need to register functions as in classical MSG programs: the actions get started anyway */
80   MSG_launch_application(argv[2]);
81
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);
87
88   /* Actually do the simulation using MSG_action_trace_run */
89   res = MSG_action_trace_run(argv[3]);
90
91   INFO1("Simulation time %g", MSG_get_clock());
92   MSG_clean();
93
94   if (res == MSG_OK)
95     return 0;
96   else
97     return 1;
98 }                               /* end_of_main */