Logo AND Algorithmique Numérique Distribuée

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