From d6e129179cb10401a20a985fcbaa0fa2dece3ea2 Mon Sep 17 00:00:00 2001 From: Pengzhan Hao Date: Tue, 17 Dec 2024 07:31:18 +0000 Subject: [PATCH] Create scaffolding of ddl-simulator Implement global server as control plane Implement device to get their info from global server and connect each others --- .gitignore | 1 + Makefile | 11 + Readme.md | 0 cmd/device/main.go | 77 ++++++ cmd/global-server/main.go | 51 ++++ go.mod | 18 ++ go.sum | 32 +++ hack/configs/basic-3layer.json | 49 ++++ hack/configs/basic.json | 30 ++ pkg/device/client.go | 52 ++++ pkg/device/device.go | 60 ++++ pkg/env/const.go | 6 + pkg/global/global.go | 92 ++++++ pkg/leader/tree.go | 17 ++ pkg/leader/tree_test.go | 24 ++ proto/device.pb.go | 492 +++++++++++++++++++++++++++++++++ proto/device.proto | 30 ++ proto/device_grpc.pb.go | 197 +++++++++++++ proto/global.pb.go | 272 ++++++++++++++++++ proto/global.proto | 21 ++ proto/global_grpc.pb.go | 121 ++++++++ 21 files changed, 1653 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 Readme.md create mode 100644 cmd/device/main.go create mode 100644 cmd/global-server/main.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 hack/configs/basic-3layer.json create mode 100644 hack/configs/basic.json create mode 100644 pkg/device/client.go create mode 100644 pkg/device/device.go create mode 100644 pkg/env/const.go create mode 100644 pkg/global/global.go create mode 100644 pkg/leader/tree.go create mode 100644 pkg/leader/tree_test.go create mode 100644 proto/device.pb.go create mode 100644 proto/device.proto create mode 100644 proto/device_grpc.pb.go create mode 100644 proto/global.pb.go create mode 100644 proto/global.proto create mode 100644 proto/global_grpc.pb.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..061c12b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +_output/ \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a00ae9d --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +gen-proto: + protoc --go_out=. --go_opt=paths=source_relative \ + --go-grpc_out=. --go-grpc_opt=paths=source_relative \ + proto/device.proto + protoc --go_out=. --go_opt=paths=source_relative \ + --go-grpc_out=. --go-grpc_opt=paths=source_relative \ + proto/global.proto + +build: + go build -o _output/global-server cmd/global-server/main.go + go build -o _output/device cmd/device/main.go \ No newline at end of file diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..e69de29 diff --git a/cmd/device/main.go b/cmd/device/main.go new file mode 100644 index 0000000..ade3300 --- /dev/null +++ b/cmd/device/main.go @@ -0,0 +1,77 @@ +package main + +import ( + "context" + "flag" + "fmt" + "net" + "os" + "os/signal" + "syscall" + "time" + + "git.pengzhan.dev/ddp-simulator/pkg/device" + "git.pengzhan.dev/ddp-simulator/pkg/env" + pb "git.pengzhan.dev/ddp-simulator/proto" + log "github.com/sirupsen/logrus" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" +) + +var ( + id = flag.String("id", "0", "The device id") + gip = flag.String("gip", "127.0.0.1", "The global server ip") + gport = flag.Int("gport", 3200, "The global server port") +) + +func init() { + flag.Parse() + + log.SetOutput(os.Stdout) + log.SetLevel(log.TraceLevel) + // log.SetReportCaller(true) +} + +func main() { + conn, err := grpc.NewClient(fmt.Sprintf("%s:%d", *gip, *gport), grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + log.Fatalf("failed to connect to global server: %v", err) + } + client := pb.NewGlobalServiceClient(conn) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + resp, err := client.Init(ctx, &pb.InitRequest{Id: *id}) + if err != nil { + log.Fatalf("failed to init: %v", err) + } + + d := device.NewDevice(*id, resp.Role, resp.Ip, int(resp.Port)) + if resp.Capacity != nil { + d.UpdateCapacity(resp.Capacity) + } + if resp.ParentId != nil { + d.UpdateParent(resp.ParentId, *resp.Pip, int(*resp.Pport)) + if err := d.RegisterClientRetry(context.TODO(), 30*time.Second); err != nil { + log.WithField("device", d.ID).Fatalf("failed to register: %v", err) + } + } + + s := grpc.NewServer() + lis, err := net.Listen(env.PROTOCOL, fmt.Sprintf("%s:%d", resp.Ip, resp.Port)) + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + go func() { + <-c + os.Exit(1) + }() + pb.RegisterDeviceServer(s, d) + + log.Infof("device %s start on %s:%d", *id, resp.Ip, resp.Port) + + if err := s.Serve(lis); err != nil { + log.Fatalf("failed to serve: %v", err) + } +} diff --git a/cmd/global-server/main.go b/cmd/global-server/main.go new file mode 100644 index 0000000..d6d326d --- /dev/null +++ b/cmd/global-server/main.go @@ -0,0 +1,51 @@ +package main + +import ( + "flag" + "fmt" + "net" + "os" + "os/signal" + "syscall" + + "git.pengzhan.dev/ddp-simulator/pkg/env" + "git.pengzhan.dev/ddp-simulator/pkg/global" + pb "git.pengzhan.dev/ddp-simulator/proto" + log "github.com/sirupsen/logrus" + "google.golang.org/grpc" +) + +var ( + ip = flag.String("ip", "0.0.0.0", "The global server ip") + port = flag.Int("port", 3200, "The global server port") + configPath = flag.String("config-path", "", "Config file path") +) + +func init() { + flag.Parse() + + log.SetOutput(os.Stdout) + log.SetLevel(log.TraceLevel) + // log.SetReportCaller(true) +} + +func main() { + lis, err := net.Listen(env.PROTOCOL, fmt.Sprintf("%s:%d", *ip, *port)) + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + go func() { + <-c + os.Exit(1) + }() + + s := grpc.NewServer() + g := global.NewGlobalServer(*configPath) + pb.RegisterGlobalServiceServer(s, g) + + if err := s.Serve(lis); err != nil { + log.Fatalf("failed to serve: %v", err) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..fc4825d --- /dev/null +++ b/go.mod @@ -0,0 +1,18 @@ +module git.pengzhan.dev/ddp-simulator + +go 1.22.2 + +require ( + github.com/cenkalti/backoff/v4 v4.3.0 + github.com/sirupsen/logrus v1.9.3 + google.golang.org/grpc v1.67.0 + google.golang.org/protobuf v1.34.2 + gopkg.in/yaml.v3 v3.0.1 +) + +require ( + golang.org/x/net v0.28.0 // indirect + golang.org/x/sys v0.24.0 // indirect + golang.org/x/text v0.17.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..56438ef --- /dev/null +++ b/go.sum @@ -0,0 +1,32 @@ +github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= +github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= +golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= +golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= +golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 h1:e7S5W7MGGLaSu8j3YjdezkZ+m1/Nm0uRVRMEMGk26Xs= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU= +google.golang.org/grpc v1.67.0 h1:IdH9y6PF5MPSdAntIcpjQ+tXO41pcQsfZV2RxtQgVcw= +google.golang.org/grpc v1.67.0/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/hack/configs/basic-3layer.json b/hack/configs/basic-3layer.json new file mode 100644 index 0000000..b1cdc46 --- /dev/null +++ b/hack/configs/basic-3layer.json @@ -0,0 +1,49 @@ +[ + { + "id": "d1", + "role": "leader", + "capacity": 1.0, + "ip": "0.0.0.0", + "port": 3700 + }, + { + "id": "d2", + "role": "leader", + "capacity": 1.0, + "ip": "0.0.0.0", + "port": 3701, + "parentID": "d1", + "parentIP": "127.0.0.1", + "parentPort": 3700 + }, + { + "id": "d3", + "role": "worker", + "capacity": 2.0, + "ip": "0.0.0.0", + "port": 3702, + "parentID": "d1", + "parentIP": "127.0.0.1", + "parentPort": 3700 + }, + { + "id": "d4", + "role": "worker", + "capacity": 1.0, + "ip": "0.0.0.0", + "port": 3703, + "parentID": "d2", + "parentIP": "127.0.0.1", + "parentPort": 3701 + }, + { + "id": "d5", + "role": "worker", + "capacity": 5.0, + "ip": "0.0.0.0", + "port": 3704, + "parentID": "d2", + "parentIP": "127.0.0.1", + "parentPort": 3701 + } +] \ No newline at end of file diff --git a/hack/configs/basic.json b/hack/configs/basic.json new file mode 100644 index 0000000..05af262 --- /dev/null +++ b/hack/configs/basic.json @@ -0,0 +1,30 @@ +[ + { + "id": "d1", + "role": "leader", + "capacity": 1.0, + "ip": "0.0.0.0", + "port": 3700 + }, + { + "id": "d2", + "role": "worker", + "capacity": 1.0, + "ip": "0.0.0.0", + "port": 3701, + "parentID": "d1", + "parentIP": "127.0.0.1", + "parentPort": 3700 + }, + { + "id": "d3", + "role": "worker", + "capacity": 2.0, + "ip": "0.0.0.0", + "port": 3702, + "parentID": "d1", + "parentIP": "127.0.0.1", + "parentPort": 3700 + } + ] + \ No newline at end of file diff --git a/pkg/device/client.go b/pkg/device/client.go new file mode 100644 index 0000000..8ec118a --- /dev/null +++ b/pkg/device/client.go @@ -0,0 +1,52 @@ +package device + +import ( + "context" + "fmt" + "time" + + pb "git.pengzhan.dev/ddp-simulator/proto" + "github.com/cenkalti/backoff/v4" + log "github.com/sirupsen/logrus" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" +) + +func (d *Device) RegisterClient() error { + conn, err := grpc.NewClient(fmt.Sprintf("%s:%d", d.ParentIP, d.ParentPort), grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + log.Fatalf("failed to connect to global server: %v", err) + } + defer conn.Close() + + client := pb.NewDeviceClient(conn) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + children := make([]string, 0) + for _, child := range d.Children { + children = append(children, child.ID) + } + + _, err = client.Register(ctx, &pb.RegisterRequest{ + Id: d.ID, + Role: d.Role, + Ip: d.IP, + Port: int32(d.Port), + ChildrenIds: children, + Capacity: d.Capacity, + }) + return err +} + +func (d *Device) RegisterClientRetry(ctx context.Context, timeout time.Duration) error { + b := backoff.NewExponentialBackOff() + b.InitialInterval = 1 * time.Second + b.MaxElapsedTime = timeout + + retryOp := func() error { + return d.RegisterClient() + } + err := backoff.Retry(retryOp, backoff.WithContext(b, ctx)) + return err +} diff --git a/pkg/device/device.go b/pkg/device/device.go new file mode 100644 index 0000000..d3154d4 --- /dev/null +++ b/pkg/device/device.go @@ -0,0 +1,60 @@ +package device + +import ( + "context" + "time" + + pb "git.pengzhan.dev/ddp-simulator/proto" + log "github.com/sirupsen/logrus" +) + +type Device struct { + pb.UnimplementedDeviceServer + ID string + Role string + IP string + Port int + ParentID *string + ParentIP string + ParentPort int + Capacity *float32 + + Children map[string]*Device +} + +func NewDevice(id, role, ip string, port int) *Device { + return &Device{ + ID: id, + Role: role, + IP: ip, + Port: port, + Children: make(map[string]*Device), + } +} + +func (d *Device) UpdateCapacity(capacity *float32) { + d.Capacity = capacity +} + +func (d *Device) UpdateParent(parentID *string, parentIP string, parentPort int) { + d.ParentID = parentID + d.ParentIP = parentIP + d.ParentPort = parentPort +} + +func (d *Device) Register(ctx context.Context, in *pb.RegisterRequest) (*pb.RegisterResponse, error) { + child := NewDevice(in.Id, in.Role, in.Ip, int(in.Port)) + if in.Capacity != nil { + child.UpdateCapacity(in.Capacity) + } + d.Children[in.Id] = child + log.WithField("device", d.ID).Infof("device %s claims on device %s as its child", child.ID, d.ID) + + // Update with parent + if d.ParentID != nil { + if err := d.RegisterClientRetry(ctx, 30*time.Second); err != nil { + log.WithField("device", d.ID).Fatalf("failed to register: %v", err) + } + } + return &pb.RegisterResponse{}, nil +} diff --git a/pkg/env/const.go b/pkg/env/const.go new file mode 100644 index 0000000..0d1f198 --- /dev/null +++ b/pkg/env/const.go @@ -0,0 +1,6 @@ +package env + +const ( + PROTOCOL = "tcp" + SOCKET = "/tmp/ddp-simulator/%s.sock" +) diff --git a/pkg/global/global.go b/pkg/global/global.go new file mode 100644 index 0000000..c89191f --- /dev/null +++ b/pkg/global/global.go @@ -0,0 +1,92 @@ +package global + +import ( + "context" + "encoding/json" + "fmt" + "io" + "os" + + pb "git.pengzhan.dev/ddp-simulator/proto" + log "github.com/sirupsen/logrus" + "google.golang.org/protobuf/proto" +) + +type GlobalServer struct { + pb.UnimplementedGlobalServiceServer + + Configs map[string]config +} + +type config struct { + ID string `json:"id"` + Role string `json:"role"` + Capacity float32 `json:"capacity"` + IP string `json:"ip"` + Port int `json:"port"` + ParentID string `json:"parentID"` + ParentIP string `json:"parentIP"` + ParentPort int `json:"parentPort"` +} + +func NewGlobalServer(configPath string) *GlobalServer { + configs, err := ReadConfigFromPath(configPath) + if err != nil { + log.Fatalf("could not read config file: %v", err) + } + return &GlobalServer{ + Configs: configs, + } +} + +func ReadConfigFromPath(configPath string) (map[string]config, error) { + file, err := os.Open(configPath) + if err != nil { + return nil, fmt.Errorf("could not open file: %v", err) + } + defer file.Close() + + fileContent, err := io.ReadAll(file) + if err != nil { + return nil, fmt.Errorf("could not read file content: %v", err) + } + + var configs []config + err = json.Unmarshal(fileContent, &configs) + if err != nil { + return nil, fmt.Errorf("could not parse JSON content: %v", err) + } + + configMap := make(map[string]config) + + for _, c := range configs { + configMap[c.ID] = c + } + + return configMap, nil +} + +func (g *GlobalServer) Init(ctx context.Context, in *pb.InitRequest) (*pb.InitResponse, error) { + config, ok := g.Configs[in.Id] + if !ok { + log.Errorf("device %s not found", in.Id) + return nil, fmt.Errorf("device %s not found", in.Id) + } + log.Infof("device %s init", config.ID) + + resp := &pb.InitResponse{ + Role: config.Role, + Capacity: proto.Float32(config.Capacity), + Ip: config.IP, + Port: int32(config.Port), + } + if config.ParentID != "" { + resp.ParentId = proto.String(config.ParentID) + resp.Pip = proto.String(config.ParentIP) + resp.Pport = proto.Int32(int32(config.ParentPort)) + } + if config.Capacity != 0 { + resp.Capacity = proto.Float32(config.Capacity) + } + return resp, nil +} diff --git a/pkg/leader/tree.go b/pkg/leader/tree.go new file mode 100644 index 0000000..9cb17ca --- /dev/null +++ b/pkg/leader/tree.go @@ -0,0 +1,17 @@ +package leader + +type TreeNode struct { + Id string `yaml:"id"` + Children []TreeNode `yaml:"children,omitempty"` +} + +func NewTreeNode(id string, children []TreeNode) *TreeNode { + return &TreeNode{ + Id: id, + Children: children, + } +} + +func (t *TreeNode) AddChild(child *TreeNode) { + t.Children = append(t.Children, *child) +} diff --git a/pkg/leader/tree_test.go b/pkg/leader/tree_test.go new file mode 100644 index 0000000..cb9e43f --- /dev/null +++ b/pkg/leader/tree_test.go @@ -0,0 +1,24 @@ +package leader_test + +import ( + "fmt" + "testing" + + yaml "gopkg.in/yaml.v3" + + "git.pengzhan.dev/ddp-simulator/pkg/leader" +) + +func TestTree(t *testing.T) { + l4 := leader.NewTreeNode("4", nil) + l3 := leader.NewTreeNode("3", nil) + l2 := leader.NewTreeNode("2", nil) + l1 := leader.NewTreeNode("1", []leader.TreeNode{*l4, *l3}) + l0 := leader.NewTreeNode("0", []leader.TreeNode{*l1, *l2}) + + yamlData, err := yaml.Marshal(l0) + if err != nil { + t.Fatalf("failed to marshal yaml: %v", err) + } + fmt.Println(string(yamlData)) +} diff --git a/proto/device.pb.go b/proto/device.pb.go new file mode 100644 index 0000000..e4181a8 --- /dev/null +++ b/proto/device.pb.go @@ -0,0 +1,492 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.34.2 +// protoc v3.21.12 +// source: proto/device.proto + +package proto + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type RegisterRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` + Role string `protobuf:"bytes,2,opt,name=role,proto3" json:"role,omitempty"` + Capacity *float32 `protobuf:"fixed32,3,opt,name=capacity,proto3,oneof" json:"capacity,omitempty"` + ChildrenIds []string `protobuf:"bytes,4,rep,name=children_ids,json=childrenIds,proto3" json:"children_ids,omitempty"` + Ip string `protobuf:"bytes,5,opt,name=ip,proto3" json:"ip,omitempty"` + Port int32 `protobuf:"varint,6,opt,name=port,proto3" json:"port,omitempty"` +} + +func (x *RegisterRequest) Reset() { + *x = RegisterRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_device_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *RegisterRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RegisterRequest) ProtoMessage() {} + +func (x *RegisterRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_device_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use RegisterRequest.ProtoReflect.Descriptor instead. +func (*RegisterRequest) Descriptor() ([]byte, []int) { + return file_proto_device_proto_rawDescGZIP(), []int{0} +} + +func (x *RegisterRequest) GetId() string { + if x != nil { + return x.Id + } + return "" +} + +func (x *RegisterRequest) GetRole() string { + if x != nil { + return x.Role + } + return "" +} + +func (x *RegisterRequest) GetCapacity() float32 { + if x != nil && x.Capacity != nil { + return *x.Capacity + } + return 0 +} + +func (x *RegisterRequest) GetChildrenIds() []string { + if x != nil { + return x.ChildrenIds + } + return nil +} + +func (x *RegisterRequest) GetIp() string { + if x != nil { + return x.Ip + } + return "" +} + +func (x *RegisterRequest) GetPort() int32 { + if x != nil { + return x.Port + } + return 0 +} + +type RegisterResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + ParentId string `protobuf:"bytes,1,opt,name=parent_id,json=parentId,proto3" json:"parent_id,omitempty"` +} + +func (x *RegisterResponse) Reset() { + *x = RegisterResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_device_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *RegisterResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RegisterResponse) ProtoMessage() {} + +func (x *RegisterResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_device_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use RegisterResponse.ProtoReflect.Descriptor instead. +func (*RegisterResponse) Descriptor() ([]byte, []int) { + return file_proto_device_proto_rawDescGZIP(), []int{1} +} + +func (x *RegisterResponse) GetParentId() string { + if x != nil { + return x.ParentId + } + return "" +} + +type ReleaseRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *ReleaseRequest) Reset() { + *x = ReleaseRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_device_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ReleaseRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ReleaseRequest) ProtoMessage() {} + +func (x *ReleaseRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_device_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ReleaseRequest.ProtoReflect.Descriptor instead. +func (*ReleaseRequest) Descriptor() ([]byte, []int) { + return file_proto_device_proto_rawDescGZIP(), []int{2} +} + +type ReleaseResposne struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *ReleaseResposne) Reset() { + *x = ReleaseResposne{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_device_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ReleaseResposne) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ReleaseResposne) ProtoMessage() {} + +func (x *ReleaseResposne) ProtoReflect() protoreflect.Message { + mi := &file_proto_device_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ReleaseResposne.ProtoReflect.Descriptor instead. +func (*ReleaseResposne) Descriptor() ([]byte, []int) { + return file_proto_device_proto_rawDescGZIP(), []int{3} +} + +type AssignJobRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + DataSize int32 `protobuf:"varint,1,opt,name=data_size,json=dataSize,proto3" json:"data_size,omitempty"` +} + +func (x *AssignJobRequest) Reset() { + *x = AssignJobRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_device_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *AssignJobRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AssignJobRequest) ProtoMessage() {} + +func (x *AssignJobRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_device_proto_msgTypes[4] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use AssignJobRequest.ProtoReflect.Descriptor instead. +func (*AssignJobRequest) Descriptor() ([]byte, []int) { + return file_proto_device_proto_rawDescGZIP(), []int{4} +} + +func (x *AssignJobRequest) GetDataSize() int32 { + if x != nil { + return x.DataSize + } + return 0 +} + +type AssignJobResposne struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *AssignJobResposne) Reset() { + *x = AssignJobResposne{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_device_proto_msgTypes[5] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *AssignJobResposne) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AssignJobResposne) ProtoMessage() {} + +func (x *AssignJobResposne) ProtoReflect() protoreflect.Message { + mi := &file_proto_device_proto_msgTypes[5] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use AssignJobResposne.ProtoReflect.Descriptor instead. +func (*AssignJobResposne) Descriptor() ([]byte, []int) { + return file_proto_device_proto_rawDescGZIP(), []int{5} +} + +var File_proto_device_proto protoreflect.FileDescriptor + +var file_proto_device_proto_rawDesc = []byte{ + 0x0a, 0x12, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xaa, 0x01, 0x0a, 0x0f, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, + 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x72, 0x6f, 0x6c, 0x65, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x72, 0x6f, 0x6c, 0x65, 0x12, 0x1f, 0x0a, 0x08, + 0x63, 0x61, 0x70, 0x61, 0x63, 0x69, 0x74, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x02, 0x48, 0x00, + 0x52, 0x08, 0x63, 0x61, 0x70, 0x61, 0x63, 0x69, 0x74, 0x79, 0x88, 0x01, 0x01, 0x12, 0x21, 0x0a, + 0x0c, 0x63, 0x68, 0x69, 0x6c, 0x64, 0x72, 0x65, 0x6e, 0x5f, 0x69, 0x64, 0x73, 0x18, 0x04, 0x20, + 0x03, 0x28, 0x09, 0x52, 0x0b, 0x63, 0x68, 0x69, 0x6c, 0x64, 0x72, 0x65, 0x6e, 0x49, 0x64, 0x73, + 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x70, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x70, + 0x12, 0x12, 0x0a, 0x04, 0x70, 0x6f, 0x72, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, + 0x70, 0x6f, 0x72, 0x74, 0x42, 0x0b, 0x0a, 0x09, 0x5f, 0x63, 0x61, 0x70, 0x61, 0x63, 0x69, 0x74, + 0x79, 0x22, 0x2f, 0x0a, 0x10, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x70, 0x61, 0x72, 0x65, 0x6e, 0x74, 0x5f, + 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x61, 0x72, 0x65, 0x6e, 0x74, + 0x49, 0x64, 0x22, 0x10, 0x0a, 0x0e, 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x22, 0x11, 0x0a, 0x0f, 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x73, 0x6e, 0x65, 0x22, 0x2f, 0x0a, 0x10, 0x41, 0x73, 0x73, 0x69, 0x67, + 0x6e, 0x4a, 0x6f, 0x62, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1b, 0x0a, 0x09, 0x64, + 0x61, 0x74, 0x61, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, + 0x64, 0x61, 0x74, 0x61, 0x53, 0x69, 0x7a, 0x65, 0x22, 0x13, 0x0a, 0x11, 0x41, 0x73, 0x73, 0x69, + 0x67, 0x6e, 0x4a, 0x6f, 0x62, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x73, 0x6e, 0x65, 0x32, 0xa1, 0x01, + 0x0a, 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x12, 0x31, 0x0a, 0x08, 0x52, 0x65, 0x67, 0x69, + 0x73, 0x74, 0x65, 0x72, 0x12, 0x10, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x11, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, + 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x2e, 0x0a, 0x07, 0x52, + 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x12, 0x0f, 0x2e, 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x10, 0x2e, 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, + 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x73, 0x6e, 0x65, 0x22, 0x00, 0x12, 0x34, 0x0a, 0x09, 0x41, + 0x73, 0x73, 0x69, 0x67, 0x6e, 0x4a, 0x6f, 0x62, 0x12, 0x11, 0x2e, 0x41, 0x73, 0x73, 0x69, 0x67, + 0x6e, 0x4a, 0x6f, 0x62, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x41, 0x73, + 0x73, 0x69, 0x67, 0x6e, 0x4a, 0x6f, 0x62, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x73, 0x6e, 0x65, 0x22, + 0x00, 0x42, 0x26, 0x5a, 0x24, 0x67, 0x69, 0x74, 0x2e, 0x70, 0x65, 0x6e, 0x67, 0x7a, 0x68, 0x61, + 0x6e, 0x2e, 0x64, 0x65, 0x76, 0x2f, 0x64, 0x64, 0x70, 0x2d, 0x73, 0x69, 0x6d, 0x75, 0x6c, 0x61, + 0x74, 0x6f, 0x72, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x33, +} + +var ( + file_proto_device_proto_rawDescOnce sync.Once + file_proto_device_proto_rawDescData = file_proto_device_proto_rawDesc +) + +func file_proto_device_proto_rawDescGZIP() []byte { + file_proto_device_proto_rawDescOnce.Do(func() { + file_proto_device_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_device_proto_rawDescData) + }) + return file_proto_device_proto_rawDescData +} + +var file_proto_device_proto_msgTypes = make([]protoimpl.MessageInfo, 6) +var file_proto_device_proto_goTypes = []any{ + (*RegisterRequest)(nil), // 0: RegisterRequest + (*RegisterResponse)(nil), // 1: RegisterResponse + (*ReleaseRequest)(nil), // 2: ReleaseRequest + (*ReleaseResposne)(nil), // 3: ReleaseResposne + (*AssignJobRequest)(nil), // 4: AssignJobRequest + (*AssignJobResposne)(nil), // 5: AssignJobResposne +} +var file_proto_device_proto_depIdxs = []int32{ + 0, // 0: device.Register:input_type -> RegisterRequest + 2, // 1: device.Release:input_type -> ReleaseRequest + 4, // 2: device.AssignJob:input_type -> AssignJobRequest + 1, // 3: device.Register:output_type -> RegisterResponse + 3, // 4: device.Release:output_type -> ReleaseResposne + 5, // 5: device.AssignJob:output_type -> AssignJobResposne + 3, // [3:6] is the sub-list for method output_type + 0, // [0:3] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_proto_device_proto_init() } +func file_proto_device_proto_init() { + if File_proto_device_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proto_device_proto_msgTypes[0].Exporter = func(v any, i int) any { + switch v := v.(*RegisterRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_device_proto_msgTypes[1].Exporter = func(v any, i int) any { + switch v := v.(*RegisterResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_device_proto_msgTypes[2].Exporter = func(v any, i int) any { + switch v := v.(*ReleaseRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_device_proto_msgTypes[3].Exporter = func(v any, i int) any { + switch v := v.(*ReleaseResposne); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_device_proto_msgTypes[4].Exporter = func(v any, i int) any { + switch v := v.(*AssignJobRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_device_proto_msgTypes[5].Exporter = func(v any, i int) any { + switch v := v.(*AssignJobResposne); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + file_proto_device_proto_msgTypes[0].OneofWrappers = []any{} + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_proto_device_proto_rawDesc, + NumEnums: 0, + NumMessages: 6, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_proto_device_proto_goTypes, + DependencyIndexes: file_proto_device_proto_depIdxs, + MessageInfos: file_proto_device_proto_msgTypes, + }.Build() + File_proto_device_proto = out.File + file_proto_device_proto_rawDesc = nil + file_proto_device_proto_goTypes = nil + file_proto_device_proto_depIdxs = nil +} diff --git a/proto/device.proto b/proto/device.proto new file mode 100644 index 0000000..a1206f9 --- /dev/null +++ b/proto/device.proto @@ -0,0 +1,30 @@ +syntax = "proto3"; + +option go_package = "git.pengzhan.dev/ddp-simulator/proto"; + +service device { + rpc Register(RegisterRequest) returns (RegisterResponse) {} + rpc Release(ReleaseRequest) returns (ReleaseResposne) {} + rpc AssignJob(AssignJobRequest) returns (AssignJobResposne) {} +} + +message RegisterRequest { + string id = 1; + string role = 2; + optional float capacity = 3; + repeated string children_ids = 4; + string ip = 5; + int32 port = 6; +} + +message RegisterResponse { + string parent_id = 1; +} + +message ReleaseRequest{} + +message ReleaseResposne{} + +message AssignJobRequest { int32 data_size = 1; } + +message AssignJobResposne {} diff --git a/proto/device_grpc.pb.go b/proto/device_grpc.pb.go new file mode 100644 index 0000000..dc2c597 --- /dev/null +++ b/proto/device_grpc.pb.go @@ -0,0 +1,197 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.5.1 +// - protoc v3.21.12 +// source: proto/device.proto + +package proto + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.64.0 or later. +const _ = grpc.SupportPackageIsVersion9 + +const ( + Device_Register_FullMethodName = "/device/Register" + Device_Release_FullMethodName = "/device/Release" + Device_AssignJob_FullMethodName = "/device/AssignJob" +) + +// DeviceClient is the client API for Device service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type DeviceClient interface { + Register(ctx context.Context, in *RegisterRequest, opts ...grpc.CallOption) (*RegisterResponse, error) + Release(ctx context.Context, in *ReleaseRequest, opts ...grpc.CallOption) (*ReleaseResposne, error) + AssignJob(ctx context.Context, in *AssignJobRequest, opts ...grpc.CallOption) (*AssignJobResposne, error) +} + +type deviceClient struct { + cc grpc.ClientConnInterface +} + +func NewDeviceClient(cc grpc.ClientConnInterface) DeviceClient { + return &deviceClient{cc} +} + +func (c *deviceClient) Register(ctx context.Context, in *RegisterRequest, opts ...grpc.CallOption) (*RegisterResponse, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(RegisterResponse) + err := c.cc.Invoke(ctx, Device_Register_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *deviceClient) Release(ctx context.Context, in *ReleaseRequest, opts ...grpc.CallOption) (*ReleaseResposne, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(ReleaseResposne) + err := c.cc.Invoke(ctx, Device_Release_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *deviceClient) AssignJob(ctx context.Context, in *AssignJobRequest, opts ...grpc.CallOption) (*AssignJobResposne, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(AssignJobResposne) + err := c.cc.Invoke(ctx, Device_AssignJob_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +// DeviceServer is the server API for Device service. +// All implementations must embed UnimplementedDeviceServer +// for forward compatibility. +type DeviceServer interface { + Register(context.Context, *RegisterRequest) (*RegisterResponse, error) + Release(context.Context, *ReleaseRequest) (*ReleaseResposne, error) + AssignJob(context.Context, *AssignJobRequest) (*AssignJobResposne, error) + mustEmbedUnimplementedDeviceServer() +} + +// UnimplementedDeviceServer must be embedded to have +// forward compatible implementations. +// +// NOTE: this should be embedded by value instead of pointer to avoid a nil +// pointer dereference when methods are called. +type UnimplementedDeviceServer struct{} + +func (UnimplementedDeviceServer) Register(context.Context, *RegisterRequest) (*RegisterResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Register not implemented") +} +func (UnimplementedDeviceServer) Release(context.Context, *ReleaseRequest) (*ReleaseResposne, error) { + return nil, status.Errorf(codes.Unimplemented, "method Release not implemented") +} +func (UnimplementedDeviceServer) AssignJob(context.Context, *AssignJobRequest) (*AssignJobResposne, error) { + return nil, status.Errorf(codes.Unimplemented, "method AssignJob not implemented") +} +func (UnimplementedDeviceServer) mustEmbedUnimplementedDeviceServer() {} +func (UnimplementedDeviceServer) testEmbeddedByValue() {} + +// UnsafeDeviceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to DeviceServer will +// result in compilation errors. +type UnsafeDeviceServer interface { + mustEmbedUnimplementedDeviceServer() +} + +func RegisterDeviceServer(s grpc.ServiceRegistrar, srv DeviceServer) { + // If the following call pancis, it indicates UnimplementedDeviceServer was + // embedded by pointer and is nil. This will cause panics if an + // unimplemented method is ever invoked, so we test this at initialization + // time to prevent it from happening at runtime later due to I/O. + if t, ok := srv.(interface{ testEmbeddedByValue() }); ok { + t.testEmbeddedByValue() + } + s.RegisterService(&Device_ServiceDesc, srv) +} + +func _Device_Register_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(RegisterRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(DeviceServer).Register(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Device_Register_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(DeviceServer).Register(ctx, req.(*RegisterRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _Device_Release_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(ReleaseRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(DeviceServer).Release(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Device_Release_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(DeviceServer).Release(ctx, req.(*ReleaseRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _Device_AssignJob_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(AssignJobRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(DeviceServer).AssignJob(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Device_AssignJob_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(DeviceServer).AssignJob(ctx, req.(*AssignJobRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// Device_ServiceDesc is the grpc.ServiceDesc for Device service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var Device_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "device", + HandlerType: (*DeviceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Register", + Handler: _Device_Register_Handler, + }, + { + MethodName: "Release", + Handler: _Device_Release_Handler, + }, + { + MethodName: "AssignJob", + Handler: _Device_AssignJob_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "proto/device.proto", +} diff --git a/proto/global.pb.go b/proto/global.pb.go new file mode 100644 index 0000000..a6d0b90 --- /dev/null +++ b/proto/global.pb.go @@ -0,0 +1,272 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.34.2 +// protoc v3.21.12 +// source: proto/global.proto + +package proto + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type InitRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` +} + +func (x *InitRequest) Reset() { + *x = InitRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_global_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *InitRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*InitRequest) ProtoMessage() {} + +func (x *InitRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_global_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use InitRequest.ProtoReflect.Descriptor instead. +func (*InitRequest) Descriptor() ([]byte, []int) { + return file_proto_global_proto_rawDescGZIP(), []int{0} +} + +func (x *InitRequest) GetId() string { + if x != nil { + return x.Id + } + return "" +} + +type InitResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Role string `protobuf:"bytes,1,opt,name=role,proto3" json:"role,omitempty"` + Capacity *float32 `protobuf:"fixed32,2,opt,name=capacity,proto3,oneof" json:"capacity,omitempty"` + Ip string `protobuf:"bytes,3,opt,name=ip,proto3" json:"ip,omitempty"` + Port int32 `protobuf:"varint,4,opt,name=port,proto3" json:"port,omitempty"` + ParentId *string `protobuf:"bytes,5,opt,name=parent_id,json=parentId,proto3,oneof" json:"parent_id,omitempty"` + Pip *string `protobuf:"bytes,6,opt,name=pip,proto3,oneof" json:"pip,omitempty"` + Pport *int32 `protobuf:"varint,7,opt,name=pport,proto3,oneof" json:"pport,omitempty"` +} + +func (x *InitResponse) Reset() { + *x = InitResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_global_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *InitResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*InitResponse) ProtoMessage() {} + +func (x *InitResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_global_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use InitResponse.ProtoReflect.Descriptor instead. +func (*InitResponse) Descriptor() ([]byte, []int) { + return file_proto_global_proto_rawDescGZIP(), []int{1} +} + +func (x *InitResponse) GetRole() string { + if x != nil { + return x.Role + } + return "" +} + +func (x *InitResponse) GetCapacity() float32 { + if x != nil && x.Capacity != nil { + return *x.Capacity + } + return 0 +} + +func (x *InitResponse) GetIp() string { + if x != nil { + return x.Ip + } + return "" +} + +func (x *InitResponse) GetPort() int32 { + if x != nil { + return x.Port + } + return 0 +} + +func (x *InitResponse) GetParentId() string { + if x != nil && x.ParentId != nil { + return *x.ParentId + } + return "" +} + +func (x *InitResponse) GetPip() string { + if x != nil && x.Pip != nil { + return *x.Pip + } + return "" +} + +func (x *InitResponse) GetPport() int32 { + if x != nil && x.Pport != nil { + return *x.Pport + } + return 0 +} + +var File_proto_global_proto protoreflect.FileDescriptor + +var file_proto_global_proto_rawDesc = []byte{ + 0x0a, 0x12, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x67, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x1d, 0x0a, 0x0b, 0x49, 0x6e, 0x69, 0x74, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x02, 0x69, 0x64, 0x22, 0xe8, 0x01, 0x0a, 0x0c, 0x49, 0x6e, 0x69, 0x74, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x72, 0x6f, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x04, 0x72, 0x6f, 0x6c, 0x65, 0x12, 0x1f, 0x0a, 0x08, 0x63, 0x61, 0x70, 0x61, + 0x63, 0x69, 0x74, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28, 0x02, 0x48, 0x00, 0x52, 0x08, 0x63, 0x61, + 0x70, 0x61, 0x63, 0x69, 0x74, 0x79, 0x88, 0x01, 0x01, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x70, 0x18, + 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x70, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x6f, 0x72, + 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x70, 0x6f, 0x72, 0x74, 0x12, 0x20, 0x0a, + 0x09, 0x70, 0x61, 0x72, 0x65, 0x6e, 0x74, 0x5f, 0x69, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, + 0x48, 0x01, 0x52, 0x08, 0x70, 0x61, 0x72, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x88, 0x01, 0x01, 0x12, + 0x15, 0x0a, 0x03, 0x70, 0x69, 0x70, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x48, 0x02, 0x52, 0x03, + 0x70, 0x69, 0x70, 0x88, 0x01, 0x01, 0x12, 0x19, 0x0a, 0x05, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x18, + 0x07, 0x20, 0x01, 0x28, 0x05, 0x48, 0x03, 0x52, 0x05, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x88, 0x01, + 0x01, 0x42, 0x0b, 0x0a, 0x09, 0x5f, 0x63, 0x61, 0x70, 0x61, 0x63, 0x69, 0x74, 0x79, 0x42, 0x0c, + 0x0a, 0x0a, 0x5f, 0x70, 0x61, 0x72, 0x65, 0x6e, 0x74, 0x5f, 0x69, 0x64, 0x42, 0x06, 0x0a, 0x04, + 0x5f, 0x70, 0x69, 0x70, 0x42, 0x08, 0x0a, 0x06, 0x5f, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x32, 0x36, + 0x0a, 0x0d, 0x67, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, + 0x25, 0x0a, 0x04, 0x49, 0x6e, 0x69, 0x74, 0x12, 0x0c, 0x2e, 0x49, 0x6e, 0x69, 0x74, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x0d, 0x2e, 0x49, 0x6e, 0x69, 0x74, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x26, 0x5a, 0x24, 0x67, 0x69, 0x74, 0x2e, 0x70, 0x65, + 0x6e, 0x67, 0x7a, 0x68, 0x61, 0x6e, 0x2e, 0x64, 0x65, 0x76, 0x2f, 0x64, 0x64, 0x70, 0x2d, 0x73, + 0x69, 0x6d, 0x75, 0x6c, 0x61, 0x74, 0x6f, 0x72, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_proto_global_proto_rawDescOnce sync.Once + file_proto_global_proto_rawDescData = file_proto_global_proto_rawDesc +) + +func file_proto_global_proto_rawDescGZIP() []byte { + file_proto_global_proto_rawDescOnce.Do(func() { + file_proto_global_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_global_proto_rawDescData) + }) + return file_proto_global_proto_rawDescData +} + +var file_proto_global_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_proto_global_proto_goTypes = []any{ + (*InitRequest)(nil), // 0: InitRequest + (*InitResponse)(nil), // 1: InitResponse +} +var file_proto_global_proto_depIdxs = []int32{ + 0, // 0: globalService.Init:input_type -> InitRequest + 1, // 1: globalService.Init:output_type -> InitResponse + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_proto_global_proto_init() } +func file_proto_global_proto_init() { + if File_proto_global_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proto_global_proto_msgTypes[0].Exporter = func(v any, i int) any { + switch v := v.(*InitRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_global_proto_msgTypes[1].Exporter = func(v any, i int) any { + switch v := v.(*InitResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + file_proto_global_proto_msgTypes[1].OneofWrappers = []any{} + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_proto_global_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_proto_global_proto_goTypes, + DependencyIndexes: file_proto_global_proto_depIdxs, + MessageInfos: file_proto_global_proto_msgTypes, + }.Build() + File_proto_global_proto = out.File + file_proto_global_proto_rawDesc = nil + file_proto_global_proto_goTypes = nil + file_proto_global_proto_depIdxs = nil +} diff --git a/proto/global.proto b/proto/global.proto new file mode 100644 index 0000000..a759425 --- /dev/null +++ b/proto/global.proto @@ -0,0 +1,21 @@ +syntax = "proto3"; + +option go_package = "git.pengzhan.dev/ddp-simulator/proto"; + +service globalService { + rpc Init(InitRequest) returns (InitResponse) {} +} + +message InitRequest { + string id = 1; +} + +message InitResponse { + string role = 1; + optional float capacity = 2; + string ip = 3; + int32 port = 4; + optional string parent_id = 5; + optional string pip = 6; + optional int32 pport = 7; +} \ No newline at end of file diff --git a/proto/global_grpc.pb.go b/proto/global_grpc.pb.go new file mode 100644 index 0000000..69e2ae4 --- /dev/null +++ b/proto/global_grpc.pb.go @@ -0,0 +1,121 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.5.1 +// - protoc v3.21.12 +// source: proto/global.proto + +package proto + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.64.0 or later. +const _ = grpc.SupportPackageIsVersion9 + +const ( + GlobalService_Init_FullMethodName = "/globalService/Init" +) + +// GlobalServiceClient is the client API for GlobalService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type GlobalServiceClient interface { + Init(ctx context.Context, in *InitRequest, opts ...grpc.CallOption) (*InitResponse, error) +} + +type globalServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewGlobalServiceClient(cc grpc.ClientConnInterface) GlobalServiceClient { + return &globalServiceClient{cc} +} + +func (c *globalServiceClient) Init(ctx context.Context, in *InitRequest, opts ...grpc.CallOption) (*InitResponse, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(InitResponse) + err := c.cc.Invoke(ctx, GlobalService_Init_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +// GlobalServiceServer is the server API for GlobalService service. +// All implementations must embed UnimplementedGlobalServiceServer +// for forward compatibility. +type GlobalServiceServer interface { + Init(context.Context, *InitRequest) (*InitResponse, error) + mustEmbedUnimplementedGlobalServiceServer() +} + +// UnimplementedGlobalServiceServer must be embedded to have +// forward compatible implementations. +// +// NOTE: this should be embedded by value instead of pointer to avoid a nil +// pointer dereference when methods are called. +type UnimplementedGlobalServiceServer struct{} + +func (UnimplementedGlobalServiceServer) Init(context.Context, *InitRequest) (*InitResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Init not implemented") +} +func (UnimplementedGlobalServiceServer) mustEmbedUnimplementedGlobalServiceServer() {} +func (UnimplementedGlobalServiceServer) testEmbeddedByValue() {} + +// UnsafeGlobalServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to GlobalServiceServer will +// result in compilation errors. +type UnsafeGlobalServiceServer interface { + mustEmbedUnimplementedGlobalServiceServer() +} + +func RegisterGlobalServiceServer(s grpc.ServiceRegistrar, srv GlobalServiceServer) { + // If the following call pancis, it indicates UnimplementedGlobalServiceServer was + // embedded by pointer and is nil. This will cause panics if an + // unimplemented method is ever invoked, so we test this at initialization + // time to prevent it from happening at runtime later due to I/O. + if t, ok := srv.(interface{ testEmbeddedByValue() }); ok { + t.testEmbeddedByValue() + } + s.RegisterService(&GlobalService_ServiceDesc, srv) +} + +func _GlobalService_Init_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(InitRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(GlobalServiceServer).Init(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: GlobalService_Init_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(GlobalServiceServer).Init(ctx, req.(*InitRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// GlobalService_ServiceDesc is the grpc.ServiceDesc for GlobalService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var GlobalService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "globalService", + HandlerType: (*GlobalServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Init", + Handler: _GlobalService_Init_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "proto/global.proto", +}