github.com/braveheart12/insolar-09-08-19@v0.8.7/network/transport/host/host.go (about)

     1  /*
     2   * The Clear BSD License
     3   *
     4   * Copyright (c) 2019 Insolar Technologies
     5   *
     6   * All rights reserved.
     7   *
     8   * Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
     9   *
    10   *  Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    11   *  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    12   *  Neither the name of Insolar Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    13   *
    14   * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    15   *
    16   */
    17  
    18  package host
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"github.com/insolar/insolar/core"
    24  	"github.com/pkg/errors"
    25  )
    26  
    27  // Host is the over-the-wire representation of a host.
    28  type Host struct {
    29  	// NodeID is unique identifier of the node
    30  	NodeID core.RecordRef
    31  	// ShortID is shortened unique identifier of the node inside the globe
    32  	ShortID core.ShortNodeID
    33  	// Address is IP and port.
    34  	Address *Address
    35  }
    36  
    37  // NewHost creates a new Host with specified physical address.
    38  func NewHost(address string) (*Host, error) {
    39  	addr, err := NewAddress(address)
    40  	if err != nil {
    41  		return nil, errors.Wrap(err, "Failed to create Host")
    42  	}
    43  	return &Host{Address: addr}, nil
    44  }
    45  
    46  // NewHostN creates a new Host with specified physical address and NodeID.
    47  func NewHostN(address string, nodeID core.RecordRef) (*Host, error) {
    48  	h, err := NewHost(address)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	h.NodeID = nodeID
    53  	return h, nil
    54  }
    55  
    56  // NewHostNS creates a new Host with specified physical address, NodeID and ShortID.
    57  func NewHostNS(address string, nodeID core.RecordRef, shortID core.ShortNodeID) (*Host, error) {
    58  	h, err := NewHostN(address, nodeID)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	h.ShortID = shortID
    63  	return h, nil
    64  }
    65  
    66  // String representation of Host.
    67  func (host Host) String() string {
    68  	return fmt.Sprintf("%s (%s)", host.NodeID.String(), host.Address.String())
    69  }
    70  
    71  // Equal checks if host equals to other host (e.g. hosts' IDs and network addresses match).
    72  func (host Host) Equal(other Host) bool {
    73  	return host.NodeID.Equal(other.NodeID) && (other.Address != nil) && host.Address.Equal(*other.Address)
    74  }