Remove dynamic exceptions
[anna.git] / example / comm / blocker / main.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 /*
10    El objetivo de éste programa es generar errores en el cliente que se conecte a él.
11
12    Como no atiendo ningún tipo de.tráfico llegará un momento en el que su cola de entrada
13    alcanzará el límite y el cliente obtendrá un error al intentar enviar el mensaje.
14
15    Se trata de verificar que los servicios de reparto son capaces de asumir ciertos
16    tipos de error.
17 */
18 #include <iostream>
19 #include <signal.h>
20
21 #include <anna/core/core.hpp>
22 #include <anna/comm/comm.hpp>
23
24 #include <anna/xml/Node.hpp>
25 #include <anna/xml/Attribute.hpp>
26
27 using namespace std;
28
29 class MyCommunicator : public Communicator {
30 public:
31    MyCommunicator () {;}
32       
33 private:
34    void eventReceiveMessage (comm::ClientSocket&, const Message&) noexcept(false);
35 };
36
37 class Blocker : public comm::Application {
38 public:
39    Blocker ();
40       
41 private:
42    MyCommunicator a_communicator;
43    comm::ServerSocket* a_blockerSocket;
44
45    void initialize () noexcept(false);
46    void run () noexcept(false);
47 };
48
49 using namespace std;
50 using namespace anna::comm;
51
52 int main (int argc, const char** argv)
53 {
54    CommandLine& commandLine (CommandLine::instantiate ());
55    Blocker app;
56
57    srand (time (NULL));
58
59    sigignore (SIGABRT);
60    
61    try {
62       commandLine.initialize (argv, argc);
63       commandLine.verify ();
64
65       Logger::setLevel (Logger::Debug); 
66       Logger::initialize ("Blocker", new TraceWriter ("file.trace",4096000));
67  
68       app.start ();
69    }
70    catch (Exception& ex) {
71       cout << ex.asString () << endl;
72    }
73    
74    return 0;
75 }
76
77 Blocker::Blocker () : 
78    Application ("Blocker", "Bloqueador de comunicaciones", "1.0") 
79 {
80    CommandLine& commandLine (CommandLine::instantiate ());
81       
82    commandLine.add ("p", CommandLine::Argument::Mandatory, "Puerto en el que atender peticiones");
83    commandLine.add ("a", CommandLine::Argument::Mandatory, "Direccin IP en la que atender");
84    commandLine.add ("trace", CommandLine::Argument::Optional, "Nivel de trazas (debug,warning, error,...)");
85 }
86
87 //-----------------------------------------------------------------------------------------
88 // Inicializa el servidor de sockets.
89 //-----------------------------------------------------------------------------------------
90 void Blocker::initialize () 
91    noexcept(false)
92 {
93    CommandLine& cl (CommandLine::instantiate ());
94
95    int port = cl.getIntegerValue ("p");
96    const comm::Device* device = Network::instantiate ().find (Device::asAddress (cl.getValue ("a")));
97
98    a_blockerSocket = new ServerSocket (INetAddress (device, port), false);
99    a_blockerSocket->setCategory (777);
100 }
101
102 //-----------------------------------------------------------------------------------------
103 // Atiende las peticiones.
104 // Cuando hay un nuevo mensaje invocar�a Communicator::eventReceiveMessage
105 //-----------------------------------------------------------------------------------------
106 void Blocker::run ()
107    noexcept(false)
108 {
109    CommandLine& cl (CommandLine::instantiate ());
110
111    a_communicator.attach (a_blockerSocket);
112
113    if (cl.exists ("trace"))
114       Logger::setLevel (Logger::asLevel (cl.getValue ("trace")));
115
116    a_communicator.accept ();
117 }
118
119 //-----------------------------------------------------------------------------------------
120 // Cuando recibe el primer mensaje deja al sistema bloqueado esperando una tecla.
121 //-----------------------------------------------------------------------------------------
122 void MyCommunicator::eventReceiveMessage (ClientSocket& clientSocket, const Message& message)
123    noexcept(false)
124 {
125    LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventReceiveMessage", ANNA_FILE_LOCATION));
126    static bool blocking = true;
127
128    if (blocking == false) 
129       return;
130
131    char aux [1024];
132    bool stop = false;
133
134    app::Application& app = app::functions::getApp ();
135    cout << "Bloqueando las comunicaciones ejecute (kill -9 " << app.getPid ()  << ") ... " << flush;
136    while (stop == false) {
137       if (fgets(aux, sizeof(aux), stdin) != NULL) {
138          if (aux [0] == 'q' || aux [0] == 'Q')
139             stop = true;
140       }
141       sleep (10);
142    } 
143
144    blocking = false;
145 }
146