Git Product home page Git Product logo

msgpack-oocrpc's Introduction

msgpack-oocrpc

This is msgpack-oocrpc, a rpc based on msgpack, you can call the remote golang service from the python client.

so you can write the webapp's frontend with python(django tornado flask), write the webapp's backend with go.

go rpc server:

package main
                                                                                                                            
import (
    "errors"
    "github.com/notedit/msgpack-oocrpc/rpc"
)

type Args struct {
    A, B int
}

type Reply struct {
    C int
}

type Arith int

func (t *Arith) Add(args *Args, reply *Reply) error {
    reply.C = args.A + args.B
    return nil
}

func (t *Arith) Mul(args *Args, reply *Reply) error {
    reply.C = args.A * args.B
    return nil
}

func (t *Arith) Div(args *Args, reply *Reply) error {
    if args.B == 0 {
        return rpc.BackendError{"InternalError", "divide by zero"}
    }
    reply.C = args.A / args.B
    return nil
}


func (t *Arith) Error(args *Args, reply *Reply) error {
    panic("ERROR")
}

func (t *Arith) NError(args *Args, reply *Reply) error {
    return errors.New("normalerror")
}

func main() {
    newServer := rpc.NewServer("localhost", 9091)
    newServer.Register(new(Arith))
    newServer.Serv()
}    

go rpc client:

package main

import (
    "fmt"
    "github.com/notedit/msgpack-oocrpc/rpc"
)

type Args struct {
    A, B int
}

type Reply struct {
    C int
}

func main() {
    client := rpc.New("localhost:9090")
    // normal test
    args := &Args{7, 8}
    reply := &Reply{}

    err := client.Call("Arith.Mul", args, reply)
    if err != nil {
        fmt.Println(err.Error())
    }

    err = client.Call("Arith.Add", args, reply)
    if err != nil {
        fmt.Println(err.Error())
    }

    // un exist method
    err = client.Call("Arith.Notfound", args, reply)
    if err != nil {
        fmt.Println(err.Error())
    }
    // un exist service
    err = client.Call("Notfound.arith", args, reply)
    if err != nil {
        fmt.Println(err.Error())
    }
    // test error 
    args = &Args{7, 0}
    reply = &Reply{}

    err = client.Call("Arith.Div", args, reply)
    if err != nil {
        fmt.Println(err.Error())
    }

    // test panic
    args = &Args{7, 8}
    reply = &Reply{}

    err = client.Call("Arith.Error", args, reply)
    if err != nil {
        fmt.Println(err.Error())
    }
}                

python rpc client:

from client import RpcClient

client = RpcClient(host='localhost',port=9090)                                                                          
ret = client.Add({'A':7,'B':8})
print 'Add',ret

ret = client.Mul({'A':7,'B':8})
print 'Mul',ret

msgpack-oocrpc's People

Contributors

notedit avatar

Stargazers

İlker G. Öztürk avatar seacoastboy avatar  avatar

Watchers

 avatar James Cloos avatar

Forkers

seacoastboy

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.