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() == "stop") {
147 response = cmd_plant_stop(command);
149 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
150 "Invalid command for 'plant' taget!");
153 response = new HTTPResponse(HTTPMessage::NOT_FOUND, "Invalid taget!");
156 response->headers["Content-Type"] = "text/html; charset=iso-8859-1";
157 //response->headers["Connection"] = "close";
158 controlserver->send(*response);
160 // FIXME con timeout no debería ser necesario. Verificar cabecera Connection
161 // para saber si hay que finish()earlo o no.
162 //if (stop_controlserver) {
163 // controlserver->finish();
167 HTTPResponse* Server::cmd_server_status(void) const {
169 stringstream response_xml;
170 response_xml << "<html>" << endl;
171 response_xml << " <head>" << endl;
172 response_xml << " <title>PlaQui v0.8</title>" << endl;
173 response_xml << " </head>" << endl;
174 response_xml << " <body>" << endl;
175 response_xml << " <h1>PlaQui</h1>" << endl;
176 response_xml << " <p>versión 0.8</p>" << endl;
177 /* response_xml << " <h2>Comando</h2>" << endl;
178 response_xml << " <ul>" << endl;
179 response_xml << " <li><b>Target:</b> " << command.get_target() << endl;
180 response_xml << " <li><b>Command:</b> " << command.get_command() << endl;
181 response_xml << " <li><b>Argumentos:</b>" << endl;
182 response_xml << " <ol>" << endl;
183 for (Command::Arguments::const_iterator i = command.get_args().begin();
184 i != command.get_args().end(); i++) {
185 response_xml << " <li>" << *i << "</li>" << endl;
187 response_xml << " </ol>" << endl;
188 response_xml << " </ul>" << endl;*/
189 response_xml << " <h2>Desarrollado por</h2>" << endl;
190 response_xml << " <ul>" << endl;
191 response_xml << " <li>Nicolás Dimov.</li>" << endl;
192 response_xml << " <li>Leandro Lucarella.</li>" << endl;
193 response_xml << " <li>Ricardo Markiewicz.</li>" << endl;
194 response_xml << " </ul>" << endl;
195 response_xml << " <address>" << endl;
196 response_xml << " Copyleft 2003 - bajo los " << endl;
197 response_xml << " términos de la licencia GPL" << endl;
198 response_xml << " </address>" << endl;
199 response_xml << " </body>" << endl;
200 response_xml << "</html>" << endl;
201 return new HTTPResponse(HTTPMessage::OK, response_xml.str());
204 HTTPResponse* Server::cmd_connection_list(void) {
206 TCPServer::ConnectionInfoList cil = get_connected();
207 stringstream response_xml;
208 response_xml << "<html>" << endl;
209 response_xml << " <head>" << endl;
210 response_xml << " <title>PlaQui v0.8</title>" << endl;
211 response_xml << " </head>" << endl;
212 response_xml << " <body>" << endl;
213 response_xml << " <h1>PlaQui</h1>" << endl;
214 response_xml << " <p>versión 0.8</p>" << endl;
215 response_xml << " <h2>Lista de conexiones:</h2>" << endl;
216 response_xml << " <ul>" << endl;
217 for (TCPServer::ConnectionInfoList::const_iterator i = cil.begin();
218 i != cil.end(); i++) {
219 response_xml << " <li>" << i->host
220 << ":" << i->port << " [<a href=\"/connection/stop/"
221 << i->host << "/" << i->port << "\">deconectar</a>]</li>"
224 response_xml << " </ul>" << endl;
225 response_xml << " <address>" << endl;
226 response_xml << " Copyleft 2003 - bajo los " << endl;
227 response_xml << " términos de la licencia GPL" << endl;
228 response_xml << " </address>" << endl;
229 response_xml << " </body>" << endl;
230 response_xml << "</html>" << endl;
231 return new HTTPResponse(HTTPMessage::OK, response_xml.str());
234 HTTPResponse* Server::cmd_connection_stop(const Command& command) {
235 const Command::Arguments& args = command.get_args();
236 Connection::Port port;
237 if (args.size() < 2) {
238 return new HTTPResponse(HTTPMessage::CONFLICT,
239 "Faltan argumentos.");
240 } else if (disconnect(args[0], String(args[1]).to(port))) {
241 return new HTTPResponse(HTTPMessage::OK,
242 string("La conexión a ") + args[0] + ":" + args[1]
243 + " se cerrará en instantes...");
245 return new HTTPResponse(HTTPMessage::NOT_FOUND,
246 string("No existe una conexión a ") + args[0]
251 HTTPResponse* Server::cmd_transmission_list(void) {
253 stringstream response_xml;
254 response_xml << "<html>" << endl;
255 response_xml << " <head>" << endl;
256 response_xml << " <title>PlaQui v0.8</title>" << endl;
257 response_xml << " </head>" << endl;
258 response_xml << " <body>" << endl;
259 response_xml << " <h1>PlaQui</h1>" << endl;
260 response_xml << " <p>versión 0.8</p>" << endl;
261 response_xml << " <h2>Lista de transmisiones:</h2>" << endl;
262 response_xml << " <ul>" << endl;
263 // TODO - recorrer todas las plantas y sus transmisiones.
264 /* transmissions_mutex.lock();
265 for (TransmitterList::const_iterator i = transmissions.begin();
266 i != transmissions.end(); i++) {
267 response_xml << " <li>" << (*i)->get_host() << ":"
268 << (*i)->get_port() << " [<a href=\"/transmission/stop/"
269 << (*i)->get_host() << "/" << (*i)->get_port()
270 << "\">desconectar</a>]</li>" << endl;
272 transmissions_mutex.unlock();*/
273 response_xml << " </ul>" << endl;
274 response_xml << " <address>" << endl;
275 response_xml << " Copyleft 2003 - bajo los " << endl;
276 response_xml << " términos de la licencia GPL" << endl;
277 response_xml << " </address>" << endl;
278 response_xml << " </body>" << endl;
279 response_xml << "</html>" << endl;
280 return new HTTPResponse(HTTPMessage::OK, response_xml.str());
283 HTTPResponse* Server::cmd_transmission_start(const Command& command) {
284 const Command::Arguments& args = command.get_args();
285 if (args.size() < 3) {
286 return new HTTPResponse(HTTPMessage::CONFLICT,
287 "Faltan argumentos.");
289 const string& plant = args[0];
290 const string& host = args[1];
291 Connection::Port port = String(args[2]).to(port);
292 Glib::Mutex::Lock lock(plants_mutex);
293 PlantList::iterator p = plants.find(plant);
294 if (p == plants.end()) {
295 return new HTTPResponse(HTTPMessage::NOT_FOUND,
296 string("No existe la planta '") + plant + "'.");
297 // TODO - agregar chequeo de que la transmision a ese host:port no
298 // exista para otra planta?
299 } else if (plants[plant]->transmission_start(host, port)) {
300 return new HTTPResponse(HTTPMessage::OK,
301 string("Se empieza a transmitir la planta '") + plant
302 + "' a " + host + ":" + args[1] + ".");
304 return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
305 string("Error al crear la transmisión a de la planta '")
306 + plant + "' a " + host + ":" + args[2] + ".");
311 HTTPResponse* Server::cmd_transmission_stop(const Command& command) {
312 const Command::Arguments& args = command.get_args();
313 if (args.size() < 2) {
314 return new HTTPResponse(HTTPMessage::CONFLICT,
315 "Faltan argumentos.");
317 const string& host = args[0];
318 Connection::Port port = String(args[1]).to(port);
319 for (PlantList::iterator i = plants.begin(); i != plants.end(); i++) {
320 // TODO - agregar chequeo para saber si existe una conexion (para
321 // tirar error de que no hay conexion o de que no se pudo
323 if (i->second->transmission_stop(host, port)) {
324 return new HTTPResponse(HTTPMessage::OK,
325 string("Se finaliza la transmisión de la planta '")
326 + i->first + "' a " + host + ":" + args[1] + ".");
329 return new HTTPResponse(HTTPMessage::NOT_FOUND,
330 string("No se puede finalizar la transmisión a ")
331 + host + ":" + args[1] + ".");
335 HTTPResponse* Server::cmd_plant_list(void) {
337 stringstream response_xml;
338 response_xml << "<html>" << endl;
339 response_xml << " <head>" << endl;
340 response_xml << " <title>PlaQui v0.8</title>" << endl;
341 response_xml << " </head>" << endl;
342 response_xml << " <body>" << endl;
343 response_xml << " <h1>PlaQui</h1>" << endl;
344 response_xml << " <p>versión 0.8</p>" << endl;
345 response_xml << " <h2>Lista de plantas:</h2>" << endl;
346 response_xml << " <ul>" << endl;
348 for (PlantList::const_iterator i = plants.begin();
349 i != plants.end(); i++) {
350 response_xml << " <li>" << i->first
351 << " [<a href=\"/plant/stop/" << i->first << "\">parar</a>]</li>"
354 plants_mutex.unlock();
355 response_xml << " </ul>" << endl;
356 response_xml << " <address>" << endl;
357 response_xml << " Copyleft 2003 - bajo los " << endl;
358 response_xml << " términos de la licencia GPL" << endl;
359 response_xml << " </address>" << endl;
360 response_xml << " </body>" << endl;
361 response_xml << "</html>" << endl;
362 return new HTTPResponse(HTTPMessage::OK, response_xml.str());
365 HTTPResponse* Server::cmd_plant_stop(const Command& command) {
366 if (!command.get_args().size()) {
367 return new HTTPResponse(HTTPMessage::CONFLICT,
368 "Faltan argumentos.");
370 Glib::Mutex::Lock lock(plants_mutex);
371 const string name = command.get_args()[0];
372 if (plants.find(name) == plants.end()) {
373 return new HTTPResponse(HTTPMessage::NOT_FOUND,
374 string("No existe la planta ") + name);
376 // TODO Ver si al frenar la planta se destruye (no deberia!!!)
377 plants[name]->finish();
378 return new HTTPResponse(HTTPMessage::OK,
379 string("La planta '") + name + "' se cerrará en instantes...");
382 } // namespace Server
384 } // namespace PlaQui