2015-12-19 23:38:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-03-14 11:01:10 +00:00
|
|
|
"log"
|
|
|
|
|
2015-12-19 23:38:46 +00:00
|
|
|
"github.com/micro/go-micro/cmd"
|
|
|
|
"github.com/micro/go-micro/examples/server/subscriber"
|
|
|
|
"github.com/micro/go-micro/server"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
example "github.com/micro/go-micro/examples/server/proto/example"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Example struct{}
|
|
|
|
|
|
|
|
func (e *Example) Call(ctx context.Context, req *example.Request, rsp *example.Response) error {
|
2016-03-14 11:01:10 +00:00
|
|
|
log.Print("Received Example.Call request")
|
2015-12-31 18:11:46 +00:00
|
|
|
rsp.Msg = server.DefaultOptions().Id + ": Hello " + req.Name
|
2015-12-19 23:38:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Example) Stream(ctx context.Context, req *example.StreamingRequest, stream example.Example_StreamStream) error {
|
2016-03-14 11:01:10 +00:00
|
|
|
log.Printf("Received Example.Stream request with count: %d", req.Count)
|
2015-12-19 23:38:46 +00:00
|
|
|
|
|
|
|
for i := 0; i < int(req.Count); i++ {
|
2016-03-14 11:01:10 +00:00
|
|
|
log.Printf("Responding: %d", i)
|
2016-01-06 12:37:35 +00:00
|
|
|
if err := stream.Send(&example.StreamingResponse{
|
2015-12-19 23:38:46 +00:00
|
|
|
Count: int64(i),
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Example) PingPong(ctx context.Context, stream example.Example_PingPongStream) error {
|
|
|
|
for {
|
2016-01-06 12:37:35 +00:00
|
|
|
req, err := stream.Recv()
|
2015-12-19 23:38:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-14 11:01:10 +00:00
|
|
|
log.Printf("Got ping %v", req.Stroke)
|
2016-01-06 12:37:35 +00:00
|
|
|
if err := stream.Send(&example.Pong{Stroke: req.Stroke}); err != nil {
|
2015-12-19 23:38:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// optionally setup command line usage
|
|
|
|
cmd.Init()
|
|
|
|
|
|
|
|
// Initialise Server
|
|
|
|
server.Init(
|
|
|
|
server.Name("go.micro.srv.example"),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Register Subscribers
|
|
|
|
server.Subscribe(
|
|
|
|
server.NewSubscriber(
|
|
|
|
"topic.go.micro.srv.example",
|
|
|
|
new(subscriber.Example),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
server.Subscribe(
|
|
|
|
server.NewSubscriber(
|
|
|
|
"topic.go.micro.srv.example",
|
|
|
|
subscriber.Handler,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Register Handler
|
|
|
|
example.RegisterExampleHandler(
|
|
|
|
server.DefaultServer, new(Example),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Run server
|
|
|
|
if err := server.Run(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|