2019-12-08 03:59:58 -05:00
|
|
|
package rpc
|
2019-12-07 05:14:40 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-12-09 03:02:49 -05:00
|
|
|
"log"
|
2019-12-07 05:14:40 -05:00
|
|
|
|
2019-12-09 03:02:49 -05:00
|
|
|
"github.com/p14yground/nezha/model"
|
2019-12-07 05:14:40 -05:00
|
|
|
pb "github.com/p14yground/nezha/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NezhaHandler ..
|
|
|
|
type NezhaHandler struct {
|
|
|
|
Auth *AuthHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReportState ..
|
|
|
|
func (s *NezhaHandler) ReportState(c context.Context, r *pb.State) (*pb.Receipt, error) {
|
|
|
|
if err := s.Auth.Check(c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fmt.Printf("ReportState receive: %s\n", r)
|
|
|
|
return &pb.Receipt{Proced: true}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Heartbeat ..
|
|
|
|
func (s *NezhaHandler) Heartbeat(r *pb.Beat, stream pb.NezhaService_HeartbeatServer) error {
|
2019-12-09 03:02:49 -05:00
|
|
|
defer log.Println("Heartbeat exit")
|
2019-12-07 05:14:40 -05:00
|
|
|
if err := s.Auth.Check(stream.Context()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-09 03:02:49 -05:00
|
|
|
err := stream.Send(&pb.Command{
|
|
|
|
Type: model.MTReportState,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Heartbeat stream.Send err:%v", err)
|
|
|
|
}
|
|
|
|
select {}
|
2019-12-07 05:14:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register ..
|
|
|
|
func (s *NezhaHandler) Register(c context.Context, r *pb.Host) (*pb.Receipt, error) {
|
|
|
|
if err := s.Auth.Check(c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fmt.Printf("Register receive: %s\n", r)
|
|
|
|
return &pb.Receipt{Proced: true}, nil
|
|
|
|
}
|