1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
12 #ifndef MAXHOSTNAMELEN
13 #define MAXHOSTNAMELEN 64
14 #pragma Definiendo MAXHOSTNAMELEN
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>
27 comm::Application& comm::functions::getApp()
28 throw(RuntimeException) {
29 if(comm::Application::st_application == NULL)
30 throw RuntimeException("No Application class has been defined", ANNA_FILE_LOCATION);
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);
35 return *(static_cast <comm::Application*>(comm::Application::st_application));
38 string comm::functions::getHostName()
39 throw(RuntimeException) {
40 char hostName [MAXHOSTNAMELEN];
42 if(gethostname(hostName, MAXHOSTNAMELEN) != 0)
43 throw RuntimeException("Cannot obtain the hostname", errno, ANNA_FILE_LOCATION);
45 return string(hostName);
51 std::string comm::functions::resolveIP(const char* hostname)
52 throw(RuntimeException) {
56 if((host = gethostbyname(hostname)) == NULL) {
57 string msg("comm::functions::resolveIP | Host to resolve: ");
60 msg += hstrerror(h_errno);
61 throw RuntimeException(msg, ANNA_FILE_LOCATION);
64 if(host->h_addrtype != AF_INET) {
65 string msg("comm::functions::resolveIP | Host to resolve: ");
67 msg += " | Address type unsupported";
68 throw RuntimeException(msg, ANNA_FILE_LOCATION);
72 string msg("comm::functions::resolveIP | Host to resolve: ");
75 if(host->h_name != NULL) { msg += " | Official name: "; msg += host->h_name; }
76 if(host->h_aliases[0] != NULL) {
79 for(int i = 0; host->h_aliases[i] != NULL; i ++) { msg += " "; msg += host->h_aliases[i]; }
81 msg += " | Address type: ";
82 msg += functions::asString(host->h_addrtype);
83 msg += " | Address length: ";
84 msg += functions::asString(host->h_length);
86 if(host->h_addr_list[0] != NULL) {
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])); }
91 Logger::debug(msg, ANNA_FILE_LOCATION);
94 if(host->h_addr_list[0] == NULL) {
95 string msg("comm::functions::resolveIP | Host to resolve: ");
97 msg += " | Address not resolved by the system";
98 throw RuntimeException(msg, ANNA_FILE_LOCATION);
102 result = inet_ntoa(*reinterpret_cast <in_addr*>(host->h_addr_list[0])); // inet_ntoa (const in_addr_t& address)