blob: ec40f72beb0eba8757425302227815a1c4664272 [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 Canizales59fdf712015-06-05 17:42:41 -070035#import <gRPC/GRXWriter+Immediate.h>
36#import <gRPC/GRXWriter+Transformations.h>
Jorge Canizales34b26292015-06-02 02:11:09 -070037#import <RouteGuide/RouteGuide.pbrpc.h>
38
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 Canizales4694cdb2015-06-04 23:06:13 -0700134 [client listFeaturesWithRequest:rectangle handler:^(BOOL done, RTGFeature *response, NSError *error) {
135 if (response) {
Jorge Canizalesc9885952015-06-05 17:41:00 -0700136 NSLog(@"Found feature at %@ called %@.", response.location, response.name);
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700137 } else if (error) {
138 NSLog(@"RPC error: %@", error);
139 }
140 }];
Jorge Canizales34b26292015-06-02 02:11:09 -0700141}
142
143@end
144
Jorge Canizales3fda5482015-06-05 12:05:00 -0700145
Jorge Canizales77606b62015-06-05 17:55:34 -0700146#pragma mark Demo: Record Route
Jorge Canizales34b26292015-06-02 02:11:09 -0700147
Jorge Canizales59fdf712015-06-05 17:42:41 -0700148// Run the recordRoute demo. Sends several randomly chosen points from the pre-generated feature
149// database with a variable delay in between. Prints the statistics when they are sent from the
150// server.
151
Jorge Canizales34b26292015-06-02 02:11:09 -0700152@interface RecordRouteViewController : UIViewController
153@end
154
155@implementation RecordRouteViewController
156
157- (void)viewDidLoad {
158 [super viewDidLoad];
Jorge Canizales59fdf712015-06-05 17:42:41 -0700159
160 NSString *dataBasePath = [NSBundle.mainBundle pathForResource:@"route_guide_db"
161 ofType:@"json"];
162 NSData *dataBaseContent = [NSData dataWithContentsOfFile:dataBasePath];
163 NSArray *features = [NSJSONSerialization JSONObjectWithData:dataBaseContent options:0 error:NULL];
164
165 GRXWriter *locations = [[GRXWriter writerWithContainer:features] map:^id(id feature) {
166 RTGPoint *location = [RTGPoint message];
167 location.longitude = [((NSNumber *) feature[@"location"][@"longitude"]) intValue];
168 location.latitude = [((NSNumber *) feature[@"location"][@"latitude"]) intValue];
169 NSLog(@"Visiting point %@", location);
170 return location;
171 }];
172
173 RTGRouteGuide *client = [[RTGRouteGuide alloc] initWithHost:kHostAddress];
174
175 [client recordRouteWithRequestsWriter:locations handler:^(RTGRouteSummary *response, NSError *error) {
176 if (response) {
177 NSLog(@"Finished trip with %i points", response.pointCount);
178 NSLog(@"Passed %i features", response.featureCount);
179 NSLog(@"Travelled %i meters", response.distance);
180 NSLog(@"It took %i seconds", response.elapsedTime);
181 } else {
182 NSLog(@"RPC error: %@", error);
183 }
184 }];
Jorge Canizales34b26292015-06-02 02:11:09 -0700185}
186
187@end
Jorge Canizales8d748b32015-06-05 17:43:40 -0700188
189
Jorge Canizales77606b62015-06-05 17:55:34 -0700190#pragma mark Demo: Route Chat
Jorge Canizales8d748b32015-06-05 17:43:40 -0700191
192// Run the routeChat demo. Send some chat messages, and print any chat messages that are sent from
193// the server.
194
195@interface RouteChatViewController : UIViewController
196@end
197
198@implementation RouteChatViewController
199
200- (void)viewDidLoad {
201 [super viewDidLoad];
202
203 NSArray *notes = @[[RTGRouteNote noteWithMessage:@"First message" latitude:0 longitude:0],
204 [RTGRouteNote noteWithMessage:@"Second message" latitude:0 longitude:1],
205 [RTGRouteNote noteWithMessage:@"Third message" latitude:1 longitude:0],
206 [RTGRouteNote noteWithMessage:@"Fourth message" latitude:0 longitude:0]];
207 GRXWriter *notesWriter = [[GRXWriter writerWithContainer:notes] map:^id(RTGRouteNote *note) {
208 NSLog(@"Sending message %@ at %@", note.message, note.location);
209 return note;
210 }];
211
212 RTGRouteGuide *client = [[RTGRouteGuide alloc] initWithHost:kHostAddress];
213
214 [client routeChatWithRequestsWriter:notesWriter handler:^(BOOL done, RTGRouteNote *note, NSError *error) {
215 if (note) {
216 NSLog(@"Got message %@ at %@", note.message, note.location);
217 } else if (error) {
218 NSLog(@"RPC error: %@", error);
219 }
220 if (done) {
221 NSLog(@"Chat ended.");
222 }
223 }];
224}
225
226@end