github.com/bhojpur/cache@v0.0.4/pkg/file/types/nodeid.go (about)

     1  package types
     2  
     3  // Copyright (c) 2018 Bhojpur Consulting Private Limited, India. All rights reserved.
     4  
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  
    12  // The above copyright notice and this permission notice shall be included in
    13  // all copies or substantial portions of the Software.
    14  
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    21  // THE SOFTWARE.
    22  
    23  import (
    24  	"encoding/json"
    25  	"errors"
    26  	"net"
    27  
    28  	"github.com/bhojpur/cache/pkg/file/crypto"
    29  )
    30  
    31  var (
    32  	// ErrInvalidIP is an error thrown when the ip to construct a NodeID is invalid.
    33  	ErrInvalidIP = errors.New("IP address to construct a NodeID is invalid")
    34  
    35  	// ErrInvalidPort is an error thrown when the port to construct a NodeID is invalid.
    36  	ErrInvalidPort = errors.New("port to construct a NodeID is invalid")
    37  )
    38  
    39  // NodeID contains the necessary data for referencing and connecting to a node.
    40  type NodeID struct {
    41  	IP   string       `json:"ip"`   // The node's IP address
    42  	Port int          `json:"port"` // The port on which the node is hosted
    43  	Hash *crypto.Hash `json:"hash"` // The hash of the node
    44  }
    45  
    46  // NewNodeID constructs a new NodeID.
    47  func NewNodeID(ip string, port int) (*NodeID, error) {
    48  	// Check that the IP address is valid
    49  	addr := net.ParseIP(ip)
    50  	if addr == nil {
    51  		return nil, ErrInvalidIP
    52  	}
    53  
    54  	// Check that the port is valid
    55  	if port == 0 {
    56  		return nil, ErrInvalidPort
    57  	}
    58  
    59  	// Create the NodeID
    60  	newNodeID := &NodeID{
    61  		IP:   ip,
    62  		Port: port,
    63  		Hash: nil,
    64  	}
    65  
    66  	hash := crypto.Sha3(newNodeID.Bytes())
    67  	newNodeID.Hash = &hash
    68  
    69  	return newNodeID, nil
    70  }
    71  
    72  /* ----- BEGIN HELPER FUNCTIONS ----- */
    73  
    74  // Bytes returns the bytes of a NodeID.
    75  func (nodeID *NodeID) Bytes() []byte {
    76  	json, _ := json.MarshalIndent(*nodeID, "", "  ")
    77  	return json
    78  }
    79  
    80  // String converts the NodeID to a string.
    81  func (nodeID *NodeID) String() string {
    82  	json, _ := json.MarshalIndent(*nodeID, "", "  ")
    83  	return string(json)
    84  }
    85  
    86  /* ----- END HELPER FUNCTIONS ----- */