github.com/gdamore/mangos@v1.4.0/protocol/pull/pull.go (about) 1 // Copyright 2018 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 pull implements the PULL protocol, which is the read side of 16 // the pipeline pattern. (PUSH is the reader.) 17 package pull 18 19 import ( 20 "time" 21 22 "nanomsg.org/go-mangos" 23 ) 24 25 type pull struct { 26 sock mangos.ProtocolSocket 27 raw bool 28 } 29 30 func (x *pull) Init(sock mangos.ProtocolSocket) { 31 x.sock = sock 32 x.sock.SetSendError(mangos.ErrProtoOp) 33 } 34 35 func (x *pull) Shutdown(time.Time) {} // No sender to drain 36 37 func (x *pull) receiver(ep mangos.Endpoint) { 38 rq := x.sock.RecvChannel() 39 cq := x.sock.CloseChannel() 40 for { 41 42 m := ep.RecvMsg() 43 if m == nil { 44 return 45 } 46 47 select { 48 case rq <- m: 49 case <-cq: 50 return 51 } 52 } 53 } 54 55 func (*pull) Number() uint16 { 56 return mangos.ProtoPull 57 } 58 59 func (*pull) PeerNumber() uint16 { 60 return mangos.ProtoPush 61 } 62 63 func (*pull) Name() string { 64 return "pull" 65 } 66 67 func (*pull) PeerName() string { 68 return "push" 69 } 70 71 func (x *pull) AddEndpoint(ep mangos.Endpoint) { 72 go x.receiver(ep) 73 } 74 75 func (x *pull) RemoveEndpoint(ep mangos.Endpoint) {} 76 77 func (*pull) SendHook(msg *mangos.Message) bool { 78 return false 79 } 80 81 func (x *pull) SetOption(name string, v interface{}) error { 82 var ok bool 83 switch name { 84 case mangos.OptionRaw: 85 if x.raw, ok = v.(bool); !ok { 86 return mangos.ErrBadValue 87 } 88 return nil 89 default: 90 return mangos.ErrBadOption 91 } 92 } 93 94 func (x *pull) GetOption(name string) (interface{}, error) { 95 switch name { 96 case mangos.OptionRaw: 97 return x.raw, nil 98 default: 99 return nil, mangos.ErrBadOption 100 } 101 } 102 103 // NewSocket allocates a new Socket using the PULL protocol. 104 func NewSocket() (mangos.Socket, error) { 105 return mangos.MakeSocket(&pull{}), nil 106 }