Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Lots of renamings from xbt_ to smx_
[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   double value;
19   char *endptr;
20
21   value=strtod(string, &endptr);
22   if (*endptr != '\0')
23           THROW1(unknown_error, 0, "%s is not a double", string);
24   return value;
25 }
26
27
28 /* Helper function */
29 static double parse_double(const char *string)
30 {
31   double value;
32   char *endptr;
33
34   value = strtod(string, &endptr);
35   if (*endptr != '\0')
36     THROW1(unknown_error, 0, "%s is not a double", string);
37   return value;
38 }
39
40
41 /* My actions */
42 static void send(xbt_dynar_t action)
43 {
44   char *name = xbt_str_join(action, " ");
45   char *to = xbt_dynar_get_as(action, 2, char *);
46   char *size = xbt_dynar_get_as(action, 3, char *);
47   INFO2("Send: %s (size: %lg)", name, parse_double(size));
48   MSG_task_send(MSG_task_create(name, 0, parse_double(size), NULL), to);
49   INFO1("Sent %s", name);
50   free(name);
51 }
52
53 static void recv(xbt_dynar_t action)
54 {
55   char *name = xbt_str_join(action, " ");
56   m_task_t task = NULL;
57   INFO1("Receiving: %s", name);
58   //FIXME: argument of action ignored so far; semantic not clear
59   //char *from=xbt_dynar_get_as(action,2,char*);
60   MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
61   INFO1("Received %s", MSG_task_get_name(task));
62   MSG_task_destroy(task);
63   free(name);
64 }
65
66 static void sleep(xbt_dynar_t action)
67 {
68   char *name = xbt_str_join(action, " ");
69   char *duration = xbt_dynar_get_as(action, 2, char *);
70   INFO1("sleep: %s", name);
71   MSG_process_sleep(parse_double(duration));
72   INFO1("sleept: %s", name);
73   free(name);
74 }
75
76 static void compute(xbt_dynar_t action)
77 {
78   char *name = xbt_str_join(action, " ");
79   char *amout = xbt_dynar_get_as(action, 2, char *);
80   m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
81   INFO1("computing: %s", name);
82   MSG_task_execute(task);
83   MSG_task_destroy(task);
84   INFO1("computed: %s", name);
85   free(name);
86 }
87
88 /** Main function */
89 int main(int argc, char *argv[])
90 {
91   MSG_error_t res = MSG_OK;
92
93   /* Check the given arguments */
94   MSG_global_init(&argc, argv);
95   if (argc < 4) {
96     printf("Usage: %s platform_file deployment_file action_files\n", argv[0]);
97     printf("example: %s msg_platform.xml msg_deployment.xml actions\n",
98            argv[0]);
99     exit(1);
100   }
101
102   /*  Simulation setting */
103   MSG_create_environment(argv[1]);
104
105   /* No need to register functions as in classical MSG programs: the actions get started anyway */
106   MSG_launch_application(argv[2]);
107
108   /*   Action registration */
109   MSG_action_register("send", send);
110   MSG_action_register("recv", recv);
111   MSG_action_register("sleep", sleep);
112   MSG_action_register("compute", compute);
113
114   /* Actually do the simulation using MSG_action_trace_run */
115   res = MSG_action_trace_run(argv[3]);
116
117   INFO1("Simulation time %g", MSG_get_clock());
118   MSG_clean();
119
120   if (res == MSG_OK)
121     return 0;
122   else
123     return 1;
124 }                               /* end_of_main */