Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix: correct trace mask checking
[simgrid.git] / src / cxx / MsgApplication.cxx
1 /*
2  * Application.cxx
3  *
4  * Copyright 2006,2007 Martin Quinson, Malek Cherier           
5  * All right reserved. 
6  *
7  * This program is free software; you can redistribute 
8  * it and/or modify it under the terms of the license 
9  *(GNU LGPL) which comes with this package. 
10  *
11  */
12  
13  /* Application member functions implementation.
14   */  
15
16 #include <MsgApplicationHandler.hpp>
17
18 #include <MsgApplication.hpp>
19
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <stdlib.h>
23
24 #include <surf/surfxml_parse.h>
25
26 #ifndef S_ISREG
27         #define S_ISREG(__mode) (((__mode) & S_IFMT) == S_IFREG)
28 #endif
29
30 namespace SimGrid
31 {
32         namespace Msg
33         {
34         
35                 Application::Application()
36                 {
37                         this->file = NULL;
38                         this->deployed = false;
39                 }
40                                 
41                 Application::Application(const Application& rApplication)
42                 {
43                                 
44                         this->file = rApplication.getFile();
45                         this->deployed = rApplication.isDeployed();
46                 }
47         
48                 Application::Application(const char* file)
49                 throw(NullPointerException, FileNotFoundException)
50                 {
51                         // check parameters
52                         
53                         if(!file)
54                                 throw NullPointerException("file");
55                         
56                         struct stat statBuf = {0};
57                                 
58                         if(stat(file, &statBuf) < 0 || !S_ISREG(statBuf.st_mode))
59                                 throw FileNotFoundException(file);
60                                 
61                         this->file = file;
62                         this->deployed = false;
63                 }
64                 
65                 Application::~Application()
66                 {
67                         // NOTHING TODO
68                 }
69                         
70                 void Application::deploy(const char* file)
71                 throw(NullPointerException, FileNotFoundException, LogicException, MsgException)
72                 {
73                         // check logic
74                         
75                         if(this->deployed)
76                                 throw LogicException("application already deployed");
77                         
78                         // check the parameters
79                                 
80                         if(!file)
81                                 throw NullPointerException("file");
82                         
83                         struct stat statBuf = {0};
84                                 
85                         if(stat(file, &statBuf) < 0 || !S_ISREG(statBuf.st_mode))
86                                 throw FileNotFoundException(file);
87                                         
88                         surf_parse_reset_parser();
89                         
90                         // set the begin of the xml process element handler
91                         surfxml_add_callback(STag_surfxml_process_cb_list, ApplicationHandler::onBeginProcess);
92                                 
93                         // set the process arg handler
94                         surfxml_add_callback(ETag_surfxml_argument_cb_list, ApplicationHandler::onProcessArg);
95                                 
96                         // set the properties handler
97                         surfxml_add_callback(STag_surfxml_prop_cb_list, ApplicationHandler::OnProperty);
98                                 
99                         // set the end of the xml process element handler
100                         surfxml_add_callback(ETag_surfxml_process_cb_list, ApplicationHandler::onEndProcess);
101
102                         surf_parse_open(file);
103                         
104                         // initialize the process factory used by the process handler to build the processes.
105                         ApplicationHandler::onStartDocument();
106                                 
107                         if(surf_parse())
108                                 throw MsgException("surf_parse() failed");
109                         
110                         surf_parse_close();
111                         
112                         // release the process factory
113                         ApplicationHandler::onEndDocument();    
114                         
115                         this->file = file;
116                         this->deployed = true;
117                 }
118                 
119                 void Application::deploy(void)
120                 throw(LogicException, MsgException)
121                 {
122                         // check logic
123                         
124                         if(this->deployed)
125                                 throw LogicException("application already deployed");
126                         
127                         // check the parameters
128                         if(!this->file)
129                                 throw LogicException("you must specify the xml file which describe the application\nuse Application::setFile()"); 
130                         
131                         surf_parse_reset_parser();
132                         surfxml_add_callback(STag_surfxml_process_cb_list, ApplicationHandler::onBeginProcess);
133                         surfxml_add_callback(ETag_surfxml_argument_cb_list, ApplicationHandler::onProcessArg);
134                         surfxml_add_callback(STag_surfxml_prop_cb_list, ApplicationHandler::OnProperty);
135                         surfxml_add_callback(ETag_surfxml_process_cb_list, ApplicationHandler::onEndProcess);
136                         
137                         // initialize the process factory used by the process handler to build the processes.
138                         ApplicationHandler::onStartDocument();
139
140                         surf_parse_open(file);
141                         
142                         if(surf_parse())
143                                 throw MsgException("surf_parse() failed");
144                         
145                         surf_parse_close();
146                         
147                         this->deployed = true;  
148                 }
149                 
150                 bool Application::isDeployed(void) const
151                 {
152                         return this->deployed;
153                 }
154                 
155                 void Application::setFile(const char* file)
156                 throw (NullPointerException, FileNotFoundException, LogicException)
157                 {
158                         // check logic
159                         
160                         if(this->deployed)
161                                 throw LogicException("your are trying to change the file of an already deployed application");
162                                 
163                         // check parameters
164                         
165                         if(!file)
166                                 throw NullPointerException("file");
167                         
168                         struct stat statBuf = {0};
169                                 
170                         if(stat(file, &statBuf) < 0 || !S_ISREG(statBuf.st_mode))
171                                 throw FileNotFoundException("file (file not found)");
172                                 
173                         this->file = file;
174                         
175                 }
176                 
177                 const char* Application::getFile(void) const
178                 {
179                         return this->file;
180                 }
181                 
182                 const Application& Application::operator = (const Application& rApplication)
183                 throw(LogicException)
184                 {
185                         // check logic
186                         
187                         if(this->deployed)
188                                 throw LogicException("application already deployed");
189                         
190                         this->file = rApplication.getFile();
191                         this->deployed = rApplication.isDeployed();
192                         
193                         return *this;
194                 }
195         } // namespace Msg
196 } // namespace SimGrid
197
198
199