Logo AND Algorithmique Numérique Distribuée

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