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
23 ApplicationHandler::ProcessFactory::processFactory = NULL;
\r
25 // Desable the default constructor, the copy constructor , the assignement operator
\r
26 // and the destructor of this class. Assume that this class is static.
\r
28 // Default constructor.
\r
29 ApplicationHandler::ApplicationHandler()
\r
34 // Copy constructor.
\r
35 ApplicationHandler::ApplicationHandler(const ApplicationHandler& rApplicationHandler)
\r
41 ApplicationHandler::~ApplicationHandler()
\r
46 // Assignement operator.
\r
47 const ApplicationHandler& ApplicationHandler::operator = (const ApplicationHandler& rApplicationHandler)
\r
52 void ApplicationHandler::onStartDocument(void)
\r
54 // instanciate the factory at the begining of the parsing
\r
55 processFactory = new ProcessFactory();
\r
58 void ApplicationHandler::onEndDocument(void)
\r
60 // release the handler at the end of the parsing.
\r
62 delete processFactroy;
\r
65 void ApplicationHandler::onBeginProcess(void)
\r
67 // set the process identity at the begin of the xml process element.
\r
68 processFactory->setProcessIdentity(A_surfxml_process_host, A_surfxml_process_function);
\r
71 void ApplicationHandler::onProcessArg(void)
\r
73 // register the argument of the current xml process element.
\r
74 processFactory->registerProcessArg(A_surfxml_argument_value);
\r
77 void ApplicationHandler::OnProperty(void)
\r
79 // set the property of the current xml process element.
\r
80 processFactory->setProperty(A_surfxml_prop_id, A_surfxml_prop_value);
\r
83 void ApplicationHandler::onEndProcess(void)
\r
85 // at the end of the xml process element create the wrapper process (of the native Msg process)
\r
86 processFactory->createProcess();
\r
89 /////////////////////////////////////////////////////////////////////////////////////////////////
\r
90 // Process factory connected member functions
\r
92 ApplicationHandler::ProcessFactory::ProcessFactory()
\r
94 this->args = xbt_dynar_new(sizeof(char*),ProcessFactory::freeCstr);
\r
95 this->properties = NULL; // TODO instanciate the dictionary
\r
96 this->hostName = NULL;
\r
97 this->function = NULL;
\r
100 // create the cxx process wrapper.
\r
101 void ApplicationHandler::ProcessFactory::createProcess()
\r
102 throw (ClassNotFoundException, HostNotFoundException)
\r
107 // try to dynamicaly create an instance fo the process from its name (which is specified by the element function
\r
108 // in the xml application file.
\r
109 // if this static method fails, it throws an exception of the class ClassNotFoundException
\r
110 process = (Process*)Class::fromName(this->function);
\r
112 // try to retrieve the host of the process from its name
\r
113 // if this method fails, it throws an exception of the class HostNotFoundException
\r
114 host = Host::getByName(this->hostName);
\r
116 // build the list of the arguments of the newly created process.
\r
117 int argc = xbt_dynar_length(this->args);
\r
119 char** argv = (char**)calloc(argc, sizeof(char*));
\r
121 for(int i = 0; i < argc; i++)
\r
122 xbt_dynar_pop(this->args, &(argv[i]));
\r
124 // finaly create the process (for more detail on the process creation see Process::create()
\r
125 process->create(host, this->function , argc, argv);
\r
127 // TODO add the properties of the process
\r
128 /*process->properties = this->properties;
\r
129 this->properties = new Properties();*/
\r
132 void ApplicationHandler::ProcessFactory::setProcessIdentity(const string& hostName, const string& function)
\r
134 this->hostName = hostName;
\r
135 this->function = function;
\r
137 /*if (!this->args->empty())
\r
138 this->args->clear();
\r
140 if(!this->properties->empty())
\r
141 this->properties->clear();*/
\r
144 // callback function used by the dynamic array to cleanup all of its elements.
\r
145 void ApplicationHandler::ProcessFactory::freeCstr(void* cstr)
\r
147 free(*(void**)str);
\r
150 void ApplicationHandler::ProcessFactory::registerProcessArg(const char* arg)
\r
152 char* cstr = strdup(arg);
\r
153 xbt_dynar_push(this->args, &cstr);
\r
156 void ApplicationHandler::ProcessFactory::setProperty(const char* id, const char* value)
\r
158 // TODO implement this function;
\r
161 const const char* ApplicationHandler::ProcessFactory::getHostName(void)
\r
163 return this->hostName;
\r
167 } // namespace SimGrid