2019-09-09 15:21:29 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright 2018 Google
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-07-31 08:12:23 -07:00
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORTransport.h"
|
2020-06-24 17:50:15 -07:00
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORTransport_Private.h"
|
2019-09-09 15:21:29 -07:00
|
|
|
|
2020-10-01 20:56:26 -04:00
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORAssert.h"
|
2020-07-31 08:12:23 -07:00
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORClock.h"
|
|
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCOREvent.h"
|
2019-09-09 15:21:29 -07:00
|
|
|
|
2020-06-24 17:50:15 -07:00
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORTransformer.h"
|
2019-09-09 15:21:29 -07:00
|
|
|
|
|
|
|
|
@implementation GDTCORTransport
|
|
|
|
|
|
2019-09-23 15:41:58 -07:00
|
|
|
- (nullable instancetype)initWithMappingID:(NSString *)mappingID
|
|
|
|
|
transformers:
|
|
|
|
|
(nullable NSArray<id<GDTCOREventTransformer>> *)transformers
|
2020-06-23 16:04:18 -07:00
|
|
|
target:(GDTCORTarget)target {
|
2019-09-09 15:21:29 -07:00
|
|
|
GDTCORAssert(mappingID.length > 0, @"A mapping ID cannot be nil or empty");
|
|
|
|
|
GDTCORAssert(target > 0, @"A target cannot be negative or 0");
|
|
|
|
|
if (mappingID == nil || mappingID.length == 0 || target <= 0) {
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
self = [super init];
|
|
|
|
|
if (self) {
|
|
|
|
|
_mappingID = mappingID;
|
|
|
|
|
_transformers = transformers;
|
|
|
|
|
_target = target;
|
|
|
|
|
_transformerInstance = [GDTCORTransformer sharedInstance];
|
|
|
|
|
}
|
2020-04-14 12:30:29 -07:00
|
|
|
GDTCORLogDebug(@"Transport object created. mappingID:%@ transformers:%@ target:%ld", mappingID,
|
2020-03-12 15:02:41 -07:00
|
|
|
transformers, (long)target);
|
2019-09-09 15:21:29 -07:00
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-03 08:15:31 -08:00
|
|
|
- (void)sendTelemetryEvent:(GDTCOREvent *)event
|
2020-04-09 13:14:15 -07:00
|
|
|
onComplete:
|
|
|
|
|
(void (^_Nullable)(BOOL wasWritten, NSError *_Nullable error))completion {
|
2020-02-03 08:15:31 -08:00
|
|
|
event.qosTier = GDTCOREventQoSTelemetry;
|
2020-03-18 15:00:37 -07:00
|
|
|
[self sendEvent:event onComplete:completion];
|
2020-02-03 08:15:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)sendDataEvent:(GDTCOREvent *)event
|
2020-04-09 13:14:15 -07:00
|
|
|
onComplete:(void (^_Nullable)(BOOL wasWritten, NSError *_Nullable error))completion {
|
2020-02-03 08:15:31 -08:00
|
|
|
GDTCORAssert(event.qosTier != GDTCOREventQoSTelemetry, @"Use -sendTelemetryEvent, please.");
|
2020-03-18 15:00:37 -07:00
|
|
|
[self sendEvent:event onComplete:completion];
|
2020-02-03 08:15:31 -08:00
|
|
|
}
|
|
|
|
|
|
2019-09-09 15:21:29 -07:00
|
|
|
- (void)sendTelemetryEvent:(GDTCOREvent *)event {
|
2020-03-18 15:00:37 -07:00
|
|
|
[self sendTelemetryEvent:event onComplete:nil];
|
2019-09-09 15:21:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)sendDataEvent:(GDTCOREvent *)event {
|
2020-03-18 15:00:37 -07:00
|
|
|
[self sendDataEvent:event onComplete:nil];
|
2019-09-09 15:21:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (GDTCOREvent *)eventForTransport {
|
|
|
|
|
return [[GDTCOREvent alloc] initWithMappingID:_mappingID target:_target];
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-03 08:15:31 -08:00
|
|
|
#pragma mark - Private helper methods
|
|
|
|
|
|
|
|
|
|
/** Sends the given event through the transport pipeline.
|
|
|
|
|
*
|
|
|
|
|
* @param event The event to send.
|
|
|
|
|
* @param completion A block that will be called when the event has been written or dropped.
|
|
|
|
|
*/
|
|
|
|
|
- (void)sendEvent:(GDTCOREvent *)event
|
2020-04-09 13:14:15 -07:00
|
|
|
onComplete:(void (^_Nullable)(BOOL wasWritten, NSError *_Nullable error))completion {
|
2020-02-03 08:15:31 -08:00
|
|
|
// TODO: Determine if sending an event before registration is allowed.
|
|
|
|
|
GDTCORAssert(event, @"You can't send a nil event");
|
|
|
|
|
GDTCOREvent *copiedEvent = [event copy];
|
|
|
|
|
copiedEvent.clockSnapshot = [GDTCORClock snapshot];
|
|
|
|
|
[self.transformerInstance transformEvent:copiedEvent
|
|
|
|
|
withTransformers:_transformers
|
|
|
|
|
onComplete:completion];
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 15:21:29 -07:00
|
|
|
@end
|