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 | 2f0d11b | 2015-10-28 06:07:26 -0700 | [diff] [blame] | 35 | #import <GRPCClient/GRPCCall+Tests.h> |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 36 | #import <RouteGuide/RouteGuide.pbrpc.h> |
Jorge Canizales | 161b51e | 2015-07-17 17:21:04 -0700 | [diff] [blame] | 37 | #import <RxLibrary/GRXWriter+Immediate.h> |
| 38 | #import <RxLibrary/GRXWriter+Transformations.h> |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 39 | |
Jorge Canizales | 8676d22 | 2015-10-28 05:51:11 -0700 | [diff] [blame] | 40 | static NSString * const kHostAddress = @"localhost:50051"; |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 41 | |
Jorge Canizales | 7fb5b3c | 2015-10-28 05:52:11 -0700 | [diff] [blame] | 42 | /** Category to override RTGPoint's description. */ |
Jorge Canizales | c988595 | 2015-06-05 17:41:00 -0700 | [diff] [blame] | 43 | @interface RTGPoint (Description) |
| 44 | - (NSString *)description; |
| 45 | @end |
| 46 | |
| 47 | @implementation RTGPoint (Description) |
| 48 | - (NSString *)description { |
| 49 | NSString *verticalDirection = self.latitude >= 0 ? @"N" : @"S"; |
| 50 | NSString *horizontalDirection = self.longitude >= 0 ? @"E" : @"W"; |
| 51 | return [NSString stringWithFormat:@"%.02f%@ %.02f%@", |
| 52 | abs(self.latitude) / 1E7f, verticalDirection, |
| 53 | abs(self.longitude) / 1E7f, horizontalDirection]; |
| 54 | } |
| 55 | @end |
Jorge Canizales | 3fda548 | 2015-06-05 12:05:00 -0700 | [diff] [blame] | 56 | |
Jorge Canizales | 7fb5b3c | 2015-10-28 05:52:11 -0700 | [diff] [blame] | 57 | /** Category to give RTGRouteNote a convenience constructor. */ |
Jorge Canizales | 8d748b3 | 2015-06-05 17:43:40 -0700 | [diff] [blame] | 58 | @interface RTGRouteNote (Constructors) |
| 59 | + (instancetype)noteWithMessage:(NSString *)message |
| 60 | latitude:(float)latitude |
| 61 | longitude:(float)longitude; |
| 62 | @end |
| 63 | |
| 64 | @implementation RTGRouteNote (Constructors) |
| 65 | + (instancetype)noteWithMessage:(NSString *)message |
| 66 | latitude:(float)latitude |
| 67 | longitude:(float)longitude { |
| 68 | RTGRouteNote *note = [self message]; |
| 69 | note.message = message; |
| 70 | note.location.latitude = (int32_t) latitude * 1E7; |
| 71 | note.location.longitude = (int32_t) longitude * 1E7; |
| 72 | return note; |
| 73 | } |
| 74 | @end |
| 75 | |
Jorge Canizales | 77606b6 | 2015-06-05 17:55:34 -0700 | [diff] [blame] | 76 | |
| 77 | #pragma mark Demo: Get Feature |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 78 | |
Jorge Canizales | 7fb5b3c | 2015-10-28 05:52:11 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Run the getFeature demo. Calls getFeature with a point known to have a feature and a point known |
| 81 | * not to have a feature. |
| 82 | */ |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 83 | @interface GetFeatureViewController : UIViewController |
| 84 | @end |
| 85 | |
| 86 | @implementation GetFeatureViewController |
| 87 | |
| 88 | - (void)viewDidLoad { |
| 89 | [super viewDidLoad]; |
| 90 | |
Jorge Canizales | 8676d22 | 2015-10-28 05:51:11 -0700 | [diff] [blame] | 91 | // This only needs to be done once per host, before creating service objects for that host. |
| 92 | [GRPCCall useInsecureConnectionsForHost:kHostAddress]; |
| 93 | |
Jorge Canizales | 2f0d11b | 2015-10-28 06:07:26 -0700 | [diff] [blame] | 94 | RTGRouteGuide *service = [[RTGRouteGuide alloc] initWithHost:kHostAddress]; |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 95 | |
Jorge Canizales | 3aed717 | 2015-06-04 22:56:21 -0700 | [diff] [blame] | 96 | void (^handler)(RTGFeature *response, NSError *error) = ^(RTGFeature *response, NSError *error) { |
| 97 | if (response.name.length) { |
| 98 | NSLog(@"Found feature called %@ at %@.", response.name, response.location); |
| 99 | } else if (response) { |
| 100 | NSLog(@"Found no features at %@", response.location); |
| 101 | } else { |
| 102 | NSLog(@"RPC error: %@", error); |
| 103 | } |
| 104 | }; |
| 105 | |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 106 | RTGPoint *point = [RTGPoint message]; |
| 107 | point.latitude = 409146138; |
| 108 | point.longitude = -746188906; |
| 109 | |
Jorge Canizales | 8676d22 | 2015-10-28 05:51:11 -0700 | [diff] [blame] | 110 | [service getFeatureWithRequest:point handler:handler]; |
| 111 | [service getFeatureWithRequest:[RTGPoint message] handler:handler]; |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @end |
| 115 | |
Jorge Canizales | 3fda548 | 2015-06-05 12:05:00 -0700 | [diff] [blame] | 116 | |
Jorge Canizales | 77606b6 | 2015-06-05 17:55:34 -0700 | [diff] [blame] | 117 | #pragma mark Demo: List Features |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 118 | |
Jorge Canizales | 7fb5b3c | 2015-10-28 05:52:11 -0700 | [diff] [blame] | 119 | /** |
| 120 | * Run the listFeatures demo. Calls listFeatures with a rectangle containing all of the features in |
| 121 | * the pre-generated database. Prints each response as it comes in. |
| 122 | */ |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 123 | @interface ListFeaturesViewController : UIViewController |
| 124 | @end |
| 125 | |
| 126 | @implementation ListFeaturesViewController |
| 127 | |
| 128 | - (void)viewDidLoad { |
| 129 | [super viewDidLoad]; |
Jorge Canizales | 4694cdb | 2015-06-04 23:06:13 -0700 | [diff] [blame] | 130 | |
Jorge Canizales | 2f0d11b | 2015-10-28 06:07:26 -0700 | [diff] [blame] | 131 | RTGRouteGuide *service = [[RTGRouteGuide alloc] initWithHost:kHostAddress]; |
Jorge Canizales | 4694cdb | 2015-06-04 23:06:13 -0700 | [diff] [blame] | 132 | |
| 133 | RTGRectangle *rectangle = [RTGRectangle message]; |
Jorge Canizales | c988595 | 2015-06-05 17:41:00 -0700 | [diff] [blame] | 134 | rectangle.lo.latitude = 405E6; |
| 135 | rectangle.lo.longitude = -750E6; |
| 136 | rectangle.hi.latitude = 410E6; |
| 137 | rectangle.hi.longitude = -745E6; |
Jorge Canizales | 4694cdb | 2015-06-04 23:06:13 -0700 | [diff] [blame] | 138 | |
Jorge Canizales | c988595 | 2015-06-05 17:41:00 -0700 | [diff] [blame] | 139 | NSLog(@"Looking for features between %@ and %@", rectangle.lo, rectangle.hi); |
Jorge Canizales | 8676d22 | 2015-10-28 05:51:11 -0700 | [diff] [blame] | 140 | [service listFeaturesWithRequest:rectangle |
| 141 | eventHandler:^(BOOL done, RTGFeature *response, NSError *error) { |
Jorge Canizales | 4694cdb | 2015-06-04 23:06:13 -0700 | [diff] [blame] | 142 | if (response) { |
Jorge Canizales | c988595 | 2015-06-05 17:41:00 -0700 | [diff] [blame] | 143 | NSLog(@"Found feature at %@ called %@.", response.location, response.name); |
Jorge Canizales | 4694cdb | 2015-06-04 23:06:13 -0700 | [diff] [blame] | 144 | } else if (error) { |
| 145 | NSLog(@"RPC error: %@", error); |
| 146 | } |
| 147 | }]; |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @end |
| 151 | |
Jorge Canizales | 3fda548 | 2015-06-05 12:05:00 -0700 | [diff] [blame] | 152 | |
Jorge Canizales | 77606b6 | 2015-06-05 17:55:34 -0700 | [diff] [blame] | 153 | #pragma mark Demo: Record Route |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 154 | |
Jorge Canizales | 7fb5b3c | 2015-10-28 05:52:11 -0700 | [diff] [blame] | 155 | /** |
| 156 | * Run the recordRoute demo. Sends several randomly chosen points from the pre-generated feature |
| 157 | * database with a variable delay in between. Prints the statistics when they are sent from the |
| 158 | * server. |
| 159 | */ |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 160 | @interface RecordRouteViewController : UIViewController |
| 161 | @end |
| 162 | |
| 163 | @implementation RecordRouteViewController |
| 164 | |
| 165 | - (void)viewDidLoad { |
| 166 | [super viewDidLoad]; |
Jorge Canizales | 59fdf71 | 2015-06-05 17:42:41 -0700 | [diff] [blame] | 167 | |
| 168 | NSString *dataBasePath = [NSBundle.mainBundle pathForResource:@"route_guide_db" |
| 169 | ofType:@"json"]; |
| 170 | NSData *dataBaseContent = [NSData dataWithContentsOfFile:dataBasePath]; |
| 171 | NSArray *features = [NSJSONSerialization JSONObjectWithData:dataBaseContent options:0 error:NULL]; |
| 172 | |
| 173 | GRXWriter *locations = [[GRXWriter writerWithContainer:features] map:^id(id feature) { |
| 174 | RTGPoint *location = [RTGPoint message]; |
| 175 | location.longitude = [((NSNumber *) feature[@"location"][@"longitude"]) intValue]; |
| 176 | location.latitude = [((NSNumber *) feature[@"location"][@"latitude"]) intValue]; |
| 177 | NSLog(@"Visiting point %@", location); |
| 178 | return location; |
| 179 | }]; |
| 180 | |
Jorge Canizales | 2f0d11b | 2015-10-28 06:07:26 -0700 | [diff] [blame] | 181 | RTGRouteGuide *service = [[RTGRouteGuide alloc] initWithHost:kHostAddress]; |
Jorge Canizales | 59fdf71 | 2015-06-05 17:42:41 -0700 | [diff] [blame] | 182 | |
Jorge Canizales | 8676d22 | 2015-10-28 05:51:11 -0700 | [diff] [blame] | 183 | [service recordRouteWithRequestsWriter:locations |
| 184 | handler:^(RTGRouteSummary *response, NSError *error) { |
Jorge Canizales | 59fdf71 | 2015-06-05 17:42:41 -0700 | [diff] [blame] | 185 | if (response) { |
| 186 | NSLog(@"Finished trip with %i points", response.pointCount); |
| 187 | NSLog(@"Passed %i features", response.featureCount); |
| 188 | NSLog(@"Travelled %i meters", response.distance); |
| 189 | NSLog(@"It took %i seconds", response.elapsedTime); |
| 190 | } else { |
| 191 | NSLog(@"RPC error: %@", error); |
| 192 | } |
| 193 | }]; |
Jorge Canizales | 34b2629 | 2015-06-02 02:11:09 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @end |
Jorge Canizales | 8d748b3 | 2015-06-05 17:43:40 -0700 | [diff] [blame] | 197 | |
| 198 | |
Jorge Canizales | 77606b6 | 2015-06-05 17:55:34 -0700 | [diff] [blame] | 199 | #pragma mark Demo: Route Chat |
Jorge Canizales | 8d748b3 | 2015-06-05 17:43:40 -0700 | [diff] [blame] | 200 | |
Jorge Canizales | 7fb5b3c | 2015-10-28 05:52:11 -0700 | [diff] [blame] | 201 | /** |
| 202 | * Run the routeChat demo. Send some chat messages, and print any chat messages that are sent from |
| 203 | * the server. |
| 204 | */ |
Jorge Canizales | 8d748b3 | 2015-06-05 17:43:40 -0700 | [diff] [blame] | 205 | @interface RouteChatViewController : UIViewController |
| 206 | @end |
| 207 | |
| 208 | @implementation RouteChatViewController |
| 209 | |
| 210 | - (void)viewDidLoad { |
| 211 | [super viewDidLoad]; |
| 212 | |
| 213 | NSArray *notes = @[[RTGRouteNote noteWithMessage:@"First message" latitude:0 longitude:0], |
| 214 | [RTGRouteNote noteWithMessage:@"Second message" latitude:0 longitude:1], |
| 215 | [RTGRouteNote noteWithMessage:@"Third message" latitude:1 longitude:0], |
| 216 | [RTGRouteNote noteWithMessage:@"Fourth message" latitude:0 longitude:0]]; |
| 217 | GRXWriter *notesWriter = [[GRXWriter writerWithContainer:notes] map:^id(RTGRouteNote *note) { |
| 218 | NSLog(@"Sending message %@ at %@", note.message, note.location); |
| 219 | return note; |
| 220 | }]; |
| 221 | |
Jorge Canizales | 2f0d11b | 2015-10-28 06:07:26 -0700 | [diff] [blame] | 222 | RTGRouteGuide *service = [[RTGRouteGuide alloc] initWithHost:kHostAddress]; |
Jorge Canizales | 8d748b3 | 2015-06-05 17:43:40 -0700 | [diff] [blame] | 223 | |
Jorge Canizales | 8676d22 | 2015-10-28 05:51:11 -0700 | [diff] [blame] | 224 | [service routeChatWithRequestsWriter:notesWriter |
| 225 | eventHandler:^(BOOL done, RTGRouteNote *note, NSError *error) { |
Jorge Canizales | 8d748b3 | 2015-06-05 17:43:40 -0700 | [diff] [blame] | 226 | if (note) { |
| 227 | NSLog(@"Got message %@ at %@", note.message, note.location); |
| 228 | } else if (error) { |
| 229 | NSLog(@"RPC error: %@", error); |
| 230 | } |
| 231 | if (done) { |
| 232 | NSLog(@"Chat ended."); |
| 233 | } |
| 234 | }]; |
| 235 | } |
| 236 | |
| 237 | @end |