GDTCORTransport.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/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. return self;
  40. }
  41. - (void)sendTelemetryEvent:(GDTCOREvent *)event {
  42. // TODO: Determine if sending an event before registration is allowed.
  43. GDTCORAssert(event, @"You can't send a nil event");
  44. GDTCOREvent *copiedEvent = [event copy];
  45. copiedEvent.qosTier = GDTCOREventQoSTelemetry;
  46. copiedEvent.clockSnapshot = [GDTCORClock snapshot];
  47. [self.transformerInstance transformEvent:copiedEvent withTransformers:_transformers];
  48. }
  49. - (void)sendDataEvent:(GDTCOREvent *)event {
  50. // TODO: Determine if sending an event before registration is allowed.
  51. GDTCORAssert(event, @"You can't send a nil event");
  52. GDTCORAssert(event.qosTier != GDTCOREventQoSTelemetry, @"Use -sendTelemetryEvent, please.");
  53. GDTCOREvent *copiedEvent = [event copy];
  54. copiedEvent.clockSnapshot = [GDTCORClock snapshot];
  55. [self.transformerInstance transformEvent:copiedEvent withTransformers:_transformers];
  56. }
  57. - (GDTCOREvent *)eventForTransport {
  58. return [[GDTCOREvent alloc] initWithMappingID:_mappingID target:_target];
  59. }
  60. @end