Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Plug a bunch of memleaks
[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,"Messages specific for this msg example");
13
14 /* My actions */
15 static void send(xbt_dynar_t action) {
16         char *name=xbt_str_join(action," ");
17         char *to = xbt_dynar_get_as(action,2,char*);
18         char *size=xbt_dynar_get_as(action,3,char*);
19         INFO1("Send: %s",name);
20         MSG_task_send(MSG_task_create(name, 0, atoi(size), NULL), to);
21         INFO1("Sent %s",name);
22    free(name);
23 }
24 static void recv(xbt_dynar_t action) {
25         char *name=xbt_str_join(action," ");
26         m_task_t task = NULL;
27         INFO1("Receiving: %s",name);
28         //FIXME: argument of action ignored so far; semantic not clear
29         //char *from=xbt_dynar_get_as(action,2,char*);
30         MSG_task_receive(&task,MSG_process_get_name(MSG_process_self()));
31         INFO1("Received %s",MSG_task_get_name(task));
32         MSG_task_destroy(task);
33    free(name);
34 }
35 static void sleep(xbt_dynar_t action) {
36         char *name=xbt_str_join(action," ");
37         char *duration=xbt_dynar_get_as(action,2,char*);
38         INFO1("sleep: %s",name);
39         MSG_process_sleep(atoi(duration));
40         INFO1("sleept: %s",name);
41    free(name);
42 }
43 static void compute(xbt_dynar_t action) {
44         char *name=xbt_str_join(action," ");
45         char *amout=xbt_dynar_get_as(action,2,char*);
46         m_task_t task = MSG_task_create(name, atoi(amout), 0, NULL);
47         INFO1("computing: %s",name);
48         MSG_task_execute(task);
49         MSG_task_destroy(task);
50         INFO1("computed: %s",name);
51    free(name);
52 }
53
54 /** Main function */
55 int main(int argc, char *argv[]){
56         MSG_error_t res = MSG_OK;
57
58         /* Check the given arguments */
59         MSG_global_init(&argc,argv);
60         if (argc < 4) {
61                 printf ("Usage: %s platform_file deployment_file action_files\n",argv[0]);
62                 printf ("example: %s msg_platform.xml msg_deployment.xml actions\n",argv[0]);
63                 exit(1);
64         }
65
66         /*  Simulation setting */
67         MSG_create_environment(argv[1]);
68
69         /* No need to register functions as in classical MSG programs: the actions get started anyway */
70         MSG_launch_application(argv[2]);
71
72         /*   Action registration */
73         MSG_action_register("send", send);
74         MSG_action_register("recv", recv);
75         MSG_action_register("sleep", sleep);
76         MSG_action_register("compute", compute);
77
78         /* Actually do the simulation using MSG_action_trace_run */
79         res = MSG_action_trace_run(argv[3]);
80
81         INFO1("Simulation time %g",MSG_get_clock());
82         MSG_clean();
83
84         if(res==MSG_OK)
85                 return 0;
86         else
87                 return 1;
88 } /* end_of_main */