goyar

package module
v0.0.0-...-b268b88 Latest Latest
Warning

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

Go to latest
Published: May 19, 2016 License: Apache-2.0 Imports: 12 Imported by: 2

README

goyar

yar rpc library with json code in golang

Yar http server Example

package main

import (
	"fmt"
	"github.com/neverlee/goyar"
	"net/http"
)

type Arith int

func (t *Arith) Echo(i *int, r *int) error {
	*r = *i
	return nil
}

func main() {
	yar := goyar.NewYarServer()
	ar := new(Arith)
	yar.Register(ar)
	yar.HandleHTTP("/api.php")

	http.ListenAndServe(":8000", nil)
}

Same as php code

<?php
class Operator {
    public function Echo($a) {
        return $a;
    }
}
$server = new Yar_Server(new Operator());
$server->handle();

Yar http client Example

import (
    "fmt"
    "github.com/neverlee/goyar"
)

func main() {
    client := goyar.NewYHClient("http://yarserver/api.php", nil)
    var r int
    var err error
    err = client.MCall("add", &r, 3, 4)
    err = client.Call("Echo", 100, &r)
}

Same as php code

<?php
$client = new yar_client("http://yarserver/api.php");
$client->SetOpt(YAR_OPT_PACKAGER, "json"); #this is necessary.
$client->add(3, 4);
$client->Echo(1100);
?>

Yar tcp server Example

package main

import (
	"fmt"
	"github.com/neverlee/goyar"
	"net"
	"os"
)

type Arith int

func (t *Arith) Echo(i *int, r *int) error {
	*r = *i
	fmt.Println("do echo", *i)
	return nil
}

func main() {
	arith := new(Arith)

	yar := goyar.NewYarServer()
	yar.Register(arith)

	tcpAddr, err := net.ResolveTCPAddr("tcp", ":1234")
	checkError(err)

	listener, err := net.ListenTCP("tcp", tcpAddr)
	checkError(err)

	for {
		conn, err := listener.Accept()
		if err != nil {
			continue
		}
		yar.ServeConn(conn)
	}
}

func checkError(err error) {
	if err != nil {
		fmt.Println("Fatal error ", err.Error())
		os.Exit(1)
	}
}

Yar tcp client Example

package main

import (
	"fmt"
	"github.com/neverlee/goyar"
	"log"
)

type Args struct {
	A, B int
}

type Quotient struct {
	Quo, Rem int
}

func main() {
	client, err := goyar.Dial("tcp", "127.0.0.1:1234")
	if err != nil {
		log.Fatal("dialing:", err)
	}

	var r int
	fmt.Println(client.Call("Echo", 15, &r))
	fmt.Println(r)
}

LICENSE

Apache License

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dial

func Dial(network, address string) (*rpc.Client, error)

Dial connects to a YAR-RPC server at the specified network address.

Example
// YarTCPClient
client, err := goyar.Dial("tcp", "127.0.0.1:1234")
if err != nil {
	log.Fatal("dialing:", err)
}
err := client.Call("Set", 15, &r)
Output:

func NewClient

func NewClient(conn io.ReadWriteCloser) *rpc.Client

NewClient returns a new rpc.Client to handle requests to the set of services at the other end of the connection.

func NewClientCodec

func NewClientCodec(conn io.ReadWriteCloser) rpc.ClientCodec

NewClientCodec returns a new rpc.ClientCodec using YAR-RPC on conn.

func NewHTTPNameServerCodec

func NewHTTPNameServerCodec(name string, conn io.ReadWriteCloser, w http.ResponseWriter, req *http.Request) rpc.ServerCodec

NewHTTPNameServerCodec returns a new rpc.ServerCodec using YAR-RPC on http conn.

func NewHTTPServerCodec

func NewHTTPServerCodec(conn io.ReadWriteCloser, w http.ResponseWriter, req *http.Request) rpc.ServerCodec

NewHTTPServerCodec returns a new rpc.ServerCodec using YAR-RPC on http conn.

func NewNameServerCodec

