Updated license
[anna.git] / source / comm / handler / Manager.cpp
1 // ANNA - Anna is Not Nothingness 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 #include <anna/core/mt/Guard.hpp>
38
39 #include <anna/comm/handler/Manager.hpp>
40
41 using namespace anna;
42 using namespace anna::comm;
43
44 template <> comm::Communicator* handler::Manager::BinderSocketAllocator::st_communicator = NULL;
45 template <> comm::Communicator* handler::Manager::ServerSocketAllocator::st_communicator = NULL;
46 template <> comm::Communicator* handler::Manager::LocalConnectionAllocator::st_communicator = NULL;
47 template <> comm::Communicator* handler::Manager::RemoteConnectionAllocator::st_communicator = NULL;
48 template <> comm::Communicator* handler::Manager::DatagramSocketAllocator::st_communicator = NULL;
49 template <> comm::Communicator* handler::Manager::ClientSocketAllocator::st_communicator = NULL;
50
51 void handler::Manager::initialize(Communicator* communicator)
52 throw() {
53   BinderSocketAllocator::st_communicator = communicator;
54   ServerSocketAllocator::st_communicator = communicator;
55   LocalConnectionAllocator::st_communicator = communicator;
56   RemoteConnectionAllocator::st_communicator = communicator;
57   DatagramSocketAllocator::st_communicator = communicator;
58   ClientSocketAllocator::st_communicator = communicator;
59 }
60
61 Handler* handler::Manager::createHandler(comm::BinderSocket* binderSocket)
62 throw(RuntimeException) {
63   handler::BinderSocket* result = a_binderSockets.create();
64   result->setup(binderSocket);
65   return result;
66 }
67
68 Handler* handler::Manager::createHandler(comm::ServerSocket* serverSocket)
69 throw(RuntimeException) {
70   handler::ServerSocket* result = a_serverSockets.create();
71   result->setup(serverSocket);
72   return result;
73 }
74
75 Handler* handler::Manager::createHandler(comm::LocalConnection* localConnection)
76 throw(RuntimeException) {
77   handler::LocalConnection* result = a_localConnections.create();
78   result->setup(localConnection);
79   return result;
80 }
81
82 Handler* handler::Manager::createHandler(comm::RemoteConnection* remoteConnection)
83 throw(RuntimeException) {
84   handler::RemoteConnection* result = a_remoteConnections.create();
85   result->setup(remoteConnection);
86   return result;
87 }
88
89 Handler* handler::Manager::createHandler(comm::DatagramSocket* datagramSocket)
90 throw(RuntimeException) {
91   handler::DatagramSocket* result = a_datagramSockets.create();
92   result->setup(datagramSocket);
93   return result;
94 }
95
96 Handler* handler::Manager::createHandler(comm::ClientSocket* clientSocket)
97 throw(RuntimeException) {
98   handler::ClientSocket* result = a_clientSockets.create();
99   result->setup(clientSocket);
100   return result;
101 }
102
103 void handler::Manager::releaseHandler(Handler* handler)
104 throw() {
105   if(handler == NULL)
106     return;
107
108   switch(handler->getType()) {
109   case Handler::Type::BinderSocket:
110     a_binderSockets.release(static_cast <BinderSocket*>(handler));
111     break;
112   case Handler::Type::ServerSocket:
113     a_serverSockets.release(static_cast <ServerSocket*>(handler));
114     break;
115   case Handler::Type::LocalConnection:
116     a_localConnections.release(static_cast <LocalConnection*>(handler));
117     break;
118   case Handler::Type::RemoteConnection:
119     a_remoteConnections.release(static_cast <RemoteConnection*>(handler));
120     break;
121   case Handler::Type::DatagramSocket:
122     a_datagramSockets.release(static_cast <DatagramSocket*>(handler));
123     break;
124   case Handler::Type::ClientSocket:
125     a_clientSockets.release(static_cast <ClientSocket*>(handler));
126     break;
127   }
128 }