GDTCORTransport.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "GDTCORLibrary/Public/GDTCORTransport.h"
  17. #import "GDTCORLibrary/Private/GDTCORTransport_Private.h"
  18. #import <GoogleDataTransport/GDTCORAssert.h>
  19. #import <GoogleDataTransport/GDTCORClock.h>
  20. #import <GoogleDataTransport/GDTCOREvent.h>
  21. #import "GDTCORLibrary/Private/GDTCORTransformer.h"
  22. @implementation GDTCORTransport
  23. - (nullable instancetype)initWithMappingID:(NSString *)mappingID
  24. transformers:
  25. (nullable NSArray<id<GDTCOREventTransformer>> *)transformers
  26. target:(NSInteger)target {
  27. GDTCORAssert(mappingID.length > 0, @"A mapping ID cannot be nil or empty");
  28. GDTCORAssert(target > 0, @"A target cannot be negative or 0");
  29. if (mappingID == nil || mappingID.length == 0 || target <= 0) {
  30. return nil;
  31. }
  32. self = [super init];
  33. if (self) {
  34. _mappingID = mappingID;
  35. _transformers = transformers;
  36. _target = target;
  37. _transformerInstance = [GDTCORTransformer sharedInstance];
  38. }
  39. GDTCORLogDebug("Transport object created. mappingID:%@ transformers:%@ target:%ld", _mappingID,
  40. _transformers, (long)_target);
  41. return self;
  42. }
  43. - (void)sendTelemetryEvent:(GDTCOREvent *)event
  44. onComplete:(void (^)(BOOL wasWritten, NSError *_Nullable error))completion {
  45. event.qosTier = GDTCOREventQoSTelemetry;
  46. [self sendEvent:event
  47. onComplete:^(BOOL wasWritten, NSError *error) {
  48. GDTCORLogDebug("Telemetry event sent: %@", event);
  49. if (completion) {
  50. completion(wasWritten, nil);
  51. }
  52. }];
  53. }
  54. - (void)sendDataEvent:(GDTCOREvent *)event
  55. onComplete:(void (^)(BOOL wasWritten, NSError *_Nullable error))completion {
  56. GDTCORAssert(event.qosTier != GDTCOREventQoSTelemetry, @"Use -sendTelemetryEvent, please.");
  57. [self sendEvent:event
  58. onComplete:^(BOOL wasWritten, NSError *error) {
  59. GDTCORLogDebug("Data event sent: %@", event);
  60. if (completion) {
  61. completion(wasWritten, nil);
  62. }
  63. }];
  64. }
  65. - (void)sendTelemetryEvent:(GDTCOREvent *)event {
  66. [self sendTelemetryEvent:event
  67. onComplete:^(BOOL wasWritten, NSError *_Nullable error){
  68. }];
  69. }
  70. - (void)sendDataEvent:(GDTCOREvent *)event {
  71. [self sendDataEvent:event
  72. onComplete:^(BOOL wasWritten, NSError *_Nullable error){
  73. }];
  74. }
  75. - (GDTCOREvent *)eventForTransport {
  76. return [[GDTCOREvent alloc] initWithMappingID:_mappingID target:_target];
  77. }
  78. #pragma mark - Private helper methods
  79. /** Sends the given event through the transport pipeline.
  80. *
  81. * @param event The event to send.
  82. * @param completion A block that will be called when the event has been written or dropped.
  83. */
  84. - (void)sendEvent:(GDTCOREvent *)event
  85. onComplete:(void (^)(BOOL wasWritten, NSError *error))completion {
  86. // TODO: Determine if sending an event before registration is allowed.
  87. GDTCORAssert(event, @"You can't send a nil event");
  88. GDTCOREvent *copiedEvent = [event copy];
  89. copiedEvent.clockSnapshot = [GDTCORClock snapshot];
  90. [self.transformerInstance transformEvent:copiedEvent
  91. withTransformers:_transformers
  92. onComplete:completion];
  93. }
  94. @end