Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e76db4e1d4d642a1be06478aeb88c48b5cd3735c
[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 <stdlib.h>
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 */
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
14                              "Messages specific for this msg example");
15
16 /* Helper function */
17 static double parse_double(const char *string)
18 {
19   double value;
20   char *endptr;
21
22   value = strtod(string, &endptr);
23   if (*endptr != '\0')
24     THROW1(unknown_error, 0, "%s is not a double", string);
25   return value;
26 }
27
28
29 /* My actions */
30 static void send(xbt_dynar_t action)
31 {
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);
38   free(name);
39 }
40
41 static void recv(xbt_dynar_t action)
42 {
43   char *name = xbt_str_join(action, " ");
44   m_task_t task = NULL;
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);
51   free(name);
52 }
53
54 static void sleep(xbt_dynar_t action)
55 {
56   char *name = xbt_str_join(action, " ");
57   char *duration = xbt_dynar_get_as(action, 2, char *);
58   INFO1("sleep: %s", name);
59   MSG_process_sleep(parse_double(duration));
60   INFO1("sleept: %s", name);
61   free(name);
62 }
63
64 static void compute(xbt_dynar_t action)
65 {
66   char *name = xbt_str_join(action, " ");
67   char *amout = xbt_dynar_get_as(action, 2, char *);
68   m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
69   INFO1("computing: %s", name);
70   MSG_task_execute(task);
71   MSG_task_destroy(task);
72   INFO1("computed: %s", name);
73   free(name);
74 }
75
76 /** Main function */
77 int main(int argc, char *argv[])
78 {
79   MSG_error_t res = MSG_OK;
80
81   /* Check the given arguments */
82   MSG_global_init(&argc, argv);
83   if (argc < 4) {
84     printf("Usage: %s platform_file deployment_file action_files\n", argv[0]);
85     printf("example: %s msg_platform.xml msg_deployment.xml actions\n",
86            argv[0]);
87     exit(1);
88   }
89
90   /*  Simulation setting */
91   MSG_create_environment(argv[1]);
92
93   /* No need to register functions as in classical MSG programs: the actions get started anyway */
94   MSG_launch_application(argv[2]);
95
96   /*   Action registration */
97   MSG_action_register("send", send);
98   MSG_action_register("recv", recv);
99   MSG_action_register("sleep", sleep);
100   MSG_action_register("compute", compute);
101
102   /* Actually do the simulation using MSG_action_trace_run */
103   res = MSG_action_trace_run(argv[3]);
104
105   INFO1("Simulation time %g", MSG_get_clock());
106   MSG_clean();
107
108   if (res == MSG_OK)
109     return 0;
110   else
111     return 1;
112 }                               /* end_of_main */