Logo AND Algorithmique Numérique Distribuée

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