From 2d6937e0515c46e5870dbc8a613849432d293f6f Mon Sep 17 00:00:00 2001 From: Gavin Norman Date: Fri, 27 Jul 2018 11:30:32 +0200 Subject: [PATCH] Add methods to set AddrPort from NodeItem and convert back As part of replacing internal usages of NodeItem with AddrPort, it's useful to have a convenient way of converting from one to the other. Part of #348. --- relnotes/addrport.feature.md | 12 ++++++++++ src/swarm/neo/AddrPort.d | 43 +++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 relnotes/addrport.feature.md diff --git a/relnotes/addrport.feature.md b/relnotes/addrport.feature.md new file mode 100644 index 00000000..42d9163d --- /dev/null +++ b/relnotes/addrport.feature.md @@ -0,0 +1,12 @@ +### Methods to convert between `AddrPort` and `NodeItem` + +`swarm.neo.AddrPort` + +Swarm uses two address/port representations internally (for historical reasons). +In the future, we will remove the old `NodeItem`, but for now it's useful to +have convenient methods for converting back and forth. + +`AddrPort` now has the following new methods: + * `typeof(this) set ( NodeItem node_item )` + * `NodeItem asNodeItem ( ref mstring buf )` + diff --git a/src/swarm/neo/AddrPort.d b/src/swarm/neo/AddrPort.d index bb614501..c2029db9 100644 --- a/src/swarm/neo/AddrPort.d +++ b/src/swarm/neo/AddrPort.d @@ -25,11 +25,15 @@ public struct AddrPort import core.sys.posix.arpa.inet: ntohl, ntohs, htonl, htons, inet_ntop; import core.stdc.string: strlen; - import ocean.util.container.map.model.StandardHash; + import swarm.Const : NodeItem; + import ocean.util.container.map.model.StandardHash; import ocean.core.Test; import swarm.util.Verify; + /// Minimum length required for an address format buffer. + public static const AddrBufLength = INET_ADDRSTRLEN; + /*************************************************************************** Node address & port in network byte order, like they are stored in POSIX @@ -270,6 +274,43 @@ public struct AddrPort return this; } + /*************************************************************************** + + Sets the address and port of this instance to those in node_item. + + Params: + node_item = the input address and port + + Returns: + this instance + + ***************************************************************************/ + + public typeof(this) set ( NodeItem node_item ) + { + this.port = node_item.Port; + this.setAddress(node_item.Address); + return this; + } + + /*************************************************************************** + + Gets the address and port of this instance in the format of a NodeItem. + + Params: + buf = buffer used to format the address. Must be at least + AddrBufLength bytes long + + Returns: + NodeItem instance + + ***************************************************************************/ + + public NodeItem asNodeItem ( ref mstring buf ) + { + return NodeItem(this.getAddress(buf), this.port); + } + /*************************************************************************** Packs the address and port of this instance in a long value: Bits