First commit
[anna.git] / include / anna / comm / ServerAllocator.hpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
17 //     * Neither the name of Google Inc. 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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #ifndef anna_comm_ServerAllocator_hpp
38 #define anna_comm_ServerAllocator_hpp
39
40 #include <string>
41
42 namespace anna {
43
44 namespace comm {
45
46 class Host;
47 class TransportFactory;
48 class Server;
49
50 /**
51    Instanciador de anna::comm::Server se usa para poder instanciar distintos tipos
52    de anna::comm::Server desde la clase anna::comm::Host.
53
54    \see Host::createServer
55    \see Server
56 */
57 class ServerAllocator {
58 public:
59   /**
60      Constructor. Recoge los parametros con los que crear el servidor.
61      \param name Nombre logico del servidor.
62      \param host Instancia de la maquina sobre la que esta atendiento peticiones.
63      \param remotePort Puerto sobre el que atiende peticiones.
64      \param autoRecovery Indica si en caso de caida se debe intentar la recuperacion
65      automatica de la conexion.
66      \param transportFactory Factoria de protocolos de transporte usada por los ClientSocket asociados a este
67      proceso servidor.
68      \param ignoreIncomingMessages Indicador de ignorar mensajes entrantes.
69   */
70   ServerAllocator(const std::string& name, const Host& host, const int remotePort, const bool autoRecovery, TransportFactory* transportFactory = NULL, const bool ignoreIncomingMessages = false) :
71     a_name(name),
72     a_host(host),
73     a_remotePort(remotePort),
74     a_autoRecovery(autoRecovery),
75     a_transportFactory(transportFactory),
76     a_ignoreIncomingMessages(ignoreIncomingMessages)
77   {;}
78
79   /**
80      Constructor copia.
81      \param other Instanciador del que copiar.
82   */
83   ServerAllocator(const ServerAllocator& other) :
84     a_name(other.a_name),
85     a_host(other.a_host),
86     a_remotePort(other.a_remotePort),
87     a_autoRecovery(other.a_autoRecovery),
88     a_transportFactory(other.a_transportFactory),
89     a_ignoreIncomingMessages(other.a_ignoreIncomingMessages)
90   {;}
91
92   /**
93    * Destructor
94    */
95   virtual ~ServerAllocator() {;}
96
97   /**
98      Devuelve el nombre logico del servidor.
99      \return el nombre logico del servidor.
100   */
101   const std::string& getName() const throw() { return a_name; }
102
103   /**
104      Devuelve el puerto sobre el que atiende peticiones.
105      \return el puerto sobre el que atiende peticiones.
106   */
107   int getRemotePort() const throw() { return a_remotePort; }
108
109   /**
110      Indica si se ha indicado que en caso de caida se debe intentar la recuperacion
111      automatica de la conexion.
112      \return \em true si se ha indicado que en caso de caida se debe intentar la recuperacion
113      automatica de la conexion o \em false en caso contrario.
114   */
115   bool autoRecovery() const throw() { return a_autoRecovery; }
116
117   /**
118    * Devuelve \em true si el indicador que ignora los mensajes entrantes está activo, o \em false en otro caso.
119    * \return \em true si el indicador que ignora los mensajes entrantes está activo, o \em false en otro caso.
120    */
121   bool getIgnoreIncomingMessages() const throw() { return a_ignoreIncomingMessages; }
122
123   /**
124      Crea una instancia particular de anna::comm::Server usando los parametros establecidos
125      en el constructor.
126      \return una instancia particular de anna::comm::Server.
127   */
128   virtual Server* apply() const throw();
129
130 protected:
131   const std::string a_name;
132   const Host& a_host;
133   const int a_remotePort;
134   bool a_autoRecovery;
135   TransportFactory* a_transportFactory;
136   const bool a_ignoreIncomingMessages;
137 };
138
139 }
140
141 }
142
143 #endif