I have some code I've been dumped with and am actually stumped - I've worked with RPC and the JSON side of things before but I can't seem to get it to work over RPC when it works fine locally.
package main
import (
"log"
"net"
"net/rpc"
"net/rpc/jsonrpc"
"reflect"
)
type Foo interface {
SayHello() error
}
type fakeFoo struct {
internalValue string
}
func NewFakeFoo() *fakeFoo {
f := &fakeFoo{}
f.internalValue = "123456789012347"
return f
}
func (m *fakeFoo) SayHello() error {
return nil
}
type FooManager struct {
availableFoos []Foo
}
func NewFooManager() *FooManager {
p := new(FooManager)
p.availableFoos = make([]Foo, 0)
return p
}
func AddFoo(mm *FooManager, m Foo) {
mm.availableFoos = append(mm.availableFoos, m)
log.Println("Added type ", reflect.TypeOf(m))
}
func (mm *FooManager) GetAvailableFoos(in []Foo, out *[]Foo) error {
log.Println("availableFoos:", reflect.TypeOf(mm.availableFoos))
log.Println("*out is", reflect.TypeOf(*out))
*out = append(in, mm.availableFoos...)
log.Println("Out is:", reflect.TypeOf(*out))
return nil
}
func startServer(mm *FooManager) {
server := rpc.NewServer()
server.Register(mm)
l, e := net.Listen("tcp", ":8222")
if e != nil {
log.Fatal("listen error:", e)
}
for {
conn, err := l.Accept()
log.Println("Incoming!")
if err != nil {
log.Fatal(err)
}
go server.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}
func main() {
fake1 := NewFakeFoo()
fooHolder := NewFooManager()
AddFoo(fooHolder, fake1)
go startServer(fooHolder)
log.Println("Using standard function call")
var foos []Foo
fooHolder.GetAvailableFoos(foos, &foos)
log.Println(foos)
log.Println("Using RPC call")
conn, err := net.Dial("tcp", "localhost:8222")
if err != nil {
log.Fatalln(err)
}
defer conn.Close()
c := jsonrpc.NewClient(conn)
err = c.Call("FooManager.GetAvailableFoos", foos, &foos)
if err != nil {
log.Println(foos)
log.Fatal("GetAvailableFoos error:", err)
}
log.Println("Success: ", foos)
}
(also here but no tcp available urgh! http://play.golang.org/p/HmK-K09D2J )
The output is pretty surprising as it indicates something going wrong with the marshalling rather than with the actual data - Running it in wireshark I can see the data being sent in the correct form (I had success using a similar technique in another question) but can't for the life of me get this to stop throwing marshalling bugs.
The output from running this is as follows:
2015/09/07 10:04:35 Added type *main.fakeFoo
2015/09/07 10:04:35 Using standard function call
2015/09/07 10:04:35 availableFoos: []main.Foo
2015/09/07 10:04:35 *out is []main.Foo
2015/09/07 10:04:35 Out is: []main.Foo
2015/09/07 10:04:35 [0x1870a540]
2015/09/07 10:04:35 Using RPC call
2015/09/07 10:04:35 Incoming!
2015/09/07 10:04:35 [0x1870a540]
2015/09/07 10:04:35 GetAvailableFoos error:json: cannot unmarshal object into Go value of type main.Foo
exit status 1
Am I missing an interface/type trick or is this a bug in Go's marshalling?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…