Documentation
¶
Overview ¶
Package service is a generated protocol buffer package.
It is generated from these files:
request.proto
It has these top-level messages:
PBRequest
Index ¶
- Constants
- Variables
- func RequestKey(pid peer.ID, rid RequestID) string
- type Handler
- type PBRequest
- type Request
- type RequestID
- type RequestMap
- type Service
- func (s *Service) GetPipe() *msg.Pipe
- func (s *Service) SendMessage(ctx context.Context, m msg.NetMessage) error
- func (s *Service) SendRequest(ctx context.Context, m msg.NetMessage) (msg.NetMessage, error)
- func (s *Service) SetHandler(h Handler)
- func (s *Service) Start(ctx context.Context) error
- func (s *Service) Stop()
Constants ¶
const (
// IDSize is the size of the ID in bytes.
IDSize int = 4
)
Variables ¶
var ErrNoResponse = errors.New("no response to request")
ErrNoResponse is returned by Service when a Request did not get a response, and no other error happened
Functions ¶
func RequestKey ¶
func RequestKey(pid peer.ID, rid RequestID) string
RequestKey is the peer.ID concatenated with the RequestID. Use with maps.
Types ¶
type Handler ¶
type Handler interface {
// HandleMessage receives an incoming message, and potentially returns
// a response message to send back.
HandleMessage(context.Context, msg.NetMessage) msg.NetMessage
}
Handler is an interface that objects must implement in order to handle a service's requests.
type PBRequest ¶
type PBRequest struct {
Data []byte `protobuf:"bytes,1,req" json:"Data,omitempty"`
Tag []byte `protobuf:"bytes,3,opt" json:"Tag,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (*PBRequest) ProtoMessage ¶
func (*PBRequest) ProtoMessage()
type Request ¶
type Request struct {
// ID is the RequestID identifying this Request-Response Flow.
ID RequestID
// PeerID identifies the peer from whom to expect the response.
PeerID peer.ID
// Response is the channel of incoming responses.
Response chan msg.NetMessage
}
Request objects are used to multiplex request-response flows.
func NewRequest ¶
func NewRequest(pid peer.ID) (*Request, error)
NewRequest creates a request for given peer.ID
type RequestID ¶
type RequestID []byte
RequestID is a field that identifies request-response flows.
func RandomRequestID ¶
func RandomRequestID() (RequestID, error)
RandomRequestID creates and returns a new random request ID
func (RequestID) IsRequest ¶
func (r RequestID) IsRequest() bool
IsRequest returns whether a RequestID identifies a request
func (RequestID) IsResponse ¶
func (r RequestID) IsResponse() bool
IsResponse returns whether a RequestID identifies a response
type RequestMap ¶
type RequestMap map[string]*Request
RequestMap is a map of Requests. the key = (peer.ID concat RequestID).
type Service ¶
type Service struct {
// Handler is the object registered to handle incoming requests.
Handler Handler
// Requests are all the pending requests on this service.
Requests RequestMap
RequestsLock sync.RWMutex
// Message Pipe (connected to the outside world)
*msg.Pipe
// contains filtered or unexported fields
}
Service is a networking component that protocols can use to multiplex messages over the same channel, and to issue + handle requests.
func NewService ¶
func NewService(h Handler) *Service
NewService creates a service object with given type ID and Handler
func (*Service) GetPipe ¶
func (s *Service) GetPipe() *msg.Pipe
GetPipe implements the mux.Protocol interface
func (*Service) SendMessage ¶
func (s *Service) SendMessage(ctx context.Context, m msg.NetMessage) error
SendMessage sends a message out
func (*Service) SendRequest ¶
func (s *Service) SendRequest(ctx context.Context, m msg.NetMessage) (msg.NetMessage, error)
SendRequest sends a request message out and awaits a response.
func (*Service) SetHandler ¶
func (s *Service) SetHandler(h Handler)
SetHandler assigns the request Handler for this service.