func NewNameServerCodec(name string, conn io.ReadWriteCloser) rpc.ServerCodec

NewNameServerCodec returns a new rpc.ServerCodec using YAR-RPC on tcp conn.

func NewServerCodec

func NewServerCodec(conn io.ReadWriteCloser) rpc.ServerCodec

NewServerCodec returns a new rpc.ServerCodec using YAR-RPC on tcp conn.

Types

type Header struct {
	ID       uint32 // transaction id
	Version  uint16 // protocl version
	MagicNum uint32 // default is: 0x80DFEC60
	Reserved uint32
	Provider [32]byte // reqeust from who
	Token    [32]byte // request token, used for authentication
	BodyLen  uint32   // request body len
	PkgName  Packager // body encode name
}

Header Yar transport Header(90 bytes)

func ReadHeader

func ReadHeader(r io.Reader) (*Header, error)

ReadHeader get a yar header

type Packager

type Packager [8]byte

Packager yar packager name

func (*Packager) Equal

func (p *Packager) Equal(str string) bool

Equal checking it is equal the string

func (*Packager) Set

func (p *Packager) Set(str string)

Set set a string as pkgname

type Request

type Request struct {
	ID     uint32        `json:"i"` // yar rpc id
	Method string        `json:"m"` // calling method name
	Params []interface{} `json:"p"` // all the params
}

Request yar request struct(only for json)

func (*Request) Write

func (r *Request) Write(w io.Writer) error

Write write the header and request

type Response

type Response struct {
	ID     uint32      `json:"i"` // yar rpc id
	Status int32       `json:"s"` // return status code
	Result interface{} `json:"r"` // return value
	Output string      `json:"o"` // the called function standard output
	Error  string      `json:"e"` // return error message
}

Response yar response struct(only for json)

func (*Response) Write

func (r *Response) Write(w io.Writer) error

Write write the header and response

type YHClient

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

YHClient yar http client

func NewYHClient

func NewYHClient(url string, client *http.Client) *YHClient

NewYHClient returns a new yar http client

Example
package main

import (
	"fmt"
	"github.com/neverlee/goyar"
)

func main() {
	// YarHTTPClient
	client := goyar.NewYHClient("http://yarserver/api.php", nil)
	var r int
	if err := client.MCall("multi", &r, 3, 4); err == nil {
		fmt.Println(r)
	}

	if err := client.Call("Set", 10, &r); err == nil {
		fmt.Println(r)
	}
}
Output:

func (*YHClient) Call

func (c *YHClient) Call(method string, param interface{}, ret interface{}) error

Call calling the remote yarrpc, only support one param. print the output and set return value

func (*YHClient) MCall

func (c *YHClient) MCall(method string, ret interface{}, params ...interface{}) error

MCall calling the remote yarrpc, print the output and set return value

func (*YHClient) MCallRaw

func (c *YHClient) MCallRaw(method string, params ...interface{}) ([]byte, error)

MCallRaw calling the remote yarrpc and return the raw byte yar response

type YarServer

type YarServer struct {
	*rpc.Server
}

YarServer yar rpc server

func NewYarServer

func NewYarServer() *YarServer

NewYarServer return a yar rpc server

func (*YarServer) HandleHTTP

func (y *YarServer) HandleHTTP(rpcPath string)

HandleHTTP registers an HTTP handler for YAR RPC messages on rpcPath, It is still necessary to invoke http.Serve(), typically in a go statement.

func (*YarServer) Register

func (y *YarServer) Register(rcvr interface{})

Register publishes in the server the set of methods of the receiver value. Default register name is 'yar'

func (*YarServer) ServeConn

func (y *YarServer) ServeConn(conn io.ReadWriteCloser)

ServeConn runs the YAR-RPC server on a single connection. ServeConn blocks, serving the connection until the client hangs up. The caller typically invokes ServeConn in a go statement.

func (*YarServer) ServeHTTP

func (y *YarServer) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP yar http serve handle

Jump to

Keyboard shortcuts

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