amqprpc

package module
v0.0.0-...-8433bc7 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2016 License: MIT Imports: 9 Imported by: 4

README

amqp-rpc

Go Report Card Coverage Status GoDoc

Example

Server

package server

import (
	"log"
	"net/rpc"

	"github.com/streadway/amqp"
	"github.com/vibhavp/amqp-rpc"
)
	
type Args struct {
	A, B int
}

type Quotient struct {
	Quo, Rem int
}

type Arith int

func (t *Arith) Multiply(args *Args, reply *int) error {
	*reply = args.A * args.B
	return nil
}

func (t *Arith) Divide(args *Args, quo *Quotient) error {
	if args.B == 0 {
		return errors.New("divide by zero")
	}
	quo.Quo = args.A / args.B
	quo.Rem = args.A % args.B
	return nil
}

func main() {
	conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
	if err != nil {
		log.Fatal(err)
	}

	serverCodec, err := amqprpc.NewServerCodec(conn, "rpc_queue", amqprpc.GobCodec{})
	if err != nil {
		log.Fatal(err)
	}

	rpc.Register(new(Arith))
	rpc.ServeCodec(serverCodec)
}

Client

package client

import (
	"github.com/streadway/amqp"
	"github.com/vibhavp/amqp-rpc"
)
	
func main() {
	conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
	if err != nil {
		log.Fatal(err)
	}

	clientCodec, err := amqprpc.NewClientCodec(conn, "rpc_queue", amqprpc.GobCodec{})
	if err != nil {
		log.Fatal(err)
	}

	reply := new(int)
	client := rpc.NewClientWithCodec(clientCodec)
	err := client.Call("Arith.Multiply", server.Args{1, 2}, reply)
	if err != nil {
		log.Fatal("arith error: ", err)
	}
	
	log.Printf("1 * 2 = %d", *reply)
}

Documentation

Overview

Package amqprpc is an AMQP driver for net/rpc. It provides an implementation of the rpc.ClientCodec and rpc.ServerCodec interfaces, which send and respond to RPC calls respecteively on a given AMQP connection.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNoConsumers = errors.New("amqprpc: No consumers in queue")

Functions

func NewClientCodec

func NewClientCodec(conn *amqp.Connection, serverRouting string, encodingCodec EncodingCodec) (rpc.ClientCodec, error)

NewClientCodec returns a new rpc.ClientCodec using AMQP on conn. serverRouting is the routing key with with RPC calls are sent, it should be the same routing key used with NewServerCodec. encodingCodec is an EncodingCoding implementation. This package provdes JSONCodec and GobCodec for the JSON and Gob encodings respectively.

Example
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
	log.Fatal(err)
}

clientCodec, err := amqprpc.NewClientCodec(conn, "rpc_queue", amqprpc.GobCodec{})
if err != nil {
	log.Fatal(err)
}

client := rpc.NewClientWithCodec(clientCodec)
client.Call("Foo.Bar", struct{}{}, &struct{}{})
Output:

func NewServerCodec

func NewServerCodec(conn *amqp.Connection, serverRouting string, encodingCodec EncodingCodec) (rpc.ServerCodec, error)

NewServerCodec returns a new rpc.ClientCodec using AMQP on conn. serverRouting is the routing key with with RPC calls are received, encodingCodec is an EncodingCoding implementation. This package provdes JSONCodec and GobCodec for the JSON and Gob encodings respectively.

Example
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
	log.Fatal(err)
}

serverCodec, err := amqprpc.NewServerCodec(conn, "rpc_queue", amqprpc.GobCodec{})
if err != nil {
	log.Fatal(err)
}

go rpc.ServeCodec(serverCodec)
Output:

Types

type EncodingCodec

type EncodingCodec interface {
	Marshal(interface{}) ([]byte, error)
	Unmarshal([]byte, interface{}) error
}

EncodingCodec implements marshaling and unmarshaling of seralized data.

type GobCodec

type GobCodec struct{}

GobCodec is an EncodingCodec implementation to send/recieve Gob data over AMQP.

func (GobCodec) Marshal

func (GobCodec) Marshal(v interface{}) ([]byte, error)

func (GobCodec) Unmarshal

func (GobCodec) Unmarshal(data []byte, v interface{}) error

type JSONCodec

type JSONCodec struct{}

JSONCodec is an EncodingCodec implementation to send/receieve JSON data over AMQP.

func (JSONCodec) Marshal

func (JSONCodec) Marshal(v interface{}) ([]byte, error)

func (JSONCodec) Unmarshal

func (JSONCodec) Unmarshal(data []byte, v interface{}) error

Jump to

Keyboard shortcuts

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