Fix local server for multiple applications
[anna.git] / comm / handler / Manager.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 #include <anna/core/mt/Guard.hpp>
10
11 #include <anna/comm/handler/Manager.hpp>
12
13 using namespace anna;
14 using namespace anna::comm;
15
16 template <> comm::Communicator* handler::Manager::BinderSocketAllocator::st_communicator = NULL;
17 template <> comm::Communicator* handler::Manager::ServerSocketAllocator::st_communicator = NULL;
18 template <> comm::Communicator* handler::Manager::LocalConnectionAllocator::st_communicator = NULL;
19 template <> comm::Communicator* handler::Manager::RemoteConnectionAllocator::st_communicator = NULL;
20 template <> comm::Communicator* handler::Manager::DatagramSocketAllocator::st_communicator = NULL;
21 template <> comm::Communicator* handler::Manager::ClientSocketAllocator::st_communicator = NULL;
22
23 void handler::Manager::initialize(Communicator* communicator)
24 throw() {
25   BinderSocketAllocator::st_communicator = communicator;
26   ServerSocketAllocator::st_communicator = communicator;
27   LocalConnectionAllocator::st_communicator = communicator;
28   RemoteConnectionAllocator::st_communicator = communicator;
29   DatagramSocketAllocator::st_communicator = communicator;
30   ClientSocketAllocator::st_communicator = communicator;
31 }
32
33 Handler* handler::Manager::createHandler(comm::BinderSocket* binderSocket)
34 throw(RuntimeException) {
35   handler::BinderSocket* result = a_binderSockets.create();
36   result->setup(binderSocket);
37   return result;
38 }
39
40 Handler* handler::Manager::createHandler(comm::ServerSocket* serverSocket)
41 throw(RuntimeException) {
42   handler::ServerSocket* result = a_serverSockets.create();
43   result->setup(serverSocket);
44   return result;
45 }
46
47 Handler* handler::Manager::createHandler(comm::LocalConnection* localConnection)
48 throw(RuntimeException) {
49   handler::LocalConnection* result = a_localConnections.create();
50   result->setup(localConnection);
51   return result;
52 }
53
54 Handler* handler::Manager::createHandler(comm::RemoteConnection* remoteConnection)
55 throw(RuntimeException) {
56   handler::RemoteConnection* result = a_remoteConnections.create();
57   result->setup(remoteConnection);
58   return result;
59 }
60
61 Handler* handler::Manager::createHandler(comm::DatagramSocket* datagramSocket)
62 throw(RuntimeException) {
63   handler::DatagramSocket* result = a_datagramSockets.create();
64   result->setup(datagramSocket);
65   return result;
66 }
67
68 Handler* handler::Manager::createHandler(comm::ClientSocket* clientSocket)
69 throw(RuntimeException) {
70   handler::ClientSocket* result = a_clientSockets.create();
71   result->setup(clientSocket);
72   return result;
73 }
74
75 void handler::Manager::releaseHandler(Handler* handler)
76 throw() {
77   if(handler == NULL)
78     return;
79
80   switch(handler->getType()) {
81   case Handler::Type::BinderSocket:
82     a_binderSockets.release(static_cast <BinderSocket*>(handler));
83     break;
84   case Handler::Type::ServerSocket:
85     a_serverSockets.release(static_cast <ServerSocket*>(handler));
86     break;
87   case Handler::Type::LocalConnection:
88     a_localConnections.release(static_cast <LocalConnection*>(handler));
89     break;
90   case Handler::Type::RemoteConnection:
91     a_remoteConnections.release(static_cast <RemoteConnection*>(handler));
92     break;
93   case Handler::Type::DatagramSocket:
94     a_datagramSockets.release(static_cast <DatagramSocket*>(handler));
95     break;
96   case Handler::Type::ClientSocket:
97     a_clientSockets.release(static_cast <ClientSocket*>(handler));
98     break;
99   default: break;
100   }
101 }