jsonrpc

package
v0.0.0-...-456eabd Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2011 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package gorilla/rpc/jsonrpc is used to build web services using the JSON-RPC 1.0 protocol.

This package is based on the standard rpc/jsonrpc package but suitable for use on Google App Engine, as it allows http transmition over non-persistent connections. Also, http.Request is passed as an argument to service methods, allowing services to retrieve the App Engine context.

Usage is the same as rpc/jsonrpc. First we define a service with a method to be called remotely:

type ArithArgs struct {
	A, B int
}

type Arith int

func (t *Arith) Multiply(r *http.Request, args ArithArgs, reply *int) os.Error {
	*reply = args.A * args.B
	return nil
}

Then register it to be served:

arith := new(Arith)
s := new(jsonrpc.Server)
s.Register(arith)

And finally setup the server as an http.Handler:

http.Handle("/rpc", server)

In this example, one method will be registered and can be accessed as "Arith.Multiply".

Only methods that satisfy these criteria will be made available for remote access:

  • The method name is exported, that is, begins with an upper case letter.
  • The method receiver is exported or local (defined in the package registering the service).
  • The method has three arguments.
  • The first argument is *http.Request.
  • The second and third arguments are exported or local types.
  • The third argument is a pointer.
  • The method has return type os.Error.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type InvalidRequest

type InvalidRequest struct{}

A value sent as a placeholder for the response when the server receives an invalid request.

type JsonRequest

type JsonRequest struct {
	// A String containing the name of the method to be invoked.
	Method string `json:"method"`
	// An Array of objects to pass as arguments to the method.
	Params *json.RawMessage `json:"params"`
	// The request id. This can be of any type. It is used to match the
	// response with the request that it is replying to.
	Id *json.RawMessage `json:"id"`
}

type JsonResponse

type JsonResponse struct {
	// The Object that was returned by the invoked method. This must be null
	// in case there was an error invoking the method.
	Result interface{} `json:"result"`
	// An Error object if there was an error invoking the method. It must be
	// null if there was no error.
	Error interface{} `json:"error"`
	// This must be the same id as the request it is responding to.
	Id *json.RawMessage `json:"id"`
}

type Server

type Server struct {
	Map *ServiceMap
}

Server represents an RPC Server.

func (*Server) Get

func (server *Server) Get(method string) (service *service, mtype *methodType,
	err error)

func (*Server) Register

func (server *Server) Register(rcvr interface{}) error

Register publishes in the server the set of methods of the receiver value that satisfy the following conditions:

  • exported method
  • first argument is a pointer to the current request
  • last two arguments are pointers to exported structs
  • one return value, of type os.Error

func (*Server) RegisterName

func (server *Server) RegisterName(name string, rcvr interface{}) error

RegisterName is like Register but uses the provided name for the type instead of the receiver's concrete type.

func (*Server) ServeHTTP

func (server *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements an http.Handler that answers RPC requests.

server := rpc.NewServer()
server.Register(MyService)
http.Handle("/rpc", server)

type ServiceMap

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

ServiceMap is a registry for services.

func (*ServiceMap) Get

func (m *ServiceMap) Get(method string) (service *service, mtype *methodType,
	err error)

Get returns a registered service given a method name.

The method name uses a dotted notation, as in "Service.Method".

func (*ServiceMap) Register

func (m *ServiceMap) Register(rcvr interface{}) error

Register registers a service in the services map.

func (*ServiceMap) RegisterName

func (m *ServiceMap) RegisterName(name string, rcvr interface{}) error

RegisterName registers a named service in the services map.

Jump to

Keyboard shortcuts

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