Created
April 4, 2012 10:12
-
-
Save spikebike/2300125 to your computer and use it in GitHub Desktop.
Add service for go-rpcgen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"log" | |
"./addservice" | |
) | |
var ( | |
addr = flag.String("addr", ":9999", "RPC Server address (transient)") | |
message = flag.String("message", "test", "Test echo message") | |
) | |
// Echo is the type which will implement the echoservice.EchoService interface | |
// and can be called remotely. In this case, it does not have any state, but | |
// it could. | |
type Add struct{} | |
// Echo is the function that can be called remotely. Note that this can be | |
// called concurrently, so if the Echo structure did have internal state, | |
// it should be designed for concurrent access. | |
func (Add) Add(in *addservice.AddMessage, out *addservice.SumMessage) error { | |
log.Printf("server: x=%d", *in.X) | |
log.Printf("server: y=%d", *in.Y) | |
out.Z = new(int32) | |
*out.Z = *in.X + *in.Y | |
log.Printf("server: zz=%d",*out.Z) | |
return nil | |
} | |
func main() { | |
flag.Parse() | |
// Serve requests on the given address. We are going to run this in the | |
// background, since we're going to connect to our own service within the | |
// same daemon. In general, these will be separate. | |
if err := addservice.ListenAndServeAddService(*addr, Add{}); err != nil { | |
log.Fatalf("listenandserve: %s", err) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package addservice; | |
message add_message { | |
required int32 x=1; | |
required int32 y=2; | |
} | |
message sum_message { | |
required int32 z=1; | |
} | |
service add_service { | |
rpc add (add_message) returns (sum_message); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"flag" | |
"io" | |
"log" | |
"os" | |
"fmt" | |
// "github.com/kylelemons/go-rpcgen/examples/echo/echoservice" | |
"../addservice" | |
) | |
var server = flag.String("server", "localhost:9999", "RPC server address") | |
func main() { | |
var x int32 | |
var y int32 | |
sum, err := addservice.DialAddService(*server) | |
if err != nil { | |
log.Fatalf("dial: %s", err) | |
} | |
lines := bufio.NewReader(os.Stdin) | |
x=2 | |
y=3 | |
for { | |
os.Stdout.WriteString("sending 2+3") | |
os.Stdout.WriteString("> ") | |
line, err := lines.ReadString('\n') | |
if err != nil { | |
if err == io.EOF { | |
return | |
} | |
log.Fatalf("read: %s", err) | |
} | |
in := &addservice.AddMessage{X: &x, Y: &y} | |
out := &addservice.SumMessage{} | |
if err := sum.Add(in, out); err != nil { | |
log.Fatalf("echo: %s", err) | |
} | |
if out.Z == nil { | |
log.Fatalf("echo: no message returned") | |
} | |
os.Stdout.WriteString("< ") | |
os.Stdout.WriteString(line) | |
fmt.Println(*out.Z) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment