1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
38 El objetivo de éste programa es generar errores en el cliente que se conecte a él.
40 Como no atiendo ningún tipo de.tráfico llegará un momento en el que su cola de entrada
41 alcanzará el límite y el cliente obtendrá un error al intentar enviar el mensaje.
43 Se trata de verificar que los servicios de reparto son capaces de asumir ciertos
49 #include <anna/core/core.hpp>
50 #include <anna/comm/comm.hpp>
52 #include <anna/xml/Node.hpp>
53 #include <anna/xml/Attribute.hpp>
57 class MyCommunicator : public Communicator {
62 void eventReceiveMessage (comm::ClientSocket&, const Message&) throw (RuntimeException);
65 class Blocker : public comm::Application {
70 MyCommunicator a_communicator;
71 comm::ServerSocket* a_blockerSocket;
73 void initialize () throw (RuntimeException);
74 void run () throw (RuntimeException);
78 using namespace anna::comm;
80 int main (int argc, const char** argv)
82 CommandLine& commandLine (CommandLine::instantiate ());
90 commandLine.initialize (argv, argc);
91 commandLine.verify ();
93 Logger::setLevel (Logger::Debug);
94 Logger::initialize ("Blocker", new TraceWriter ("file.trace",4096000));
98 catch (Exception& ex) {
99 cout << ex.asString () << endl;
105 Blocker::Blocker () :
106 Application ("Blocker", "Bloqueador de comunicaciones", "1.0")
108 CommandLine& commandLine (CommandLine::instantiate ());
110 commandLine.add ("p", CommandLine::Argument::Mandatory, "Puerto en el que atender peticiones");
111 commandLine.add ("a", CommandLine::Argument::Mandatory, "Direccin IP en la que atender");
112 commandLine.add ("trace", CommandLine::Argument::Optional, "Nivel de trazas (debug,warning, error,...)");
115 //-----------------------------------------------------------------------------------------
116 // Inicializa el servidor de sockets.
117 //-----------------------------------------------------------------------------------------
118 void Blocker::initialize ()
119 throw (RuntimeException)
121 CommandLine& cl (CommandLine::instantiate ());
123 int port = cl.getIntegerValue ("p");
124 const comm::Device* device = Network::instantiate ().find (Device::asAddress (cl.getValue ("a")));
126 a_blockerSocket = new ServerSocket (INetAddress (device, port), false);
127 a_blockerSocket->setCategory (777);
130 //-----------------------------------------------------------------------------------------
131 // Atiende las peticiones.
132 // Cuando hay un nuevo mensaje invocar�a Communicator::eventReceiveMessage
133 //-----------------------------------------------------------------------------------------
135 throw (RuntimeException)
137 CommandLine& cl (CommandLine::instantiate ());
139 a_communicator.attach (a_blockerSocket);
141 if (cl.exists ("trace"))
142 Logger::setLevel (Logger::asLevel (cl.getValue ("trace")));
144 a_communicator.accept ();
147 //-----------------------------------------------------------------------------------------
148 // Cuando recibe el primer mensaje deja al sistema bloqueado esperando una tecla.
149 //-----------------------------------------------------------------------------------------
150 void MyCommunicator::eventReceiveMessage (ClientSocket& clientSocket, const Message& message)
151 throw (RuntimeException)
153 LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventReceiveMessage", ANNA_FILE_LOCATION));
154 static bool blocking = true;
156 if (blocking == false)
162 app::Application& app = app::functions::getApp ();
163 cout << "Bloqueando las comunicaciones ejecute (kill -9 " << app.getPid () << ") ... " << flush;
164 while (stop == false) {
165 if (gets (aux) != NULL) {
166 if (aux [0] == 'q' || aux [0] == 'Q')