2 * ApplicationHandler.cxx
\r
4 * Copyright 2006,2007 Martin Quinson, Malek Cherier
\r
5 * All right reserved.
\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
13 /* ApplicationHandler member functions implementation.
\r
17 #include <Object.hpp>
\r
18 #include <ApplicationHandler.hpp>
\r
20 #include <Process.hpp>
\r
29 #include <surf/surfxml_parse.h>
\r
30 #include <xbt/sysdep.h>
\r
38 ApplicationHandler::ProcessFactory* ApplicationHandler::processFactory = NULL;
\r
40 // Desable the default constructor, the copy constructor , the assignement operator
\r
41 // and the destructor of this class. Assume that this class is static.
\r
43 // Default constructor.
\r
44 ApplicationHandler::ApplicationHandler()
\r
49 // Copy constructor.
\r
50 ApplicationHandler::ApplicationHandler(const ApplicationHandler& rApplicationHandler)
\r
56 ApplicationHandler::~ApplicationHandler()
\r
61 // Assignement operator.
\r
62 const ApplicationHandler& ApplicationHandler::operator = (const ApplicationHandler& rApplicationHandler)
\r
67 void ApplicationHandler::onStartDocument(void)
\r
69 // instanciate the factory at the begining of the parsing
\r
70 processFactory = new ProcessFactory();
\r
73 void ApplicationHandler::onEndDocument(void)
\r
75 // release the handler at the end of the parsing.
\r
77 delete processFactory;
\r
80 void ApplicationHandler::onBeginProcess(void)
\r
82 // set the process identity at the begin of the xml process element.
\r
83 processFactory->setProcessIdentity(A_surfxml_process_host, A_surfxml_process_function);
\r
86 void ApplicationHandler::onProcessArg(void)
\r
88 // register the argument of the current xml process element.
\r
89 processFactory->registerProcessArg(A_surfxml_argument_value);
\r
92 void ApplicationHandler::OnProperty(void)
\r
94 // set the property of the current xml process element.
\r
95 processFactory->setProperty(A_surfxml_prop_id, A_surfxml_prop_value);
\r
98 void ApplicationHandler::onEndProcess(void)
\r
100 // at the end of the xml process element create the wrapper process (of the native Msg process)
\r
101 processFactory->createProcess();
\r
104 /////////////////////////////////////////////////////////////////////////////////////////////////
\r
105 // Process factory connected member functions
\r
107 ApplicationHandler::ProcessFactory::ProcessFactory()
\r
109 this->args = xbt_dynar_new(sizeof(char*),ApplicationHandler::ProcessFactory::freeCstr);
\r
110 this->properties = NULL; // TODO instanciate the dictionary
\r
111 this->hostName = NULL;
\r
112 this->function = NULL;
\r
115 ApplicationHandler::ProcessFactory::~ProcessFactory()
\r
117 xbt_dynar_free(&(this->args));
\r
120 // create the cxx process wrapper.
\r
121 void ApplicationHandler::ProcessFactory::createProcess()
\r
122 throw (ClassNotFoundException, HostNotFoundException)
\r
128 // try to dynamicaly create an instance fo the process from its name (which is specified by the element function
\r
129 // in the xml application file.
\r
130 // if this static method fails, it throws an exception of the class ClassNotFoundException
\r
131 c = Class::fromName(this->function);
\r
132 process = reinterpret_cast<Process*>(c->createObject());
\r
134 // try to retrieve the host of the process from its name
\r
135 // if this method fails, it throws an exception of the class HostNotFoundException
\r
136 host = Host::getByName(this->hostName);
\r
138 // build the list of the arguments of the newly created process.
\r
139 int argc = xbt_dynar_length(this->args);
\r
141 char** argv = (char**)calloc(argc, sizeof(char*));
\r
143 for(int i = argc -1; i >= 0; i--)
\r
144 xbt_dynar_pop(this->args, &(argv[i]));
\r
146 // finaly create the process (for more detail on the process creation see Process::create()
\r
147 process->create(host, this->function , argc, argv);
\r
149 // TODO add the properties of the process
\r
150 /*process->properties = this->properties;
\r
151 this->properties = new Properties();*/
\r
154 void ApplicationHandler::ProcessFactory::setProcessIdentity(const char* hostName, const char* function)
\r
156 this->hostName = hostName;
\r
157 this->function = function;
\r
159 /*if (!this->args->empty())
\r
160 this->args->clear();
\r
162 if(!this->properties->empty())
\r
163 this->properties->clear();*/
\r
166 // callback function used by the dynamic array to cleanup all of its elements.
\r
167 void ApplicationHandler::ProcessFactory::freeCstr(void* cstr)
\r
169 free(*(void**)cstr);
\r
172 void ApplicationHandler::ProcessFactory::registerProcessArg(const char* arg)
\r
174 char* cstr = _strdup(arg);
\r
175 xbt_dynar_push(this->args, &cstr);
\r
178 void ApplicationHandler::ProcessFactory::setProperty(const char* id, const char* value)
\r
180 // TODO implement this function;
\r
183 const char* ApplicationHandler::ProcessFactory::getHostName(void)
\r
185 return this->hostName;
\r
189 } // namespace SimGrid
\r