jsonrpc

package module
v0.0.0-...-53b17ec Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2016 License: MIT Imports: 7 Imported by: 0

README

Neptulon JSON-RPC

Build Status GoDoc

JSON-RPC 2.0 protocol implementation for Neptulon framework.

Example

Following example creates a TLS listener with JSON-RPC 2.0 protocol and starts listening for 'ping' requests and replies with a typical 'pong'.

nep, _ := neptulon.NewServer(cert, privKey, nil, "127.0.0.1:3000", true)
rpc, _ := jsonrpc.NewServer(nep)
route, _ := jsonrpc.NewRouter(rpc)

route.Request("ping", func(ctx *jsonrpc.ReqCtx) {
	ctx.Res = "pong"
})

nep.Run()

Users

Titan mobile messaging server is written entirely using the Neptulon framework. It uses JSON-RPC 2.0 package over Neptulon to act as the server part of a mobile messaging app. You can visit its repo to see a complete use case of Neptulon framework + JSON-RPC package.

Testing

All the tests can be executed with GORACE="halt_on_error=1" go test -race -cover ./... command. Optionally you can add -v flag to observe all connection logs.

License

MIT

Documentation

Overview

Package jsonrpc implements JSON-RPC 2.0 protocol for Neptulon framework.

Example

Example demonstrating the Neptulon JSON-RPC server.

package main

const debug = false

// Example demonstrating the Neptulon JSON-RPC server.
func main() {

	// ** Output: Server started
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Middleware
	Conn *neptulon.Conn
	// contains filtered or unexported fields
}

Client is a Neptulon JSON-RPC client.

func NewClient

func NewClient(msgWG *sync.WaitGroup, disconnHandler func(client *neptulon.Client)) *Client

NewClient creates a new Client object. msgWG = (optional) sets the given *sync.WaitGroup reference to be used for counting active gorotuines that are used for handling incoming/outgoing messages. disconnHandler = (optional) registers a function to handle client disconnection events.

func UseClient

func UseClient(client *neptulon.Client) *Client

UseClient wraps an established Neptulon Client into a JSON-RPC Client.

func (*Client) Close

func (c *Client) Close() error

Close closes a client connection.

func (*Client) ConnID

func (c *Client) ConnID() string

ConnID is a randomly generated unique client connection ID.

func (*Client) Connect

func (c *Client) Connect(addr string, debug bool) error

Connect connectes to the server at given network address and starts receiving messages.

func (*Client) HandleNotification

func (c *Client) HandleNotification(route string, handler func(ctx *NotCtx) error)

HandleNotification regiters a handler for incoming notifications.

func (*Client) HandleRequest

func (c *Client) HandleRequest(route string, handler func(ctx *ReqCtx) error)

HandleRequest regiters a handler for incoming requests.

func (*Client) SendNotification

func (c *Client) SendNotification(method string, params interface{}) error

SendNotification sends a JSON-RPC notification through the client connection with structured params object.

func (*Client) SendNotificationArr

func (c *Client) SendNotificationArr(method string, params ...interface{}) error

SendNotificationArr sends a JSON-RPC notification message through the client connection with array params.

func (*Client) SendRequest

func (c *Client) SendRequest(method string, params interface{}, resHandler func(ctx *ResCtx) error) (reqID string, err error)

SendRequest sends a JSON-RPC request through the client connection with an auto generated request ID. resHandler is called when a response is returned.

func (*Client) SendRequestArr

func (c *Client) SendRequestArr(method string, resHandler func(ctx *ResCtx) error, params ...interface{}) (reqID string, err error)

SendRequestArr sends a JSON-RPC request through the client connection, with array params and auto generated request ID. resHandler is called when a response is returned.

func (*Client) SendResponse

func (c *Client) SendResponse(id string, result interface{}, err *ResError) error

SendResponse sends a JSON-RPC response through the client connection.

func (*Client) Session

func (c *Client) Session() *cmap.CMap

Session is a thread-safe data store for storing arbitrary data for this connection session.

func (*Client) SetDeadline

func (c *Client) SetDeadline(seconds int)

SetDeadline set the read/write deadlines for the connection, in seconds.

func (*Client) UseTLS

func (c *Client) UseTLS(ca, clientCert, clientCertKey []byte)

UseTLS enables Transport Layer Security for the connection. ca = Optional CA certificate to be used for verifying the server certificate. Useful for using self-signed server certificates. clientCert, clientCertKey = Optional certificate/privat key pair for TLS client certificate authentication. All certificates/private keys are in PEM encoded X.509 format.

type Middleware

type Middleware struct {
	// contains filtered or unexported fields
}

Middleware is a Neptulon middleware for handling JSON-RPC protocol and relevant JSON-RPC middleware.

func (*Middleware) NotMiddleware

func (mw *Middleware) NotMiddleware(notMiddleware ...func(ctx *NotCtx) error)

NotMiddleware registers middleware to handle notification messages.

func (*Middleware) ReqMiddleware

func (mw *Middleware) ReqMiddleware(reqMiddleware ...func(ctx *ReqCtx) error)

ReqMiddleware registers middleware to handle request messages.

func (*Middleware) ResMiddleware

func (mw *Middleware) ResMiddleware(resMiddleware ...func(ctx *ResCtx) error)

