nanomsg.org/go/mangos/v2@v2.0.9-0.20200203084354-8a092611e461/protocol/pub/pub.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 pub implements the PUB protocol.  This protocol publishes messages
    16  // to subscribers (SUB peers).  The subscribers will filter incoming messages
    17  // from the publisher based on their subscription.
    18  package pub
    19  
    20  import (
    21  	"nanomsg.org/go/mangos/v2/protocol"
    22  	"nanomsg.org/go/mangos/v2/protocol/xpub"
    23  )
    24  
    25  type socket struct {
    26  	protocol.Protocol
    27  }
    28  
    29  // Protocol identity information.
    30  const (
    31  	Self     = protocol.ProtoPub
    32  	Peer     = protocol.ProtoSub
    33  	SelfName = "pub"
    34  	PeerName = "sub"
    35  )
    36  
    37  func (s *socket) GetOption(name string) (interface{}, error) {
    38  	switch name {
    39  	case protocol.OptionRaw:
    40  		return false, nil
    41  	}
    42  	return s.Protocol.GetOption(name)
    43  }
    44  
    45  // NewProtocol returns a new protocol implementation.
    46  func NewProtocol() protocol.Protocol {
    47  	s := &socket{
    48  		Protocol: xpub.NewProtocol(),
    49  	}
    50  	return s
    51  }
    52  
    53  // NewSocket allocates a raw Socket using the PAIR protocol.
    54  func NewSocket() (protocol.Socket, error) {
    55  	return protocol.MakeSocket(NewProtocol()), nil
    56  }