github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/internal/testEnv/pb/service/service.rony.go (about) 1 // Code generated by Rony's protoc plugin; DO NOT EDIT. 2 // ProtoC ver. v3.17.3 3 // Rony ver. v0.16.24 4 // Source: service.proto 5 6 package service 7 8 import ( 9 bytes "bytes" 10 context "context" 11 fmt "fmt" 12 rony "github.com/ronaksoft/rony" 13 config "github.com/ronaksoft/rony/config" 14 edge "github.com/ronaksoft/rony/edge" 15 edgec "github.com/ronaksoft/rony/edgec" 16 errors "github.com/ronaksoft/rony/errors" 17 pools "github.com/ronaksoft/rony/pools" 18 registry "github.com/ronaksoft/rony/registry" 19 tools "github.com/ronaksoft/rony/tools" 20 cobra "github.com/spf13/cobra" 21 protojson "google.golang.org/protobuf/encoding/protojson" 22 proto "google.golang.org/protobuf/proto" 23 http "net/http" 24 sync "sync" 25 ) 26 27 var _ = pools.Imported 28 29 const C_GetRequest uint64 = 8186060648624618456 30 31 type poolGetRequest struct { 32 pool sync.Pool 33 } 34 35 func (p *poolGetRequest) Get() *GetRequest { 36 x, ok := p.pool.Get().(*GetRequest) 37 if !ok { 38 x = &GetRequest{} 39 } 40 41 return x 42 } 43 44 func (p *poolGetRequest) Put(x *GetRequest) { 45 if x == nil { 46 return 47 } 48 49 x.Key = x.Key[:0] 50 51 p.pool.Put(x) 52 } 53 54 var PoolGetRequest = poolGetRequest{} 55 56 func (x *GetRequest) DeepCopy(z *GetRequest) { 57 z.Key = append(z.Key[:0], x.Key...) 58 } 59 60 func (x *GetRequest) Clone() *GetRequest { 61 z := &GetRequest{} 62 x.DeepCopy(z) 63 return z 64 } 65 66 func (x *GetRequest) Unmarshal(b []byte) error { 67 return proto.UnmarshalOptions{Merge: true}.Unmarshal(b, x) 68 } 69 70 func (x *GetRequest) Marshal() ([]byte, error) { 71 return proto.Marshal(x) 72 } 73 74 func (x *GetRequest) UnmarshalJSON(b []byte) error { 75 return protojson.Unmarshal(b, x) 76 } 77 78 func (x *GetRequest) MarshalJSON() ([]byte, error) { 79 return protojson.Marshal(x) 80 } 81 82 func factoryGetRequest() registry.Message { 83 return &GetRequest{} 84 } 85 86 func (x *GetRequest) PushToContext(ctx *edge.RequestCtx) { 87 ctx.PushMessage(C_GetRequest, x) 88 } 89 90 const C_GetResponse uint64 = 10382375233116730107 91 92 type poolGetResponse struct { 93 pool sync.Pool 94 } 95 96 func (p *poolGetResponse) Get() *GetResponse { 97 x, ok := p.pool.Get().(*GetResponse) 98 if !ok { 99 x = &GetResponse{} 100 } 101 102 return x 103 } 104 105 func (p *poolGetResponse) Put(x *GetResponse) { 106 if x == nil { 107 return 108 } 109 110 x.Key = x.Key[:0] 111 x.Value = x.Value[:0] 112 113 p.pool.Put(x) 114 } 115 116 var PoolGetResponse = poolGetResponse{} 117 118 func (x *GetResponse) DeepCopy(z *GetResponse) { 119 z.Key = append(z.Key[:0], x.Key...) 120 z.Value = append(z.Value[:0], x.Value...) 121 } 122 123 func (x *GetResponse) Clone() *GetResponse { 124 z := &GetResponse{} 125 x.DeepCopy(z) 126 return z 127 } 128 129 func (x *GetResponse) Unmarshal(b []byte) error { 130 return proto.UnmarshalOptions{Merge: true}.Unmarshal(b, x) 131 } 132 133 func (x *GetResponse) Marshal() ([]byte, error) { 134 return proto.Marshal(x) 135 } 136 137 func (x *GetResponse) UnmarshalJSON(b []byte) error { 138 return protojson.Unmarshal(b, x) 139 } 140 141 func (x *GetResponse) MarshalJSON() ([]byte, error) { 142 return protojson.Marshal(x) 143 } 144 145 func factoryGetResponse() registry.Message { 146 return &GetResponse{} 147 } 148 149 func (x *GetResponse) PushToContext(ctx *edge.RequestCtx) { 150 ctx.PushMessage(C_GetResponse, x) 151 } 152 153 const C_SetRequest uint64 = 8181913290764647384 154 155 type poolSetRequest struct { 156 pool sync.Pool 157 } 158 159 func (p *poolSetRequest) Get() *SetRequest { 160 x, ok := p.pool.Get().(*SetRequest) 161 if !ok { 162 x = &SetRequest{} 163 } 164 165 return x 166 } 167 168 func (p *poolSetRequest) Put(x *SetRequest) { 169 if x == nil { 170 return 171 } 172 173 x.Key = x.Key[:0] 174 x.Value = x.Value[:0] 175 176 p.pool.Put(x) 177 } 178 179 var PoolSetRequest = poolSetRequest{} 180 181 func (x *SetRequest) DeepCopy(z *SetRequest) { 182 z.Key = append(z.Key[:0], x.Key...) 183 z.Value = append(z.Value[:0], x.Value...) 184 } 185 186 func (x *SetRequest) Clone() *SetRequest { 187 z := &SetRequest{} 188 x.DeepCopy(z) 189 return z 190 } 191 192 func (x *SetRequest) Unmarshal(b []byte) error { 193 return proto.UnmarshalOptions{Merge: true}.Unmarshal(b, x) 194 } 195 196 func (x *SetRequest) Marshal() ([]byte, error) { 197 return proto.Marshal(x) 198 } 199 200 func (x *SetRequest) UnmarshalJSON(b []byte) error { 201 return protojson.Unmarshal(b, x) 202 } 203 204 func (x *SetRequest) MarshalJSON() ([]byte, error) { 205 return protojson.Marshal(x) 206 } 207 208 func factorySetRequest() registry.Message { 209 return &SetRequest{} 210 } 211 212 func (x *SetRequest) PushToContext(ctx *edge.RequestCtx) { 213 ctx.PushMessage(C_SetRequest, x) 214 } 215 216 const C_SetResponse uint64 = 10382356249361281787 217 218 type poolSetResponse struct { 219 pool sync.Pool 220 } 221 222 func (p *poolSetResponse) Get() *SetResponse { 223 x, ok := p.pool.Get().(*SetResponse) 224 if !ok { 225 x = &SetResponse{} 226 } 227 228 return x 229 } 230 231 func (p *poolSetResponse) Put(x *SetResponse) { 232 if x == nil { 233 return 234 } 235 236 x.OK = false 237 238 p.pool.Put(x) 239 } 240 241 var PoolSetResponse = poolSetResponse{} 242 243 func (x *SetResponse) DeepCopy(z *SetResponse) { 244 z.OK = x.OK 245 } 246 247 func (x *SetResponse) Clone() *SetResponse { 248 z := &SetResponse{} 249 x.DeepCopy(z) 250 return z 251 } 252 253 func (x *SetResponse) Unmarshal(b []byte) error { 254 return proto.UnmarshalOptions{Merge: true}.Unmarshal(b, x) 255 } 256 257 func (x *SetResponse) Marshal() ([]byte, error) { 258 return proto.Marshal(x) 259 } 260 261 func (x *SetResponse) UnmarshalJSON(b []byte) error { 262 return protojson.Unmarshal(b, x) 263 } 264 265 func (x *SetResponse) MarshalJSON() ([]byte, error) { 266 return protojson.Marshal(x) 267 } 268 269 func factorySetResponse() registry.Message { 270 return &SetResponse{} 271 } 272 273 func (x *SetResponse) PushToContext(ctx *edge.RequestCtx) { 274 ctx.PushMessage(C_SetResponse, x) 275 } 276 277 const C_EchoRequest uint64 = 634453994073422796 278 279 type poolEchoRequest struct { 280 pool sync.Pool 281 } 282 283 func (p *poolEchoRequest) Get() *EchoRequest { 284 x, ok := p.pool.Get().(*EchoRequest) 285 if !ok { 286 x = &EchoRequest{} 287 } 288 289 return x 290 } 291 292 func (p *poolEchoRequest) Put(x *EchoRequest) { 293 if x == nil { 294 return 295 } 296 297 x.Int = 0 298 x.Timestamp = 0 299 x.ReplicaSet = 0 300 x.SomeData = x.SomeData[:0] 301 302 p.pool.Put(x) 303 } 304 305 var PoolEchoRequest = poolEchoRequest{} 306 307 func (x *EchoRequest) DeepCopy(z *EchoRequest) { 308 z.Int = x.Int 309 z.Timestamp = x.Timestamp 310 z.ReplicaSet = x.ReplicaSet 311 z.SomeData = append(z.SomeData[:0], x.SomeData...) 312 } 313 314 func (x *EchoRequest) Clone() *EchoRequest { 315 z := &EchoRequest{} 316 x.DeepCopy(z) 317 return z 318 } 319 320 func (x *EchoRequest) Unmarshal(b []byte) error { 321 return proto.UnmarshalOptions{Merge: true}.Unmarshal(b, x) 322 } 323 324 func (x *EchoRequest) Marshal() ([]byte, error) { 325 return proto.Marshal(x) 326 } 327 328 func (x *EchoRequest) UnmarshalJSON(b []byte) error { 329 return protojson.Unmarshal(b, x) 330 } 331 332 func (x *EchoRequest) MarshalJSON() ([]byte, error) { 333 return protojson.Marshal(x) 334 } 335 336 func factoryEchoRequest() registry.Message { 337 return &EchoRequest{} 338 } 339 340 func (x *EchoRequest) PushToContext(ctx *edge.RequestCtx) { 341 ctx.PushMessage(C_EchoRequest, x) 342 } 343 344 const C_EchoResponse uint64 = 10208763112635265787 345 346 type poolEchoResponse struct { 347 pool sync.Pool 348 } 349 350 func (p *poolEchoResponse) Get() *EchoResponse { 351 x, ok := p.pool.Get().(*EchoResponse) 352 if !ok { 353 x = &EchoResponse{} 354 } 355 356 return x 357 } 358 359 func (p *poolEchoResponse) Put(x *EchoResponse) { 360 if x == nil { 361 return 362 } 363 364 x.Int = 0 365 x.Responder = "" 366 x.Timestamp = 0 367 x.Delay = 0 368 x.ServerID = "" 369 x.SomeData = x.SomeData[:0] 370 371 p.pool.Put(x) 372 } 373 374 var PoolEchoResponse = poolEchoResponse{} 375 376 func (x *EchoResponse) DeepCopy(z *EchoResponse) { 377 z.Int = x.Int 378 z.Responder = x.Responder 379 z.Timestamp = x.Timestamp 380 z.Delay = x.Delay 381 z.ServerID = x.ServerID 382 z.SomeData = append(z.SomeData[:0], x.SomeData...) 383 } 384 385 func (x *EchoResponse) Clone() *EchoResponse { 386 z := &EchoResponse{} 387 x.DeepCopy(z) 388 return z 389 } 390 391 func (x *EchoResponse) Unmarshal(b []byte) error { 392 return proto.UnmarshalOptions{Merge: true}.Unmarshal(b, x) 393 } 394 395 func (x *EchoResponse) Marshal() ([]byte, error) { 396 return proto.Marshal(x) 397 } 398 399 func (x *EchoResponse) UnmarshalJSON(b []byte) error { 400 return protojson.Unmarshal(b, x) 401 } 402 403 func (x *EchoResponse) MarshalJSON() ([]byte, error) { 404 return protojson.Marshal(x) 405 } 406 407 func factoryEchoResponse() registry.Message { 408 return &EchoResponse{} 409 } 410 411 func (x *EchoResponse) PushToContext(ctx *edge.RequestCtx) { 412 ctx.PushMessage(C_EchoResponse, x) 413 } 414 415 const C_Message1 uint64 = 1806736971761742569 416 417 type poolMessage1 struct { 418 pool sync.Pool 419 } 420 421 func (p *poolMessage1) Get() *Message1 { 422 x, ok := p.pool.Get().(*Message1) 423 if !ok { 424 x = &Message1{} 425 } 426 427 x.M2 = PoolMessage2.Get() 428 429 return x 430 } 431 432 func (p *poolMessage1) Put(x *Message1) { 433 if x == nil { 434 return 435 } 436 437 x.Param1 = 0 438 x.Param2 = "" 439 PoolMessage2.Put(x.M2) 440 for _, z := range x.M2S { 441 PoolMessage2.Put(z) 442 } 443 x.M2S = x.M2S[:0] 444 445 p.pool.Put(x) 446 } 447 448 var PoolMessage1 = poolMessage1{} 449 450 func (x *Message1) DeepCopy(z *Message1) { 451 z.Param1 = x.Param1 452 z.Param2 = x.Param2 453 if x.M2 != nil { 454 if z.M2 == nil { 455 z.M2 = PoolMessage2.Get() 456 } 457 x.M2.DeepCopy(z.M2) 458 } else { 459 PoolMessage2.Put(z.M2) 460 z.M2 = nil 461 } 462 for idx := range x.M2S { 463 if x.M2S[idx] == nil { 464 continue 465 } 466 xx := PoolMessage2.Get() 467 x.M2S[idx].DeepCopy(xx) 468 z.M2S = append(z.M2S, xx) 469 } 470 } 471 472 func (x *Message1) Clone() *Message1 { 473 z := &Message1{} 474 x.DeepCopy(z) 475 return z 476 } 477 478 func (x *Message1) Unmarshal(b []byte) error { 479 return proto.UnmarshalOptions{Merge: true}.Unmarshal(b, x) 480 } 481 482 func (x *Message1) Marshal() ([]byte, error) { 483 return proto.Marshal(x) 484 } 485 486 func (x *Message1) UnmarshalJSON(b []byte) error { 487 return protojson.Unmarshal(b, x) 488 } 489 490 func (x *Message1) MarshalJSON() ([]byte, error) { 491 return protojson.Marshal(x) 492 } 493 494 func factoryMessage1() registry.Message { 495 return &Message1{} 496 } 497 498 func (x *Message1) PushToContext(ctx *edge.RequestCtx) { 499 ctx.PushMessage(C_Message1, x) 500 } 501 502 const C_Message2 uint64 = 2000391755738673897 503 504 type poolMessage2 struct { 505 pool sync.Pool 506 } 507 508 func (p *poolMessage2) Get() *Message2 { 509 x, ok := p.pool.Get().(*Message2) 510 if !ok { 511 x = &Message2{} 512 } 513 514 x.M1 = PoolMessage1.Get() 515 516 return x 517 } 518 519 func (p *poolMessage2) Put(x *Message2) { 520 if x == nil { 521 return 522 } 523 524 x.Param1 = 0 525 x.P2 = x.P2[:0] 526 x.P3 = x.P3[:0] 527 PoolMessage1.Put(x.M1) 528 529 p.pool.Put(x) 530 } 531 532 var PoolMessage2 = poolMessage2{} 533 534 func (x *Message2) DeepCopy(z *Message2) { 535 z.Param1 = x.Param1 536 z.P2 = append(z.P2[:0], x.P2...) 537 z.P3 = append(z.P3[:0], x.P3...) 538 if x.M1 != nil { 539 if z.M1 == nil { 540 z.M1 = PoolMessage1.Get() 541 } 542 x.M1.DeepCopy(z.M1) 543 } else { 544 PoolMessage1.Put(z.M1) 545 z.M1 = nil 546 } 547 } 548 549 func (x *Message2) Clone() *Message2 { 550 z := &Message2{} 551 x.DeepCopy(z) 552 return z 553 } 554 555 func (x *Message2) Unmarshal(b []byte) error { 556 return proto.UnmarshalOptions{Merge: true}.Unmarshal(b, x) 557 } 558 559 func (x *Message2) Marshal() ([]byte, error) { 560 return proto.Marshal(x) 561 } 562 563 func (x *Message2) UnmarshalJSON(b []byte) error { 564 return protojson.Unmarshal(b, x) 565 } 566 567 func (x *Message2) MarshalJSON() ([]byte, error) { 568 return protojson.Marshal(x) 569 } 570 571 func factoryMessage2() registry.Message { 572 return &Message2{} 573 } 574 575 func (x *Message2) PushToContext(ctx *edge.RequestCtx) { 576 ctx.PushMessage(C_Message2, x) 577 } 578 579 const C_SampleEcho uint64 = 5610266072904040111 580 const C_SampleSet uint64 = 859899950148792148 581 const C_SampleGet uint64 = 859879334305771348 582 const C_SampleEchoTunnel uint64 = 16129836997487988187 583 const C_SampleEchoInternal uint64 = 8481593834425277560 584 const C_SampleEchoDelay uint64 = 5258191516040289195 585 586 // register constructors of the messages to the registry package 587 func init() { 588 registry.Register(8186060648624618456, "GetRequest", factoryGetRequest) 589 registry.Register(10382375233116730107, "GetResponse", factoryGetResponse) 590 registry.Register(8181913290764647384, "SetRequest", factorySetRequest) 591 registry.Register(10382356249361281787, "SetResponse", factorySetResponse) 592 registry.Register(634453994073422796, "EchoRequest", factoryEchoRequest) 593 registry.Register(10208763112635265787, "EchoResponse", factoryEchoResponse) 594 registry.Register(1806736971761742569, "Message1", factoryMessage1) 595 registry.Register(2000391755738673897, "Message2", factoryMessage2) 596 registry.Register(5610266072904040111, "SampleEcho", factoryEchoRequest) 597 registry.Register(859899950148792148, "SampleSet", factorySetRequest) 598 registry.Register(859879334305771348, "SampleGet", factoryGetRequest) 599 registry.Register(16129836997487988187, "SampleEchoTunnel", factoryEchoRequest) 600 registry.Register(8481593834425277560, "SampleEchoInternal", factoryEchoRequest) 601 registry.Register(5258191516040289195, "SampleEchoDelay", factoryEchoRequest) 602 603 } 604 605 var _ = tools.TimeUnix() 606 607 type ISample interface { 608 Echo(ctx *edge.RequestCtx, req *EchoRequest, res *EchoResponse) *rony.Error 609 Set(ctx *edge.RequestCtx, req *SetRequest, res *SetResponse) *rony.Error 610 Get(ctx *edge.RequestCtx, req *GetRequest, res *GetResponse) *rony.Error 611 EchoTunnel(ctx *edge.RequestCtx, req *EchoRequest, res *EchoResponse) *rony.Error 612 EchoInternal(ctx *edge.RequestCtx, req *EchoRequest, res *EchoResponse) *rony.Error 613 EchoDelay(ctx *edge.RequestCtx, req *EchoRequest, res *EchoResponse) *rony.Error 614 } 615 616 func RegisterSample(h ISample, e *edge.Server, preHandlers ...edge.Handler) { 617 w := sampleWrapper{ 618 h: h, 619 } 620 w.Register(e, func(c uint64) []edge.Handler { 621 return preHandlers 622 }) 623 } 624 625 func RegisterSampleWithFunc(h ISample, e *edge.Server, handlerFunc func(c uint64) []edge.Handler) { 626 w := sampleWrapper{ 627 h: h, 628 } 629 w.Register(e, handlerFunc) 630 } 631 632 type sampleWrapper struct { 633 h ISample 634 } 635 636 func (sw *sampleWrapper) echoWrapper(ctx *edge.RequestCtx, in *rony.MessageEnvelope) { 637 req := &EchoRequest{} 638 res := &EchoResponse{} 639 640 var err error 641 if in.JsonEncoded { 642 err = protojson.Unmarshal(in.Message, req) 643 } else { 644 err = proto.UnmarshalOptions{Merge: true}.Unmarshal(in.Message, req) 645 } 646 if err != nil { 647 ctx.PushError(errors.ErrInvalidRequest) 648 return 649 } 650 651 rErr := sw.h.Echo(ctx, req, res) 652 if rErr != nil { 653 ctx.PushError(rErr) 654 return 655 } 656 if !ctx.Stopped() { 657 ctx.PushMessage(C_EchoResponse, res) 658 } 659 } 660 func (sw *sampleWrapper) setWrapper(ctx *edge.RequestCtx, in *rony.MessageEnvelope) { 661 req := &SetRequest{} 662 res := &SetResponse{} 663 664 var err error 665 if in.JsonEncoded { 666 err = protojson.Unmarshal(in.Message, req) 667 } else { 668 err = proto.UnmarshalOptions{Merge: true}.Unmarshal(in.Message, req) 669 } 670 if err != nil { 671 ctx.PushError(errors.ErrInvalidRequest) 672 return 673 } 674 675 rErr := sw.h.Set(ctx, req, res) 676 if rErr != nil { 677 ctx.PushError(rErr) 678 return 679 } 680 if !ctx.Stopped() { 681 ctx.PushMessage(C_SetResponse, res) 682 } 683 } 684 func (sw *sampleWrapper) getWrapper(ctx *edge.RequestCtx, in *rony.MessageEnvelope) { 685 req := &GetRequest{} 686 res := &GetResponse{} 687 688 var err error 689 if in.JsonEncoded { 690 err = protojson.Unmarshal(in.Message, req) 691 } else { 692 err = proto.UnmarshalOptions{Merge: true}.Unmarshal(in.Message, req) 693 } 694 if err != nil { 695 ctx.PushError(errors.ErrInvalidRequest) 696 return 697 } 698 699 rErr := sw.h.Get(ctx, req, res) 700 if rErr != nil { 701 ctx.PushError(rErr) 702 return 703 } 704 if !ctx.Stopped() { 705 ctx.PushMessage(C_GetResponse, res) 706 } 707 } 708 func (sw *sampleWrapper) echoTunnelWrapper(ctx *edge.RequestCtx, in *rony.MessageEnvelope) { 709 req := &EchoRequest{} 710 res := &EchoResponse{} 711 712 var err error 713 if in.JsonEncoded { 714 err = protojson.Unmarshal(in.Message, req) 715 } else { 716 err = proto.UnmarshalOptions{Merge: true}.Unmarshal(in.Message, req) 717 } 718 if err != nil { 719 ctx.PushError(errors.ErrInvalidRequest) 720 return 721 } 722 723 rErr := sw.h.EchoTunnel(ctx, req, res) 724 if rErr != nil { 725 ctx.PushError(rErr) 726 return 727 } 728 if !ctx.Stopped() { 729 ctx.PushMessage(C_EchoResponse, res) 730 } 731 } 732 func (sw *sampleWrapper) echoInternalWrapper(ctx *edge.RequestCtx, in *rony.MessageEnvelope) { 733 req := &EchoRequest{} 734 res := &EchoResponse{} 735 736 var err error 737 if in.JsonEncoded { 738 err = protojson.Unmarshal(in.Message, req) 739 } else { 740 err = proto.UnmarshalOptions{Merge: true}.Unmarshal(in.Message, req) 741 } 742 if err != nil { 743 ctx.PushError(errors.ErrInvalidRequest) 744 return 745 } 746 747 rErr := sw.h.EchoInternal(ctx, req, res) 748 if rErr != nil { 749 ctx.PushError(rErr) 750 return 751 } 752 if !ctx.Stopped() { 753 ctx.PushMessage(C_EchoResponse, res) 754 } 755 } 756 func (sw *sampleWrapper) echoDelayWrapper(ctx *edge.RequestCtx, in *rony.MessageEnvelope) { 757 req := &EchoRequest{} 758 res := &EchoResponse{} 759 760 var err error 761 if in.JsonEncoded { 762 err = protojson.Unmarshal(in.Message, req) 763 } else { 764 err = proto.UnmarshalOptions{Merge: true}.Unmarshal(in.Message, req) 765 } 766 if err != nil { 767 ctx.PushError(errors.ErrInvalidRequest) 768 return 769 } 770 771 rErr := sw.h.EchoDelay(ctx, req, res) 772 if rErr != nil { 773 ctx.PushError(rErr) 774 return 775 } 776 if !ctx.Stopped() { 777 ctx.PushMessage(C_EchoResponse, res) 778 } 779 } 780 781 func (sw *sampleWrapper) Register(e *edge.Server, handlerFunc func(c uint64) []edge.Handler) { 782 if handlerFunc == nil { 783 handlerFunc = func(c uint64) []edge.Handler { 784 return nil 785 } 786 } 787 e.SetHandler( 788 edge.NewHandlerOptions(). 789 SetConstructor(C_SampleEcho). 790 SetServiceName("Sample"). 791 SetMethodName("Echo"). 792 SetHandler(handlerFunc(C_SampleEcho)...). 793 Append(sw.echoWrapper), 794 ) 795 e.SetRestProxy( 796 "get", "/echo/:replica_set", 797 edge.NewRestProxy(sw.echoRestClient, sw.echoRestServer), 798 ) 799 e.SetHandler( 800 edge.NewHandlerOptions(). 801 SetConstructor(C_SampleSet). 802 SetServiceName("Sample"). 803 SetMethodName("Set"). 804 SetHandler(handlerFunc(C_SampleSet)...). 805 Append(sw.setWrapper), 806 ) 807 e.SetRestProxy( 808 "post", "/set", 809 edge.NewRestProxy(sw.setRestClient, sw.setRestServer), 810 ) 811 e.SetHandler( 812 edge.NewHandlerOptions(). 813 SetConstructor(C_SampleGet). 814 SetServiceName("Sample"). 815 SetMethodName("Get"). 816 SetHandler(handlerFunc(C_SampleGet)...). 817 Append(sw.getWrapper), 818 ) 819 e.SetRestProxy( 820 "get", "/req/:Key/something", 821 edge.NewRestProxy(sw.getRestClient, sw.getRestServer), 822 ) 823 e.SetHandler( 824 edge.NewHandlerOptions(). 825 SetConstructor(C_SampleEchoTunnel). 826 SetServiceName("Sample"). 827 SetMethodName("EchoTunnel"). 828 SetHandler(handlerFunc(C_SampleEchoTunnel)...). 829 Append(sw.echoTunnelWrapper), 830 ) 831 e.SetRestProxy( 832 "get", "/echo_tunnel/:X/:YY", 833 edge.NewRestProxy(sw.echoTunnelRestClient, sw.echoTunnelRestServer), 834 ) 835 e.SetHandler( 836 edge.NewHandlerOptions(). 837 SetConstructor(C_SampleEchoInternal). 838 SetServiceName("Sample"). 839 SetMethodName("EchoInternal"). 840 SetHandler(handlerFunc(C_SampleEchoInternal)...). 841 Append(sw.echoInternalWrapper).TunnelOnly(), 842 ) 843 e.SetHandler( 844 edge.NewHandlerOptions(). 845 SetConstructor(C_SampleEchoDelay). 846 SetServiceName("Sample"). 847 SetMethodName("EchoDelay"). 848 SetHandler(handlerFunc(C_SampleEchoDelay)...). 849 Append(sw.echoDelayWrapper), 850 ) 851 } 852 853 func TunnelRequestSampleEcho( 854 ctx *edge.RequestCtx, replicaSet uint64, 855 req *EchoRequest, res *EchoResponse, 856 kvs ...*rony.KeyValue, 857 ) error { 858 out := rony.PoolMessageEnvelope.Get() 859 defer rony.PoolMessageEnvelope.Put(out) 860 in := rony.PoolMessageEnvelope.Get() 861 defer rony.PoolMessageEnvelope.Put(in) 862 out.Fill(ctx.ReqID(), C_SampleEcho, req, kvs...) 863 err := ctx.TunnelRequest(replicaSet, out, in) 864 if err != nil { 865 return err 866 } 867 868 switch in.GetConstructor() { 869 case C_EchoResponse: 870 _ = res.Unmarshal(in.GetMessage()) 871 return nil 872 case rony.C_Error: 873 x := &rony.Error{} 874 _ = x.Unmarshal(in.GetMessage()) 875 return x 876 default: 877 return errors.ErrUnexpectedTunnelResponse 878 } 879 } 880 func TunnelRequestSampleSet( 881 ctx *edge.RequestCtx, replicaSet uint64, 882 req *SetRequest, res *SetResponse, 883 kvs ...*rony.KeyValue, 884 ) error { 885 out := rony.PoolMessageEnvelope.Get() 886 defer rony.PoolMessageEnvelope.Put(out) 887 in := rony.PoolMessageEnvelope.Get() 888 defer rony.PoolMessageEnvelope.Put(in) 889 out.Fill(ctx.ReqID(), C_SampleSet, req, kvs...) 890 err := ctx.TunnelRequest(replicaSet, out, in) 891 if err != nil { 892 return err 893 } 894 895 switch in.GetConstructor() { 896 case C_SetResponse: 897 _ = res.Unmarshal(in.GetMessage()) 898 return nil 899 case rony.C_Error: 900 x := &rony.Error{} 901 _ = x.Unmarshal(in.GetMessage()) 902 return x 903 default: 904 return errors.ErrUnexpectedTunnelResponse 905 } 906 } 907 func TunnelRequestSampleGet( 908 ctx *edge.RequestCtx, replicaSet uint64, 909 req *GetRequest, res *GetResponse, 910 kvs ...*rony.KeyValue, 911 ) error { 912 out := rony.PoolMessageEnvelope.Get() 913 defer rony.PoolMessageEnvelope.Put(out) 914 in := rony.PoolMessageEnvelope.Get() 915 defer rony.PoolMessageEnvelope.Put(in) 916 out.Fill(ctx.ReqID(), C_SampleGet, req, kvs...) 917 err := ctx.TunnelRequest(replicaSet, out, in) 918 if err != nil { 919 return err 920 } 921 922 switch in.GetConstructor() { 923 case C_GetResponse: 924 _ = res.Unmarshal(in.GetMessage()) 925 return nil 926 case rony.C_Error: 927 x := &rony.Error{} 928 _ = x.Unmarshal(in.GetMessage()) 929 return x 930 default: 931 return errors.ErrUnexpectedTunnelResponse 932 } 933 } 934 func TunnelRequestSampleEchoTunnel( 935 ctx *edge.RequestCtx, replicaSet uint64, 936 req *EchoRequest, res *EchoResponse, 937 kvs ...*rony.KeyValue, 938 ) error { 939 out := rony.PoolMessageEnvelope.Get() 940 defer rony.PoolMessageEnvelope.Put(out) 941 in := rony.PoolMessageEnvelope.Get() 942 defer rony.PoolMessageEnvelope.Put(in) 943 out.Fill(ctx.ReqID(), C_SampleEchoTunnel, req, kvs...) 944 err := ctx.TunnelRequest(replicaSet, out, in) 945 if err != nil { 946 return err 947 } 948 949 switch in.GetConstructor() { 950 case C_EchoResponse: 951 _ = res.Unmarshal(in.GetMessage()) 952 return nil 953 case rony.C_Error: 954 x := &rony.Error{} 955 _ = x.Unmarshal(in.GetMessage()) 956 return x 957 default: 958 return errors.ErrUnexpectedTunnelResponse 959 } 960 } 961 func TunnelRequestSampleEchoInternal( 962 ctx *edge.RequestCtx, replicaSet uint64, 963 req *EchoRequest, res *EchoResponse, 964 kvs ...*rony.KeyValue, 965 ) error { 966 out := rony.PoolMessageEnvelope.Get() 967 defer rony.PoolMessageEnvelope.Put(out) 968 in := rony.PoolMessageEnvelope.Get() 969 defer rony.PoolMessageEnvelope.Put(in) 970 out.Fill(ctx.ReqID(), C_SampleEchoInternal, req, kvs...) 971 err := ctx.TunnelRequest(replicaSet, out, in) 972 if err != nil { 973 return err 974 } 975 976 switch in.GetConstructor() { 977 case C_EchoResponse: 978 _ = res.Unmarshal(in.GetMessage()) 979 return nil 980 case rony.C_Error: 981 x := &rony.Error{} 982 _ = x.Unmarshal(in.GetMessage()) 983 return x 984 default: 985 return errors.ErrUnexpectedTunnelResponse 986 } 987 } 988 func TunnelRequestSampleEchoDelay( 989 ctx *edge.RequestCtx, replicaSet uint64, 990 req *EchoRequest, res *EchoResponse, 991 kvs ...*rony.KeyValue, 992 ) error { 993 out := rony.PoolMessageEnvelope.Get() 994 defer rony.PoolMessageEnvelope.Put(out) 995 in := rony.PoolMessageEnvelope.Get() 996 defer rony.PoolMessageEnvelope.Put(in) 997 out.Fill(ctx.ReqID(), C_SampleEchoDelay, req, kvs...) 998 err := ctx.TunnelRequest(replicaSet, out, in) 999 if err != nil { 1000 return err 1001 } 1002 1003 switch in.GetConstructor() { 1004 case C_EchoResponse: 1005 _ = res.Unmarshal(in.GetMessage()) 1006 return nil 1007 case rony.C_Error: 1008 x := &rony.Error{} 1009 _ = x.Unmarshal(in.GetMessage()) 1010 return x 1011 default: 1012 return errors.ErrUnexpectedTunnelResponse 1013 } 1014 } 1015 1016 func (sw *sampleWrapper) echoRestClient(conn rony.RestConn, ctx *edge.DispatchCtx) error { 1017 req := &EchoRequest{} 1018 if len(conn.Body()) > 0 { 1019 err := req.UnmarshalJSON(conn.Body()) 1020 if err != nil { 1021 return err 1022 } 1023 } 1024 req.ReplicaSet = tools.StrToUInt64(tools.GetString(conn.Get("replica_set"), "0")) 1025 1026 ctx.Fill(conn.ConnID(), C_SampleEcho, req) 1027 return nil 1028 } 1029 func (sw *sampleWrapper) echoRestServer(conn rony.RestConn, ctx *edge.DispatchCtx) (err error) { 1030 conn.WriteHeader("Content-Type", "application/json") 1031 if !ctx.BufferPop(func(envelope *rony.MessageEnvelope) { 1032 switch envelope.Constructor { 1033 case C_EchoResponse: 1034 x := &EchoResponse{} 1035 _ = x.Unmarshal(envelope.Message) 1036 var b []byte 1037 b, err = x.MarshalJSON() 1038 if err != nil { 1039 return 1040 } 1041 err = conn.WriteBinary(ctx.StreamID(), b) 1042 return 1043 case rony.C_Error: 1044 x := &rony.Error{} 1045 _ = x.Unmarshal(envelope.Message) 1046 err = x 1047 return 1048 case rony.C_Redirect: 1049 x := &rony.Redirect{} 1050 _ = x.Unmarshal(envelope.Message) 1051 if len(x.Edges) == 0 || len(x.Edges[0].HostPorts) == 0 { 1052 break 1053 } 1054 switch x.Reason { 1055 case rony.RedirectReason_ReplicaSetSession: 1056 conn.Redirect(http.StatusPermanentRedirect, x.Edges[0].HostPorts[0]) 1057 case rony.RedirectReason_ReplicaSetRequest: 1058 conn.Redirect(http.StatusTemporaryRedirect, x.Edges[0].HostPorts[0]) 1059 } 1060 return 1061 } 1062 err = errors.ErrUnexpectedResponse 1063 }) { 1064 err = errors.ErrInternalServer 1065 } 1066 1067 return 1068 } 1069 1070 func (sw *sampleWrapper) setRestClient(conn rony.RestConn, ctx *edge.DispatchCtx) error { 1071 req := &SetRequest{} 1072 err := req.Unmarshal(conn.Body()) 1073 if err != nil { 1074 return err 1075 } 1076 1077 ctx.Fill(conn.ConnID(), C_SampleSet, req) 1078 return nil 1079 } 1080 func (sw *sampleWrapper) setRestServer(conn rony.RestConn, ctx *edge.DispatchCtx) (err error) { 1081 if !ctx.BufferPop(func(envelope *rony.MessageEnvelope) { 1082 switch envelope.Constructor { 1083 case C_SetResponse: 1084 x := &SetResponse{} 1085 _ = x.Unmarshal(envelope.Message) 1086 var b []byte 1087 b, err = x.Marshal() 1088 if err != nil { 1089 return 1090 } 1091 err = conn.WriteBinary(ctx.StreamID(), b) 1092 return 1093 case rony.C_Error: 1094 x := &rony.Error{} 1095 _ = x.Unmarshal(envelope.Message) 1096 err = x 1097 return 1098 case rony.C_Redirect: 1099 x := &rony.Redirect{} 1100 _ = x.Unmarshal(envelope.Message) 1101 if len(x.Edges) == 0 || len(x.Edges[0].HostPorts) == 0 { 1102 break 1103 } 1104 switch x.Reason { 1105 case rony.RedirectReason_ReplicaSetSession: 1106 conn.Redirect(http.StatusPermanentRedirect, x.Edges[0].HostPorts[0]) 1107 case rony.RedirectReason_ReplicaSetRequest: 1108 conn.Redirect(http.StatusTemporaryRedirect, x.Edges[0].HostPorts[0]) 1109 } 1110 return 1111 } 1112 err = errors.ErrUnexpectedResponse 1113 }) { 1114 err = errors.ErrInternalServer 1115 } 1116 1117 return 1118 } 1119 1120 func (sw *sampleWrapper) getRestClient(conn rony.RestConn, ctx *edge.DispatchCtx) error { 1121 req := &GetRequest{} 1122 req.Key = tools.S2B(tools.GetString(conn.Get("Key"), "")) 1123 1124 ctx.Fill(conn.ConnID(), C_SampleGet, req) 1125 return nil 1126 } 1127 func (sw *sampleWrapper) getRestServer(conn rony.RestConn, ctx *edge.DispatchCtx) (err error) { 1128 if !ctx.BufferPop(func(envelope *rony.MessageEnvelope) { 1129 switch envelope.Constructor { 1130 case C_GetResponse: 1131 x := &GetResponse{} 1132 _ = x.Unmarshal(envelope.Message) 1133 var b []byte 1134 b, err = x.Marshal() 1135 if err != nil { 1136 return 1137 } 1138 err = conn.WriteBinary(ctx.StreamID(), b) 1139 return 1140 case rony.C_Error: 1141 x := &rony.Error{} 1142 _ = x.Unmarshal(envelope.Message) 1143 err = x 1144 return 1145 case rony.C_Redirect: 1146 x := &rony.Redirect{} 1147 _ = x.Unmarshal(envelope.Message) 1148 if len(x.Edges) == 0 || len(x.Edges[0].HostPorts) == 0 { 1149 break 1150 } 1151 switch x.Reason { 1152 case rony.RedirectReason_ReplicaSetSession: 1153 conn.Redirect(http.StatusPermanentRedirect, x.Edges[0].HostPorts[0]) 1154 case rony.RedirectReason_ReplicaSetRequest: 1155 conn.Redirect(http.StatusTemporaryRedirect, x.Edges[0].HostPorts[0]) 1156 } 1157 return 1158 } 1159 err = errors.ErrUnexpectedResponse 1160 }) { 1161 err = errors.ErrInternalServer 1162 } 1163 1164 return 1165 } 1166 1167 func (sw *sampleWrapper) echoTunnelRestClient(conn rony.RestConn, ctx *edge.DispatchCtx) error { 1168 req := &EchoRequest{} 1169 err := req.Unmarshal(conn.Body()) 1170 if err != nil { 1171 return err 1172 } 1173 req.Int = tools.StrToInt64(tools.GetString(conn.Get("X"), "0")) 1174 req.Timestamp = tools.StrToInt64(tools.GetString(conn.Get("YY"), "0")) 1175 1176 ctx.Fill(conn.ConnID(), C_SampleEchoTunnel, req) 1177 return nil 1178 } 1179 func (sw *sampleWrapper) echoTunnelRestServer(conn rony.RestConn, ctx *edge.DispatchCtx) (err error) { 1180 if !ctx.BufferPop(func(envelope *rony.MessageEnvelope) { 1181 switch envelope.Constructor { 1182 case C_EchoResponse: 1183 x := &EchoResponse{} 1184 _ = x.Unmarshal(envelope.Message) 1185 var b []byte 1186 b, err = x.Marshal() 1187 if err != nil { 1188 return 1189 } 1190 err = conn.WriteBinary(ctx.StreamID(), b) 1191 return 1192 case rony.C_Error: 1193 x := &rony.Error{} 1194 _ = x.Unmarshal(envelope.Message) 1195 err = x 1196 return 1197 case rony.C_Redirect: 1198 x := &rony.Redirect{} 1199 _ = x.Unmarshal(envelope.Message) 1200 if len(x.Edges) == 0 || len(x.Edges[0].HostPorts) == 0 { 1201 break 1202 } 1203 switch x.Reason { 1204 case rony.RedirectReason_ReplicaSetSession: 1205 conn.Redirect(http.StatusPermanentRedirect, x.Edges[0].HostPorts[0]) 1206 case rony.RedirectReason_ReplicaSetRequest: 1207 conn.Redirect(http.StatusTemporaryRedirect, x.Edges[0].HostPorts[0]) 1208 } 1209 return 1210 } 1211 err = errors.ErrUnexpectedResponse 1212 }) { 1213 err = errors.ErrInternalServer 1214 } 1215 1216 return 1217 } 1218 1219 func (sw *sampleWrapper) echoInternalRestClient(conn rony.RestConn, ctx *edge.DispatchCtx) error { 1220 req := &EchoRequest{} 1221 1222 ctx.Fill(conn.ConnID(), C_SampleEchoInternal, req) 1223 return nil 1224 } 1225 func (sw *sampleWrapper) echoInternalRestServer(conn rony.RestConn, ctx *edge.DispatchCtx) (err error) { 1226 if !ctx.BufferPop(func(envelope *rony.MessageEnvelope) { 1227 switch envelope.Constructor { 1228 case C_EchoResponse: 1229 x := &EchoResponse{} 1230 _ = x.Unmarshal(envelope.Message) 1231 var b []byte 1232 b, err = x.Marshal() 1233 if err != nil { 1234 return 1235 } 1236 err = conn.WriteBinary(ctx.StreamID(), b) 1237 return 1238 case rony.C_Error: 1239 x := &rony.Error{} 1240 _ = x.Unmarshal(envelope.Message) 1241 err = x 1242 return 1243 case rony.C_Redirect: 1244 x := &rony.Redirect{} 1245 _ = x.Unmarshal(envelope.Message) 1246 if len(x.Edges) == 0 || len(x.Edges[0].HostPorts) == 0 { 1247 break 1248 } 1249 switch x.Reason { 1250 case rony.RedirectReason_ReplicaSetSession: 1251 conn.Redirect(http.StatusPermanentRedirect, x.Edges[0].HostPorts[0]) 1252 case rony.RedirectReason_ReplicaSetRequest: 1253 conn.Redirect(http.StatusTemporaryRedirect, x.Edges[0].HostPorts[0]) 1254 } 1255 return 1256 } 1257 err = errors.ErrUnexpectedResponse 1258 }) { 1259 err = errors.ErrInternalServer 1260 } 1261 1262 return 1263 } 1264 1265 func (sw *sampleWrapper) echoDelayRestClient(conn rony.RestConn, ctx *edge.DispatchCtx) error { 1266 req := &EchoRequest{} 1267 1268 ctx.Fill(conn.ConnID(), C_SampleEchoDelay, req) 1269 return nil 1270 } 1271 func (sw *sampleWrapper) echoDelayRestServer(conn rony.RestConn, ctx *edge.DispatchCtx) (err error) { 1272 if !ctx.BufferPop(func(envelope *rony.MessageEnvelope) { 1273 switch envelope.Constructor { 1274 case C_EchoResponse: 1275 x := &EchoResponse{} 1276 _ = x.Unmarshal(envelope.Message) 1277 var b []byte 1278 b, err = x.Marshal() 1279 if err != nil { 1280 return 1281 } 1282 err = conn.WriteBinary(ctx.StreamID(), b) 1283 return 1284 case rony.C_Error: 1285 x := &rony.Error{} 1286 _ = x.Unmarshal(envelope.Message) 1287 err = x 1288 return 1289 case rony.C_Redirect: 1290 x := &rony.Redirect{} 1291 _ = x.Unmarshal(envelope.Message) 1292 if len(x.Edges) == 0 || len(x.Edges[0].HostPorts) == 0 { 1293 break 1294 } 1295 switch x.Reason { 1296 case rony.RedirectReason_ReplicaSetSession: 1297 conn.Redirect(http.StatusPermanentRedirect, x.Edges[0].HostPorts[0]) 1298 case rony.RedirectReason_ReplicaSetRequest: 1299 conn.Redirect(http.StatusTemporaryRedirect, x.Edges[0].HostPorts[0]) 1300 } 1301 return 1302 } 1303 err = errors.ErrUnexpectedResponse 1304 }) { 1305 err = errors.ErrInternalServer 1306 } 1307 1308 return 1309 } 1310 1311 type ISampleClient interface { 1312 Echo(ctx context.Context, req *EchoRequest, kvs ...*rony.KeyValue) (*EchoResponse, error) 1313 Set(ctx context.Context, req *SetRequest, kvs ...*rony.KeyValue) (*SetResponse, error) 1314 Get(ctx context.Context, req *GetRequest, kvs ...*rony.KeyValue) (*GetResponse, error) 1315 EchoTunnel(ctx context.Context, req *EchoRequest, kvs ...*rony.KeyValue) (*EchoResponse, error) 1316 EchoDelay(ctx context.Context, req *EchoRequest, kvs ...*rony.KeyValue) (*EchoResponse, error) 1317 } 1318 1319 type SampleClient struct { 1320 name string 1321 c edgec.Client 1322 } 1323 1324 func NewSampleClient(name string, ec edgec.Client) *SampleClient { 1325 return &SampleClient{ 1326 name: name, 1327 c: ec, 1328 } 1329 } 1330 func (c *SampleClient) Echo( 1331 ctx context.Context, req *EchoRequest, kvs ...*rony.KeyValue, 1332 ) (*EchoResponse, error) { 1333 out := rony.PoolMessageEnvelope.Get() 1334 defer rony.PoolMessageEnvelope.Put(out) 1335 in := rony.PoolMessageEnvelope.Get() 1336 defer rony.PoolMessageEnvelope.Put(in) 1337 out.Fill(c.c.GetRequestID(), C_SampleEcho, req, kvs...) 1338 err := c.c.Send(ctx, out, in) 1339 if err != nil { 1340 return nil, err 1341 } 1342 switch in.GetConstructor() { 1343 case C_EchoResponse: 1344 x := &EchoResponse{} 1345 _ = proto.Unmarshal(in.Message, x) 1346 return x, nil 1347 case rony.C_Error: 1348 x := &rony.Error{} 1349 _ = x.Unmarshal(in.Message) 1350 return nil, x 1351 default: 1352 return nil, fmt.Errorf("unknown message :%d", in.GetConstructor()) 1353 } 1354 } 1355 func (c *SampleClient) Set( 1356 ctx context.Context, req *SetRequest, kvs ...*rony.KeyValue, 1357 ) (*SetResponse, error) { 1358 out := rony.PoolMessageEnvelope.Get() 1359 defer rony.PoolMessageEnvelope.Put(out) 1360 in := rony.PoolMessageEnvelope.Get() 1361 defer rony.PoolMessageEnvelope.Put(in) 1362 out.Fill(c.c.GetRequestID(), C_SampleSet, req, kvs...) 1363 err := c.c.Send(ctx, out, in) 1364 if err != nil { 1365 return nil, err 1366 } 1367 switch in.GetConstructor() { 1368 case C_SetResponse: 1369 x := &SetResponse{} 1370 _ = proto.Unmarshal(in.Message, x) 1371 return x, nil 1372 case rony.C_Error: 1373 x := &rony.Error{} 1374 _ = x.Unmarshal(in.Message) 1375 return nil, x 1376 default: 1377 return nil, fmt.Errorf("unknown message :%d", in.GetConstructor()) 1378 } 1379 } 1380 func (c *SampleClient) Get( 1381 ctx context.Context, req *GetRequest, kvs ...*rony.KeyValue, 1382 ) (*GetResponse, error) { 1383 out := rony.PoolMessageEnvelope.Get() 1384 defer rony.PoolMessageEnvelope.Put(out) 1385 in := rony.PoolMessageEnvelope.Get() 1386 defer rony.PoolMessageEnvelope.Put(in) 1387 out.Fill(c.c.GetRequestID(), C_SampleGet, req, kvs...) 1388 err := c.c.Send(ctx, out, in) 1389 if err != nil { 1390 return nil, err 1391 } 1392 switch in.GetConstructor() { 1393 case C_GetResponse: 1394 x := &GetResponse{} 1395 _ = proto.Unmarshal(in.Message, x) 1396 return x, nil 1397 case rony.C_Error: 1398 x := &rony.Error{} 1399 _ = x.Unmarshal(in.Message) 1400 return nil, x 1401 default: 1402 return nil, fmt.Errorf("unknown message :%d", in.GetConstructor()) 1403 } 1404 } 1405 func (c *SampleClient) EchoTunnel( 1406 ctx context.Context, req *EchoRequest, kvs ...*rony.KeyValue, 1407 ) (*EchoResponse, error) { 1408 out := rony.PoolMessageEnvelope.Get() 1409 defer rony.PoolMessageEnvelope.Put(out) 1410 in := rony.PoolMessageEnvelope.Get() 1411 defer rony.PoolMessageEnvelope.Put(in) 1412 out.Fill(c.c.GetRequestID(), C_SampleEchoTunnel, req, kvs...) 1413 err := c.c.Send(ctx, out, in) 1414 if err != nil { 1415 return nil, err 1416 } 1417 switch in.GetConstructor() { 1418 case C_EchoResponse: 1419 x := &EchoResponse{} 1420 _ = proto.Unmarshal(in.Message, x) 1421 return x, nil 1422 case rony.C_Error: 1423 x := &rony.Error{} 1424 _ = x.Unmarshal(in.Message) 1425 return nil, x 1426 default: 1427 return nil, fmt.Errorf("unknown message :%d", in.GetConstructor()) 1428 } 1429 } 1430 func (c *SampleClient) EchoDelay( 1431 ctx context.Context, req *EchoRequest, kvs ...*rony.KeyValue, 1432 ) (*EchoResponse, error) { 1433 out := rony.PoolMessageEnvelope.Get() 1434 defer rony.PoolMessageEnvelope.Put(out) 1435 in := rony.PoolMessageEnvelope.Get() 1436 defer rony.PoolMessageEnvelope.Put(in) 1437 out.Fill(c.c.GetRequestID(), C_SampleEchoDelay, req, kvs...) 1438 err := c.c.Send(ctx, out, in) 1439 if err != nil { 1440 return nil, err 1441 } 1442 switch in.GetConstructor() { 1443 case C_EchoResponse: 1444 x := &EchoResponse{} 1445 _ = proto.Unmarshal(in.Message, x) 1446 return x, nil 1447 case rony.C_Error: 1448 x := &rony.Error{} 1449 _ = x.Unmarshal(in.Message) 1450 return nil, x 1451 default: 1452 return nil, fmt.Errorf("unknown message :%d", in.GetConstructor()) 1453 } 1454 } 1455 1456 func prepareSampleCommand(cmd *cobra.Command, c edgec.Client) (*SampleClient, error) { 1457 // Bind current flags to the registered flags in config package 1458 err := config.BindCmdFlags(cmd) 1459 if err != nil { 1460 return nil, err 1461 } 1462 1463 return NewSampleClient("Sample", c), nil 1464 } 1465 1466 var genSampleEchoCmd = func(h ISampleCli, c edgec.Client) *cobra.Command { 1467 cmd := &cobra.Command{ 1468 Use: "echo", 1469 RunE: func(cmd *cobra.Command, args []string) error { 1470 cli, err := prepareSampleCommand(cmd, c) 1471 if err != nil { 1472 return err 1473 } 1474 return h.Echo(cli, cmd, args) 1475 }, 1476 } 1477 config.SetFlags(cmd, 1478 config.Int64Flag("int", tools.StrToInt64(""), ""), 1479 config.Int64Flag("timestamp", tools.StrToInt64(""), ""), 1480 config.Uint64Flag("replicaSet", tools.StrToUInt64(""), ""), 1481 config.StringFlag("someData", "", ""), 1482 ) 1483 return cmd 1484 } 1485 var genSampleSetCmd = func(h ISampleCli, c edgec.Client) *cobra.Command { 1486 cmd := &cobra.Command{ 1487 Use: "set", 1488 RunE: func(cmd *cobra.Command, args []string) error { 1489 cli, err := prepareSampleCommand(cmd, c) 1490 if err != nil { 1491 return err 1492 } 1493 return h.Set(cli, cmd, args) 1494 }, 1495 } 1496 config.SetFlags(cmd, 1497 config.StringFlag("key", "", ""), 1498 config.StringFlag("value", "", ""), 1499 ) 1500 return cmd 1501 } 1502 var genSampleGetCmd = func(h ISampleCli, c edgec.Client) *cobra.Command { 1503 cmd := &cobra.Command{ 1504 Use: "get", 1505 RunE: func(cmd *cobra.Command, args []string) error { 1506 cli, err := prepareSampleCommand(cmd, c) 1507 if err != nil { 1508 return err 1509 } 1510 return h.Get(cli, cmd, args) 1511 }, 1512 } 1513 config.SetFlags(cmd, 1514 config.StringFlag("key", "somekey", "enter some random key"), 1515 ) 1516 return cmd 1517 } 1518 var genSampleEchoTunnelCmd = func(h ISampleCli, c edgec.Client) *cobra.Command { 1519 cmd := &cobra.Command{ 1520 Use: "echo-tunnel", 1521 RunE: func(cmd *cobra.Command, args []string) error { 1522 cli, err := prepareSampleCommand(cmd, c) 1523 if err != nil { 1524 return err 1525 } 1526 return h.EchoTunnel(cli, cmd, args) 1527 }, 1528 } 1529 config.SetFlags(cmd, 1530 config.Int64Flag("int", tools.StrToInt64(""), ""), 1531 config.Int64Flag("timestamp", tools.StrToInt64(""), ""), 1532 config.Uint64Flag("replicaSet", tools.StrToUInt64(""), ""), 1533 config.StringFlag("someData", "", ""), 1534 ) 1535 return cmd 1536 } 1537 var genSampleEchoDelayCmd = func(h ISampleCli, c edgec.Client) *cobra.Command { 1538 cmd := &cobra.Command{ 1539 Use: "echo-delay", 1540 RunE: func(cmd *cobra.Command, args []string) error { 1541 cli, err := prepareSampleCommand(cmd, c) 1542 if err != nil { 1543 return err 1544 } 1545 return h.EchoDelay(cli, cmd, args) 1546 }, 1547 } 1548 config.SetFlags(cmd, 1549 config.Int64Flag("int", tools.StrToInt64(""), ""), 1550 config.Int64Flag("timestamp", tools.StrToInt64(""), ""), 1551 config.Uint64Flag("replicaSet", tools.StrToUInt64(""), ""), 1552 config.StringFlag("someData", "", ""), 1553 ) 1554 return cmd 1555 } 1556 1557 type ISampleCli interface { 1558 Echo(cli *SampleClient, cmd *cobra.Command, args []string) error 1559 Set(cli *SampleClient, cmd *cobra.Command, args []string) error 1560 Get(cli *SampleClient, cmd *cobra.Command, args []string) error 1561 EchoTunnel(cli *SampleClient, cmd *cobra.Command, args []string) error 1562 EchoDelay(cli *SampleClient, cmd *cobra.Command, args []string) error 1563 } 1564 1565 func RegisterSampleCli(h ISampleCli, c edgec.Client, rootCmd *cobra.Command) { 1566 subCommand := &cobra.Command{ 1567 Use: "Sample", 1568 } 1569 subCommand.AddCommand( 1570 genSampleEchoCmd(h, c), 1571 genSampleSetCmd(h, c), 1572 genSampleGetCmd(h, c), 1573 genSampleEchoTunnelCmd(h, c), 1574 genSampleEchoDelayCmd(h, c), 1575 ) 1576 1577 rootCmd.AddCommand(subCommand) 1578 } 1579 1580 var _ = bytes.MinRead