Logo AND Algorithmique Numérique Distribuée

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