dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/grpc/internal/routeguide/server.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package routeguide 19 20 import ( 21 "context" 22 "encoding/json" 23 "fmt" 24 "io" 25 "math" 26 "net" 27 "sync" 28 "time" 29 ) 30 31 import ( 32 log "github.com/dubbogo/gost/log/logger" 33 34 "github.com/golang/protobuf/proto" 35 36 "google.golang.org/grpc" 37 ) 38 39 type routeGuideServer struct { 40 *RouteGuideProviderBase 41 savedFeatures []*Feature // read-only after initialized 42 43 mu sync.Mutex // protects routeNotes 44 routeNotes map[string][]*RouteNote 45 } 46 47 func NewService() (*routeGuideServer, error) { 48 s := &routeGuideServer{ 49 RouteGuideProviderBase: &RouteGuideProviderBase{}, 50 routeNotes: make(map[string][]*RouteNote), 51 } 52 err := json.Unmarshal(exampleData, &s.savedFeatures) 53 if err != nil { 54 return nil, err 55 } 56 return s, nil 57 } 58 59 // GetFeature returns the feature at the given point. 60 func (s *routeGuideServer) GetFeature(ctx context.Context, point *Point) (*Feature, error) { 61 for _, feature := range s.savedFeatures { 62 if proto.Equal(feature.Location, point) { 63 return feature, nil 64 } 65 } 66 // No feature was found, return an unnamed feature 67 return &Feature{Location: point}, nil 68 } 69 70 // ListFeatures lists all features contained within the given bounding Rectangle. 71 func (s *routeGuideServer) ListFeatures(rect *Rectangle, stream RouteGuide_ListFeaturesServer) error { 72 for _, feature := range s.savedFeatures { 73 if inRange(feature.Location, rect) { 74 if err := stream.Send(feature); err != nil { 75 return err 76 } 77 } 78 } 79 return nil 80 } 81 82 // RecordRoute records a route composited of a sequence of points. 83 // 84 // It gets a stream of points, and responds with statistics about the "trip": 85 // number of points, number of known features visited, total distance traveled, and 86 // total time spent. 87 func (s *routeGuideServer) RecordRoute(stream RouteGuide_RecordRouteServer) error { 88 var pointCount, featureCount, distance int32 89 var lastPoint *Point 90 startTime := time.Now() 91 for { 92 point, err := stream.Recv() 93 if err == io.EOF { 94 endTime := time.Now() 95 return stream.SendAndClose(&RouteSummary{ 96 PointCount: pointCount, 97 FeatureCount: featureCount, 98 Distance: distance, 99 ElapsedTime: int32(endTime.Sub(startTime).Seconds()), 100 }) 101 } 102 if err != nil { 103 return err 104 } 105 pointCount++ 106 for _, feature := range s.savedFeatures { 107 if proto.Equal(feature.Location, point) { 108 featureCount++ 109 } 110 } 111 if lastPoint != nil { 112 distance += calcDistance(lastPoint, point) 113 } 114 lastPoint = point 115 } 116 } 117 118 // RouteChat receives a stream of message/location pairs, and responds with a stream of all 119 // previous messages at each of those locations. 120 func (s *routeGuideServer) RouteChat(stream RouteGuide_RouteChatServer) error { 121 for { 122 in, err := stream.Recv() 123 if err == io.EOF { 124 return nil 125 } 126 if err != nil { 127 return err 128 } 129 key := serialize(in.Location) 130 131 s.mu.Lock() 132 s.routeNotes[key] = append(s.routeNotes[key], in) 133 // Note: this copy prevents blocking other clients while serving this one. 134 // We don't need to do a deep copy, because elements in the slice are 135 // insert-only and never modified. 136 rn := make([]*RouteNote, len(s.routeNotes[key])) 137 copy(rn, s.routeNotes[key]) 138 s.mu.Unlock() 139 140 for _, note := range rn { 141 if err := stream.Send(note); err != nil { 142 return err 143 } 144 } 145 } 146 } 147 148 type Server struct { 149 listener net.Listener 150 server *grpc.Server 151 } 152 153 func NewServer(address string) (*Server, error) { 154 listener, err := net.Listen("tcp", address) 155 if err != nil { 156 return nil, err 157 } 158 159 server := grpc.NewServer() 160 service, err := NewService() 161 if err != nil { 162 return nil, err 163 } 164 RegisterRouteGuideServer(server, service) 165 166 s := Server{ 167 listener: listener, 168 server: server, 169 } 170 return &s, nil 171 } 172 173 func (s *Server) Start() { 174 if err := s.server.Serve(s.listener); err != nil { 175 log.Fatalf("failed to serve: %v", err) 176 } 177 } 178 179 func (s *Server) Stop() { 180 s.server.GracefulStop() 181 } 182 183 func toRadians(num float64) float64 { 184 return num * math.Pi / float64(180) 185 } 186 187 // calcDistance calculates the distance between two points using the "haversine" formula. 188 // The formula is based on http://mathforum.org/library/drmath/view/51879.html. 189 func calcDistance(p1 *Point, p2 *Point) int32 { 190 const CordFactor float64 = 1e7 191 const R = float64(6371000) // earth radius in meters 192 lat1 := toRadians(float64(p1.Latitude) / CordFactor) 193 lat2 := toRadians(float64(p2.Latitude) / CordFactor) 194 lng1 := toRadians(float64(p1.Longitude) / CordFactor) 195 lng2 := toRadians(float64(p2.Longitude) / CordFactor) 196 dlat := lat2 - lat1 197 dlng := lng2 - lng1 198 199 a := math.Sin(dlat/2)*math.Sin(dlat/2) + 200 math.Cos(lat1)*math.Cos(lat2)* 201 math.Sin(dlng/2)*math.Sin(dlng/2) 202 c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a)) 203 204 distance := R * c 205 return int32(distance) 206 } 207 208 func inRange(point *Point, rect *Rectangle) bool { 209 left := math.Min(float64(rect.Lo.Longitude), float64(rect.Hi.Longitude)) 210 right := math.Max(float64(rect.Lo.Longitude), float64(rect.Hi.Longitude)) 211 top := math.Max(float64(rect.Lo.Latitude), float64(rect.Hi.Latitude)) 212 bottom := math.Min(float64(rect.Lo.Latitude), float64(rect.Hi.Latitude)) 213 214 if float64(point.Longitude) >= left && 215 float64(point.Longitude) <= right && 216 float64(point.Latitude) >= bottom && 217 float64(point.Latitude) <= top { 218 return true 219 } 220 return false 221 } 222 223 func serialize(point *Point) string { 224 return fmt.Sprintf("%d %d", point.Latitude, point.Longitude) 225 } 226 227 // exampleData is a copy of testdata/route_guide_db.json. It's to avoid 228 // specifying file path with `go run`. 229 var exampleData = []byte(`[{ 230 "location": { 231 "latitude": 407838351, 232 "longitude": -746143763 233 }, 234 "name": "Patriots Path, Mendham, NJ 07945, USA" 235 }, { 236 "location": { 237 "latitude": 408122808, 238 "longitude": -743999179 239 }, 240 "name": "101 New Jersey 10, Whippany, NJ 07981, USA" 241 }, { 242 "location": { 243 "latitude": 413628156, 244 "longitude": -749015468 245 }, 246 "name": "U.S. 6, Shohola, PA 18458, USA" 247 }, { 248 "location": { 249 "latitude": 419999544, 250 "longitude": -740371136 251 }, 252 "name": "5 Conners Road, Kingston, NY 12401, USA" 253 }, { 254 "location": { 255 "latitude": 414008389, 256 "longitude": -743951297 257 }, 258 "name": "Mid Hudson Psychiatric Center, New Hampton, NY 10958, USA" 259 }, { 260 "location": { 261 "latitude": 419611318, 262 "longitude": -746524769 263 }, 264 "name": "287 Flugertown Road, Livingston Manor, NY 12758, USA" 265 }, { 266 "location": { 267 "latitude": 406109563, 268 "longitude": -742186778 269 }, 270 "name": "4001 Tremley Point Road, Linden, NJ 07036, USA" 271 }, { 272 "location": { 273 "latitude": 416802456, 274 "longitude": -742370183 275 }, 276 "name": "352 South Mountain Road, Wallkill, NY 12589, USA" 277 }, { 278 "location": { 279 "latitude": 412950425, 280 "longitude": -741077389 281 }, 282 "name": "Bailey Turn Road, Harriman, NY 10926, USA" 283 }, { 284 "location": { 285 "latitude": 412144655, 286 "longitude": -743949739 287 }, 288 "name": "193-199 Wawayanda Road, Hewitt, NJ 07421, USA" 289 }, { 290 "location": { 291 "latitude": 415736605, 292 "longitude": -742847522 293 }, 294 "name": "406-496 Ward Avenue, Pine Bush, NY 12566, USA" 295 }, { 296 "location": { 297 "latitude": 413843930, 298 "longitude": -740501726 299 }, 300 "name": "162 Merrill Road, Highland Mills, NY 10930, USA" 301 }, { 302 "location": { 303 "latitude": 410873075, 304 "longitude": -744459023 305 }, 306 "name": "Clinton Road, West Milford, NJ 07480, USA" 307 }, { 308 "location": { 309 "latitude": 412346009, 310 "longitude": -744026814 311 }, 312 "name": "16 Old Brook Lane, Warwick, NY 10990, USA" 313 }, { 314 "location": { 315 "latitude": 402948455, 316 "longitude": -747903913 317 }, 318 "name": "3 Drake Lane, Pennington, NJ 08534, USA" 319 }, { 320 "location": { 321 "latitude": 406337092, 322 "longitude": -740122226 323 }, 324 "name": "6324 8th Avenue, Brooklyn, NY 11220, USA" 325 }, { 326 "location": { 327 "latitude": 406421967, 328 "longitude": -747727624 329 }, 330 "name": "1 Merck Access Road, Whitehouse Station, NJ 08889, USA" 331 }, { 332 "location": { 333 "latitude": 416318082, 334 "longitude": -749677716 335 }, 336 "name": "78-98 Schalck Road, Narrowsburg, NY 12764, USA" 337 }, { 338 "location": { 339 "latitude": 415301720, 340 "longitude": -748416257 341 }, 342 "name": "282 Lakeview Drive Road, Highland Lake, NY 12743, USA" 343 }, { 344 "location": { 345 "latitude": 402647019, 346 "longitude": -747071791 347 }, 348 "name": "330 Evelyn Avenue, Hamilton Township, NJ 08619, USA" 349 }, { 350 "location": { 351 "latitude": 412567807, 352 "longitude": -741058078 353 }, 354 "name": "New York State Reference Route 987E, Southfields, NY 10975, USA" 355 }, { 356 "location": { 357 "latitude": 416855156, 358 "longitude": -744420597 359 }, 360 "name": "103-271 Tempaloni Road, Ellenville, NY 12428, USA" 361 }, { 362 "location": { 363 "latitude": 404663628, 364 "longitude": -744820157 365 }, 366 "name": "1300 Airport Road, North Brunswick Township, NJ 08902, USA" 367 }, { 368 "location": { 369 "latitude": 407113723, 370 "longitude": -749746483 371 }, 372 "name": "" 373 }, { 374 "location": { 375 "latitude": 402133926, 376 "longitude": -743613249 377 }, 378 "name": "" 379 }, { 380 "location": { 381 "latitude": 400273442, 382 "longitude": -741220915 383 }, 384 "name": "" 385 }, { 386 "location": { 387 "latitude": 411236786, 388 "longitude": -744070769 389 }, 390 "name": "" 391 }, { 392 "location": { 393 "latitude": 411633782, 394 "longitude": -746784970 395 }, 396 "name": "211-225 Plains Road, Augusta, NJ 07822, USA" 397 }, { 398 "location": { 399 "latitude": 415830701, 400 "longitude": -742952812 401 }, 402 "name": "" 403 }, { 404 "location": { 405 "latitude": 413447164, 406 "longitude": -748712898 407 }, 408 "name": "165 Pedersen Ridge Road, Milford, PA 18337, USA" 409 }, { 410 "location": { 411 "latitude": 405047245, 412 "longitude": -749800722 413 }, 414 "name": "100-122 Locktown Road, Frenchtown, NJ 08825, USA" 415 }, { 416 "location": { 417 "latitude": 418858923, 418 "longitude": -746156790 419 }, 420 "name": "" 421 }, { 422 "location": { 423 "latitude": 417951888, 424 "longitude": -748484944 425 }, 426 "name": "650-652 Willi Hill Road, Swan Lake, NY 12783, USA" 427 }, { 428 "location": { 429 "latitude": 407033786, 430 "longitude": -743977337 431 }, 432 "name": "26 East 3rd Street, New Providence, NJ 07974, USA" 433 }, { 434 "location": { 435 "latitude": 417548014, 436 "longitude": -740075041 437 }, 438 "name": "" 439 }, { 440 "location": { 441 "latitude": 410395868, 442 "longitude": -744972325 443 }, 444 "name": "" 445 }, { 446 "location": { 447 "latitude": 404615353, 448 "longitude": -745129803 449 }, 450 "name": "" 451 }, { 452 "location": { 453 "latitude": 406589790, 454 "longitude": -743560121 455 }, 456 "name": "611 Lawrence Avenue, Westfield, NJ 07090, USA" 457 }, { 458 "location": { 459 "latitude": 414653148, 460 "longitude": -740477477 461 }, 462 "name": "18 Lannis Avenue, New Windsor, NY 12553, USA" 463 }, { 464 "location": { 465 "latitude": 405957808, 466 "longitude": -743255336 467 }, 468 "name": "82-104 Amherst Avenue, Colonia, NJ 07067, USA" 469 }, { 470 "location": { 471 "latitude": 411733589, 472 "longitude": -741648093 473 }, 474 "name": "170 Seven Lakes Drive, Sloatsburg, NY 10974, USA" 475 }, { 476 "location": { 477 "latitude": 412676291, 478 "longitude": -742606606 479 }, 480 "name": "1270 Lakes Road, Monroe, NY 10950, USA" 481 }, { 482 "location": { 483 "latitude": 409224445, 484 "longitude": -748286738 485 }, 486 "name": "509-535 Alphano Road, Great Meadows, NJ 07838, USA" 487 }, { 488 "location": { 489 "latitude": 406523420, 490 "longitude": -742135517 491 }, 492 "name": "652 Garden Street, Elizabeth, NJ 07202, USA" 493 }, { 494 "location": { 495 "latitude": 401827388, 496 "longitude": -740294537 497 }, 498 "name": "349 Sea Spray Court, Neptune City, NJ 07753, USA" 499 }, { 500 "location": { 501 "latitude": 410564152, 502 "longitude": -743685054 503 }, 504 "name": "13-17 Stanley Street, West Milford, NJ 07480, USA" 505 }, { 506 "location": { 507 "latitude": 408472324, 508 "longitude": -740726046 509 }, 510 "name": "47 Industrial Avenue, Teterboro, NJ 07608, USA" 511 }, { 512 "location": { 513 "latitude": 412452168, 514 "longitude": -740214052 515 }, 516 "name": "5 White Oak Lane, Stony Point, NY 10980, USA" 517 }, { 518 "location": { 519 "latitude": 409146138, 520 "longitude": -746188906 521 }, 522 "name": "Berkshire Valley Management Area Trail, Jefferson, NJ, USA" 523 }, { 524 "location": { 525 "latitude": 404701380, 526 "longitude": -744781745 527 }, 528 "name": "1007 Jersey Avenue, New Brunswick, NJ 08901, USA" 529 }, { 530 "location": { 531 "latitude": 409642566, 532 "longitude": -746017679 533 }, 534 "name": "6 East Emerald Isle Drive, Lake Hopatcong, NJ 07849, USA" 535 }, { 536 "location": { 537 "latitude": 408031728, 538 "longitude": -748645385 539 }, 540 "name": "1358-1474 New Jersey 57, Port Murray, NJ 07865, USA" 541 }, { 542 "location": { 543 "latitude": 413700272, 544 "longitude": -742135189 545 }, 546 "name": "367 Prospect Road, Chester, NY 10918, USA" 547 }, { 548 "location": { 549 "latitude": 404310607, 550 "longitude": -740282632 551 }, 552 "name": "10 Simon Lake Drive, Atlantic Highlands, NJ 07716, USA" 553 }, { 554 "location": { 555 "latitude": 409319800, 556 "longitude": -746201391 557 }, 558 "name": "11 Ward Street, Mount Arlington, NJ 07856, USA" 559 }, { 560 "location": { 561 "latitude": 406685311, 562 "longitude": -742108603 563 }, 564 "name": "300-398 Jefferson Avenue, Elizabeth, NJ 07201, USA" 565 }, { 566 "location": { 567 "latitude": 419018117, 568 "longitude": -749142781 569 }, 570 "name": "43 Dreher Road, Roscoe, NY 12776, USA" 571 }, { 572 "location": { 573 "latitude": 412856162, 574 "longitude": -745148837 575 }, 576 "name": "Swan Street, Pine Island, NY 10969, USA" 577 }, { 578 "location": { 579 "latitude": 416560744, 580 "longitude": -746721964 581 }, 582 "name": "66 Pleasantview Avenue, Monticello, NY 12701, USA" 583 }, { 584 "location": { 585 "latitude": 405314270, 586 "longitude": -749836354 587 }, 588 "name": "" 589 }, { 590 "location": { 591 "latitude": 414219548, 592 "longitude": -743327440 593 }, 594 "name": "" 595 }, { 596 "location": { 597 "latitude": 415534177, 598 "longitude": -742900616 599 }, 600 "name": "565 Winding Hills Road, Montgomery, NY 12549, USA" 601 }, { 602 "location": { 603 "latitude": 406898530, 604 "longitude": -749127080 605 }, 606 "name": "231 Rocky Run Road, Glen Gardner, NJ 08826, USA" 607 }, { 608 "location": { 609 "latitude": 407586880, 610 "longitude": -741670168 611 }, 612 "name": "100 Mount Pleasant Avenue, Newark, NJ 07104, USA" 613 }, { 614 "location": { 615 "latitude": 400106455, 616 "longitude": -742870190 617 }, 618 "name": "517-521 Huntington Drive, Manchester Township, NJ 08759, USA" 619 }, { 620 "location": { 621 "latitude": 400066188, 622 "longitude": -746793294 623 }, 624 "name": "" 625 }, { 626 "location": { 627 "latitude": 418803880, 628 "longitude": -744102673 629 }, 630 "name": "40 Mountain Road, Napanoch, NY 12458, USA" 631 }, { 632 "location": { 633 "latitude": 414204288, 634 "longitude": -747895140 635 }, 636 "name": "" 637 }, { 638 "location": { 639 "latitude": 414777405, 640 "longitude": -740615601 641 }, 642 "name": "" 643 }, { 644 "location": { 645 "latitude": 415464475, 646 "longitude": -747175374 647 }, 648 "name": "48 North Road, Forestburgh, NY 12777, USA" 649 }, { 650 "location": { 651 "latitude": 404062378, 652 "longitude": -746376177 653 }, 654 "name": "" 655 }, { 656 "location": { 657 "latitude": 405688272, 658 "longitude": -749285130 659 }, 660 "name": "" 661 }, { 662 "location": { 663 "latitude": 400342070, 664 "longitude": -748788996 665 }, 666 "name": "" 667 }, { 668 "location": { 669 "latitude": 401809022, 670 "longitude": -744157964 671 }, 672 "name": "" 673 }, { 674 "location": { 675 "latitude": 404226644, 676 "longitude": -740517141 677 }, 678 "name": "9 Thompson Avenue, Leonardo, NJ 07737, USA" 679 }, { 680 "location": { 681 "latitude": 410322033, 682 "longitude": -747871659 683 }, 684 "name": "" 685 }, { 686 "location": { 687 "latitude": 407100674, 688 "longitude": -747742727 689 }, 690 "name": "" 691 }, { 692 "location": { 693 "latitude": 418811433, 694 "longitude": -741718005 695 }, 696 "name": "213 Bush Road, Stone Ridge, NY 12484, USA" 697 }, { 698 "location": { 699 "latitude": 415034302, 700 "longitude": -743850945 701 }, 702 "name": "" 703 }, { 704 "location": { 705 "latitude": 411349992, 706 "longitude": -743694161 707 }, 708 "name": "" 709 }, { 710 "location": { 711 "latitude": 404839914, 712 "longitude": -744759616 713 }, 714 "name": "1-17 Bergen Court, New Brunswick, NJ 08901, USA" 715 }, { 716 "location": { 717 "latitude": 414638017, 718 "longitude": -745957854 719 }, 720 "name": "35 Oakland Valley Road, Cuddebackville, NY 12729, USA" 721 }, { 722 "location": { 723 "latitude": 412127800, 724 "longitude": -740173578 725 }, 726 "name": "" 727 }, { 728 "location": { 729 "latitude": 401263460, 730 "longitude": -747964303 731 }, 732 "name": "" 733 }, { 734 "location": { 735 "latitude": 412843391, 736 "longitude": -749086026 737 }, 738 "name": "" 739 }, { 740 "location": { 741 "latitude": 418512773, 742 "longitude": -743067823 743 }, 744 "name": "" 745 }, { 746 "location": { 747 "latitude": 404318328, 748 "longitude": -740835638 749 }, 750 "name": "42-102 Main Street, Belford, NJ 07718, USA" 751 }, { 752 "location": { 753 "latitude": 419020746, 754 "longitude": -741172328 755 }, 756 "name": "" 757 }, { 758 "location": { 759 "latitude": 404080723, 760 "longitude": -746119569 761 }, 762 "name": "" 763 }, { 764 "location": { 765 "latitude": 401012643, 766 "longitude": -744035134 767 }, 768 "name": "" 769 }, { 770 "location": { 771 "latitude": 404306372, 772 "longitude": -741079661 773 }, 774 "name": "" 775 }, { 776 "location": { 777 "latitude": 403966326, 778 "longitude": -748519297 779 }, 780 "name": "" 781 }, { 782 "location": { 783 "latitude": 405002031, 784 "longitude": -748407866 785 }, 786 "name": "" 787 }, { 788 "location": { 789 "latitude": 409532885, 790 "longitude": -742200683 791 }, 792 "name": "" 793 }, { 794 "location": { 795 "latitude": 416851321, 796 "longitude": -742674555 797 }, 798 "name": "" 799 }, { 800 "location": { 801 "latitude": 406411633, 802 "longitude": -741722051 803 }, 804 "name": "3387 Richmond Terrace, Staten Island, NY 10303, USA" 805 }, { 806 "location": { 807 "latitude": 413069058, 808 "longitude": -744597778 809 }, 810 "name": "261 Van Sickle Road, Goshen, NY 10924, USA" 811 }, { 812 "location": { 813 "latitude": 418465462, 814 "longitude": -746859398 815 }, 816 "name": "" 817 }, { 818 "location": { 819 "latitude": 411733222, 820 "longitude": -744228360 821 }, 822 "name": "" 823 }, { 824 "location": { 825 "latitude": 410248224, 826 "longitude": -747127767 827 }, 828 "name": "3 Hasta Way, Newton, NJ 07860, USA" 829 }]`)