OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, 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 "GRPCHost.h" |
| 35 |
| 36 #include <grpc/grpc.h> |
| 37 #import <GRPCClient/GRPCCall+ChannelArg.h> |
| 38 |
| 39 #import "GRPCChannel.h" |
| 40 #import "GRPCCompletionQueue.h" |
| 41 #import "NSDictionary+GRPC.h" |
| 42 |
| 43 // TODO(jcanizales): Generate the version in a standalone header, from templates
. Like |
| 44 // templates/src/core/surface/version.c.template . |
| 45 #define GRPC_OBJC_VERSION_STRING @"0.13.0" |
| 46 |
| 47 @interface GRPCHost () |
| 48 // TODO(mlumish): Investigate whether caching channels with strong links is a go
od idea. |
| 49 @property(nonatomic, strong) GRPCChannel *channel; |
| 50 @end |
| 51 |
| 52 @implementation GRPCHost |
| 53 |
| 54 + (instancetype)hostWithAddress:(NSString *)address { |
| 55 return [[self alloc] initWithAddress:address]; |
| 56 } |
| 57 |
| 58 - (instancetype)init { |
| 59 return [self initWithAddress:nil]; |
| 60 } |
| 61 |
| 62 // Default initializer. |
| 63 - (instancetype)initWithAddress:(NSString *)address { |
| 64 if (!address) { |
| 65 return nil; |
| 66 } |
| 67 |
| 68 // To provide a default port, we try to interpret the address. If it's just a
host name without |
| 69 // scheme and without port, we'll use port 443. If it has a scheme, we pass it
untouched to the C |
| 70 // gRPC library. |
| 71 // TODO(jcanizales): Add unit tests for the types of addresses we want to let
pass untouched. |
| 72 NSURL *hostURL = [NSURL URLWithString:[@"https://" stringByAppendingString:add
ress]]; |
| 73 if (hostURL.host && !hostURL.port) { |
| 74 address = [hostURL.host stringByAppendingString:@":443"]; |
| 75 } |
| 76 |
| 77 // Look up the GRPCHost in the cache. |
| 78 static NSMutableDictionary *hostCache; |
| 79 static dispatch_once_t cacheInitialization; |
| 80 dispatch_once(&cacheInitialization, ^{ |
| 81 hostCache = [NSMutableDictionary dictionary]; |
| 82 }); |
| 83 @synchronized(hostCache) { |
| 84 GRPCHost *cachedHost = hostCache[address]; |
| 85 if (cachedHost) { |
| 86 return cachedHost; |
| 87 } |
| 88 |
| 89 if ((self = [super init])) { |
| 90 _address = address; |
| 91 _secure = YES; |
| 92 hostCache[address] = self; |
| 93 } |
| 94 } |
| 95 return self; |
| 96 } |
| 97 |
| 98 - (grpc_call *)unmanagedCallWithPath:(NSString *)path completionQueue:(GRPCCompl
etionQueue *)queue { |
| 99 if (!queue || !path || !self.channel) { |
| 100 return NULL; |
| 101 } |
| 102 return grpc_channel_create_call(self.channel.unmanagedChannel, |
| 103 NULL, GRPC_PROPAGATE_DEFAULTS, |
| 104 queue.unmanagedQueue, |
| 105 path.UTF8String, |
| 106 self.hostName.UTF8String, |
| 107 gpr_inf_future(GPR_CLOCK_REALTIME), NULL); |
| 108 } |
| 109 |
| 110 - (GRPCChannel *)channel { |
| 111 // Create it lazily, because we don't want to open a connection just because s
omeone is |
| 112 // configuring a host. |
| 113 |
| 114 if (!_channel) { |
| 115 NSMutableDictionary *args = [NSMutableDictionary dictionary]; |
| 116 |
| 117 // TODO(jcanizales): Add OS and device information (see |
| 118 // https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#user-agent
s ). |
| 119 NSString *userAgent = @"grpc-objc/" GRPC_OBJC_VERSION_STRING; |
| 120 if (_userAgentPrefix) { |
| 121 userAgent = [@[_userAgentPrefix, userAgent] componentsJoinedByString:@" "]
; |
| 122 } |
| 123 args[@GRPC_ARG_PRIMARY_USER_AGENT_STRING] = userAgent; |
| 124 |
| 125 if (_secure) { |
| 126 if (_hostNameOverride) { |
| 127 args[@GRPC_SSL_TARGET_NAME_OVERRIDE_ARG] = _hostNameOverride; |
| 128 } |
| 129 |
| 130 _channel = [GRPCChannel secureChannelWithHost:_address |
| 131 pathToCertificates:_pathToCertificates |
| 132 channelArgs:args]; |
| 133 } else { |
| 134 _channel = [GRPCChannel insecureChannelWithHost:_address channelArgs:args]
; |
| 135 } |
| 136 } |
| 137 return _channel; |
| 138 } |
| 139 |
| 140 - (NSString *)hostName { |
| 141 // TODO(jcanizales): Default to nil instead of _address when Issue #2635 is cl
arified. |
| 142 return _hostNameOverride ?: _address; |
| 143 } |
| 144 |
| 145 // TODO(jcanizales): Don't let set |secure| to |NO| if |pathToCertificates| or |
hostNameOverride| |
| 146 // have been set. Don't let set either of the latter if |secure| has been set to
|NO|. |
| 147 |
| 148 @end |
OLD | NEW |