3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #import <UIKit/UIKit.h>
20 #import <RouteGuide/RouteGuide.pbrpc.h>
22 static NSString * const kHostAddress = @"localhost:50051";
24 /** Category to override RTGPoint's description. */
25 @interface RTGPoint (Description)
26 - (NSString *)description;
29 @implementation RTGPoint (Description)
30 - (NSString *)description {
31 NSString *verticalDirection = self.latitude >= 0 ? @"N" : @"S";
32 NSString *horizontalDirection = self.longitude >= 0 ? @"E" : @"W";
33 return [NSString stringWithFormat:@"%.02f%@ %.02f%@",
34 abs(self.latitude) / 1E7f, verticalDirection,
35 abs(self.longitude) / 1E7f, horizontalDirection];
39 /** Category to give RTGRouteNote a convenience constructor. */
40 @interface RTGRouteNote (Constructors)
41 + (instancetype)noteWithMessage:(NSString *)message
42 latitude:(float)latitude
43 longitude:(float)longitude;
46 @implementation RTGRouteNote (Constructors)
47 + (instancetype)noteWithMessage:(NSString *)message
48 latitude:(float)latitude
49 longitude:(float)longitude {
50 RTGRouteNote *note = [self message];
51 note.message = message;
52 note.location.latitude = (int32_t) latitude * 1E7;
53 note.location.longitude = (int32_t) longitude * 1E7;
59 #pragma mark Demo: Get Feature
62 * Run the getFeature demo. Calls getFeature with a point known to have a feature and a point known
63 * not to have a feature.
65 @interface GetFeatureViewController : UIViewController<GRPCProtoResponseHandler>
67 @property (weak, nonatomic) IBOutlet UILabel *outputLabel;
71 @implementation GetFeatureViewController {
72 RTGRouteGuide *_service;
75 - (dispatch_queue_t)dispatchQueue {
76 return dispatch_get_main_queue();
79 - (void)didReceiveProtoMessage:(GPBMessage *)message {
80 RTGFeature *response = (RTGFeature *)message;
82 // TODO(makdharma): Remove boilerplate by consolidating into one log function.
83 if (response.name.length != 0) {
84 NSString *str =[NSString stringWithFormat:@"%@\nFound feature called %@ at %@.", self.outputLabel.text, response.location, response.name];
85 self.outputLabel.text = str;
86 NSLog(@"Found feature called %@ at %@.", response.name, response.location);
87 } else if (response) {
88 NSString *str =[NSString stringWithFormat:@"%@\nFound no features at %@", self.outputLabel.text,response.location];
89 self.outputLabel.text = str;
90 NSLog(@"Found no features at %@", response.location);
94 - (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error {
96 NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error];
97 self.outputLabel.text = str;
98 NSLog(@"RPC error: %@", error);
102 - (void)execRequest {
103 RTGPoint *point = [RTGPoint message];
104 point.latitude = 409146138;
105 point.longitude = -746188906;
107 GRPCUnaryProtoCall *call = [_service getFeatureWithMessage:point
111 call = [_service getFeatureWithMessage:[RTGPoint message]
118 - (void)viewDidLoad {
121 GRPCMutableCallOptions *options = [[GRPCMutableCallOptions alloc] init];
122 options.transportType = GRPCTransportTypeInsecure;
124 _service = [[RTGRouteGuide alloc] initWithHost:kHostAddress callOptions:options];
127 - (void)viewDidAppear:(BOOL)animated {
128 self.outputLabel.text = @"RPC log:";
129 self.outputLabel.numberOfLines = 0;
130 self.outputLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:8.0];
137 #pragma mark Demo: List Features
140 * Run the listFeatures demo. Calls listFeatures with a rectangle containing all of the features in
141 * the pre-generated database. Prints each response as it comes in.
143 @interface ListFeaturesViewController : UIViewController<GRPCProtoResponseHandler>
145 @property (weak, nonatomic) IBOutlet UILabel *outputLabel;
149 @implementation ListFeaturesViewController {
150 RTGRouteGuide *_service;
153 - (dispatch_queue_t)dispatchQueue {
154 return dispatch_get_main_queue();
157 - (void)execRequest {
158 RTGRectangle *rectangle = [RTGRectangle message];
159 rectangle.lo.latitude = 405E6;
160 rectangle.lo.longitude = -750E6;
161 rectangle.hi.latitude = 410E6;
162 rectangle.hi.longitude = -745E6;
164 NSLog(@"Looking for features between %@ and %@", rectangle.lo, rectangle.hi);
165 GRPCUnaryProtoCall *call = [_service listFeaturesWithMessage:rectangle
171 - (void)didReceiveProtoMessage:(GPBMessage *)message {
172 RTGFeature *response = (RTGFeature *)message;
174 NSString *str =[NSString stringWithFormat:@"%@\nFound feature at %@ called %@.", self.outputLabel.text, response.location, response.name];
175 self.outputLabel.text = str;
176 NSLog(@"Found feature at %@ called %@.", response.location, response.name);
180 - (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error {
182 NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error];
183 self.outputLabel.text = str;
184 NSLog(@"RPC error: %@", error);
188 - (void)viewDidLoad {
191 GRPCMutableCallOptions *options = [[GRPCMutableCallOptions alloc] init];
192 options.transportType = GRPCTransportTypeInsecure;
194 _service = [[RTGRouteGuide alloc] initWithHost:kHostAddress callOptions:options];
197 - (void)viewDidAppear:(BOOL)animated {
198 self.outputLabel.text = @"RPC log:";
199 self.outputLabel.numberOfLines = 0;
200 self.outputLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:8.0];
206 #pragma mark Demo: Record Route
209 * Run the recordRoute demo. Sends several randomly chosen points from the pre-generated feature
210 * database with a variable delay in between. Prints the statistics when they are sent from the
213 @interface RecordRouteViewController : UIViewController<GRPCProtoResponseHandler>
215 @property (weak, nonatomic) IBOutlet UILabel *outputLabel;
219 @implementation RecordRouteViewController {
220 RTGRouteGuide *_service;
223 - (dispatch_queue_t)dispatchQueue {
224 return dispatch_get_main_queue();
227 - (void)execRequest {
228 NSString *dataBasePath = [NSBundle.mainBundle pathForResource:@"route_guide_db"
230 NSData *dataBaseContent = [NSData dataWithContentsOfFile:dataBasePath];
232 NSArray *features = [NSJSONSerialization JSONObjectWithData:dataBaseContent options:0 error:&error];
235 NSLog(@"Error reading database.");
236 NSString *str = @"Error reading database.";
237 self.outputLabel.text = str;
241 GRPCStreamingProtoCall *call = [_service recordRouteWithResponseHandler:self
244 for (id feature in features) {
245 RTGPoint *location = [RTGPoint message];
246 location.longitude = [((NSNumber *) feature[@"location"][@"longitude"]) intValue];
247 location.latitude = [((NSNumber *) feature[@"location"][@"latitude"]) intValue];
248 NSString *str =[NSString stringWithFormat:@"%@\nVisiting point %@", self.outputLabel.text, location];
249 self.outputLabel.text = str;
250 NSLog(@"Visiting point %@", location);
251 [call writeMessage:location];
256 - (void)didReceiveProtoMessage:(GPBMessage *)message {
257 RTGRouteSummary *response = (RTGRouteSummary *)message;
260 NSString *str =[NSString stringWithFormat:
261 @"%@\nFinished trip with %i points\nPassed %i features\n"
262 "Travelled %i meters\nIt took %i seconds",
263 self.outputLabel.text, response.pointCount, response.featureCount,
264 response.distance, response.elapsedTime];
265 self.outputLabel.text = str;
266 NSLog(@"Finished trip with %i points", response.pointCount);
267 NSLog(@"Passed %i features", response.featureCount);
268 NSLog(@"Travelled %i meters", response.distance);
269 NSLog(@"It took %i seconds", response.elapsedTime);
273 - (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error {
275 NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error];
276 self.outputLabel.text = str;
277 NSLog(@"RPC error: %@", error);
281 - (void)viewDidLoad {
284 GRPCMutableCallOptions *options = [[GRPCMutableCallOptions alloc] init];
285 options.transportType = GRPCTransportTypeInsecure;
287 _service = [[RTGRouteGuide alloc] initWithHost:kHostAddress callOptions:options];
290 - (void)viewDidAppear:(BOOL)animated {
291 self.outputLabel.text = @"RPC log:";
292 self.outputLabel.numberOfLines = 0;
293 self.outputLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:8.0];
300 #pragma mark Demo: Route Chat
303 * Run the routeChat demo. Send some chat messages, and print any chat messages that are sent from
306 @interface RouteChatViewController : UIViewController<GRPCProtoResponseHandler>
308 @property (weak, nonatomic) IBOutlet UILabel *outputLabel;
312 @implementation RouteChatViewController {
313 RTGRouteGuide *_service;
316 - (dispatch_queue_t)dispatchQueue {
317 return dispatch_get_main_queue();
320 - (void)execRequest {
321 NSArray *notes = @[[RTGRouteNote noteWithMessage:@"First message" latitude:0 longitude:0],
322 [RTGRouteNote noteWithMessage:@"Second message" latitude:0 longitude:1],
323 [RTGRouteNote noteWithMessage:@"Third message" latitude:1 longitude:0],
324 [RTGRouteNote noteWithMessage:@"Fourth message" latitude:0 longitude:0]];
326 GRPCStreamingProtoCall *call = [_service routeChatWithResponseHandler:self
329 for (RTGRouteNote *note in notes) {
330 [call writeMessage:note];
335 - (void)didReceiveProtoMessage:(GPBMessage *)message {
336 RTGRouteNote *note = (RTGRouteNote *)message;
338 NSString *str =[NSString stringWithFormat:@"%@\nGot message %@ at %@",
339 self.outputLabel.text, note.message, note.location];
340 self.outputLabel.text = str;
341 NSLog(@"Got message %@ at %@", note.message, note.location);
345 - (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error {
347 NSLog(@"Chat ended.");
349 NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error];
350 self.outputLabel.text = str;
351 NSLog(@"RPC error: %@", error);
355 - (void)viewDidLoad {
358 GRPCMutableCallOptions *options = [[GRPCMutableCallOptions alloc] init];
359 options.transportType = GRPCTransportTypeInsecure;
361 _service = [[RTGRouteGuide alloc] initWithHost:kHostAddress callOptions:options];
364 - (void)viewDidAppear:(BOOL)animated {
365 // TODO(makarandd): Set these properties through UI builder
366 self.outputLabel.text = @"RPC log:";
367 self.outputLabel.numberOfLines = 0;
368 self.outputLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:8.0];