GDTCORTransport.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. - (instancetype)initWithMappingID:(NSString *)mappingID
  24. transformers:(nullable NSArray<id<GDTCOREventTransformer>> *)transformers
  25. target:(NSInteger)target {
  26. GDTCORAssert(mappingID.length > 0, @"A mapping ID cannot be nil or empty");
  27. GDTCORAssert(target > 0, @"A target cannot be negative or 0");
  28. if (mappingID == nil || mappingID.length == 0 || target <= 0) {
  29. return nil;
  30. }
  31. self = [super init];
  32. if (self) {
  33. _mappingID = mappingID;
  34. _transformers = transformers;
  35. _target = target;
  36. _transformerInstance = [GDTCORTransformer sharedInstance];
  37. }
  38. return self;
  39. }
  40. - (void)sendTelemetryEvent:(GDTCOREvent *)event {
  41. // TODO: Determine if sending an event before registration is allowed.
  42. GDTCORAssert(event, @"You can't send a nil event");
  43. GDTCOREvent *copiedEvent = [event copy];
  44. copiedEvent.qosTier = GDTCOREventQoSTelemetry;
  45. copiedEvent.clockSnapshot = [GDTCORClock snapshot];
  46. [self.transformerInstance transformEvent:copiedEvent withTransformers:_transformers];
  47. }
  48. - (void)sendDataEvent:(GDTCOREvent *)event {
  49. // TODO: Determine if sending an event before registration is allowed.
  50. GDTCORAssert(event, @"You can't send a nil event");
  51. GDTCORAssert(event.qosTier != GDTCOREventQoSTelemetry, @"Use -sendTelemetryEvent, please.");
  52. GDTCOREvent *copiedEvent = [event copy];
  53. copiedEvent.clockSnapshot = [GDTCORClock snapshot];
  54. [self.transformerInstance transformEvent:copiedEvent withTransformers:_transformers];
  55. }
  56. - (GDTCOREvent *)eventForTransport {
  57. return [[GDTCOREvent alloc] initWithMappingID:_mappingID target:_target];
  58. }
  59. @end