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