github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/chapar/path.go (about) 1 /* For license and copyright information please see LEGAL file in repository */ 2 3 package chapar 4 5 import ( 6 "io" 7 8 "../protocol" 9 ) 10 11 // Path indicate Chapar switch route plan! 12 type Path struct { 13 path [MaxHopCount]byte 14 len byte 15 } 16 17 // Init sets path from the given frame 18 func (p *Path) Init(frame []byte) { 19 if len(frame) == 0 { 20 p.len = MaxHopCount // broadcast frame 21 } else { 22 var hopCount = GetHopCount(frame) 23 copy(p.path[:], frame[FixedHeaderLength:FixedHeaderLength+hopCount]) 24 p.len = hopCount 25 } 26 } 27 28 func (p *Path) Set(path []byte) { 29 copy(p.path[:], path) 30 p.len = byte(len(path)) 31 } 32 33 func (p *Path) Get() []byte { 34 return p.path[:p.len] 35 } 36 37 func (p *Path) GetAsString() string { 38 // TODO::: ?? 39 return string(p.path[:p.len]) 40 } 41 42 func (p *Path) LenAsByte() byte { 43 return p.len 44 } 45 46 // GetReverse return path in all hops just in reverse. 47 func (p *Path) GetReverse() (reverse Path) { 48 reverse = Path{ 49 path: ReversePath(p.path[:]), 50 len: p.len, 51 } 52 return 53 } 54 55 /* 56 ********** protocol.Codec interface ********** 57 */ 58 59 func (p *Path) MediaType() protocol.MediaType { return nil } 60 func (p *Path) CompressType() protocol.CompressType { return nil } 61 func (p *Path) Len() int { return int(p.len) } 62 63 // Marshal return the path in the given frame. 64 func (p *Path) Decode(reader io.Reader) (err protocol.Error) { 65 return 66 } 67 68 // Marshal return the path in the given frame. 69 func (p *Path) Encode(writer io.Writer) (err error) { 70 return 71 } 72 73 // Unmarshal sets path from the given path 74 func (p *Path) Unmarshal(path []byte) (err protocol.Error) { 75 if len(path) == 0 { 76 p.len = MaxHopCount // broadcast frame 77 } else { 78 copy(p.path[:], path) 79 p.len = byte(len(path)) 80 } 81 } 82 83 // Marshal return the path in the given frame. 84 func (p *Path) Marshal() (path []byte) { 85 return p.path[:p.len] 86 } 87 88 // MarshalTo sets the path in the given frame. 89 func (p *Path) MarshalTo(frame []byte) { 90 copy(frame[FixedHeaderLength:], p.path[:p.len]) 91 }