OLD | NEW |
(Empty) | |
| 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 <GRPCClient/GRPCCall+Tests.h> |
| 36 #import <RouteGuide/RouteGuide.pbrpc.h> |
| 37 #import <RxLibrary/GRXWriter+Immediate.h> |
| 38 #import <RxLibrary/GRXWriter+Transformations.h> |
| 39 |
| 40 static NSString * const kHostAddress = @"localhost:50051"; |
| 41 |
| 42 /** Category to override RTGPoint's description. */ |
| 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 |
| 56 |
| 57 /** Category to give RTGRouteNote a convenience constructor. */ |
| 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 |
| 76 |
| 77 #pragma mark Demo: Get Feature |
| 78 |
| 79 /** |
| 80 * Run the getFeature demo. Calls getFeature with a point known to have a featur
e and a point known |
| 81 * not to have a feature. |
| 82 */ |
| 83 @interface GetFeatureViewController : UIViewController |
| 84 @end |
| 85 |
| 86 @implementation GetFeatureViewController |
| 87 |
| 88 - (void)viewDidLoad { |
| 89 [super viewDidLoad]; |
| 90 |
| 91 // This only needs to be done once per host, before creating service objects f
or that host. |
| 92 [GRPCCall useInsecureConnectionsForHost:kHostAddress]; |
| 93 |
| 94 RTGRouteGuide *service = [[RTGRouteGuide alloc] initWithHost:kHostAddress]; |
| 95 |
| 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 |
| 106 RTGPoint *point = [RTGPoint message]; |
| 107 point.latitude = 409146138; |
| 108 point.longitude = -746188906; |
| 109 |
| 110 [service getFeatureWithRequest:point handler:handler]; |
| 111 [service getFeatureWithRequest:[RTGPoint message] handler:handler]; |
| 112 } |
| 113 |
| 114 @end |
| 115 |
| 116 |
| 117 #pragma mark Demo: List Features |
| 118 |
| 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 */ |
| 123 @interface ListFeaturesViewController : UIViewController |
| 124 @end |
| 125 |
| 126 @implementation ListFeaturesViewController |
| 127 |
| 128 - (void)viewDidLoad { |
| 129 [super viewDidLoad]; |
| 130 |
| 131 RTGRouteGuide *service = [[RTGRouteGuide alloc] initWithHost:kHostAddress]; |
| 132 |
| 133 RTGRectangle *rectangle = [RTGRectangle message]; |
| 134 rectangle.lo.latitude = 405E6; |
| 135 rectangle.lo.longitude = -750E6; |
| 136 rectangle.hi.latitude = 410E6; |
| 137 rectangle.hi.longitude = -745E6; |
| 138 |
| 139 NSLog(@"Looking for features between %@ and %@", rectangle.lo, rectangle.hi); |
| 140 [service listFeaturesWithRequest:rectangle |
| 141 eventHandler:^(BOOL done, RTGFeature *response, NSError *e
rror) { |
| 142 if (response) { |
| 143 NSLog(@"Found feature at %@ called %@.", response.location, response.name)
; |
| 144 } else if (error) { |
| 145 NSLog(@"RPC error: %@", error); |
| 146 } |
| 147 }]; |
| 148 } |
| 149 |
| 150 @end |
| 151 |
| 152 |
| 153 #pragma mark Demo: Record Route |
| 154 |
| 155 /** |
| 156 * Run the recordRoute demo. Sends several randomly chosen points from the pre-g
enerated feature |
| 157 * database with a variable delay in between. Prints the statistics when they ar
e sent from the |
| 158 * server. |
| 159 */ |
| 160 @interface RecordRouteViewController : UIViewController |
| 161 @end |
| 162 |
| 163 @implementation RecordRouteViewController |
| 164 |
| 165 - (void)viewDidLoad { |
| 166 [super viewDidLoad]; |
| 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 op
tions:0 error:NULL]; |
| 172 |
| 173 GRXWriter *locations = [[GRXWriter writerWithContainer:features] map:^id(id fe
ature) { |
| 174 RTGPoint *location = [RTGPoint message]; |
| 175 location.longitude = [((NSNumber *) feature[@"location"][@"longitude"]) intV
alue]; |
| 176 location.latitude = [((NSNumber *) feature[@"location"][@"latitude"]) intVal
ue]; |
| 177 NSLog(@"Visiting point %@", location); |
| 178 return location; |
| 179 }]; |
| 180 |
| 181 RTGRouteGuide *service = [[RTGRouteGuide alloc] initWithHost:kHostAddress]; |
| 182 |
| 183 [service recordRouteWithRequestsWriter:locations |
| 184 handler:^(RTGRouteSummary *response, NSError *e
rror) { |
| 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 }]; |
| 194 } |
| 195 |
| 196 @end |
| 197 |
| 198 |
| 199 #pragma mark Demo: Route Chat |
| 200 |
| 201 /** |
| 202 * Run the routeChat demo. Send some chat messages, and print any chat messages
that are sent from |
| 203 * the server. |
| 204 */ |
| 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 l
ongitude:0], |
| 214 [RTGRouteNote noteWithMessage:@"Second message" latitude:0
longitude:1], |
| 215 [RTGRouteNote noteWithMessage:@"Third message" latitude:1 l
ongitude:0], |
| 216 [RTGRouteNote noteWithMessage:@"Fourth message" latitude:0
longitude:0]]; |
| 217 GRXWriter *notesWriter = [[GRXWriter writerWithContainer:notes] map:^id(RTGRou
teNote *note) { |
| 218 NSLog(@"Sending message %@ at %@", note.message, note.location); |
| 219 return note; |
| 220 }]; |
| 221 |
| 222 RTGRouteGuide *service = [[RTGRouteGuide alloc] initWithHost:kHostAddress]; |
| 223 |
| 224 [service routeChatWithRequestsWriter:notesWriter |
| 225 eventHandler:^(BOOL done, RTGRouteNote *note, NSError
*error) { |
| 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 |
OLD | NEW |