GDTCORTransport.m 3.3 KB

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