github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/endpoint.go (about)

     1  package hedera
     2  
     3  /*-
     4   *
     5   * Hedera Go SDK
     6   *
     7   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
     8   *
     9   * Licensed under the Apache License, Version 2.0 (the "License");
    10   * you may not use this file except in compliance with the License.
    11   * You may obtain a copy of the License at
    12   *
    13   *      http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing, software
    16   * distributed under the License is distributed on an "AS IS" BASIS,
    17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18   * See the License for the specific language governing permissions and
    19   * limitations under the License.
    20   *
    21   */
    22  
    23  import (
    24  	"fmt"
    25  
    26  	"github.com/hashgraph/hedera-protobufs-go/services"
    27  )
    28  
    29  type Endpoint struct {
    30  	address    []byte
    31  	port       int32
    32  	domainName string
    33  }
    34  
    35  func (endpoint *Endpoint) SetAddress(address []byte) *Endpoint {
    36  	endpoint.address = address
    37  	return endpoint
    38  }
    39  
    40  func (endpoint *Endpoint) GetAddress() []byte {
    41  	return endpoint.address
    42  }
    43  
    44  func (endpoint *Endpoint) SetPort(port int32) *Endpoint {
    45  	endpoint.port = port
    46  	return endpoint
    47  }
    48  
    49  func (endpoint *Endpoint) GetPort() int32 {
    50  	return endpoint.port
    51  }
    52  
    53  func (endpoint *Endpoint) SetDomainName(domainName string) *Endpoint {
    54  	endpoint.domainName = domainName
    55  	return endpoint
    56  }
    57  
    58  func (endpoint *Endpoint) GetDomainName() string {
    59  	return endpoint.domainName
    60  }
    61  
    62  func EndpointFromProtobuf(serviceEndpoint *services.ServiceEndpoint) Endpoint {
    63  	port := serviceEndpoint.GetPort()
    64  
    65  	if port == 0 || port == 50111 {
    66  		port = 50211
    67  	}
    68  
    69  	return Endpoint{
    70  		address:    serviceEndpoint.GetIpAddressV4(),
    71  		port:       port,
    72  		domainName: serviceEndpoint.GetDomainName(),
    73  	}
    74  }
    75  
    76  func (endpoint *Endpoint) _ToProtobuf() *services.ServiceEndpoint {
    77  	return &services.ServiceEndpoint{
    78  		IpAddressV4: endpoint.address,
    79  		Port:        endpoint.port,
    80  		DomainName:  endpoint.domainName,
    81  	}
    82  }
    83  
    84  func (endpoint *Endpoint) String() string {
    85  	if endpoint.domainName != "" {
    86  		// If domain name is populated domainName + port
    87  		return endpoint.domainName + ":" + fmt.Sprintf("%d", endpoint.port)
    88  	} else {
    89  		return fmt.Sprintf("%d.%d.%d.%d:%d",
    90  			int(endpoint.address[0])&0xFF,
    91  			int(endpoint.address[1])&0xFF,
    92  			int(endpoint.address[2])&0xFF,
    93  			int(endpoint.address[3])&0xFF,
    94  			endpoint.port)
    95  	}
    96  }