GRPCTransport.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. *
  3. * Copyright 2019 gRPC authors.
  4. *
  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
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  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.
  16. *
  17. */
  18. #import "GRPCTransport.h"
  19. static const GRPCTransportId gGRPCCoreSecureId = "io.grpc.transport.core.secure";
  20. static const GRPCTransportId gGRPCCoreInsecureId = "io.grpc.transport.core.insecure";
  21. const struct GRPCDefaultTransportImplList GRPCDefaultTransportImplList = {
  22. .core_secure = gGRPCCoreSecureId, .core_insecure = gGRPCCoreInsecureId};
  23. static const GRPCTransportId gDefaultTransportId = gGRPCCoreSecureId;
  24. static GRPCTransportRegistry *gTransportRegistry = nil;
  25. static dispatch_once_t initTransportRegistry;
  26. BOOL TransportIdIsEqual(GRPCTransportId lhs, GRPCTransportId rhs) {
  27. // Directly comparing pointers works because we require users to use the id provided by each
  28. // implementation, not coming up with their own string.
  29. return lhs == rhs;
  30. }
  31. NSUInteger TransportIdHash(GRPCTransportId transportId) {
  32. if (transportId == NULL) {
  33. transportId = gDefaultTransportId;
  34. }
  35. return [NSString stringWithCString:transportId encoding:NSUTF8StringEncoding].hash;
  36. }
  37. @implementation GRPCTransportRegistry {
  38. NSMutableDictionary<NSString *, id<GRPCTransportFactory>> *_registry;
  39. id<GRPCTransportFactory> _defaultFactory;
  40. }
  41. + (instancetype)sharedInstance {
  42. dispatch_once(&initTransportRegistry, ^{
  43. gTransportRegistry = [[GRPCTransportRegistry alloc] init];
  44. NSAssert(gTransportRegistry != nil, @"Unable to initialize transport registry.");
  45. if (gTransportRegistry == nil) {
  46. NSLog(@"Unable to initialize transport registry.");
  47. [NSException raise:NSGenericException format:@"Unable to initialize transport registry."];
  48. }
  49. });
  50. return gTransportRegistry;
  51. }
  52. - (instancetype)init {
  53. if ((self = [super init])) {
  54. _registry = [NSMutableDictionary dictionary];
  55. }
  56. return self;
  57. }
  58. - (void)registerTransportWithId:(GRPCTransportId)transportId
  59. factory:(id<GRPCTransportFactory>)factory {
  60. NSString *nsTransportId = [NSString stringWithCString:transportId encoding:NSUTF8StringEncoding];
  61. NSAssert(_registry[nsTransportId] == nil, @"The transport %@ has already been registered.",
  62. nsTransportId);
  63. if (_registry[nsTransportId] != nil) {
  64. NSLog(@"The transport %@ has already been registered.", nsTransportId);
  65. return;
  66. }
  67. _registry[nsTransportId] = factory;
  68. // if the default transport is registered, mark it.
  69. if (0 == strcmp(transportId, gDefaultTransportId)) {
  70. _defaultFactory = factory;
  71. }
  72. }
  73. - (id<GRPCTransportFactory>)getTransportFactoryWithId:(GRPCTransportId)transportId {
  74. if (transportId == NULL) {
  75. if (_defaultFactory == nil) {
  76. [NSException raise:NSInvalidArgumentException
  77. format:@"Unable to get default transport factory"];
  78. return nil;
  79. }
  80. return _defaultFactory;
  81. }
  82. NSString *nsTransportId = [NSString stringWithCString:transportId encoding:NSUTF8StringEncoding];
  83. id<GRPCTransportFactory> transportFactory = _registry[nsTransportId];
  84. if (transportFactory == nil) {
  85. // User named a transport id that was not registered with the registry.
  86. [NSException raise:NSInvalidArgumentException
  87. format:@"Unable to get transport factory with id %s", transportId];
  88. return nil;
  89. }
  90. return transportFactory;
  91. }
  92. @end
  93. @implementation GRPCTransport
  94. - (dispatch_queue_t)dispatchQueue {
  95. [NSException raise:NSGenericException
  96. format:@"Implementations should override the dispatch queue"];
  97. return nil;
  98. }
  99. - (void)startWithRequestOptions:(nonnull GRPCRequestOptions *)requestOptions
  100. callOptions:(nonnull GRPCCallOptions *)callOptions {
  101. [NSException raise:NSGenericException
  102. format:@"Implementations should override the methods of GRPCTransport"];
  103. }
  104. - (void)writeData:(nonnull id)data {
  105. [NSException raise:NSGenericException
  106. format:@"Implementations should override the methods of GRPCTransport"];
  107. }
  108. - (void)cancel {
  109. [NSException raise:NSGenericException
  110. format:@"Implementations should override the methods of GRPCTransport"];
  111. }
  112. - (void)finish {
  113. [NSException raise:NSGenericException
  114. format:@"Implementations should override the methods of GRPCTransport"];
  115. }
  116. - (void)receiveNextMessages:(NSUInteger)numberOfMessages {
  117. [NSException raise:NSGenericException
  118. format:@"Implementations should override the methods of GRPCTransport"];
  119. }
  120. @end