ResMiddleware registers middleware to handle response messages.

type MiddlewareHandler

type MiddlewareHandler interface {
	ReqMiddleware(reqMiddleware ...func(ctx *ReqCtx) error)
	NotMiddleware(notMiddleware ...func(ctx *NotCtx) error)
	ResMiddleware(resMiddleware ...func(ctx *ResCtx) error)
}

MiddlewareHandler defines the middleware registrar functions for a middleware stack.

type NotCtx

type NotCtx struct {
	Client *Client
	// contains filtered or unexported fields
}

NotCtx encapsulates connection and notification objects.

func (*NotCtx) Next

func (ctx *NotCtx) Next() error

Next executes the next middleware in the middleware stack.

func (*NotCtx) Params

func (ctx *NotCtx) Params(v interface{}) error

Params reads response parameters into given object. Object should be passed by reference.

func (*NotCtx) Session

func (ctx *NotCtx) Session() *cmap.CMap

Session is a data store for storing arbitrary data within this context to communicate with other middleware handling this message.

type Notification

type Notification struct {
	Method string      `json:"method"`
	Params interface{} `json:"params,omitempty"`
}

Notification is a JSON-RPC notification object.

type ReqCtx

type ReqCtx struct {
	Res    interface{} // Response to be returned.
	Err    *ResError   // Error to be returned.
	Client *Client     // Client connection.
	// contains filtered or unexported fields
}

ReqCtx encapsulates connection, request, and reponse objects.

func (*ReqCtx) Next

func (ctx *ReqCtx) Next() error

Next executes the next middleware in the middleware stack.

func (*ReqCtx) Params

func (ctx *ReqCtx) Params(v interface{}) error

Params reads request parameters into given object. Object should be passed by reference.

func (*ReqCtx) Session

func (ctx *ReqCtx) Session() *cmap.CMap

Session is a data store for storing arbitrary data within this context to communicate with other middleware handling this message.

type Request

type Request struct {
	ID     string      `json:"id"`
	Method string      `json:"method"`
	Params interface{} `json:"params,omitempty"`
}

Request is a JSON-RPC request object.

type ResCtx

type ResCtx struct {
	Client *Client
	// contains filtered or unexported fields
}

ResCtx encapsulates connection and response objects.

func (*ResCtx) Next

func (ctx *ResCtx) Next() error

Next executes the next middleware in the middleware stack.

func (*ResCtx) Result

func (ctx *ResCtx) Result(v interface{}) error

Result reads response result data into given object. Object should be passed by reference.

func (*ResCtx) Session

func (ctx *ResCtx) Session() *cmap.CMap

Session is a data store for storing arbitrary data within this context to communicate with other middleware handling this message.

type ResError

type ResError struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

ResError is a JSON-RPC response error object.

type Response

type Response struct {
	ID     string      `json:"id"`
	Result interface{} `json:"result,omitempty"`
	Error  *ResError   `json:"error,omitempty"`
}

Response is a JSON-RPC response object.

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router is a JSON-RPC message routing middleware.

func NewRouter

func NewRouter(m MiddlewareHandler) (*Router, error)

NewRouter creates a JSON-RPC router instance and registers it as a Neptulon JSON-RPC middleware.

func (*Router) Notification

func (r *Router) Notification(route string, handler func(ctx *NotCtx) error)

Notification adds a new notification route registry.

func (*Router) Request

func (r *Router) Request(route string, handler func(ctx *ReqCtx) error)

Request adds a new request route registry.

type Sender

type Sender struct {
	// contains filtered or unexported fields
}

Sender is a JSON-RPC middleware for sending requests and handling responses asynchronously.

func NewSender

func NewSender(m *Middleware, send func(connID string, msg []byte) error) Sender

NewSender creates a new Sender middleware.

func (*Sender) SendNotification

func (s *Sender) SendNotification(connID string, method string, params interface{}) error

SendNotification sends a JSON-RPC notification through the connection denoted by the connection ID with structured params object.

func (*Sender) SendNotificationArr

func (s *Sender) SendNotificationArr(connID string, method string, params ...interface{}) error

SendNotificationArr sends a JSON-RPC notification message through the connection denoted by the connection ID with array params.

func (*Sender) SendRequest

func (s *Sender) SendRequest(connID string, method string, params interface{}, resHandler func(ctx *ResCtx) error) (reqID string, err error)

SendRequest sends a JSON-RPC request through the connection denoted by the connection ID with an auto generated request ID. resHandler is called when a response is returned.

func (*Sender) SendRequestArr

func (s *Sender) SendRequestArr(connID string, method string, resHandler func(ctx *ResCtx) error, params ...interface{}) (reqID string, err error)

SendRequestArr sends a JSON-RPC request through the connection denoted by the connection ID, with array params and auto generated request ID. resHandler is called when a response is returned.

func (*Sender) SendResponse

func (s *Sender) SendResponse(connID string, id string, result interface{}, err *ResError) error

SendResponse sends a JSON-RPC response message through the connection denoted by the connection ID.

type Server

type Server struct {
	Middleware
	Sender
	// contains filtered or unexported fields
}

Server is a Neptulon JSON-RPC server.

func NewServer

func NewServer(n *neptulon.Server) (*Server, error)

NewServer creates a Neptulon JSON-RPC server.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL