blob: aeb8dcae76d0c839fc61d8d9bffae6283efd2801 [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 Canizales34b26292015-06-02 02:11:09 -070056#pragma mark Get Feature
57
Jorge Canizales3aed7172015-06-04 22:56:21 -070058// Run the getFeature demo. Calls getFeature with a point known to have a feature and a point known
59// not to have a feature.
60
Jorge Canizales34b26292015-06-02 02:11:09 -070061@interface GetFeatureViewController : UIViewController
62@end
63
64@implementation GetFeatureViewController
65
66- (void)viewDidLoad {
67 [super viewDidLoad];
68
69 RTGRouteGuide *client = [[RTGRouteGuide alloc] initWithHost:kHostAddress];
70
Jorge Canizales3aed7172015-06-04 22:56:21 -070071 void (^handler)(RTGFeature *response, NSError *error) = ^(RTGFeature *response, NSError *error) {
72 if (response.name.length) {
73 NSLog(@"Found feature called %@ at %@.", response.name, response.location);
74 } else if (response) {
75 NSLog(@"Found no features at %@", response.location);
76 } else {
77 NSLog(@"RPC error: %@", error);
78 }
79 };
80
Jorge Canizales34b26292015-06-02 02:11:09 -070081 RTGPoint *point = [RTGPoint message];
82 point.latitude = 409146138;
83 point.longitude = -746188906;
84
Jorge Canizales3aed7172015-06-04 22:56:21 -070085 [client getFeatureWithRequest:point handler:handler];
86 [client getFeatureWithRequest:[RTGPoint message] handler:handler];
Jorge Canizales34b26292015-06-02 02:11:09 -070087}
88
89@end
90
Jorge Canizales3fda5482015-06-05 12:05:00 -070091
Jorge Canizales34b26292015-06-02 02:11:09 -070092#pragma mark List Features
93
Jorge Canizales4694cdb2015-06-04 23:06:13 -070094// Run the listFeatures demo. Calls listFeatures with a rectangle containing all of the features in
95// the pre-generated database. Prints each response as it comes in.
96
Jorge Canizales34b26292015-06-02 02:11:09 -070097@interface ListFeaturesViewController : UIViewController
98@end
99
100@implementation ListFeaturesViewController
101
102- (void)viewDidLoad {
103 [super viewDidLoad];
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700104
105 RTGRouteGuide *client = [[RTGRouteGuide alloc] initWithHost:kHostAddress];
106
107 RTGRectangle *rectangle = [RTGRectangle message];
Jorge Canizalesc9885952015-06-05 17:41:00 -0700108 rectangle.lo.latitude = 405E6;
109 rectangle.lo.longitude = -750E6;
110 rectangle.hi.latitude = 410E6;
111 rectangle.hi.longitude = -745E6;
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700112
Jorge Canizalesc9885952015-06-05 17:41:00 -0700113 NSLog(@"Looking for features between %@ and %@", rectangle.lo, rectangle.hi);
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700114 [client listFeaturesWithRequest:rectangle handler:^(BOOL done, RTGFeature *response, NSError *error) {
115 if (response) {
Jorge Canizalesc9885952015-06-05 17:41:00 -0700116 NSLog(@"Found feature at %@ called %@.", response.location, response.name);
Jorge Canizales4694cdb2015-06-04 23:06:13 -0700117 } else if (error) {
118 NSLog(@"RPC error: %@", error);
119 }
120 }];
Jorge Canizales34b26292015-06-02 02:11:09 -0700121}
122
123@end
124
Jorge Canizales3fda5482015-06-05 12:05:00 -0700125
Jorge Canizales34b26292015-06-02 02:11:09 -0700126#pragma mark Record Route
127
Jorge Canizales59fdf712015-06-05 17:42:41 -0700128// Run the recordRoute demo. Sends several randomly chosen points from the pre-generated feature
129// database with a variable delay in between. Prints the statistics when they are sent from the
130// server.
131
Jorge Canizales34b26292015-06-02 02:11:09 -0700132@interface RecordRouteViewController : UIViewController
133@end
134
135@implementation RecordRouteViewController
136
137- (void)viewDidLoad {
138 [super viewDidLoad];
139 // Do any additional setup after loading the view.
140}
141
142@end
143
Jorge Canizales3fda5482015-06-05 12:05:00 -0700144
Jorge Canizales34b26292015-06-02 02:11:09 -0700145#pragma mark Route Chat
146
147@interface RouteChatViewController : UIViewController
148@end
149
150@implementation RouteChatViewController
151
152- (void)viewDidLoad {
153 [super viewDidLoad];
Jorge Canizales59fdf712015-06-05 17:42:41 -0700154
155 NSString *dataBasePath = [NSBundle.mainBundle pathForResource:@"route_guide_db"
156 ofType:@"json"];
157 NSData *dataBaseContent = [NSData dataWithContentsOfFile:dataBasePath];
158 NSArray *features = [NSJSONSerialization JSONObjectWithData:dataBaseContent options:0 error:NULL];
159
160 GRXWriter *locations = [[GRXWriter writerWithContainer:features] map:^id(id feature) {
161 RTGPoint *location = [RTGPoint message];
162 location.longitude = [((NSNumber *) feature[@"location"][@"longitude"]) intValue];
163 location.latitude = [((NSNumber *) feature[@"location"][@"latitude"]) intValue];
164 NSLog(@"Visiting point %@", location);
165 return location;
166 }];
167
168 RTGRouteGuide *client = [[RTGRouteGuide alloc] initWithHost:kHostAddress];
169
170 [client recordRouteWithRequestsWriter:locations handler:^(RTGRouteSummary *response, NSError *error) {
171 if (response) {
172 NSLog(@"Finished trip with %i points", response.pointCount);
173 NSLog(@"Passed %i features", response.featureCount);
174 NSLog(@"Travelled %i meters", response.distance);
175 NSLog(@"It took %i seconds", response.elapsedTime);
176 } else {
177 NSLog(@"RPC error: %@", error);
178 }
179 }];
Jorge Canizales34b26292015-06-02 02:11:09 -0700180}
181
182@end