Merge pull request #21316 from muxi/update-unary-response-handler-generics

Update GRPCUnaryResponseHandler with generics
pull/21339/head
Muxi Yan 5 years ago committed by GitHub
commit e2ae404dcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 158
      examples/objective-c/route_guide/ViewControllers.m
  2. 7
      src/objective-c/ProtoRPC/ProtoRPC.h
  3. 4
      src/objective-c/ProtoRPC/ProtoRPC.m

@ -25,7 +25,7 @@
#import <GRPCClient/GRPCTransport.h>
static NSString * const kHostAddress = @"localhost:50051";
static NSString *const kHostAddress = @"localhost:50051";
/** Category to override RTGPoint's description. */
@interface RTGPoint (Description)
@ -36,8 +36,8 @@ static NSString * const kHostAddress = @"localhost:50051";
- (NSString *)description {
NSString *verticalDirection = self.latitude >= 0 ? @"N" : @"S";
NSString *horizontalDirection = self.longitude >= 0 ? @"E" : @"W";
return [NSString stringWithFormat:@"%.02f%@ %.02f%@",
abs(self.latitude) / 1E7f, verticalDirection,
return
[NSString stringWithFormat:@"%.02f%@ %.02f%@", abs(self.latitude) / 1E7f, verticalDirection,
abs(self.longitude) / 1E7f, horizontalDirection];
}
@end
@ -55,22 +55,21 @@ static NSString * const kHostAddress = @"localhost:50051";
longitude:(float)longitude {
RTGRouteNote *note = [self message];
note.message = message;
note.location.latitude = (int32_t) latitude * 1E7;
note.location.longitude = (int32_t) longitude * 1E7;
note.location.latitude = (int32_t)latitude * 1E7;
note.location.longitude = (int32_t)longitude * 1E7;
return note;
}
@end
#pragma mark Demo: Get Feature
/**
* Run the getFeature demo. Calls getFeature with a point known to have a feature and a point known
* not to have a feature.
*/
@interface GetFeatureViewController : UIViewController<GRPCProtoResponseHandler>
@interface GetFeatureViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
@property(weak, nonatomic) IBOutlet UILabel *outputLabel;
@end
@ -78,47 +77,44 @@ static NSString * const kHostAddress = @"localhost:50051";
RTGRouteGuide *_service;
}
- (dispatch_queue_t)dispatchQueue {
return dispatch_get_main_queue();
}
- (void)didReceiveProtoMessage:(GPBMessage *)message {
RTGFeature *response = (RTGFeature *)message;
- (void)execRequest {
void (^handler)(RTGFeature *response, NSError *error) = ^(RTGFeature *response, NSError *error) {
// TODO(makdharma): Remove boilerplate by consolidating into one log function.
if (response.name.length != 0) {
NSString *str =[NSString stringWithFormat:@"%@\nFound feature called %@ at %@.", self.outputLabel.text, response.location, response.name];
if (response.name.length) {
NSString *str =
[NSString stringWithFormat:@"%@\nFound feature called %@ at %@.", self.outputLabel.text,
response.location, response.name];
self.outputLabel.text = str;
NSLog(@"Found feature called %@ at %@.", response.name, response.location);
} else if (response) {
NSString *str =[NSString stringWithFormat:@"%@\nFound no features at %@", self.outputLabel.text,response.location];
NSString *str = [NSString stringWithFormat:@"%@\nFound no features at %@",
self.outputLabel.text, response.location];
self.outputLabel.text = str;
NSLog(@"Found no features at %@", response.location);
}
}
- (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error {
if (error) {
NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error];
} else {
NSString *str =
[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error];
self.outputLabel.text = str;
NSLog(@"RPC error: %@", error);
}
}
};
- (void)execRequest {
RTGPoint *point = [RTGPoint message];
point.latitude = 409146138;
point.longitude = -746188906;
GRPCUnaryProtoCall *call = [_service getFeatureWithMessage:point
responseHandler:self
GRPCUnaryProtoCall *call = [_service
getFeatureWithMessage:point
responseHandler:[[GRPCUnaryResponseHandler alloc] initWithResponseHandler:handler
responseDispatchQueue:nil]
callOptions:nil];
[call start];
call = [_service getFeatureWithMessage:[RTGPoint message]
responseHandler:self
call = [_service
getFeatureWithMessage:[RTGPoint message]
responseHandler:[[GRPCUnaryResponseHandler alloc] initWithResponseHandler:handler
responseDispatchQueue:nil]
callOptions:nil];
[call start];
}
- (void)viewDidLoad {
@ -139,16 +135,15 @@ static NSString * const kHostAddress = @"localhost:50051";
@end
#pragma mark Demo: List Features
/**
* Run the listFeatures demo. Calls listFeatures with a rectangle containing all of the features in
* the pre-generated database. Prints each response as it comes in.
*/
@interface ListFeaturesViewController : UIViewController<GRPCProtoResponseHandler>
@interface ListFeaturesViewController : UIViewController <GRPCProtoResponseHandler>
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
@property(weak, nonatomic) IBOutlet UILabel *outputLabel;
@end
@ -177,7 +172,9 @@ static NSString * const kHostAddress = @"localhost:50051";
- (void)didReceiveProtoMessage:(GPBMessage *)message {
RTGFeature *response = (RTGFeature *)message;
if (response) {
NSString *str =[NSString stringWithFormat:@"%@\nFound feature at %@ called %@.", self.outputLabel.text, response.location, response.name];
NSString *str =
[NSString stringWithFormat:@"%@\nFound feature at %@ called %@.", self.outputLabel.text,
response.location, response.name];
self.outputLabel.text = str;
NSLog(@"Found feature at %@ called %@.", response.location, response.name);
}
@ -185,7 +182,7 @@ static NSString * const kHostAddress = @"localhost:50051";
- (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error {
if (error) {
NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error];
NSString *str = [NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error];
self.outputLabel.text = str;
NSLog(@"RPC error: %@", error);
}
@ -216,9 +213,9 @@ static NSString * const kHostAddress = @"localhost:50051";
* database with a variable delay in between. Prints the statistics when they are sent from the
* server.
*/
@interface RecordRouteViewController : UIViewController<GRPCProtoResponseHandler>
@interface RecordRouteViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
@property(weak, nonatomic) IBOutlet UILabel *outputLabel;
@end
@ -226,16 +223,13 @@ static NSString * const kHostAddress = @"localhost:50051";
RTGRouteGuide *_service;
}
- (dispatch_queue_t)dispatchQueue {
return dispatch_get_main_queue();
}
- (void)execRequest {
NSString *dataBasePath = [NSBundle.mainBundle pathForResource:@"route_guide_db"
ofType:@"json"];
NSString *dataBasePath = [NSBundle.mainBundle pathForResource:@"route_guide_db" ofType:@"json"];
NSData *dataBaseContent = [NSData dataWithContentsOfFile:dataBasePath];
NSError *error;
NSArray *features = [NSJSONSerialization JSONObjectWithData:dataBaseContent options:0 error:&error];
NSArray *features = [NSJSONSerialization JSONObjectWithData:dataBaseContent
options:0
error:&error];
if (error) {
NSLog(@"Error reading database.");
@ -244,27 +238,11 @@ static NSString * const kHostAddress = @"localhost:50051";
return;
}
GRPCStreamingProtoCall *call = [_service recordRouteWithResponseHandler:self
callOptions:nil];
[call start];
for (id feature in features) {
RTGPoint *location = [RTGPoint message];
location.longitude = [((NSNumber *) feature[@"location"][@"longitude"]) intValue];
location.latitude = [((NSNumber *) feature[@"location"][@"latitude"]) intValue];
NSString *str =[NSString stringWithFormat:@"%@\nVisiting point %@", self.outputLabel.text, location];
self.outputLabel.text = str;
NSLog(@"Visiting point %@", location);
[call writeMessage:location];
}
[call finish];
}
- (void)didReceiveProtoMessage:(GPBMessage *)message {
RTGRouteSummary *response = (RTGRouteSummary *)message;
void (^handler)(RTGRouteSummary *response, NSError *error) =
^(RTGRouteSummary *response, NSError *error) {
if (response) {
NSString *str =[NSString stringWithFormat:
@"%@\nFinished trip with %i points\nPassed %i features\n"
NSString *str = [NSString
stringWithFormat:@"%@\nFinished trip with %i points\nPassed %i features\n"
"Travelled %i meters\nIt took %i seconds",
self.outputLabel.text, response.pointCount, response.featureCount,
response.distance, response.elapsedTime];
@ -273,15 +251,33 @@ static NSString * const kHostAddress = @"localhost:50051";
NSLog(@"Passed %i features", response.featureCount);
NSLog(@"Travelled %i meters", response.distance);
NSLog(@"It took %i seconds", response.elapsedTime);
}
}
- (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error {
if (error) {
NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error];
} else {
NSString *str =
[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error];
self.outputLabel.text = str;
NSLog(@"RPC error: %@", error);
}
};
// We can use unary response handler here because, despite the requests being a stream, the
// response of the RPC is unary.
GRPCStreamingProtoCall *call =
[_service recordRouteWithResponseHandler:[[GRPCUnaryResponseHandler alloc]
initWithResponseHandler:handler
responseDispatchQueue:nil]
callOptions:nil];
[call start];
for (id feature in features) {
RTGPoint *location = [RTGPoint message];
location.longitude = [((NSNumber *)feature[@"location"][@"longitude"]) intValue];
location.latitude = [((NSNumber *)feature[@"location"][@"latitude"]) intValue];
NSString *str =
[NSString stringWithFormat:@"%@\nVisiting point %@", self.outputLabel.text, location];
self.outputLabel.text = str;
NSLog(@"Visiting point %@", location);
[call writeMessage:location];
}
[call finish];
}
- (void)viewDidLoad {
@ -302,16 +298,15 @@ static NSString * const kHostAddress = @"localhost:50051";
@end
#pragma mark Demo: Route Chat
/**
* Run the routeChat demo. Send some chat messages, and print any chat messages that are sent from
* the server.
*/
@interface RouteChatViewController : UIViewController<GRPCProtoResponseHandler>
@interface RouteChatViewController : UIViewController <GRPCProtoResponseHandler>
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
@property(weak, nonatomic) IBOutlet UILabel *outputLabel;
@end
@ -324,13 +319,14 @@ static NSString * const kHostAddress = @"localhost:50051";
}
- (void)execRequest {
NSArray *notes = @[[RTGRouteNote noteWithMessage:@"First message" latitude:0 longitude:0],
NSArray *notes = @[
[RTGRouteNote noteWithMessage:@"First message" latitude:0 longitude:0],
[RTGRouteNote noteWithMessage:@"Second message" latitude:0 longitude:1],
[RTGRouteNote noteWithMessage:@"Third message" latitude:1 longitude:0],
[RTGRouteNote noteWithMessage:@"Fourth message" latitude:0 longitude:0]];
[RTGRouteNote noteWithMessage:@"Fourth message" latitude:0 longitude:0]
];
GRPCStreamingProtoCall *call = [_service routeChatWithResponseHandler:self
callOptions:nil];
GRPCStreamingProtoCall *call = [_service routeChatWithResponseHandler:self callOptions:nil];
[call start];
for (RTGRouteNote *note in notes) {
[call writeMessage:note];
@ -341,8 +337,8 @@ static NSString * const kHostAddress = @"localhost:50051";
- (void)didReceiveProtoMessage:(GPBMessage *)message {
RTGRouteNote *note = (RTGRouteNote *)message;
if (note) {
NSString *str =[NSString stringWithFormat:@"%@\nGot message %@ at %@",
self.outputLabel.text, note.message, note.location];
NSString *str = [NSString stringWithFormat:@"%@\nGot message %@ at %@", self.outputLabel.text,
note.message, note.location];
self.outputLabel.text = str;
NSLog(@"Got message %@ at %@", note.message, note.location);
}
@ -352,7 +348,7 @@ static NSString * const kHostAddress = @"localhost:50051";
if (!error) {
NSLog(@"Chat ended.");
} else {
NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error];
NSString *str = [NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error];
self.outputLabel.text = str;
NSLog(@"RPC error: %@", error);
}

@ -73,8 +73,11 @@ NS_ASSUME_NONNULL_BEGIN
/**
* A convenience class of objects that act as response handlers of calls. Issues
* response to a single handler when the response is completed.
*
* The object is stateful and should not be reused for multiple calls. If multiple calls share the
* same response handling logic, create separate GRPCUnaryResponseHandler objects for each call.
*/
@interface GRPCUnaryResponseHandler : NSObject<GRPCProtoResponseHandler>
@interface GRPCUnaryResponseHandler<ResponseType> : NSObject<GRPCProtoResponseHandler>
/**
* Creates a responsehandler object with a unary call handler.
@ -83,7 +86,7 @@ NS_ASSUME_NONNULL_BEGIN
* responseDispatchQueue: the dispatch queue on which the response handler
* should be issued. If it's nil, the handler will use the main queue.
*/
- (nullable instancetype)initWithResponseHandler:(void (^)(GPBMessage *, NSError *))handler
- (nullable instancetype)initWithResponseHandler:(void (^)(ResponseType, NSError *))handler
responseDispatchQueue:(nullable dispatch_queue_t)dispatchQueue;
/** Response headers received during the call. */

@ -28,13 +28,13 @@
#import <RxLibrary/GRXWriter+Transformations.h>
@implementation GRPCUnaryResponseHandler {
void (^_responseHandler)(GPBMessage *, NSError *);
void (^_responseHandler)(id, NSError *);
dispatch_queue_t _responseDispatchQueue;
GPBMessage *_message;
}
- (nullable instancetype)initWithResponseHandler:(void (^)(GPBMessage *, NSError *))handler
- (nullable instancetype)initWithResponseHandler:(void (^)(id, NSError *))handler
responseDispatchQueue:(dispatch_queue_t)dispatchQueue {
if ((self = [super init])) {
_responseHandler = handler;

Loading…
Cancel
Save