Spinning Topp Logo BlackTopp Studios
inc
systemaddress.cpp
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 
41 #ifndef _networksystemaddress_cpp
42 #define _networksystemaddress_cpp
43 
44 #include "Network/platformincludes.h.cpp"
45 
46 #include "Network/systemaddress.h"
47 
48 #include "exception.h"
49 #include "serialization.h"
50 
51 namespace Mezzanine
52 {
53  namespace Network
54  {
56  V6Flow(0),
57  V6Scope(0),
58  Port(0)
59  { }
60 
61  SystemAddress::SystemAddress(const IPAddress& TargetAddress) :
62  Address(TargetAddress),
63  V6Flow(0),
64  V6Scope(0),
65  Port(0)
66  { }
67 
68  SystemAddress::SystemAddress(const UInt16 TargetPort, const Boole NBO) :
69  V6Flow(0),
70  V6Scope(0)
71  { this->SetPort(TargetPort,NBO); }
72 
73  SystemAddress::SystemAddress(const IPAddress& TargetAddress, const UInt16 TargetPort, const Boole NBO) :
74  Address(TargetAddress),
75  V6Flow(0),
76  V6Scope(0)
77  { this->SetPort(TargetPort,NBO); }
78 
80  Address(Other.Address),
81  V6Flow(Other.V6Flow),
82  V6Scope(Other.V6Scope),
83  Port(Other.Port)
84  { }
85 
87  { }
88 
89  ///////////////////////////////////////////////////////////////////////////////
90  // Utility
91 
92  void SystemAddress::SetAddress(const IPAddress& TargetAddress)
93  { this->Address = TargetAddress; }
94 
96  { return this->Address; }
97 
99  { return this->Address; }
100 
102  { this->V6Flow = Flow; }
103 
105  { return this->V6Flow; }
106 
108  { this->V6Scope = Scope; }
109 
111  { return this->V6Scope; }
112 
113  void SystemAddress::SetPort(const UInt16 TargetPort, const Boole NBO)
114  { this->Port = ( NBO ? TargetPort : htons(TargetPort) ); }
115 
117  { return ( NBO ? this->Port : ntohs(this->Port) ); }
118 
119  ///////////////////////////////////////////////////////////////////////////////
120  // Operators
121 
123  {
124  this->Address = Other.Address;
125  this->Port = Other.Port;
126  this->V6Flow = Other.V6Flow;
127  this->V6Scope = Other.V6Scope;
128  }
129 
131  {
132  return ( this->Address == Other.Address ) &&
133  ( this->Port == Other.Port ) &&
134  ( this->V6Flow == Other.V6Flow ) &&
135  ( this->V6Scope == Other.V6Scope );
136  }
137 
139  {
140  return ( this->Address != Other.Address ) ||
141  ( this->Port != Other.Port ) ||
142  ( this->V6Flow != Other.V6Flow ) ||
143  ( this->V6Scope != Other.V6Scope );
144  }
145 
146  ///////////////////////////////////////////////////////////////////////////////
147  // Serialization
148 
149  void SystemAddress::ProtoSerialize(XML::Node& ParentNode) const
150  {
151  XML::Node SelfRoot = ParentNode.AppendChild( SystemAddress::GetSerializableName() );
152 
153  if( SelfRoot.AppendAttribute("Version").SetValue("1") &&
154  SelfRoot.AppendAttribute("Port").SetValue( this->GetPort() ) &&
155  SelfRoot.AppendAttribute("V6Flow").SetValue( this->V6Flow ) &&
156  SelfRoot.AppendAttribute("V6Scope").SetValue( this->V6Scope ) )
157  {
158  this->Address.ProtoSerialize(SelfRoot);
159 
160  return;
161  }else{
162  SerializeError("Create XML Attribute Values",SystemAddress::GetSerializableName(),true);
163  }
164  }
165 
167  {
168  XML::Attribute CurrAttrib;
169 
170  if( !SelfRoot.Empty() && SelfRoot.Name() == SystemAddress::GetSerializableName() ) {
171  if( SelfRoot.GetAttribute("Version").AsInt() == 1 ) {
172  CurrAttrib = SelfRoot.GetAttribute("Port");
173  if( !CurrAttrib.Empty() )
174  this->SetPort( static_cast<UInt16>( CurrAttrib.AsUint() ) );
175 
176  CurrAttrib = SelfRoot.GetAttribute("V6Flow");
177  if( !CurrAttrib.Empty() )
178  this->V6Flow = static_cast<UInt32>( CurrAttrib.AsUint() );
179 
180  CurrAttrib = SelfRoot.GetAttribute("V6Scope");
181  if( !CurrAttrib.Empty() )
182  this->V6Scope = static_cast<UInt32>( CurrAttrib.AsUint() );
183 
184  XML::Node AddressNode = SelfRoot.GetFirstChild();
185  if( !AddressNode.Empty() && AddressNode.Name() == IPAddress::GetSerializableName() ) {
186  this->Address.ProtoDeSerialize(AddressNode);
187  }
188  }else{
189  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + SystemAddress::GetSerializableName() + ": Not Version 1.");
190  }
191  }else{
192  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,SystemAddress::GetSerializableName() + " was not found in the provided XML node, which was expected.");
193  }
194  }
195 
197  { return "SystemAddress"; }
198  }
199 }
200 
201 #endif
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
void SetPort(const UInt16 TargetPort, const Boole NBO=false)
Sets the Port portion of the target system address.
void ProtoDeSerialize(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite this object with it.
Definition: ipaddress.cpp:250
const IPAddress & GetAddress() const
Gets the IP portion of the target system address.
Thrown when the requested identity could not be found.
Definition: exception.h:94
UInt32 V6Flow
The (optional) IPv6 Flow value, used for routing IPv6 packets.
Definition: systemaddress.h:61
Node GetFirstChild() const
Get the first child Node of this Node.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
void ProtoDeSerialize(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite this object with it.
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
Boole operator==(const SystemAddress &Other) const
Equality operator.
UInt32 GetV6Flow() const
Gets the flow information to be used for routing packets to the target system.
bool Empty() const
Is this storing anything at all?
This implements the exception hiearchy for Mezzanine.
The interface for serialization.
void SetV6Scope(const UInt32 Scope)
Sets the scope-id for the address.
bool SetValue(const Char8 *rhs)
Set the value of this.
uint16_t UInt16
An 16-bit unsigned integer.
Definition: datatypes.h:122
static String GetSerializableName()
Get the name of the the XML tag the SystemAddress class will leave behind as its instances are serial...
SystemAddress()
Class constructor.
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
unsigned int AsUint(unsigned int def=0) const
Attempts to convert the value of the attribute to an unsigned int and returns the results...
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
UInt32 GetV6Scope() const
Gets the scope-id for the address.
bool Empty() const
Is this storing anything at all?
This is a simple class for representing IP addresses used throughout the Network subsystem.
Definition: ipaddress.h:57
UInt32 V6Scope
The (optional) IPv6 Scope-ID, used to describe the reach of the address.
Definition: systemaddress.h:64
void ProtoSerialize(XML::Node &ParentNode) const
Convert this class to an XML::Node ready for serialization.
~SystemAddress()
Class destructor.
IPAddress Address
The IPAddress of the target system.
Definition: systemaddress.h:58
UInt16 Port
The open port of the target system.
Definition: systemaddress.h:67
UInt16 GetPort(const Boole NBO=false) const
Gets the Port portion of the target system address.
void operator=(const SystemAddress &Other)
Assignment operator.
Boole operator!=(const SystemAddress &Other) const
Inequality operator.
void SetV6Flow(const UInt32 Flow)
Sets the flow information to be used.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
void SetAddress(const IPAddress &TargetAddress)
Sets the IP portion of the target system address.
void ProtoSerialize(XML::Node &ParentNode) const
Convert this class to an XML::Node ready for serialization.
Definition: ipaddress.cpp:237
const Char8 * Name() const
ptrdiff_tGet the name of this Node.
void SerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
static String GetSerializableName()
Get the name of the the XML tag the IPAddress class will leave behind as its instances are serialized...
Definition: ipaddress.cpp:267
Node AppendChild(NodeType Type=NodeElement)
Creates a Node and makes it a child of this one.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
A simple class that stores a complete set of information for establishing connections.
Definition: systemaddress.h:53
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.