go.nanomsg.org/mangos/v3@v3.4.3-0.20240217232803-46464076f1f5/protocol/star/star.go (about) 1 // Copyright 2019 The Mangos Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use file except in compliance with the License. 5 // You may obtain a copy of the license at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package star implements a new, experimental protocol called "STAR". 16 // This is like the BUS protocol, except that each member of the network 17 // automatically forwards any message it receives to any other peers. 18 // In a star network, this means that all members should receive all messages, 19 // assuming that there is a central server. Its important to ensure that 20 // the topology is free from cycles, as there is limited protection against 21 // that, and cycles can lead to significant message duplication. 22 package star 23 24 import ( 25 "go.nanomsg.org/mangos/v3/protocol" 26 "go.nanomsg.org/mangos/v3/protocol/xstar" 27 ) 28 29 // Protocol identity information. 30 const ( 31 Self = protocol.ProtoStar 32 Peer = protocol.ProtoStar 33 SelfName = "star" 34 PeerName = "star" 35 ) 36 37 type socket struct { 38 protocol.Protocol 39 } 40 41 func (s *socket) GetOption(name string) (interface{}, error) { 42 switch name { 43 case protocol.OptionRaw: 44 return false, nil 45 } 46 return s.Protocol.GetOption(name) 47 } 48 49 func (s *socket) SendMsg(m *protocol.Message) error { 50 m.Header = make([]byte, 4) 51 err := s.Protocol.SendMsg(m) 52 if err != nil { 53 m.Header = m.Header[:0] 54 } 55 return err 56 } 57 58 func (s *socket) RecvMsg() (*protocol.Message, error) { 59 m, err := s.Protocol.RecvMsg() 60 if err == nil && m != nil { 61 m.Header = m.Header[:0] 62 } 63 return m, err 64 } 65 66 // NewProtocol returns a new protocol implementation. 67 func NewProtocol() protocol.Protocol { 68 s := &socket{ 69 Protocol: xstar.NewProtocol(), 70 } 71 return s 72 } 73 74 // NewSocket allocates a new Socket using the STAR protocol. 75 func NewSocket() (protocol.Socket, error) { 76 return protocol.MakeSocket(NewProtocol()), nil 77 }