blob: 0b1a1cf48239c19ae4a5b81921bc65c5ae6b11c8 [file] [log] [blame]
Jorge Canizales34b26292015-06-02 02:11:09 -07001/*
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 Canizales2f0d11b2015-10-28 06:07:26 -070035#import <GRPCClient/GRPCCall+Tests.h>
Jorge Canizales34b26292015-06-02 02:11:09 -070036#import <RouteGuide/RouteGuide.pbrpc.h>
Jorge Canizales161b51e2015-07-17 17:21:04 -070037#import <RxLibrary/GRXWriter+Immediate.h>
38#import <RxLibrary/GRXWriter+Transformations.h>
Jorge Canizales34b26292015-06-02 02:11:09 -070039
Jorge Canizales8676d222015-10-28 05:51:11 -070040static NSString * const kHostAddress = @"localhost:50051";
Jorge Canizales34b26292015-06-02 02:11:09 -070041
Jorge Canizales7fb5b3c2015-10-28 05:52:11 -070042/** Category to override RTGPoint's description. */
Jorge Canizalesc9885952015-06-05 17:41:00 -070043@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 Canizales3fda5482015-06-05 12:05:00 -070056
Jorge Canizales7fb5b3c2015-10-28 05:52:11 -070057/** Category to give RTGRouteNote a convenience constructor. */
Jorge Canizales8d748b32015-06-05 17:43:40 -070058@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 Canizales77606b62015-06-05 17:55:34 -070076
77#pragma mark Demo: Get Feature
Jorge Canizales34b26292015-06-02 02:11:09 -070078
Jorge Canizales7fb5b3c2015-10-28 05:52:11 -070079/**
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 Canizales34b26292015-06-02 02:11:09 -070083@interface GetFeatureViewController : UIViewController
84@end
85
86@implementation GetFeatureViewController
87
88- (void)viewDidLoad {
89 [super viewDidLoad];
90
Jorge Canizales8676d222015-10-28 05:51:11 -070091 // This only needs to be done once per host, before creating service objects for that host.
92 [GRPCCall useInsecureConnectionsForHost:kHostAddress];
93
Jorge Canizales2f0d11b2015-10-28 06:07:26 -070094 RTGRouteGuide *service = [[RTGRouteGuide alloc] initWithHost:kHostAddress];
Jorge Canizales34b26292015-06-02 02:11:09 -070095
Jorge Canizales3aed7172015-06-04 22:56:21 -070096 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 Canizales34b26292015-06-02 02:11:09 -0700106 RTGPoint *point = [RTGPoint message];
107 point.latitude = 409146138;
108 point.longitude = -746188906;
109
Jorge Canizales8676d222015-10-28 05:51:11 -0700110 [service getFeatureWithRequest:point handler:handler];
111 [service getFeatureWithRequest:[RTGPoint message] handler:handler];
Jorge Canizales34b26292015-06-02 02:11:09 -0700112}
113
114@end
115
Jorge Canizales3fda5482015-06-05 12:05:00 -0700116
Jorge Canizales77606b62015-06-05 17:55:34 -0700117#pragma mark Demo: List Features
Jorge Canizales34b26292015-06-02 02:11:09 -0700118
Jorge Canizales7fb5b3c2015-10-28 05:52:11 -0700119/**
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 Canizales34b26292015-06-02 02:11:09 -0700123@interface ListFeaturesViewController : UIViewController
124@end
125
126@implementation ListFeaturesViewController
127
128- (void)viewDidLoad {
129 [super viewDidLoad];
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700130
Jorge Canizales2f0d11b2015-10-28 06:07:26 -0700131 RTGRouteGuide *service = [[RTGRouteGuide alloc] initWithHost:kHostAddress];
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700132
133 RTGRectangle *rectangle = [RTGRectangle message];
Jorge Canizalesc9885952015-06-05 17:41:00 -0700134 rectangle.lo.latitude = 405E6;
135 rectangle.lo.longitude = -750E6;
136 rectangle.hi.latitude = 410E6;
137 rectangle.hi.longitude = -745E6;
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700138
Jorge Canizalesc9885952015-06-05 17:41:00 -0700139 NSLog(@"Looking for features between %@ and %@", rectangle.lo, rectangle.hi);
Jorge Canizales8676d222015-10-28 05:51:11 -0700140 [service listFeaturesWithRequest:rectangle
141 eventHandler:^(BOOL done, RTGFeature *response, NSError *error) {
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700142 if (response) {
Jorge Canizalesc9885952015-06-05 17:41:00 -0700143 NSLog(@"Found feature at %@ called %@.", response.location, response.name);
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700144 } else if (error) {
145 NSLog(@"RPC error: %@", error);
146 }
147 }];
Jorge Canizales34b26292015-06-02 02:11:09 -0700148}
149
150@end
151
Jorge Canizales3fda5482015-06-05 12:05:00 -0700152
Jorge Canizales77606b62015-06-05 17:55:34 -0700153#pragma mark Demo: Record Route
Jorge Canizales34b26292015-06-02 02:11:09 -0700154
Jorge Canizales7fb5b3c2015-10-28 05:52:11 -0700155/**
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 Canizales34b26292015-06-02 02:11:09 -0700160@interface RecordRouteViewController : UIViewController
161@end
162
163@implementation RecordRouteViewController
164
165- (void)viewDidLoad {
166 [super viewDidLoad];
Jorge Canizales59fdf712015-06-05 17:42:41 -0700167
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 Canizales2f0d11b2015-10-28 06:07:26 -0700181 RTGRouteGuide *service = [[RTGRouteGuide alloc] initWithHost:kHostAddress];
Jorge Canizales59fdf712015-06-05 17:42:41 -0700182
Jorge Canizales8676d222015-10-28 05:51:11 -0700183 [service recordRouteWithRequestsWriter:locations
184 handler:^(RTGRouteSummary *response, NSError *error) {
Jorge Canizales59fdf712015-06-05 17:42:41 -0700185 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 Canizales34b26292015-06-02 02:11:09 -0700194}
195
196@end
Jorge Canizales8d748b32015-06-05 17:43:40 -0700197
198
Jorge Canizales77606b62015-06-05 17:55:34 -0700199#pragma mark Demo: Route Chat
Jorge Canizales8d748b32015-06-05 17:43:40 -0700200
Jorge Canizales7fb5b3c2015-10-28 05:52:11 -0700201/**
202 * Run the routeChat demo. Send some chat messages, and print any chat messages that are sent from
203 * the server.
204 */
Jorge Canizales8d748b32015-06-05 17:43:40 -0700205@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 Canizales2f0d11b2015-10-28 06:07:26 -0700222 RTGRouteGuide *service = [[RTGRouteGuide alloc] initWithHost:kHostAddress];
Jorge Canizales8d748b32015-06-05 17:43:40 -0700223
Jorge Canizales8676d222015-10-28 05:51:11 -0700224 [service routeChatWithRequestsWriter:notesWriter
225 eventHandler:^(BOOL done, RTGRouteNote *note, NSError *error) {
Jorge Canizales8d748b32015-06-05 17:43:40 -0700226 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