Logo AND Algorithmique Numérique Distribuée

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