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 "El server se apagará en instantes...");
120 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
121 "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 "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 "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 "Invalid command for 'plant' taget!");
157 response = new HTTPResponse(HTTPMessage::NOT_FOUND, "Invalid taget!");
160 response->headers["Content-Type"] = "text/xml; charset=iso-8859-1";
161 //response->headers["Connection"] = "close";
162 controlserver->send(*response);
164 // FIXME con timeout no debería ser necesario. Verificar cabecera Connection
165 // para saber si hay que finish()earlo o no.
166 //if (stop_controlserver) {
167 // controlserver->finish();
171 HTTPResponse* Server::cmd_server_status(void) const {
173 stringstream response_xml;
174 response_xml << "<serverstatus>" << endl;
175 response_xml << "\t<version>0.9</version>" << endl;
176 response_xml << "\t<authors>" << endl;
177 response_xml << "\t\t<author>Nicolás Dimov</author>" << endl;
178 response_xml << "\t\t<author>Leandro Lucarella</author>" << endl;
179 response_xml << "\t\t<author>Ricardo Markiewicz</author>" << endl;
180 response_xml << "\t</authors>" << endl;
181 response_xml << "</serverstatus>" << endl;
182 return new HTTPResponse(HTTPMessage::OK, response_xml.str());
185 HTTPResponse* Server::cmd_connection_list(void) {
187 TCPServer::ConnectionInfoList cil = get_connected();
188 stringstream response_xml;
189 response_xml << "<list type=\"connection\">" << endl;
190 for (TCPServer::ConnectionInfoList::const_iterator i = cil.begin();
191 i != cil.end(); i++) {
192 response_xml << "\t<row>" << endl;
193 response_xml << "\t\t<cell>" << i->host << "</cell>" << endl;
194 response_xml << "\t\t<cell>" << i->port << "</cell>" << endl;
195 response_xml << "\t</row>" << endl;
197 response_xml << "</list>" << endl;
198 return new HTTPResponse(HTTPMessage::OK, response_xml.str());
201 HTTPResponse* Server::cmd_connection_stop(const Command& command) {
202 const Command::Arguments& args = command.get_args();
203 Connection::Port port;
204 if (args.size() < 2) {
205 return new HTTPResponse(HTTPMessage::CONFLICT,
206 "Faltan argumentos.");
207 } else if (disconnect(args[0], String(args[1]).to(port))) {
208 return new HTTPResponse(HTTPMessage::OK,
209 string("La conexión a ") + args[0] + ":" + args[1]
210 + " se cerrará en instantes...");
212 return new HTTPResponse(HTTPMessage::NOT_FOUND,
213 string("No existe una conexión a ") + args[0]
218 HTTPResponse* Server::cmd_transmission_list(void) {
220 stringstream response_xml;
221 response_xml << "<list type=\"transmission\">" << endl;
222 /*TODO plants_mutex.lock();
223 for (PlantList::const_iterator i = plants.begin();
224 i != plants.end(); i++) {
226 response_xml << " <li>" << (*i)->get_host() << ":"
227 << (*i)->get_port() << " [<a href=\"/transmission/stop/"
228 << (*i)->get_host() << "/" << (*i)->get_port()
229 << "\">desconectar</a>]</li>" << endl;
231 transmissions_mutex.unlock();*/
232 response_xml << "</list>" << endl;
233 return new HTTPResponse(HTTPMessage::OK, response_xml.str());
236 HTTPResponse* Server::cmd_transmission_start(const Command& command) {
237 const Command::Arguments& args = command.get_args();
238 if (args.size() < 3) {
239 return new HTTPResponse(HTTPMessage::CONFLICT,
240 "Faltan argumentos.");
242 string plant = args[0];
243 string host = args[1];
244 Connection::Port port = String(args[2]).to(port);
245 Glib::Mutex::Lock lock(plants_mutex);
246 PlantList::iterator p = plants.find(plant);
247 if (p == plants.end()) {
248 return new HTTPResponse(HTTPMessage::NOT_FOUND,
249 string("No existe la planta '") + plant + "'.");
250 // TODO - agregar chequeo de que la transmision a ese host:port no
251 // exista para otra planta?
252 } else if (plants[plant]->transmission_start(host, port)) {
253 return new HTTPResponse(HTTPMessage::OK,
254 string("Se empieza a transmitir la planta '") + plant
255 + "' a " + host + ":" + String().from(port) + ".");
257 return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
258 string("Error al crear la transmisión a de la planta '")
259 + plant + "' a " + host + ":" + args[2] + ".");
264 HTTPResponse* Server::cmd_transmission_stop(const Command& command) {
265 const Command::Arguments& args = command.get_args();
266 if (args.size() < 2) {
267 return new HTTPResponse(HTTPMessage::CONFLICT,
268 "Faltan argumentos.");
270 const string& host = args[0];
271 Connection::Port port = String(args[1]).to(port);
272 for (PlantList::iterator i = plants.begin(); i != plants.end(); i++) {
273 // TODO - agregar chequeo para saber si existe una conexion (para
274 // tirar error de que no hay conexion o de que no se pudo
276 if (i->second->transmission_stop(host, port)) {
277 return new HTTPResponse(HTTPMessage::OK,
278 string("Se finaliza la transmisión de la planta '")
279 + i->first + "' a " + host + ":" + args[1] + ".");
282 return new HTTPResponse(HTTPMessage::NOT_FOUND,
283 string("No se puede finalizar la transmisión a ")
284 + host + ":" + args[1] + ".");
288 HTTPResponse* Server::cmd_plant_list(void) {
290 stringstream response_xml;
291 response_xml << "<list type=\"plant\">" << endl;
293 for (PlantList::const_iterator i = plants.begin();
294 i != plants.end(); i++) {
295 response_xml << "\t<row>" << endl;
296 response_xml << "\t\t<cell>" << i->first << "</cell>" << endl;
297 response_xml << "\t</row>" << endl;
299 plants_mutex.unlock();
300 response_xml << "</list>" << endl;
301 return new HTTPResponse(HTTPMessage::OK, response_xml.str());
304 HTTPResponse* Server::cmd_plant_get(const Command& command) {
305 if (!command.get_args().size()) {
306 return new HTTPResponse(HTTPMessage::CONFLICT,
307 "Faltan argumentos.");
309 Glib::Mutex::Lock lock(plants_mutex);
310 string plant = command.get_args()[0];
311 if (plants.find(plant) == plants.end()) {
312 return new HTTPResponse(HTTPMessage::NOT_FOUND,
313 string("No existe la planta ") + plant);
315 string xml = plants[plant]->get_xml();
317 return new HTTPResponse(HTTPMessage::OK, xml);
319 return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
320 ("No se pudo obtener el XML de la planta ") + plant);
324 HTTPResponse* Server::cmd_plant_set(const Command& command) {
325 const Command::Arguments& args = command.get_args();
326 if (args.size() < 4) {
327 return new HTTPResponse(HTTPMessage::CONFLICT,
328 "Faltan argumentos.");
330 string plant = args[0];
331 string element = args[1];
332 string key = args[2];
333 string value = args[3];
334 Glib::Mutex::Lock lock(plants_mutex);
335 PlantList::iterator p = plants.find(plant);
336 if (p == plants.end()) {
337 return new HTTPResponse(HTTPMessage::NOT_FOUND,
338 string("No existe la planta '") + plant + "'.");
340 /* if (!plants[plant]->set_element(host, port)) {
341 return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
342 string("Error al crear la transmisión a de la planta '")
343 + plant + "' a " + host + ":" + args[2] + ".");
345 return new HTTPResponse(HTTPMessage::OK);
348 HTTPResponse* Server::cmd_plant_stop(const Command& command) {
349 if (!command.get_args().size()) {
350 return new HTTPResponse(HTTPMessage::CONFLICT,
351 "Faltan argumentos.");
353 Glib::Mutex::Lock lock(plants_mutex);
354 const string name = command.get_args()[0];
355 if (plants.find(name) == plants.end()) {
356 return new HTTPResponse(HTTPMessage::NOT_FOUND,
357 string("No existe la planta ") + name);
359 // TODO Ver si al frenar la planta se destruye (no deberia!!!)
360 plants[name]->finish();
361 return new HTTPResponse(HTTPMessage::OK,
362 string("La planta '") + name + "' se cerrará en instantes...");
365 } // namespace Server
367 } // namespace PlaQui