github.com/cyverse/go-irodsclient@v0.13.2/irods/message/startup_pack.go (about) 1 package message 2 3 import ( 4 "encoding/xml" 5 "fmt" 6 7 "github.com/cyverse/go-irodsclient/irods/common" 8 "github.com/cyverse/go-irodsclient/irods/types" 9 "golang.org/x/xerrors" 10 ) 11 12 const ( 13 // RODS_MESSAGE_CONNECT_TYPE is a message type for establishing a new connection 14 RODS_MESSAGE_CONNECT_TYPE MessageType = "RODS_CONNECT" 15 // RequestNegotiationOptionString is an option string for requesting server negotiation 16 RequestNegotiationOptionString string = "request_server_negotiation" 17 ) 18 19 // IRODSMessageStartupPack stores startup message 20 type IRODSMessageStartupPack struct { 21 XMLName xml.Name `xml:"StartupPack_PI"` 22 Protocol int `xml:"irodsProt"` 23 ReconnectFlag int `xml:"reconnFlag"` 24 ConnectionCount int `xml:"connectCnt"` 25 ProxyUser string `xml:"proxyUser"` 26 ProxyRcatZone string `xml:"proxyRcatZone"` 27 ClientUser string `xml:"clientUser"` 28 ClientRcatZone string `xml:"clientRcatZone"` 29 ReleaseVersion string `xml:"relVersion"` 30 APIVersion string `xml:"apiVersion"` 31 Option string `xml:"option"` 32 } 33 34 // NewIRODSMessageStartupPack creates a IRODSMessageStartupPack message 35 func NewIRODSMessageStartupPack(account *types.IRODSAccount, option string, requireNegotiation bool) *IRODSMessageStartupPack { 36 optionString := option 37 if requireNegotiation { 38 // append a flag 39 optionString = fmt.Sprintf("%s;%s", optionString, RequestNegotiationOptionString) 40 } 41 42 return &IRODSMessageStartupPack{ 43 Protocol: 1, 44 ReleaseVersion: fmt.Sprintf("rods%s", common.IRODSVersionRelease), 45 APIVersion: common.IRODSVersionAPI, 46 ConnectionCount: 0, 47 ReconnectFlag: 0, 48 ProxyUser: account.ProxyUser, 49 ProxyRcatZone: account.ProxyZone, 50 ClientUser: account.ClientUser, 51 ClientRcatZone: account.ClientZone, 52 Option: optionString, 53 } 54 } 55 56 // GetBytes returns byte array 57 func (msg *IRODSMessageStartupPack) GetBytes() ([]byte, error) { 58 xmlBytes, err := xml.Marshal(msg) 59 if err != nil { 60 return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err) 61 } 62 return xmlBytes, nil 63 } 64 65 // FromBytes returns struct from bytes 66 func (msg *IRODSMessageStartupPack) FromBytes(bytes []byte) error { 67 err := xml.Unmarshal(bytes, msg) 68 if err != nil { 69 return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err) 70 } 71 return nil 72 } 73 74 // GetMessage builds a message 75 func (msg *IRODSMessageStartupPack) GetMessage() (*IRODSMessage, error) { 76 bytes, err := msg.GetBytes() 77 if err != nil { 78 return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err) 79 } 80 81 msgBody := IRODSMessageBody{ 82 Type: RODS_MESSAGE_CONNECT_TYPE, 83 Message: bytes, 84 Error: nil, 85 Bs: nil, 86 IntInfo: 0, 87 } 88 89 msgHeader, err := msgBody.BuildHeader() 90 if err != nil { 91 return nil, xerrors.Errorf("failed to build header from irods message: %w", err) 92 } 93 94 return &IRODSMessage{ 95 Header: msgHeader, 96 Body: &msgBody, 97 }, nil 98 } 99 100 // FromMessage returns struct from IRODSMessage 101 func (msg *IRODSMessageStartupPack) FromMessage(msgIn *IRODSMessage) error { 102 if msgIn.Body == nil { 103 return xerrors.Errorf("empty message body") 104 } 105 106 err := msg.FromBytes(msgIn.Body.Message) 107 if err != nil { 108 return xerrors.Errorf("failed to get irods message from message body") 109 } 110 return nil 111 }