blob: 8332661d57ff0c80ec8b63abe9daca8bc685e4f4 [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
Jorge Canizales8676d222015-10-28 05:51:11 -070039static NSString * const kHostAddress = @"localhost:50051";
Jorge Canizales34b26292015-06-02 02:11:09 -070040
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
Jorge Canizales8676d222015-10-28 05:51:11 -070089 // 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 Canizales34b26292015-06-02 02:11:09 -070093
Jorge Canizales3aed7172015-06-04 22:56:21 -070094 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 Canizales34b26292015-06-02 02:11:09 -0700104 RTGPoint *point = [RTGPoint message];
105 point.latitude = 409146138;
106 point.longitude = -746188906;
107
Jorge Canizales8676d222015-10-28 05:51:11 -0700108 [service getFeatureWithRequest:point handler:handler];
109 [service getFeatureWithRequest:[RTGPoint message] handler:handler];
Jorge Canizales34b26292015-06-02 02:11:09 -0700110}
111
112@end
113
Jorge Canizales3fda5482015-06-05 12:05:00 -0700114
Jorge Canizales77606b62015-06-05 17:55:34 -0700115#pragma mark Demo: List Features
Jorge Canizales34b26292015-06-02 02:11:09 -0700116
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700117// 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 Canizales34b26292015-06-02 02:11:09 -0700120@interface ListFeaturesViewController : UIViewController
121@end
122
123@implementation ListFeaturesViewController
124
125- (void)viewDidLoad {
126 [super viewDidLoad];
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700127
Jorge Canizales8676d222015-10-28 05:51:11 -0700128 RTGRouteGuide *service = [RTGRouteGuide serviceWithHost:kHostAddress];
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700129
130 RTGRectangle *rectangle = [RTGRectangle message];
Jorge Canizalesc9885952015-06-05 17:41:00 -0700131 rectangle.lo.latitude = 405E6;
132 rectangle.lo.longitude = -750E6;
133 rectangle.hi.latitude = 410E6;
134 rectangle.hi.longitude = -745E6;
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700135
Jorge Canizalesc9885952015-06-05 17:41:00 -0700136 NSLog(@"Looking for features between %@ and %@", rectangle.lo, rectangle.hi);
Jorge Canizales8676d222015-10-28 05:51:11 -0700137 [service listFeaturesWithRequest:rectangle
138 eventHandler:^(BOOL done, RTGFeature *response, NSError *error) {
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700139 if (response) {
Jorge Canizalesc9885952015-06-05 17:41:00 -0700140 NSLog(@"Found feature at %@ called %@.", response.location, response.name);
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700141 } else if (error) {
142 NSLog(@"RPC error: %@", error);
143 }
144 }];
Jorge Canizales34b26292015-06-02 02:11:09 -0700145}
146
147@end
148
Jorge Canizales3fda5482015-06-05 12:05:00 -0700149
Jorge Canizales77606b62015-06-05 17:55:34 -0700150#pragma mark Demo: Record Route
Jorge Canizales34b26292015-06-02 02:11:09 -0700151
Jorge Canizales59fdf712015-06-05 17:42:41 -0700152// 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 Canizales34b26292015-06-02 02:11:09 -0700156@interface RecordRouteViewController : UIViewController
157@end
158
159@implementation RecordRouteViewController
160
161- (void)viewDidLoad {
162 [super viewDidLoad];
Jorge Canizales59fdf712015-06-05 17:42:41 -0700163
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 Canizales8676d222015-10-28 05:51:11 -0700177 RTGRouteGuide *service = [RTGRouteGuide serviceWithHost:kHostAddress];
Jorge Canizales59fdf712015-06-05 17:42:41 -0700178
Jorge Canizales8676d222015-10-28 05:51:11 -0700179 [service recordRouteWithRequestsWriter:locations
180 handler:^(RTGRouteSummary *response, NSError *error) {
Jorge Canizales59fdf712015-06-05 17:42:41 -0700181 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 Canizales34b26292015-06-02 02:11:09 -0700190}
191
192@end
Jorge Canizales8d748b32015-06-05 17:43:40 -0700193
194
Jorge Canizales77606b62015-06-05 17:55:34 -0700195#pragma mark Demo: Route Chat
Jorge Canizales8d748b32015-06-05 17:43:40 -0700196
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 Canizales8676d222015-10-28 05:51:11 -0700217 RTGRouteGuide *service = [RTGRouteGuide serviceWithHost:kHostAddress];
Jorge Canizales8d748b32015-06-05 17:43:40 -0700218
Jorge Canizales8676d222015-10-28 05:51:11 -0700219 [service routeChatWithRequestsWriter:notesWriter
220 eventHandler:^(BOOL done, RTGRouteNote *note, NSError *error) {
Jorge Canizales8d748b32015-06-05 17:43:40 -0700221 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