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
16 #include <ApplicationHandler.hpp>
\r
18 #include <Object.hpp>
\r
19 #include <ClassNotFoundException.hpp>
\r
20 #include <HostNotFoundException.hpp>
\r
22 #include <Process.hpp>
\r
26 #include <surf/surfxml_parse.h>
\r
27 #include <xbt/sysdep.h>
\r
35 ApplicationHandler::ProcessFactory* ApplicationHandler::processFactory = NULL;
\r
37 // Desable the default constructor, the copy constructor , the assignement operator
\r
38 // and the destructor of this class. Assume that this class is static.
\r
40 // Default constructor.
\r
41 ApplicationHandler::ApplicationHandler()
\r
46 // Copy constructor.
\r
47 ApplicationHandler::ApplicationHandler(const ApplicationHandler& rApplicationHandler)
\r
53 ApplicationHandler::~ApplicationHandler()
\r
58 // Assignement operator.
\r
59 const ApplicationHandler& ApplicationHandler::operator = (const ApplicationHandler& rApplicationHandler)
\r
64 void ApplicationHandler::onStartDocument(void)
\r
66 // instanciate the factory at the begining of the parsing
\r
67 processFactory = new ProcessFactory();
\r
70 void ApplicationHandler::onEndDocument(void)
\r
72 // release the handler at the end of the parsing.
\r
74 delete processFactory;
\r
77 void ApplicationHandler::onBeginProcess(void)
\r
79 // set the process identity at the begin of the xml process element.
\r
80 processFactory->setProcessIdentity(A_surfxml_process_host, A_surfxml_process_function);
\r
83 void ApplicationHandler::onProcessArg(void)
\r
85 // register the argument of the current xml process element.
\r
86 processFactory->registerProcessArg(A_surfxml_argument_value);
\r
89 void ApplicationHandler::OnProperty(void)
\r
91 // set the property of the current xml process element.
\r
92 processFactory->setProperty(A_surfxml_prop_id, A_surfxml_prop_value);
\r
95 void ApplicationHandler::onEndProcess(void)
\r
97 // at the end of the xml process element create the wrapper process (of the native Msg process)
\r
98 processFactory->createProcess();
\r
101 /////////////////////////////////////////////////////////////////////////////////////////////////
\r
102 // Process factory connected member functions
\r
104 ApplicationHandler::ProcessFactory::ProcessFactory()
\r
106 this->args = xbt_dynar_new(sizeof(char*),ApplicationHandler::ProcessFactory::freeCstr);
\r
107 this->properties = NULL; // TODO instanciate the dictionary
\r
108 this->hostName = NULL;
\r
109 this->function = NULL;
\r
112 // create the cxx process wrapper.
\r
113 void ApplicationHandler::ProcessFactory::createProcess()
\r
114 throw (ClassNotFoundException, HostNotFoundException)
\r
120 // try to dynamicaly create an instance fo the process from its name (which is specified by the element function
\r
121 // in the xml application file.
\r
122 // if this static method fails, it throws an exception of the class ClassNotFoundException
\r
123 c = Class::fromName(this->function);
\r
124 process = reinterpret_cast<Process*>(c->createObject());
\r
126 // try to retrieve the host of the process from its name
\r
127 // if this method fails, it throws an exception of the class HostNotFoundException
\r
128 host = Host::getByName(this->hostName);
\r
130 // build the list of the arguments of the newly created process.
\r
131 int argc = xbt_dynar_length(this->args);
\r
133 char** argv = (char**)calloc(argc, sizeof(char*));
\r
135 for(int i = argc -1; i >= 0; i--)
\r
136 xbt_dynar_pop(this->args, &(argv[i]));
\r
138 // finaly create the process (for more detail on the process creation see Process::create()
\r
139 process->create(host, this->function , argc, argv);
\r
141 // TODO add the properties of the process
\r
142 /*process->properties = this->properties;
\r
143 this->properties = new Properties();*/
\r
146 void ApplicationHandler::ProcessFactory::setProcessIdentity(const char* hostName, const char* function)
\r
148 this->hostName = hostName;
\r
149 this->function = function;
\r
151 /*if (!this->args->empty())
\r
152 this->args->clear();
\r
154 if(!this->properties->empty())
\r
155 this->properties->clear();*/
\r
158 // callback function used by the dynamic array to cleanup all of its elements.
\r
159 void ApplicationHandler::ProcessFactory::freeCstr(void* cstr)
\r
161 free(*(void**)cstr);
\r
164 void ApplicationHandler::ProcessFactory::registerProcessArg(const char* arg)
\r
166 char* cstr = _strdup(arg);
\r
167 xbt_dynar_push(this->args, &cstr);
\r
170 void ApplicationHandler::ProcessFactory::setProperty(const char* id, const char* value)
\r
172 // TODO implement this function;
\r
175 const char* ApplicationHandler::ProcessFactory::getHostName(void)
\r
177 return this->hostName;
\r
181 } // namespace SimGrid