From fec98be959ca751a2a7b88cd4818d080eb872b8f Mon Sep 17 00:00:00 2001 From: Morgan Mccauley Date: Mon, 4 Dec 2023 10:02:01 +1300 Subject: [PATCH] chore: Remove example route guide RPC service --- block-streamer/build.rs | 2 - block-streamer/data/route_guide_db.json | 702 ------------------ block-streamer/proto/route_guide.proto | 106 --- block-streamer/src/data.rs | 34 - block-streamer/src/main.rs | 1 - block-streamer/src/server/mod.rs | 16 +- .../src/server/route_guide_service.rs | 188 ----- 7 files changed, 4 insertions(+), 1045 deletions(-) delete mode 100644 block-streamer/data/route_guide_db.json delete mode 100644 block-streamer/proto/route_guide.proto delete mode 100644 block-streamer/src/data.rs delete mode 100644 block-streamer/src/server/route_guide_service.rs diff --git a/block-streamer/build.rs b/block-streamer/build.rs index a7a4eb570..4683c5353 100644 --- a/block-streamer/build.rs +++ b/block-streamer/build.rs @@ -1,6 +1,4 @@ fn main() -> Result<(), Box> { - tonic_build::compile_protos("proto/route_guide.proto") - .unwrap_or_else(|e| panic!("Failed to compile protos {:?}", e)); tonic_build::compile_protos("proto/block_streamer.proto")?; Ok(()) diff --git a/block-streamer/data/route_guide_db.json b/block-streamer/data/route_guide_db.json deleted file mode 100644 index 22e93e313..000000000 --- a/block-streamer/data/route_guide_db.json +++ /dev/null @@ -1,702 +0,0 @@ -[ - { - "location": { - "latitude": 407838351, - "longitude": -746143763 - }, - "name": "Patriots Path, Mendham, NJ 07945, USA" - }, - { - "location": { - "latitude": 408122808, - "longitude": -743999179 - }, - "name": "101 New Jersey 10, Whippany, NJ 07981, USA" - }, - { - "location": { - "latitude": 413628156, - "longitude": -749015468 - }, - "name": "U.S. 6, Shohola, PA 18458, USA" - }, - { - "location": { - "latitude": 419999544, - "longitude": -740371136 - }, - "name": "5 Conners Road, Kingston, NY 12401, USA" - }, - { - "location": { - "latitude": 414008389, - "longitude": -743951297 - }, - "name": "Mid Hudson Psychiatric Center, New Hampton, NY 10958, USA" - }, - { - "location": { - "latitude": 419611318, - "longitude": -746524769 - }, - "name": "287 Flugertown Road, Livingston Manor, NY 12758, USA" - }, - { - "location": { - "latitude": 406109563, - "longitude": -742186778 - }, - "name": "4001 Tremley Point Road, Linden, NJ 07036, USA" - }, - { - "location": { - "latitude": 416802456, - "longitude": -742370183 - }, - "name": "352 South Mountain Road, Wallkill, NY 12589, USA" - }, - { - "location": { - "latitude": 412950425, - "longitude": -741077389 - }, - "name": "Bailey Turn Road, Harriman, NY 10926, USA" - }, - { - "location": { - "latitude": 412144655, - "longitude": -743949739 - }, - "name": "193-199 Wawayanda Road, Hewitt, NJ 07421, USA" - }, - { - "location": { - "latitude": 415736605, - "longitude": -742847522 - }, - "name": "406-496 Ward Avenue, Pine Bush, NY 12566, USA" - }, - { - "location": { - "latitude": 413843930, - "longitude": -740501726 - }, - "name": "162 Merrill Road, Highland Mills, NY 10930, USA" - }, - { - "location": { - "latitude": 410873075, - "longitude": -744459023 - }, - "name": "Clinton Road, West Milford, NJ 07480, USA" - }, - { - "location": { - "latitude": 412346009, - "longitude": -744026814 - }, - "name": "16 Old Brook Lane, Warwick, NY 10990, USA" - }, - { - "location": { - "latitude": 402948455, - "longitude": -747903913 - }, - "name": "3 Drake Lane, Pennington, NJ 08534, USA" - }, - { - "location": { - "latitude": 406337092, - "longitude": -740122226 - }, - "name": "6324 8th Avenue, Brooklyn, NY 11220, USA" - }, - { - "location": { - "latitude": 406421967, - "longitude": -747727624 - }, - "name": "1 Merck Access Road, Whitehouse Station, NJ 08889, USA" - }, - { - "location": { - "latitude": 416318082, - "longitude": -749677716 - }, - "name": "78-98 Schalck Road, Narrowsburg, NY 12764, USA" - }, - { - "location": { - "latitude": 415301720, - "longitude": -748416257 - }, - "name": "282 Lakeview Drive Road, Highland Lake, NY 12743, USA" - }, - { - "location": { - "latitude": 402647019, - "longitude": -747071791 - }, - "name": "330 Evelyn Avenue, Hamilton Township, NJ 08619, USA" - }, - { - "location": { - "latitude": 412567807, - "longitude": -741058078 - }, - "name": "New York State Reference Route 987E, Southfields, NY 10975, USA" - }, - { - "location": { - "latitude": 416855156, - "longitude": -744420597 - }, - "name": "103-271 Tempaloni Road, Ellenville, NY 12428, USA" - }, - { - "location": { - "latitude": 404663628, - "longitude": -744820157 - }, - "name": "1300 Airport Road, North Brunswick Township, NJ 08902, USA" - }, - { - "location": { - "latitude": 407113723, - "longitude": -749746483 - }, - "name": "" - }, - { - "location": { - "latitude": 402133926, - "longitude": -743613249 - }, - "name": "" - }, - { - "location": { - "latitude": 400273442, - "longitude": -741220915 - }, - "name": "" - }, - { - "location": { - "latitude": 411236786, - "longitude": -744070769 - }, - "name": "" - }, - { - "location": { - "latitude": 411633782, - "longitude": -746784970 - }, - "name": "211-225 Plains Road, Augusta, NJ 07822, USA" - }, - { - "location": { - "latitude": 415830701, - "longitude": -742952812 - }, - "name": "" - }, - { - "location": { - "latitude": 413447164, - "longitude": -748712898 - }, - "name": "165 Pedersen Ridge Road, Milford, PA 18337, USA" - }, - { - "location": { - "latitude": 405047245, - "longitude": -749800722 - }, - "name": "100-122 Locktown Road, Frenchtown, NJ 08825, USA" - }, - { - "location": { - "latitude": 418858923, - "longitude": -746156790 - }, - "name": "" - }, - { - "location": { - "latitude": 417951888, - "longitude": -748484944 - }, - "name": "650-652 Willi Hill Road, Swan Lake, NY 12783, USA" - }, - { - "location": { - "latitude": 407033786, - "longitude": -743977337 - }, - "name": "26 East 3rd Street, New Providence, NJ 07974, USA" - }, - { - "location": { - "latitude": 417548014, - "longitude": -740075041 - }, - "name": "" - }, - { - "location": { - "latitude": 410395868, - "longitude": -744972325 - }, - "name": "" - }, - { - "location": { - "latitude": 404615353, - "longitude": -745129803 - }, - "name": "" - }, - { - "location": { - "latitude": 406589790, - "longitude": -743560121 - }, - "name": "611 Lawrence Avenue, Westfield, NJ 07090, USA" - }, - { - "location": { - "latitude": 414653148, - "longitude": -740477477 - }, - "name": "18 Lannis Avenue, New Windsor, NY 12553, USA" - }, - { - "location": { - "latitude": 405957808, - "longitude": -743255336 - }, - "name": "82-104 Amherst Avenue, Colonia, NJ 07067, USA" - }, - { - "location": { - "latitude": 411733589, - "longitude": -741648093 - }, - "name": "170 Seven Lakes Drive, Sloatsburg, NY 10974, USA" - }, - { - "location": { - "latitude": 412676291, - "longitude": -742606606 - }, - "name": "1270 Lakes Road, Monroe, NY 10950, USA" - }, - { - "location": { - "latitude": 409224445, - "longitude": -748286738 - }, - "name": "509-535 Alphano Road, Great Meadows, NJ 07838, USA" - }, - { - "location": { - "latitude": 406523420, - "longitude": -742135517 - }, - "name": "652 Garden Street, Elizabeth, NJ 07202, USA" - }, - { - "location": { - "latitude": 401827388, - "longitude": -740294537 - }, - "name": "349 Sea Spray Court, Neptune City, NJ 07753, USA" - }, - { - "location": { - "latitude": 410564152, - "longitude": -743685054 - }, - "name": "13-17 Stanley Street, West Milford, NJ 07480, USA" - }, - { - "location": { - "latitude": 408472324, - "longitude": -740726046 - }, - "name": "47 Industrial Avenue, Teterboro, NJ 07608, USA" - }, - { - "location": { - "latitude": 412452168, - "longitude": -740214052 - }, - "name": "5 White Oak Lane, Stony Point, NY 10980, USA" - }, - { - "location": { - "latitude": 409146138, - "longitude": -746188906 - }, - "name": "Berkshire Valley Management Area Trail, Jefferson, NJ, USA" - }, - { - "location": { - "latitude": 404701380, - "longitude": -744781745 - }, - "name": "1007 Jersey Avenue, New Brunswick, NJ 08901, USA" - }, - { - "location": { - "latitude": 409642566, - "longitude": -746017679 - }, - "name": "6 East Emerald Isle Drive, Lake Hopatcong, NJ 07849, USA" - }, - { - "location": { - "latitude": 408031728, - "longitude": -748645385 - }, - "name": "1358-1474 New Jersey 57, Port Murray, NJ 07865, USA" - }, - { - "location": { - "latitude": 413700272, - "longitude": -742135189 - }, - "name": "367 Prospect Road, Chester, NY 10918, USA" - }, - { - "location": { - "latitude": 404310607, - "longitude": -740282632 - }, - "name": "10 Simon Lake Drive, Atlantic Highlands, NJ 07716, USA" - }, - { - "location": { - "latitude": 409319800, - "longitude": -746201391 - }, - "name": "11 Ward Street, Mount Arlington, NJ 07856, USA" - }, - { - "location": { - "latitude": 406685311, - "longitude": -742108603 - }, - "name": "300-398 Jefferson Avenue, Elizabeth, NJ 07201, USA" - }, - { - "location": { - "latitude": 419018117, - "longitude": -749142781 - }, - "name": "43 Dreher Road, Roscoe, NY 12776, USA" - }, - { - "location": { - "latitude": 412856162, - "longitude": -745148837 - }, - "name": "Swan Street, Pine Island, NY 10969, USA" - }, - { - "location": { - "latitude": 416560744, - "longitude": -746721964 - }, - "name": "66 Pleasantview Avenue, Monticello, NY 12701, USA" - }, - { - "location": { - "latitude": 405314270, - "longitude": -749836354 - }, - "name": "" - }, - { - "location": { - "latitude": 414219548, - "longitude": -743327440 - }, - "name": "" - }, - { - "location": { - "latitude": 415534177, - "longitude": -742900616 - }, - "name": "565 Winding Hills Road, Montgomery, NY 12549, USA" - }, - { - "location": { - "latitude": 406898530, - "longitude": -749127080 - }, - "name": "231 Rocky Run Road, Glen Gardner, NJ 08826, USA" - }, - { - "location": { - "latitude": 407586880, - "longitude": -741670168 - }, - "name": "100 Mount Pleasant Avenue, Newark, NJ 07104, USA" - }, - { - "location": { - "latitude": 400106455, - "longitude": -742870190 - }, - "name": "517-521 Huntington Drive, Manchester Township, NJ 08759, USA" - }, - { - "location": { - "latitude": 400066188, - "longitude": -746793294 - }, - "name": "" - }, - { - "location": { - "latitude": 418803880, - "longitude": -744102673 - }, - "name": "40 Mountain Road, Napanoch, NY 12458, USA" - }, - { - "location": { - "latitude": 414204288, - "longitude": -747895140 - }, - "name": "" - }, - { - "location": { - "latitude": 414777405, - "longitude": -740615601 - }, - "name": "" - }, - { - "location": { - "latitude": 415464475, - "longitude": -747175374 - }, - "name": "48 North Road, Forestburgh, NY 12777, USA" - }, - { - "location": { - "latitude": 404062378, - "longitude": -746376177 - }, - "name": "" - }, - { - "location": { - "latitude": 405688272, - "longitude": -749285130 - }, - "name": "" - }, - { - "location": { - "latitude": 400342070, - "longitude": -748788996 - }, - "name": "" - }, - { - "location": { - "latitude": 401809022, - "longitude": -744157964 - }, - "name": "" - }, - { - "location": { - "latitude": 404226644, - "longitude": -740517141 - }, - "name": "9 Thompson Avenue, Leonardo, NJ 07737, USA" - }, - { - "location": { - "latitude": 410322033, - "longitude": -747871659 - }, - "name": "" - }, - { - "location": { - "latitude": 407100674, - "longitude": -747742727 - }, - "name": "" - }, - { - "location": { - "latitude": 418811433, - "longitude": -741718005 - }, - "name": "213 Bush Road, Stone Ridge, NY 12484, USA" - }, - { - "location": { - "latitude": 415034302, - "longitude": -743850945 - }, - "name": "" - }, - { - "location": { - "latitude": 411349992, - "longitude": -743694161 - }, - "name": "" - }, - { - "location": { - "latitude": 404839914, - "longitude": -744759616 - }, - "name": "1-17 Bergen Court, New Brunswick, NJ 08901, USA" - }, - { - "location": { - "latitude": 414638017, - "longitude": -745957854 - }, - "name": "35 Oakland Valley Road, Cuddebackville, NY 12729, USA" - }, - { - "location": { - "latitude": 412127800, - "longitude": -740173578 - }, - "name": "" - }, - { - "location": { - "latitude": 401263460, - "longitude": -747964303 - }, - "name": "" - }, - { - "location": { - "latitude": 412843391, - "longitude": -749086026 - }, - "name": "" - }, - { - "location": { - "latitude": 418512773, - "longitude": -743067823 - }, - "name": "" - }, - { - "location": { - "latitude": 404318328, - "longitude": -740835638 - }, - "name": "42-102 Main Street, Belford, NJ 07718, USA" - }, - { - "location": { - "latitude": 419020746, - "longitude": -741172328 - }, - "name": "" - }, - { - "location": { - "latitude": 404080723, - "longitude": -746119569 - }, - "name": "" - }, - { - "location": { - "latitude": 401012643, - "longitude": -744035134 - }, - "name": "" - }, - { - "location": { - "latitude": 404306372, - "longitude": -741079661 - }, - "name": "" - }, - { - "location": { - "latitude": 403966326, - "longitude": -748519297 - }, - "name": "" - }, - { - "location": { - "latitude": 405002031, - "longitude": -748407866 - }, - "name": "" - }, - { - "location": { - "latitude": 409532885, - "longitude": -742200683 - }, - "name": "" - }, - { - "location": { - "latitude": 416851321, - "longitude": -742674555 - }, - "name": "" - }, - { - "location": { - "latitude": 406411633, - "longitude": -741722051 - }, - "name": "3387 Richmond Terrace, Staten Island, NY 10303, USA" - }, - { - "location": { - "latitude": 413069058, - "longitude": -744597778 - }, - "name": "261 Van Sickle Road, Goshen, NY 10924, USA" - }, - { - "location": { - "latitude": 418465462, - "longitude": -746859398 - }, - "name": "" - }, - { - "location": { - "latitude": 411733222, - "longitude": -744228360 - }, - "name": "" - }, - { - "location": { - "latitude": 410248224, - "longitude": -747127767 - }, - "name": "3 Hasta Way, Newton, NJ 07860, USA" - } -] diff --git a/block-streamer/proto/route_guide.proto b/block-streamer/proto/route_guide.proto deleted file mode 100644 index 224f22024..000000000 --- a/block-streamer/proto/route_guide.proto +++ /dev/null @@ -1,106 +0,0 @@ -// Copyright 2015 gRPC authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -syntax = "proto3"; - -package routeguide; - -// Interface exported by the server. -service RouteGuide { - // A simple RPC. - // - // Obtains the feature at a given position. - // - // A feature with an empty name is returned if there's no feature at the given - // position. - rpc GetFeature(Point) returns (Feature) {} - - // A server-to-client streaming RPC. - // - // Obtains the Features available within the given Rectangle. Results are - // streamed rather than returned at once (e.g. in a response message with a - // repeated field), as the rectangle may cover a large area and contain a - // huge number of features. - rpc ListFeatures(Rectangle) returns (stream Feature) {} - - // A client-to-server streaming RPC. - // - // Accepts a stream of Points on a route being traversed, returning a - // RouteSummary when traversal is completed. - rpc RecordRoute(stream Point) returns (RouteSummary) {} - - // A Bidirectional streaming RPC. - // - // Accepts a stream of RouteNotes sent while a route is being traversed, - // while receiving other RouteNotes (e.g. from other users). - rpc RouteChat(stream RouteNote) returns (stream RouteNote) {} -} - -// Points are represented as latitude-longitude pairs in the E7 representation -// (degrees multiplied by 10**7 and rounded to the nearest integer). -// Latitudes should be in the range +/- 90 degrees and longitude should be in -// the range +/- 180 degrees (inclusive). -message Point { - int32 latitude = 1; - int32 longitude = 2; -} - -// A latitude-longitude rectangle, represented as two diagonally opposite -// points "lo" and "hi". -message Rectangle { - // One corner of the rectangle. - Point lo = 1; - - // The other corner of the rectangle. - Point hi = 2; -} - -// A feature names something at a given point. -// -// If a feature could not be named, the name is empty. -message Feature { - // The name of the feature. - string name = 1; - - // The point where the feature is detected. - Point location = 2; -} - -// A RouteNote is a message sent while at a given point. -message RouteNote { - // The location from which the message is sent. - Point location = 1; - - // The message to be sent. - string message = 2; -} - -// A RouteSummary is received in response to a RecordRoute rpc. -// -// It contains the number of individual points received, the number of -// detected features, and the total distance covered as the cumulative sum of -// the distance between each point. -message RouteSummary { - // The number of points received. - int32 point_count = 1; - - // The number of known features passed while traversing the route. - int32 feature_count = 2; - - // The distance covered in metres. - int32 distance = 3; - - // The duration of the traversal in seconds. - int32 elapsed_time = 4; -} diff --git a/block-streamer/src/data.rs b/block-streamer/src/data.rs deleted file mode 100644 index 86ee0969b..000000000 --- a/block-streamer/src/data.rs +++ /dev/null @@ -1,34 +0,0 @@ -use serde::Deserialize; -use std::fs::File; - -#[derive(Debug, Deserialize)] -struct Feature { - location: Location, - name: String, -} - -#[derive(Debug, Deserialize)] -struct Location { - latitude: i32, - longitude: i32, -} - -#[allow(dead_code)] -pub fn load() -> Vec { - let data_dir = std::path::PathBuf::from_iter([std::env!("CARGO_MANIFEST_DIR"), "data"]); - let file = File::open(data_dir.join("route_guide_db.json")).expect("failed to open data file"); - - let decoded: Vec = - serde_json::from_reader(&file).expect("failed to deserialize features"); - - decoded - .into_iter() - .map(|feature| crate::server::routeguide::Feature { - name: feature.name, - location: Some(crate::server::routeguide::Point { - longitude: feature.location.longitude, - latitude: feature.location.latitude, - }), - }) - .collect() -} diff --git a/block-streamer/src/main.rs b/block-streamer/src/main.rs index 29eeea714..aa32b5e9e 100644 --- a/block-streamer/src/main.rs +++ b/block-streamer/src/main.rs @@ -5,7 +5,6 @@ use tracing_subscriber::prelude::*; // use crate::rules::{IndexerRule, IndexerRuleKind, MatchingRule, Status}; mod block_stream; -mod data; mod delta_lake_client; mod indexer_config; mod redis; diff --git a/block-streamer/src/server/mod.rs b/block-streamer/src/server/mod.rs index 526d3a8bc..544482dda 100644 --- a/block-streamer/src/server/mod.rs +++ b/block-streamer/src/server/mod.rs @@ -1,9 +1,4 @@ mod block_streamer_service; -mod route_guide_service; - -pub mod routeguide { - tonic::include_proto!("routeguide"); -} pub mod blockstreamer { tonic::include_proto!("blockstreamer"); @@ -13,13 +8,11 @@ pub async fn init( redis_connection_manager: crate::redis::ConnectionManager, delta_lake_client: crate::delta_lake_client::DeltaLakeClient, ) -> anyhow::Result<()> { - let addr = "[::1]:10000".parse().unwrap(); - - println!("RouteGuideServer listening on: {}", addr); + let addr = "[::1]:10000" + .parse() + .expect("Failed to parse RPC socket address"); - let route_guide_service = route_guide_service::RouteGuideService::new(); - let route_guide_server = - routeguide::route_guide_server::RouteGuideServer::new(route_guide_service); + tracing::info!("Starting RPC server at {}", addr); let block_streamer_service = block_streamer_service::BlockStreamerService::new( redis_connection_manager, @@ -29,7 +22,6 @@ pub async fn init( blockstreamer::block_streamer_server::BlockStreamerServer::new(block_streamer_service); tonic::transport::Server::builder() - .add_service(route_guide_server) .add_service(block_streamer_server) .serve(addr) .await diff --git a/block-streamer/src/server/route_guide_service.rs b/block-streamer/src/server/route_guide_service.rs deleted file mode 100644 index dda352f89..000000000 --- a/block-streamer/src/server/route_guide_service.rs +++ /dev/null @@ -1,188 +0,0 @@ -use std::collections::HashMap; -use std::hash::{Hash, Hasher}; -use std::pin::Pin; -use std::sync::Arc; -use std::time::Instant; - -use tokio::sync::mpsc; -use tokio_stream::{wrappers::ReceiverStream, Stream, StreamExt}; -use tonic::{Request, Response, Status}; - -use crate::server::routeguide::route_guide_server::RouteGuide; -use crate::server::routeguide::{Feature, Point, Rectangle, RouteNote, RouteSummary}; - -#[derive(Debug)] -pub struct RouteGuideService { - pub features: Arc>, -} - -impl RouteGuideService { - pub fn new() -> Self { - Self { - features: Arc::new(crate::data::load()), - } - } -} - -#[tonic::async_trait] -impl RouteGuide for RouteGuideService { - async fn get_feature(&self, request: Request) -> Result, Status> { - println!("GetFeature = {:?}", request); - - for feature in &self.features[..] { - if feature.location.as_ref() == Some(request.get_ref()) { - return Ok(Response::new(feature.clone())); - } - } - - Ok(Response::new(Feature::default())) - } - - type ListFeaturesStream = ReceiverStream>; - - async fn list_features( - &self, - request: Request, - ) -> Result, Status> { - println!("ListFeatures = {:?}", request); - - let (tx, rx) = mpsc::channel(4); - let features = self.features.clone(); - - tokio::spawn(async move { - for feature in &features[..] { - if in_range(feature.location.as_ref().unwrap(), request.get_ref()) { - println!(" => send {:?}", feature); - tx.send(Ok(feature.clone())).await.unwrap(); - } - } - - println!(" /// done sending"); - }); - - Ok(Response::new(ReceiverStream::new(rx))) - } - - async fn record_route( - &self, - request: Request>, - ) -> Result, Status> { - println!("RecordRoute"); - - let mut stream = request.into_inner(); - - let mut summary = RouteSummary::default(); - let mut last_point = None; - let now = Instant::now(); - - while let Some(point) = stream.next().await { - let point = point?; - - println!(" ==> Point = {:?}", point); - - // Increment the point count - summary.point_count += 1; - - // Find features - for feature in &self.features[..] { - if feature.location.as_ref() == Some(&point) { - summary.feature_count += 1; - } - } - - // Calculate the distance - if let Some(ref last_point) = last_point { - summary.distance += calc_distance(last_point, &point); - } - - last_point = Some(point); - } - - summary.elapsed_time = now.elapsed().as_secs() as i32; - - Ok(Response::new(summary)) - } - - type RouteChatStream = Pin> + Send + 'static>>; - - async fn route_chat( - &self, - request: Request>, - ) -> Result, Status> { - println!("RouteChat"); - - let mut notes = HashMap::new(); - let mut stream = request.into_inner(); - - let output = async_stream::try_stream! { - while let Some(note) = stream.next().await { - let note = note?; - - let location = note.location.clone().unwrap(); - - let location_notes = notes.entry(location).or_insert(vec![]); - location_notes.push(note); - - for note in location_notes { - yield note.clone(); - } - } - }; - - Ok(Response::new(Box::pin(output) as Self::RouteChatStream)) - } -} - -impl Hash for Point { - fn hash(&self, state: &mut H) - where - H: Hasher, - { - self.latitude.hash(state); - self.longitude.hash(state); - } -} - -impl Eq for Point {} - -fn in_range(point: &Point, rect: &Rectangle) -> bool { - use std::cmp; - - let lo = rect.lo.as_ref().unwrap(); - let hi = rect.hi.as_ref().unwrap(); - - let left = cmp::min(lo.longitude, hi.longitude); - let right = cmp::max(lo.longitude, hi.longitude); - let top = cmp::max(lo.latitude, hi.latitude); - let bottom = cmp::min(lo.latitude, hi.latitude); - - point.longitude >= left - && point.longitude <= right - && point.latitude >= bottom - && point.latitude <= top -} - -/// Calculates the distance between two points using the "haversine" formula. -/// This code was taken from http://www.movable-type.co.uk/scripts/latlong.html. -fn calc_distance(p1: &Point, p2: &Point) -> i32 { - const CORD_FACTOR: f64 = 1e7; - const R: f64 = 6_371_000.0; // meters - - let lat1 = p1.latitude as f64 / CORD_FACTOR; - let lat2 = p2.latitude as f64 / CORD_FACTOR; - let lng1 = p1.longitude as f64 / CORD_FACTOR; - let lng2 = p2.longitude as f64 / CORD_FACTOR; - - let lat_rad1 = lat1.to_radians(); - let lat_rad2 = lat2.to_radians(); - - let delta_lat = (lat2 - lat1).to_radians(); - let delta_lng = (lng2 - lng1).to_radians(); - - let a = (delta_lat / 2f64).sin() * (delta_lat / 2f64).sin() - + (lat_rad1).cos() * (lat_rad2).cos() * (delta_lng / 2f64).sin() * (delta_lng / 2f64).sin(); - - let c = 2f64 * a.sqrt().atan2((1f64 - a).sqrt()); - - (R * c) as i32 -}