1
votes

Ok I have a rpc server package and a rpc client package. I'd like to ask the server to return all domains.

In server.go I import server.DomainServer and serve it over http.

In client.go I import client.DomainClient and send a rpc call to the listening server.

The response is empty [].

The expected response is ["dom1.de","dom2.de"]

Why is the response empty? How can I debug this?

Do I have to share the GetAllDomainsRequest and GetAllDomainsRequest struct so they have the same type, e.g. in a package called common?

Update: Ran wireshark to capture the response, and the response was, amongst other stuff, []string

server.go

package main

import (
    "log"
    "net/http"
    "net/rpc"

    "bitbucket.org/idalu/hostd/server"
)

func main() {
    var authkey = "123"
    ds := &server.DomainServer{
        Authkey: authkey,
    }

    rpc.Register(ds)
    rpc.HandleHTTP()
    log.Fatalln(http.ListenAndServe(":1312", nil))
}

server/domain.go

package server

import (
    "errors"
    "fmt"
)

type DomainServer struct {
    Authkey string
}

type GetAllDomainsRequest struct {
    Authkey string
}

type GetAllDomainsResponse struct {
    Domains []string
}

func (s *DomainServer) GetAllDomains(req GetAllDomainsRequest, rsp *GetAllDomainsResponse) error {
    if req.Authkey != s.Authkey {
        return errors.New("forbidden")
    }
    rsp = &GetAllDomainsResponse{
        Domains: []string{"dom1.de", "dom2.de"},
    }
    fmt.Println(rsp)
    return nil
}

client.go

package main

import (
    "log"

    "bitbucket.org/idalu/hostd/client"
)

func main() {
    dc := &client.DomainClient{
        Authkey:    "123",
        ServerAddr: "127.0.0.1:1312",
    }
    if e := dc.GetAllDomains(); e != nil {
        log.Fatalln(e.Error())
    }
}

client/domain.go

package client

import (
    "fmt"
    "net/rpc"
)

type DomainClient struct {
    ServerAddr string
    Authkey    string
}

type GetAllDomainsRequest struct {
    Authkey string
}

type GetAllDomainsResponse struct {
    Domains []string
}

func (c *DomainClient) GetAllDomains() error {
    client, e := rpc.DialHTTP("tcp", c.ServerAddr)
    if e != nil {
        return e
    }
    req := GetAllDomainsRequest{
        Authkey: c.Authkey,
    }
    rsp := GetAllDomainsResponse{}
    if e := client.Call("DomainServer.GetAllDomains", req, &rsp); e != nil {
        return e
    }
    fmt.Println(rsp.Domains)
    return nil
}
1

1 Answers

2
votes

The bug is in server/domain.go

rsp = &GetAllDomainsResponse{
    Domains: []string{"dom1.de", "dom2.de"},
}

where you are not updating the response value provided during the call but assign a new one to your local variable. It should be something like

rsp.Domains = []string{"dom1.de", "dom2.de"}