Skip to content

Commit

Permalink
Add methods to set AddrPort from NodeItem and convert back
Browse files Browse the repository at this point in the history
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 sociomantic-tsunami#348.
  • Loading branch information
Gavin Norman committed Aug 23, 2018
1 parent e1fa67b commit 2d6937e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
12 changes: 12 additions & 0 deletions relnotes/addrport.feature.md
Original file line number Diff line number Diff line change
@@ -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 )`

43 changes: 42 additions & 1 deletion src/swarm/neo/AddrPort.d
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2d6937e

Please sign in to comment.