github.com/cyverse/go-irodsclient@v0.13.2/irods/message/host.go (about)

     1  package message
     2  
     3  import (
     4  	"encoding/xml"
     5  	"net"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/cyverse/go-irodsclient/irods/types"
    10  	"golang.org/x/xerrors"
    11  )
    12  
    13  // IRODSMessageHost stores startup message
    14  type IRODSMessageHost struct {
    15  	XMLName  xml.Name `xml:"RHostAddr_PI"`
    16  	Addr     string   `xml:"hostAddr"`
    17  	Zone     string   `xml:"rodsZone"`
    18  	Port     int      `xml:"port"`
    19  	DummyInt int      `xml:"dummyInt"`
    20  }
    21  
    22  // NewIRODSMessageHost creates a IRODSMessageHost message
    23  func NewIRODSMessageHost(resource *types.IRODSResource) (*IRODSMessageHost, error) {
    24  	addr := resource.Location
    25  	port := 1247
    26  
    27  	if strings.Contains(resource.Location, ":") {
    28  		newAddr, portStr, err := net.SplitHostPort(resource.Location)
    29  		if err != nil {
    30  			return nil, xerrors.Errorf("failed to split host port: %w", err)
    31  		}
    32  
    33  		port, err = strconv.Atoi(portStr)
    34  		if err != nil {
    35  			return nil, xerrors.Errorf("failed to convert ascii '%s' to int: %w", portStr, err)
    36  		}
    37  
    38  		addr = newAddr
    39  	}
    40  
    41  	return &IRODSMessageHost{
    42  		Addr: addr,
    43  		Zone: resource.Zone,
    44  		Port: port,
    45  	}, nil
    46  }
    47  
    48  // GetBytes returns byte array
    49  func (msg *IRODSMessageHost) GetBytes() ([]byte, error) {
    50  	xmlBytes, err := xml.Marshal(msg)
    51  	if err != nil {
    52  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    53  	}
    54  	return xmlBytes, nil
    55  }
    56  
    57  // FromBytes returns struct from bytes
    58  func (msg *IRODSMessageHost) FromBytes(bytes []byte) error {
    59  	err := xml.Unmarshal(bytes, msg)
    60  	if err != nil {
    61  		return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err)
    62  	}
    63  	return nil
    64  }