service

package
v0.0.0-...-bebc87d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2014 License: MIT Imports: 11 Imported by: 0

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

View Source
const (
	// IDSize is the size of the ID in bytes.
	IDSize int = 4
)

Variables

View Source
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) GetData

func (m *PBRequest) GetData() []byte

func (*PBRequest) GetTag

func (m *PBRequest) GetTag() []byte

func (*PBRequest) ProtoMessage

func (*PBRequest) ProtoMessage()

func (*PBRequest) Reset

func (m *PBRequest) Reset()

func (*PBRequest) String

func (m *PBRequest) String() string

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

func (*Request) Key

func (r *Request) Key() string

Key returns the RequestKey for this request. Use with maps.

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

func (RequestID) Request

func (r RequestID) Request() RequestID

Request turns a RequestID into a Request (unsetting first bit)

func (RequestID) Response

func (r RequestID) Response() RequestID

Response turns a RequestID into a Response (setting first bit)

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.

func (*Service) Start

func (s *Service) Start(ctx context.Context) error

Start kicks off the Service goroutines.

func (*Service) Stop

func (s *Service) Stop()

Stop stops Service activity.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