go-hep.org/x/hep@v0.38.1/xrootd/xrdproto/prepare/prepare.go (about)

     1  // Copyright ©2018 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package prepare contains the types related to the prepare request.
     6  // See xrootd protocol specification (http://xrootd.org/doc/dev45/XRdv310.pdf, p. 69) for more details.
     7  package prepare // import "go-hep.org/x/hep/xrootd/xrdproto/prepare"
     8  
     9  import (
    10  	"strings"
    11  
    12  	"go-hep.org/x/hep/xrootd/internal/xrdenc"
    13  )
    14  
    15  // Prepare request options.
    16  const (
    17  	Cancel   = 1  // Cancel will cancel a prepare request.
    18  	Notify   = 2  // Notify will send a message when the file has been processed.
    19  	NoErrors = 4  // NoErrors will not send a notification for preparation errors.
    20  	Stage    = 8  // Stage will stage the file to disk if it is not online.
    21  	Write    = 16 // Write will prepare the file with write access.
    22  	Colocate = 32 // Colocate will co-locate the staged files, if at all possible.
    23  	Refresh  = 64 // Refresh will refresh the file access time even when location is known.
    24  )
    25  
    26  // RequestID is the id of the request, it is sent as part of message.
    27  // See xrootd protocol specification for details: http://xrootd.org/doc/dev45/XRdv310.pdf, 2.3 Client Request Format.
    28  const RequestID uint16 = 3021
    29  
    30  // Request holds the prepare request parameters.
    31  type Request struct {
    32  	Options  byte   // Options is a set of flags that apply to each path.
    33  	Priority byte   // Priority the request will have. 0: lowest priority, 3: highest.
    34  	Port     uint16 // UDP port number to which a message is to be sent.
    35  	_        [12]byte
    36  	Paths    []string
    37  }
    38  
    39  // MarshalXrd implements xrdproto.Marshaler.
    40  func (req Request) MarshalXrd(w *xrdenc.WBuffer) error {
    41  	w.WriteU8(req.Options)
    42  	w.WriteU8(req.Priority)
    43  	w.WriteU16(req.Port)
    44  	w.Next(12)
    45  
    46  	var raw []byte
    47  	switch len(req.Paths) {
    48  	case 0:
    49  		// no-op
    50  	default:
    51  		for i, p := range req.Paths {
    52  			if i > 0 {
    53  				raw = append(raw, '\n')
    54  			}
    55  			raw = append(raw, []byte(p)...)
    56  		}
    57  	}
    58  	w.WriteI32(int32(len(raw)))
    59  	w.WriteBytes(raw)
    60  	return nil
    61  }
    62  
    63  // UnmarshalXrd implements xrdproto.Unmarshaler.
    64  func (req *Request) UnmarshalXrd(r *xrdenc.RBuffer) error {
    65  	req.Options = r.ReadU8()
    66  	req.Priority = r.ReadU8()
    67  	req.Port = r.ReadU16()
    68  	r.Skip(12)
    69  	n := r.ReadI32()
    70  	raw := make([]byte, n)
    71  	r.ReadBytes(raw)
    72  	switch n {
    73  	case 0:
    74  		req.Paths = []string{}
    75  	default:
    76  		req.Paths = strings.Split(string(raw), "\n")
    77  	}
    78  	return nil
    79  }
    80  
    81  // ReqID implements xrdproto.Request.ReqID.
    82  func (*Request) ReqID() uint16 { return RequestID }
    83  
    84  // ShouldSign implements xrdproto.Request.ShouldSign.
    85  func (*Request) ShouldSign() bool { return false }
    86  
    87  // Response is the response issued by the server to a prepare request.
    88  type Response struct {
    89  	Data []byte
    90  }
    91  
    92  // RespID implements xrdproto.Response.RespID.
    93  func (resp *Response) RespID() uint16 { return RequestID }
    94  
    95  // MarshalXrd implements xrdproto.Marshaler.
    96  func (o Response) MarshalXrd(w *xrdenc.WBuffer) error {
    97  	w.WriteBytes(o.Data)
    98  	return nil
    99  }
   100  
   101  // UnmarshalXrd implements xrdproto.Unmarshaler.
   102  func (o *Response) UnmarshalXrd(r *xrdenc.RBuffer) error {
   103  	o.Data = make([]byte, r.Len())
   104  	r.ReadBytes(o.Data)
   105  	return nil
   106  }