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