Remove dynamic exceptions
[anna.git] / source / comm / functions.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 <unistd.h>
10 #include <netdb.h>
11
12 #ifndef MAXHOSTNAMELEN
13 #define MAXHOSTNAMELEN 64
14 #pragma Definiendo MAXHOSTNAMELEN
15 #endif
16
17 #include <anna/comm/functions.hpp>
18 #include <anna/comm/Application.hpp>
19 #include <anna/comm/Communicator.hpp>
20 #include <anna/comm/Service.hpp>
21 #include <anna/comm/Server.hpp>
22 #include <anna/core/tracing/Logger.hpp>
23
24 using namespace std;
25 using namespace anna;
26
27 comm::Application& comm::functions::getApp()
28 noexcept(false) {
29   if(comm::Application::st_application == NULL)
30     throw RuntimeException("No Application class has been defined", ANNA_FILE_LOCATION);
31
32   if(comm::Application::st_application->supportCommunication() == false)
33     throw RuntimeException("Defined Application class has no communications support. Must inherit from anna::comm::Application or superior", ANNA_FILE_LOCATION);
34
35   return *(static_cast <comm::Application*>(comm::Application::st_application));
36 }
37
38 string comm::functions::getHostName()
39 noexcept(false) {
40   char hostName [MAXHOSTNAMELEN];
41
42   if(gethostname(hostName, MAXHOSTNAMELEN) != 0)
43     throw RuntimeException("Cannot obtain the hostname", errno, ANNA_FILE_LOCATION);
44
45   return string(hostName);
46 }
47
48
49
50
51 std::string comm::functions::resolveIP(const char* hostname)
52 noexcept(false) {
53   std::string result;
54   struct hostent *host;
55
56   if((host = gethostbyname(hostname)) == NULL) {
57     string msg("comm::functions::resolveIP | Host to resolve: ");
58     msg += hostname;
59     msg += " | ";
60     msg += hstrerror(h_errno);
61     throw RuntimeException(msg, ANNA_FILE_LOCATION);
62   }
63
64   if(host->h_addrtype != AF_INET) {
65     string msg("comm::functions::resolveIP | Host to resolve: ");
66     msg += hostname;
67     msg += " | Address type unsupported";
68     throw RuntimeException(msg, ANNA_FILE_LOCATION);
69   }
70
71   LOGDEBUG(
72     string msg("comm::functions::resolveIP | Host to resolve: ");
73     msg += hostname;
74
75   if(host->h_name != NULL) { msg += " | Official name: "; msg += host->h_name; }
76 if(host->h_aliases[0] != NULL) {
77   msg += " | Aliases:";
78
79   for(int i = 0; host->h_aliases[i] != NULL; i ++) { msg += " "; msg += host->h_aliases[i]; }
80   }
81   msg += " | Address type: ";
82   msg += functions::asString(host->h_addrtype);
83   msg += " | Address length: ";
84   msg += functions::asString(host->h_length);
85
86   if(host->h_addr_list[0] != NULL) {
87   msg += " | IPs:";
88
89   for(int i = 0; host->h_addr_list[i] != NULL; i ++) { msg += " "; msg += inet_ntoa(*reinterpret_cast <in_addr*>(host->h_addr_list[i])); }
90   }
91   Logger::debug(msg, ANNA_FILE_LOCATION);
92   );
93
94   if(host->h_addr_list[0] == NULL) {
95     string msg("comm::functions::resolveIP | Host to resolve: ");
96     msg += hostname;
97     msg += " | Address not resolved by the system";
98     throw RuntimeException(msg, ANNA_FILE_LOCATION);
99   }
100
101   // Assignment:
102   result = inet_ntoa(*reinterpret_cast <in_addr*>(host->h_addr_list[0]));   // inet_ntoa (const in_addr_t& address)
103   return result;
104 }
105