github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/channels/channel.go (about) 1 package channels 2 3 import ( 4 "regexp" 5 "sort" 6 7 "github.com/onflow/flow-go/model/flow" 8 ) 9 10 // Channel specifies a virtual and isolated communication medium. 11 // Nodes subscribed to the same channel can disseminate epidemic messages among 12 // each other, i.e: multicast and publish. 13 type Channel string 14 type ChannelList []Channel 15 16 func (c Channel) String() string { 17 return string(c) 18 } 19 20 // Len returns length of the ChannelList in the number of stored Channels. 21 // It satisfies the sort.Interface making the ChannelList sortable. 22 func (cl ChannelList) Len() int { 23 return len(cl) 24 } 25 26 // Less returns true if element i in the ChannelList is less than j based on the numerical value of its Channel. 27 // Otherwise it returns true. 28 // It satisfies the sort.Interface making the ChannelList sortable. 29 func (cl ChannelList) Less(i, j int) bool { 30 return cl[i] < cl[j] 31 } 32 33 // Swap swaps the element i and j in the ChannelList. 34 // It satisfies the sort.Interface making the ChannelList sortable. 35 func (cl ChannelList) Swap(i, j int) { 36 cl[i], cl[j] = cl[j], cl[i] 37 } 38 39 // ID returns hash of the content of ChannelList. It first sorts the ChannelList and then takes its 40 // hash value. 41 func (cl ChannelList) ID() flow.Identifier { 42 sort.Sort(cl) 43 return flow.MakeID(cl) 44 } 45 46 // Contains returns true if the ChannelList contains the given channel. 47 func (cl ChannelList) Contains(channel Channel) bool { 48 for _, c := range cl { 49 if c == channel { 50 return true 51 } 52 } 53 return false 54 } 55 56 // ExcludeChannels returns list of channels that are in the ChannelList but not in the other list. 57 func (cl ChannelList) ExcludeChannels(other ChannelList) ChannelList { 58 var result ChannelList 59 for _, c := range cl { 60 if !other.Contains(c) { 61 result = append(result, c) 62 } 63 } 64 return result 65 } 66 67 // ExcludePattern returns a new ChannelList excluding the Channels that satisfy the given predicate. 68 func (cl ChannelList) ExcludePattern(regexp *regexp.Regexp) ChannelList { 69 var result ChannelList 70 for _, c := range cl { 71 if regexp.MatchString(c.String()) { 72 continue 73 } 74 result = append(result, c) 75 } 76 return result 77 } 78 79 func (cl ChannelList) String() []string { 80 var result []string 81 for _, c := range cl { 82 result = append(result, c.String()) 83 } 84 return result 85 }