Fix xml representations
[anna.git] / source / comm / INetAddress.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/comm/functions.hpp>
10
11 #include <anna/comm/INetAddress.hpp>
12 #include <anna/comm/Device.hpp>
13
14 #include <anna/xml/Node.hpp>
15 #include <anna/xml/Attribute.hpp>
16
17 using namespace std;
18 using namespace anna;
19
20 const comm::Device* comm::INetAddress::getDevice(const bool exceptionWhenNull) const
21 throw(RuntimeException) {
22   if(a_device == NULL && exceptionWhenNull == true) {
23     string msg(asString());
24     msg += " | No device attached";
25     throw RuntimeException(msg, ANNA_FILE_LOCATION);
26   }
27
28   return a_device;
29 }
30
31 string comm::INetAddress::asString() const
32 throw() {
33   string result("comm::INetAddress { ");
34   result += functions::asString(a_device);
35   return result += anna::functions::asString(" | Port: %d }", a_port);
36 }
37
38 string comm::INetAddress::serialize() const
39 throw() {
40   in_addr_t inaddr = (a_device) ? a_device->getAddress() : 0;
41   string result(Device::asString(inaddr));
42   return result += anna::functions::asString(".%d", a_port);
43 }
44
45 xml::Node* comm::INetAddress::asXML(xml::Node* parent) const
46 throw(RuntimeException) {
47   xml::Node* result = parent->createChild("comm.INetAddress");
48
49   if(a_device != NULL)
50     a_device->asAttribute(result);
51   else
52     result->createAttribute("Device", "[null]");
53
54   result->createAttribute("Port", a_port);
55   return result;
56 }
57