2015-01-14 02:31:27 +03:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2015-01-31 18:49:21 +03:00
|
|
|
log "github.com/golang/glog"
|
2015-11-20 19:17:33 +03:00
|
|
|
c "github.com/micro/go-micro/context"
|
|
|
|
example "github.com/micro/go-micro/examples/server/proto/example"
|
|
|
|
"github.com/micro/go-micro/server"
|
2015-05-23 13:53:40 +03:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
2015-01-14 02:31:27 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Example struct{}
|
|
|
|
|
|
|
|
func (e *Example) Call(ctx context.Context, req *example.Request, rsp *example.Response) error {
|
2015-05-27 00:39:48 +03:00
|
|
|
md, _ := c.GetMetadata(ctx)
|
2015-06-01 20:55:27 +03:00
|
|
|
log.Infof("Received Example.Call request with metadata: %v", md)
|
2015-05-27 00:39:48 +03:00
|
|
|
rsp.Msg = server.Config().Id() + ": Hello " + req.Name
|
2015-01-14 02:31:27 +03:00
|
|
|
return nil
|
|
|
|
}
|
2015-06-01 20:55:27 +03:00
|
|
|
|
2015-12-18 04:01:59 +03:00
|
|
|
func (e *Example) Stream(ctx context.Context, stream server.Streamer) error {
|
|
|
|
log.Info("Executing streaming handler")
|
|
|
|
req := &example.StreamingRequest{}
|
|
|
|
|
|
|
|
// We just want to receive 1 request and then process here
|
|
|
|
if err := stream.Recv(req); err != nil {
|
|
|
|
log.Errorf("Error receiving streaming request: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
log.Infof("Received Example.Stream request with count: %d", req.Count)
|
2015-12-18 04:01:59 +03:00
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
for i := 0; i < int(req.Count); i++ {
|
|
|
|
log.Infof("Responding: %d", i)
|
|
|
|
|
2015-12-18 04:01:59 +03:00
|
|
|
if err := stream.Send(&example.StreamingResponse{
|
2015-06-01 20:55:27 +03:00
|
|
|
Count: int64(i),
|
2015-12-18 04:01:59 +03:00
|
|
|
}); err != nil {
|
2015-06-01 20:55:27 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-18 23:28:50 +03:00
|
|
|
|
|
|
|
func (e *Example) PingPong(ctx context.Context, stream server.Streamer) error {
|
|
|
|
for {
|
|
|
|
req := &example.Ping{}
|
|
|
|
if err := stream.Recv(req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Infof("Got ping %v", req.Stroke)
|
|
|
|
if err := stream.Send(&example.Pong{Stroke: req.Stroke}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|