88 lines
2.8 KiB
Go
88 lines
2.8 KiB
Go
package rpc
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.loafle.net/commons_go/rpc/protocol"
|
|
cuc "git.loafle.net/commons_go/util/context"
|
|
)
|
|
|
|
type ServletHandlers struct {
|
|
// The maximum number of pending messages in the queue.
|
|
//
|
|
// The number of pending requsts should exceed the expected number
|
|
// of concurrent goroutines calling client's methods.
|
|
// Otherwise a lot of ClientError.Overflow errors may appear.
|
|
//
|
|
// Default is DefaultPendingMessages.
|
|
PendingResponses int
|
|
|
|
codecs map[string]protocol.ServerCodec
|
|
}
|
|
|
|
func (sh *ServletHandlers) ServletContext(parent cuc.Context) ServletContext {
|
|
return newServletContext(parent)
|
|
}
|
|
|
|
func (sh *ServletHandlers) Init(servletCTX ServletContext) error {
|
|
return nil
|
|
}
|
|
|
|
func (sh *ServletHandlers) ReadRequest(servletCTX ServletContext, codec protocol.ServerCodec, conn interface{}) (protocol.ServerRequestCodec, error) {
|
|
return nil, fmt.Errorf("Servlet Handler: ReadRequest is not implemented")
|
|
}
|
|
|
|
func (sh *ServletHandlers) Invoke(servletCTX ServletContext, requestCodec protocol.RegistryCodec) (result interface{}, err error) {
|
|
return nil, fmt.Errorf("Servlet Handler: Invoke is not implemented")
|
|
}
|
|
|
|
func (sh *ServletHandlers) WriteResponse(servletCTX ServletContext, conn interface{}, requestCodec protocol.ServerRequestCodec, result interface{}, err error) error {
|
|
return fmt.Errorf("Servlet Handler: WriteResponse is not implemented")
|
|
}
|
|
|
|
func (sh *ServletHandlers) WriteNotification(servletCTX ServletContext, conn interface{}, codec protocol.ServerCodec, method string, args []interface{}) error {
|
|
return fmt.Errorf("Servlet Handler: WriteNotification is not implemented")
|
|
}
|
|
|
|
func (sh *ServletHandlers) Destroy(servletCTX ServletContext) {
|
|
// no op
|
|
}
|
|
|
|
// RegisterCodec adds a new codec to the server.
|
|
//
|
|
// Codecs are defined to process a given serialization scheme, e.g., JSON or
|
|
// XML. A codec is chosen based on the "Content-Type" header from the request,
|
|
// excluding the charset definition.
|
|
func (sh *ServletHandlers) RegisterCodec(contentType string, codec protocol.ServerCodec) {
|
|
if nil == sh.codecs {
|
|
sh.codecs = make(map[string]protocol.ServerCodec)
|
|
}
|
|
sh.codecs[strings.ToLower(contentType)] = codec
|
|
}
|
|
|
|
func (sh *ServletHandlers) getCodec(contentType string) (protocol.ServerCodec, error) {
|
|
var codec protocol.ServerCodec
|
|
if contentType == "" && len(sh.codecs) == 1 {
|
|
// If Content-Type is not set and only one codec has been registered,
|
|
// then default to that codec.
|
|
for _, c := range sh.codecs {
|
|
codec = c
|
|
}
|
|
} else if codec = sh.codecs[strings.ToLower(contentType)]; codec == nil {
|
|
return nil, fmt.Errorf("Unrecognized Content-Type: %s", contentType)
|
|
}
|
|
|
|
return codec, nil
|
|
}
|
|
|
|
func (sh *ServletHandlers) GetPendingResponses() int {
|
|
return sh.PendingResponses
|
|
}
|
|
|
|
func (sh *ServletHandlers) Validate() {
|
|
if 0 >= sh.PendingResponses {
|
|
sh.PendingResponses = DefaultPendingResponses
|
|
}
|
|
}
|