Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #import <UIKit/UIKit.h> |
Jorge Canizales | 59fdf71 | 2015-06-05 17:42:41 -0700 | [diff] [blame^] | 35 | #import <gRPC/GRXWriter+Immediate.h> |
| 36 | #import <gRPC/GRXWriter+Transformations.h> |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 37 | #import <RouteGuide/RouteGuide.pbrpc.h> |
| 38 | |
| 39 | static NSString * const kHostAddress = @"http://localhost:50051"; |
| 40 | |
Jorge Canizales | c988595 | 2015-06-05 17:41:00 -0700 | [diff] [blame] | 41 | // Category to override RTGPoint's description. |
| 42 | @interface RTGPoint (Description) |
| 43 | - (NSString *)description; |
| 44 | @end |
| 45 | |
| 46 | @implementation RTGPoint (Description) |
| 47 | - (NSString *)description { |
| 48 | NSString *verticalDirection = self.latitude >= 0 ? @"N" : @"S"; |
| 49 | NSString *horizontalDirection = self.longitude >= 0 ? @"E" : @"W"; |
| 50 | return [NSString stringWithFormat:@"%.02f%@ %.02f%@", |
| 51 | abs(self.latitude) / 1E7f, verticalDirection, |
| 52 | abs(self.longitude) / 1E7f, horizontalDirection]; |
| 53 | } |
| 54 | @end |
Jorge Canizales | 3fda548 | 2015-06-05 12:05:00 -0700 | [diff] [blame] | 55 | |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 56 | #pragma mark Get Feature |
| 57 | |
Jorge Canizales | 3aed717 | 2015-06-04 22:56:21 -0700 | [diff] [blame] | 58 | // Run the getFeature demo. Calls getFeature with a point known to have a feature and a point known |
| 59 | // not to have a feature. |
| 60 | |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 61 | @interface GetFeatureViewController : UIViewController |
| 62 | @end |
| 63 | |
| 64 | @implementation GetFeatureViewController |
| 65 | |
| 66 | - (void)viewDidLoad { |
| 67 | [super viewDidLoad]; |
| 68 | |
| 69 | RTGRouteGuide *client = [[RTGRouteGuide alloc] initWithHost:kHostAddress]; |
| 70 | |
Jorge Canizales | 3aed717 | 2015-06-04 22:56:21 -0700 | [diff] [blame] | 71 | void (^handler)(RTGFeature *response, NSError *error) = ^(RTGFeature *response, NSError *error) { |
| 72 | if (response.name.length) { |
| 73 | NSLog(@"Found feature called %@ at %@.", response.name, response.location); |
| 74 | } else if (response) { |
| 75 | NSLog(@"Found no features at %@", response.location); |
| 76 | } else { |
| 77 | NSLog(@"RPC error: %@", error); |
| 78 | } |
| 79 | }; |
| 80 | |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 81 | RTGPoint *point = [RTGPoint message]; |
| 82 | point.latitude = 409146138; |
| 83 | point.longitude = -746188906; |
| 84 | |
Jorge Canizales | 3aed717 | 2015-06-04 22:56:21 -0700 | [diff] [blame] | 85 | [client getFeatureWithRequest:point handler:handler]; |
| 86 | [client getFeatureWithRequest:[RTGPoint message] handler:handler]; |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @end |
| 90 | |
Jorge Canizales | 3fda548 | 2015-06-05 12:05:00 -0700 | [diff] [blame] | 91 | |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 92 | #pragma mark List Features |
| 93 | |
Jorge Canizales | 4694cdb | 2015-06-04 23:06:13 -0700 | [diff] [blame] | 94 | // Run the listFeatures demo. Calls listFeatures with a rectangle containing all of the features in |
| 95 | // the pre-generated database. Prints each response as it comes in. |
| 96 | |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 97 | @interface ListFeaturesViewController : UIViewController |
| 98 | @end |
| 99 | |
| 100 | @implementation ListFeaturesViewController |
| 101 | |
| 102 | - (void)viewDidLoad { |
| 103 | [super viewDidLoad]; |
Jorge Canizales | 4694cdb | 2015-06-04 23:06:13 -0700 | [diff] [blame] | 104 | |
| 105 | RTGRouteGuide *client = [[RTGRouteGuide alloc] initWithHost:kHostAddress]; |
| 106 | |
| 107 | RTGRectangle *rectangle = [RTGRectangle message]; |
Jorge Canizales | c988595 | 2015-06-05 17:41:00 -0700 | [diff] [blame] | 108 | rectangle.lo.latitude = 405E6; |
| 109 | rectangle.lo.longitude = -750E6; |
| 110 | rectangle.hi.latitude = 410E6; |
| 111 | rectangle.hi.longitude = -745E6; |
Jorge Canizales | 4694cdb | 2015-06-04 23:06:13 -0700 | [diff] [blame] | 112 | |
Jorge Canizales | c988595 | 2015-06-05 17:41:00 -0700 | [diff] [blame] | 113 | NSLog(@"Looking for features between %@ and %@", rectangle.lo, rectangle.hi); |
Jorge Canizales | 4694cdb | 2015-06-04 23:06:13 -0700 | [diff] [blame] | 114 | [client listFeaturesWithRequest:rectangle handler:^(BOOL done, RTGFeature *response, NSError *error) { |
| 115 | if (response) { |
Jorge Canizales | c988595 | 2015-06-05 17:41:00 -0700 | [diff] [blame] | 116 | NSLog(@"Found feature at %@ called %@.", response.location, response.name); |
Jorge Canizales | 4694cdb | 2015-06-04 23:06:13 -0700 | [diff] [blame] | 117 | } else if (error) { |
| 118 | NSLog(@"RPC error: %@", error); |
| 119 | } |
| 120 | }]; |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @end |
| 124 | |
Jorge Canizales | 3fda548 | 2015-06-05 12:05:00 -0700 | [diff] [blame] | 125 | |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 126 | #pragma mark Record Route |
| 127 | |
Jorge Canizales | 59fdf71 | 2015-06-05 17:42:41 -0700 | [diff] [blame^] | 128 | // Run the recordRoute demo. Sends several randomly chosen points from the pre-generated feature |
| 129 | // database with a variable delay in between. Prints the statistics when they are sent from the |
| 130 | // server. |
| 131 | |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 132 | @interface RecordRouteViewController : UIViewController |
| 133 | @end |
| 134 | |
| 135 | @implementation RecordRouteViewController |
| 136 | |
| 137 | - (void)viewDidLoad { |
| 138 | [super viewDidLoad]; |
| 139 | // Do any additional setup after loading the view. |
| 140 | } |
| 141 | |
| 142 | @end |
| 143 | |
Jorge Canizales | 3fda548 | 2015-06-05 12:05:00 -0700 | [diff] [blame] | 144 | |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 145 | #pragma mark Route Chat |
| 146 | |
| 147 | @interface RouteChatViewController : UIViewController |
| 148 | @end |
| 149 | |
| 150 | @implementation RouteChatViewController |
| 151 | |
| 152 | - (void)viewDidLoad { |
| 153 | [super viewDidLoad]; |
Jorge Canizales | 59fdf71 | 2015-06-05 17:42:41 -0700 | [diff] [blame^] | 154 | |
| 155 | NSString *dataBasePath = [NSBundle.mainBundle pathForResource:@"route_guide_db" |
| 156 | ofType:@"json"]; |
| 157 | NSData *dataBaseContent = [NSData dataWithContentsOfFile:dataBasePath]; |
| 158 | NSArray *features = [NSJSONSerialization JSONObjectWithData:dataBaseContent options:0 error:NULL]; |
| 159 | |
| 160 | GRXWriter *locations = [[GRXWriter writerWithContainer:features] map:^id(id feature) { |
| 161 | RTGPoint *location = [RTGPoint message]; |
| 162 | location.longitude = [((NSNumber *) feature[@"location"][@"longitude"]) intValue]; |
| 163 | location.latitude = [((NSNumber *) feature[@"location"][@"latitude"]) intValue]; |
| 164 | NSLog(@"Visiting point %@", location); |
| 165 | return location; |
| 166 | }]; |
| 167 | |
| 168 | RTGRouteGuide *client = [[RTGRouteGuide alloc] initWithHost:kHostAddress]; |
| 169 | |
| 170 | [client recordRouteWithRequestsWriter:locations handler:^(RTGRouteSummary *response, NSError *error) { |
| 171 | if (response) { |
| 172 | NSLog(@"Finished trip with %i points", response.pointCount); |
| 173 | NSLog(@"Passed %i features", response.featureCount); |
| 174 | NSLog(@"Travelled %i meters", response.distance); |
| 175 | NSLog(@"It took %i seconds", response.elapsedTime); |
| 176 | } else { |
| 177 | NSLog(@"RPC error: %@", error); |
| 178 | } |
| 179 | }]; |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @end |