blob: cfc3338bcacc3501cc4462494ff2746bb9ff6504 [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>
35#import <RouteGuide/RouteGuide.pbrpc.h>
Jorge Canizales161b51e2015-07-17 17:21:04 -070036#import <RxLibrary/GRXWriter+Immediate.h>
37#import <RxLibrary/GRXWriter+Transformations.h>
Jorge Canizales34b26292015-06-02 02:11:09 -070038
39static NSString * const kHostAddress = @"http://localhost:50051";
40
Jorge Canizalesc9885952015-06-05 17:41:00 -070041// 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 Canizales3fda5482015-06-05 12:05:00 -070055
Jorge Canizales8d748b32015-06-05 17:43:40 -070056// 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 Canizales77606b62015-06-05 17:55:34 -070075
76#pragma mark Demo: Get Feature
Jorge Canizales34b26292015-06-02 02:11:09 -070077
Jorge Canizales3aed7172015-06-04 22:56:21 -070078// 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 Canizales34b26292015-06-02 02:11:09 -070081@interface GetFeatureViewController : UIViewController
82@end
83
84@implementation GetFeatureViewController
85
86- (void)viewDidLoad {
87 [super viewDidLoad];
88
89 RTGRouteGuide *client = [[RTGRouteGuide alloc] initWithHost:kHostAddress];
90
Jorge Canizales3aed7172015-06-04 22:56:21 -070091 void (^handler)(RTGFeature *response, NSError *error) = ^(RTGFeature *response, NSError *error) {
92 if (response.name.length) {
93 NSLog(@"Found feature called %@ at %@.", response.name, response.location);
94 } else if (response) {
95 NSLog(@"Found no features at %@", response.location);
96 } else {
97 NSLog(@"RPC error: %@", error);
98 }
99 };
100
Jorge Canizales34b26292015-06-02 02:11:09 -0700101 RTGPoint *point = [RTGPoint message];
102 point.latitude = 409146138;
103 point.longitude = -746188906;
104
Jorge Canizales3aed7172015-06-04 22:56:21 -0700105 [client getFeatureWithRequest:point handler:handler];
106 [client getFeatureWithRequest:[RTGPoint message] handler:handler];
Jorge Canizales34b26292015-06-02 02:11:09 -0700107}
108
109@end
110
Jorge Canizales3fda5482015-06-05 12:05:00 -0700111
Jorge Canizales77606b62015-06-05 17:55:34 -0700112#pragma mark Demo: List Features
Jorge Canizales34b26292015-06-02 02:11:09 -0700113
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700114// Run the listFeatures demo. Calls listFeatures with a rectangle containing all of the features in
115// the pre-generated database. Prints each response as it comes in.
116
Jorge Canizales34b26292015-06-02 02:11:09 -0700117@interface ListFeaturesViewController : UIViewController
118@end
119
120@implementation ListFeaturesViewController
121
122- (void)viewDidLoad {
123 [super viewDidLoad];
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700124
125 RTGRouteGuide *client = [[RTGRouteGuide alloc] initWithHost:kHostAddress];
126
127 RTGRectangle *rectangle = [RTGRectangle message];
Jorge Canizalesc9885952015-06-05 17:41:00 -0700128 rectangle.lo.latitude = 405E6;
129 rectangle.lo.longitude = -750E6;
130 rectangle.hi.latitude = 410E6;
131 rectangle.hi.longitude = -745E6;
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700132
Jorge Canizalesc9885952015-06-05 17:41:00 -0700133 NSLog(@"Looking for features between %@ and %@", rectangle.lo, rectangle.hi);
Jorge Canizales161b51e2015-07-17 17:21:04 -0700134 [client listFeaturesWithRequest:rectangle
135 eventHandler:^(BOOL done, RTGFeature *response, NSError *error) {
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700136 if (response) {
Jorge Canizalesc9885952015-06-05 17:41:00 -0700137 NSLog(@"Found feature at %@ called %@.", response.location, response.name);
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700138 } else if (error) {
139 NSLog(@"RPC error: %@", error);
140 }
141 }];
Jorge Canizales34b26292015-06-02 02:11:09 -0700142}
143
144@end
145
Jorge Canizales3fda5482015-06-05 12:05:00 -0700146
Jorge Canizales77606b62015-06-05 17:55:34 -0700147#pragma mark Demo: Record Route
Jorge Canizales34b26292015-06-02 02:11:09 -0700148
Jorge Canizales59fdf712015-06-05 17:42:41 -0700149// Run the recordRoute demo. Sends several randomly chosen points from the pre-generated feature
150// database with a variable delay in between. Prints the statistics when they are sent from the
151// server.
152
Jorge Canizales34b26292015-06-02 02:11:09 -0700153@interface RecordRouteViewController : UIViewController
154@end
155
156@implementation RecordRouteViewController
157
158- (void)viewDidLoad {
159 [super viewDidLoad];
Jorge Canizales59fdf712015-06-05 17:42:41 -0700160
161 NSString *dataBasePath = [NSBundle.mainBundle pathForResource:@"route_guide_db"
162 ofType:@"json"];
163 NSData *dataBaseContent = [NSData dataWithContentsOfFile:dataBasePath];
164 NSArray *features = [NSJSONSerialization JSONObjectWithData:dataBaseContent options:0 error:NULL];
165
166 GRXWriter *locations = [[GRXWriter writerWithContainer:features] map:^id(id feature) {
167 RTGPoint *location = [RTGPoint message];
168 location.longitude = [((NSNumber *) feature[@"location"][@"longitude"]) intValue];
169 location.latitude = [((NSNumber *) feature[@"location"][@"latitude"]) intValue];
170 NSLog(@"Visiting point %@", location);
171 return location;
172 }];
173
174 RTGRouteGuide *client = [[RTGRouteGuide alloc] initWithHost:kHostAddress];
175
176 [client recordRouteWithRequestsWriter:locations handler:^(RTGRouteSummary *response, NSError *error) {
177 if (response) {
178 NSLog(@"Finished trip with %i points", response.pointCount);
179 NSLog(@"Passed %i features", response.featureCount);
180 NSLog(@"Travelled %i meters", response.distance);
181 NSLog(@"It took %i seconds", response.elapsedTime);
182 } else {
183 NSLog(@"RPC error: %@", error);
184 }
185 }];
Jorge Canizales34b26292015-06-02 02:11:09 -0700186}
187
188@end
Jorge Canizales8d748b32015-06-05 17:43:40 -0700189
190
Jorge Canizales77606b62015-06-05 17:55:34 -0700191#pragma mark Demo: Route Chat
Jorge Canizales8d748b32015-06-05 17:43:40 -0700192
193// Run the routeChat demo. Send some chat messages, and print any chat messages that are sent from
194// the server.
195
196@interface RouteChatViewController : UIViewController
197@end
198
199@implementation RouteChatViewController
200
201- (void)viewDidLoad {
202 [super viewDidLoad];
203
204 NSArray *notes = @[[RTGRouteNote noteWithMessage:@"First message" latitude:0 longitude:0],
205 [RTGRouteNote noteWithMessage:@"Second message" latitude:0 longitude:1],
206 [RTGRouteNote noteWithMessage:@"Third message" latitude:1 longitude:0],
207 [RTGRouteNote noteWithMessage:@"Fourth message" latitude:0 longitude:0]];
208 GRXWriter *notesWriter = [[GRXWriter writerWithContainer:notes] map:^id(RTGRouteNote *note) {
209 NSLog(@"Sending message %@ at %@", note.message, note.location);
210 return note;
211 }];
212
213 RTGRouteGuide *client = [[RTGRouteGuide alloc] initWithHost:kHostAddress];
214
Jorge Canizales161b51e2015-07-17 17:21:04 -0700215 [client routeChatWithRequestsWriter:notesWriter
216 eventHandler:^(BOOL done, RTGRouteNote *note, NSError *error) {
Jorge Canizales8d748b32015-06-05 17:43:40 -0700217 if (note) {
218 NSLog(@"Got message %@ at %@", note.message, note.location);
219 } else if (error) {
220 NSLog(@"RPC error: %@", error);
221 }
222 if (done) {
223 NSLog(@"Chat ended.");
224 }
225 }];
226}
227
228@end