1 // vim: set noexpandtab tabstop=4 shiftwidth=4:
2 //----------------------------------------------------------------------------
4 //----------------------------------------------------------------------------
5 // This file is part of PlaQui.
7 // PlaQui is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the Free Software
9 // Foundation; either version 2 of the License, or (at your option) any later
12 // PlaQui is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 // You should have received a copy of the GNU General Public License along
18 // with PlaQui; if not, write to the Free Software Foundation, Inc., 59 Temple
19 // Place, Suite 330, Boston, MA 02111-1307 USA
20 //----------------------------------------------------------------------------
21 // Creado: Sat Oct 18 18:18:36 2003
22 // Autores: Leandro Lucarella <llucare@fi.uba.ar>
23 //----------------------------------------------------------------------------
28 #include "plaqui/server/server.h"
29 #include "plaqui/server/connection.h"
30 #include "plaqui/server/controlserver.h"
31 #include <sigc++/class_slot.h>
32 // FIXME - sacar sstream (a menos que se necesite)
35 # include "plaqui/server/string.h"
45 Server::~Server(void) {
47 cerr << __FILE__ << ": destructor." << endl;
50 Glib::Mutex::Lock lock(plants_mutex);
51 for (PlantList::iterator i = plants.end(); i != plants.end(); i++) {
52 i->second->finish(true);
56 Server::Server(const string& plant_filename, const Connection::Port& port):
59 cerr << __FILE__ << ": port = " << port << endl;
62 Glib::Mutex::Lock lock(plants_mutex);
63 plants["default"] = new Plant(plant_filename);
64 plants["default"]->signal_finished().connect(
65 SigC::bind<const char*>(
66 SigC::slot_class(*this, &Server::on_plant_finished),
68 plants["default"]->run();
71 Connection* Server::new_connection(
72 const sockbuf::sockdesc& sd) {
74 cerr << __FILE__ << ": new_connection(sd = " << sd.sock << ")"
77 ControlServer* connection = new ControlServer(sd);
78 // TODO verificar si el new se hace bien? no creo.
79 connection->signal_command_received().connect(
80 SigC::bind<ControlServer*>(
81 SigC::slot_class(*this, &Server::on_control_command_received),
87 void Server::on_plant_updated(const Plant* plant) {
89 cerr << __FILE__ << ": on_plant_updated(plant = " << plant << ")." << endl;
93 void Server::on_plant_finished(const char* plant) {
95 cerr << __FILE__ << ": on_plant_finished(plant_name = " << plant << endl;
97 Glib::Mutex::Lock lock(plants_mutex);
101 /// \todo Terminar de implementar.
102 void Server::on_control_command_received(const Command& command,
103 ControlServer* controlserver) {
105 cerr << __FILE__ << ": on_control_command_received(target = "
106 << command.get_target() << ", command = " << command.get_command()
107 << ", args = [" << String::join(command.get_args(), ", ") << "])"
110 HTTPResponse* response;
111 //bool stop_controlserver = false;
112 if (command.get_target() == "server") {
113 if (command.get_command() == "status") {
114 response = cmd_server_status();
115 } else if (command.get_command() == "stop") {
117 response = new HTTPResponse(HTTPMessage::OK,
118 "<response desc=\"El server se apagará en instantes...\" />");
120 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
121 "<response desc=\"Invalid command for 'server' taget!\" />");
123 } else if (command.get_target() == "connection") {
124 if (command.get_command() == "list") {
125 response = cmd_connection_list();
126 } else if (command.get_command() == "stop") {
127 response = cmd_connection_stop(command);
129 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
130 "<response desc=\"Invalid command for 'connection' taget!\" />");
132 } else if (command.get_target() == "transmission") {
133 if (command.get_command() == "list") {
134 response = cmd_transmission_list();
135 } else if (command.get_command() == "start") {
136 response = cmd_transmission_start(command);
137 } else if (command.get_command() == "stop") {
138 response = cmd_transmission_stop(command);
140 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
141 "<response desc=\"Invalid command for 'transmission' taget!\" />");
143 } else if (command.get_target() == "plant") {
144 if (command.get_command() == "list") {
145 response = cmd_plant_list();
146 } else if (command.get_command() == "get") {
147 response = cmd_plant_get(command);
148 } else if (command.get_command() == "set") {
149 response = cmd_plant_set(command);
150 } else if (command.get_command() == "stop") {
151 response = cmd_plant_stop(command);
153 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
154 "<response desc=\"Invalid command for 'plant' taget!\" />");
157 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
158 "<response desc=\"Invalid taget!\" />");
161 response->headers["Content-Type"] = "text/xml; charset=iso-8859-1";
162 //response->headers["Connection"] = "close";
163 controlserver->send(*response);
165 // FIXME con timeout no debería ser necesario. Verificar cabecera Connection
166 // para saber si hay que finish()earlo o no.
167 //if (stop_controlserver) {
168 // controlserver->finish();
172 HTTPResponse* Server::cmd_server_status(void) const {
175 xml << "<serverstatus>" << endl;
176 xml << "\t<version>0.9</version>" << endl;
177 xml << "\t<authors>" << endl;
178 xml << "\t\t<author>Nicolás Dimov</author>" << endl;
179 xml << "\t\t<author>Leandro Lucarella</author>" << endl;
180 xml << "\t\t<author>Ricardo Markiewicz</author>" << endl;
181 xml << "\t</authors>" << endl;
182 xml << "</serverstatus>" << endl;
183 return new HTTPResponse(HTTPMessage::OK, xml.str());
186 HTTPResponse* Server::cmd_connection_list(void) {
188 TCPServer::ConnectionInfoList cil = get_connected();
190 xml << "<list type=\"connection\">" << endl;
191 for (TCPServer::ConnectionInfoList::const_iterator i = cil.begin();
192 i != cil.end(); i++) {
193 xml << "\t<row>" << endl;
194 xml << "\t\t<cell>" << i->host << "</cell>" << endl;
195 xml << "\t\t<cell>" << i->port << "</cell>" << endl;
196 xml << "\t</row>" << endl;
198 xml << "</list>" << endl;
199 return new HTTPResponse(HTTPMessage::OK, xml.str());
202 HTTPResponse* Server::cmd_connection_stop(const Command& command) {
203 const Command::Arguments& args = command.get_args();
204 Connection::Port port;
205 if (args.size() < 2) {
206 return new HTTPResponse(HTTPMessage::CONFLICT,
207 "<response desc=\"Faltan argumentos.\" />");
208 } else if (disconnect(args[0], to(args[1], port))) {
209 return new HTTPResponse(HTTPMessage::OK,
210 string("<response desc=\"La conexión a ") + args[0] + ":" + args[1]
211 + " se cerrará en instantes...\" />");
213 return new HTTPResponse(HTTPMessage::NOT_FOUND,
214 string("<response desc=\"No existe una conexión a ") + args[0]
215 + ":" + args[1] + "\" />");
219 HTTPResponse* Server::cmd_transmission_list(void) {
222 xml << "<list type=\"transmission\">" << endl;
223 /*TODO plants_mutex.lock();
224 for (PlantList::const_iterator i = plants.begin();
225 i != plants.end(); i++) {
227 xml << " <li>" << (*i)->get_host() << ":"
228 << (*i)->get_port() << " [<a href=\"/transmission/stop/"
229 << (*i)->get_host() << "/" << (*i)->get_port()
230 << "\">desconectar</a>]</li>" << endl;
232 transmissions_mutex.unlock();*/
233 xml << "</list>" << endl;
234 return new HTTPResponse(HTTPMessage::OK, xml.str());
237 HTTPResponse* Server::cmd_transmission_start(const Command& command) {
238 const Command::Arguments& args = command.get_args();
239 if (args.size() < 3) {
240 return new HTTPResponse(HTTPMessage::CONFLICT,
241 "<response desc=\"Faltan argumentos.\" />");
243 string plant = args[0];
244 string host = args[1];
245 Connection::Port port = to(args[2], port);
246 Glib::Mutex::Lock lock(plants_mutex);
247 PlantList::iterator p = plants.find(plant);
248 if (p == plants.end()) {
249 return new HTTPResponse(HTTPMessage::NOT_FOUND,
250 string("<response desc=\"No existe la planta '") + plant + "'.\" />");
251 // TODO - agregar chequeo de que la transmision a ese host:port no
252 // exista para otra planta?
253 } else if (plants[plant]->transmission_start(host, port)) {
254 return new HTTPResponse(HTTPMessage::OK,
255 string("<response desc=\"Se empieza a transmitir la planta '") + plant
256 + "' a " + host + ":" + String().from(port) + ".\" />");
258 return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
259 string("<response desc=\"Error al crear la transmisión a de la planta '")
260 + plant + "' a " + host + ":" + args[2] + ".\" />");
265 HTTPResponse* Server::cmd_transmission_stop(const Command& command) {
266 const Command::Arguments& args = command.get_args();
267 if (args.size() < 2) {
268 return new HTTPResponse(HTTPMessage::CONFLICT,
269 "<response desc=\"Faltan argumentos.\" />");
271 const string& host = args[0];
272 Connection::Port port = to(args[1], port);
273 for (PlantList::iterator i = plants.begin(); i != plants.end(); i++) {
274 // TODO - agregar chequeo para saber si existe una conexion (para
275 // tirar error de que no hay conexion o de que no se pudo
277 if (i->second->transmission_stop(host, port)) {
278 return new HTTPResponse(HTTPMessage::OK,
279 string("<response desc=\"Se finaliza la transmisión de la planta '")
280 + i->first + "' a " + host + ":" + args[1] + ".\" />");
283 return new HTTPResponse(HTTPMessage::NOT_FOUND,
284 string("<response desc=\"No se puede finalizar la transmisión a ")
285 + host + ":" + args[1] + ".\" />");
289 HTTPResponse* Server::cmd_plant_list(void) {
292 xml << "<list type=\"plant\">" << endl;
294 for (PlantList::const_iterator i = plants.begin();
295 i != plants.end(); i++) {
296 xml << "\t<row>" << endl;
297 xml << "\t\t<cell>" << i->first << "</cell>" << endl;
298 xml << "\t</row>" << endl;
300 plants_mutex.unlock();
301 xml << "</list>" << endl;
302 return new HTTPResponse(HTTPMessage::OK, xml.str());
305 HTTPResponse* Server::cmd_plant_get(const Command& command) {
306 if (!command.get_args().size()) {
307 return new HTTPResponse(HTTPMessage::CONFLICT,
308 "<response desc=\"Faltan argumentos.\" />");
310 Glib::Mutex::Lock lock(plants_mutex);
311 string plant = command.get_args()[0];
312 if (plants.find(plant) == plants.end()) {
313 return new HTTPResponse(HTTPMessage::NOT_FOUND,
314 string("<response desc=\"No existe la planta ") + plant + "\" />");
316 string xml = plants[plant]->get_xml();
318 return new HTTPResponse(HTTPMessage::OK, xml);
320 return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
321 ("<response desc=\"No se pudo obtener el XML de la planta ") + plant + "\" />");
325 HTTPResponse* Server::cmd_plant_set(const Command& command) {
326 const Command::Arguments& args = command.get_args();
327 if (args.size() < 4) {
328 return new HTTPResponse(HTTPMessage::CONFLICT,
329 "<response desc=\"Faltan argumentos.\" />");
331 string plant = args[0];
332 string element = args[1];
333 string key = args[2];
335 return new HTTPResponse(HTTPMessage::NOT_FOUND,
336 string("<response desc=\"La clave '") + key + "' es inválida.\" />");
338 string value = args[3];
339 Glib::Mutex::Lock lock(plants_mutex);
340 PlantList::iterator p = plants.find(plant);
341 if (p == plants.end()) {
342 return new HTTPResponse(HTTPMessage::NOT_FOUND,
343 string("<response desc=\"No existe la planta '") + plant + "'.\" />");
346 if ((value == "false") || (value == "0") || (value == "off")
347 || (value == "no")) {
350 if (!plants[plant]->set_open(element, open)) {
351 return new HTTPResponse(HTTPMessage::CONFLICT,
352 string("<response desc=\"No se pudo cambiar el estado del elemento '") + element + "'.\" />");
354 return new HTTPResponse(HTTPMessage::OK,
355 string("<response desc=\"Se cambió el estado del elemento '") + element + "'.\" />");
358 HTTPResponse* Server::cmd_plant_stop(const Command& command) {
359 if (!command.get_args().size()) {
360 return new HTTPResponse(HTTPMessage::CONFLICT,
361 "<response desc=\"Faltan argumentos.\" />");
363 Glib::Mutex::Lock lock(plants_mutex);
364 const string name = command.get_args()[0];
365 if (plants.find(name) == plants.end()) {
366 return new HTTPResponse(HTTPMessage::NOT_FOUND,
367 string("<response desc=\"No existe la planta ") + name + "\" />");
369 // TODO Ver si al frenar la planta se destruye (no deberia!!!)
370 plants[name]->finish();
371 return new HTTPResponse(HTTPMessage::OK,
372 string("<response desc=\"La planta '") + name + "' se cerrará en instantes...\" />");
375 } // namespace Server
377 } // namespace PlaQui