GDTCORTransport.m 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORTransport.h"
  17. #import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORTransport_Private.h"
  18. #import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORAssert.h"
  19. #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORClock.h"
  20. #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCOREvent.h"
  21. #import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORTransformer.h"
  22. @implementation GDTCORTransport
  23. - (nullable instancetype)initWithMappingID:(NSString *)mappingID
  24. transformers:
  25. (nullable NSArray<id<GDTCOREventTransformer>> *)transformers
  26. target:(GDTCORTarget)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:
  45. (void (^_Nullable)(BOOL wasWritten, NSError *_Nullable error))completion {
  46. event.qosTier = GDTCOREventQoSTelemetry;
  47. [self sendEvent:event onComplete:completion];
  48. }
  49. - (void)sendDataEvent:(GDTCOREvent *)event
  50. onComplete:(void (^_Nullable)(BOOL wasWritten, NSError *_Nullable error))completion {
  51. GDTCORAssert(event.qosTier != GDTCOREventQoSTelemetry, @"Use -sendTelemetryEvent, please.");
  52. [self sendEvent:event onComplete:completion];
  53. }
  54. - (void)sendTelemetryEvent:(GDTCOREvent *)event {
  55. [self sendTelemetryEvent:event onComplete:nil];
  56. }
  57. - (void)sendDataEvent:(GDTCOREvent *)event {
  58. [self sendDataEvent:event onComplete:nil];
  59. }
  60. - (GDTCOREvent *)eventForTransport {
  61. return [[GDTCOREvent alloc] initWithMappingID:_mappingID target:_target];
  62. }
  63. #pragma mark - Private helper methods
  64. /** Sends the given event through the transport pipeline.
  65. *
  66. * @param event The event to send.
  67. * @param completion A block that will be called when the event has been written or dropped.
  68. */
  69. - (void)sendEvent:(GDTCOREvent *)event
  70. onComplete:(void (^_Nullable)(BOOL wasWritten, NSError *_Nullable error))completion {
  71. // TODO: Determine if sending an event before registration is allowed.
  72. GDTCORAssert(event, @"You can't send a nil event");
  73. GDTCOREvent *copiedEvent = [event copy];
  74. copiedEvent.clockSnapshot = [GDTCORClock snapshot];
  75. [self.transformerInstance transformEvent:copiedEvent
  76. withTransformers:_transformers
  77. onComplete:completion];
  78. }
  79. @end