1 |
efrain |
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 |
|
|
|
17 |
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORTransport.h"
|
|
|
18 |
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORTransport_Private.h"
|
|
|
19 |
|
|
|
20 |
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORAssert.h"
|
|
|
21 |
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORClock.h"
|
|
|
22 |
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCOREvent.h"
|
|
|
23 |
|
|
|
24 |
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORTransformer.h"
|
|
|
25 |
|
|
|
26 |
@implementation GDTCORTransport
|
|
|
27 |
|
|
|
28 |
- (nullable instancetype)initWithMappingID:(NSString *)mappingID
|
|
|
29 |
transformers:
|
|
|
30 |
(nullable NSArray<id<GDTCOREventTransformer>> *)transformers
|
|
|
31 |
target:(GDTCORTarget)target {
|
|
|
32 |
GDTCORAssert(mappingID.length > 0, @"A mapping ID cannot be nil or empty");
|
|
|
33 |
GDTCORAssert(target > 0, @"A target cannot be negative or 0");
|
|
|
34 |
if (mappingID == nil || mappingID.length == 0 || target <= 0) {
|
|
|
35 |
return nil;
|
|
|
36 |
}
|
|
|
37 |
self = [super init];
|
|
|
38 |
if (self) {
|
|
|
39 |
_mappingID = mappingID;
|
|
|
40 |
_transformers = transformers;
|
|
|
41 |
_target = target;
|
|
|
42 |
_transformerInstance = [GDTCORTransformer sharedInstance];
|
|
|
43 |
}
|
|
|
44 |
GDTCORLogDebug(@"Transport object created. mappingID:%@ transformers:%@ target:%ld", mappingID,
|
|
|
45 |
transformers, (long)target);
|
|
|
46 |
return self;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
- (void)sendTelemetryEvent:(GDTCOREvent *)event
|
|
|
50 |
onComplete:
|
|
|
51 |
(void (^_Nullable)(BOOL wasWritten, NSError *_Nullable error))completion {
|
|
|
52 |
event.qosTier = GDTCOREventQoSTelemetry;
|
|
|
53 |
[self sendEvent:event onComplete:completion];
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
- (void)sendDataEvent:(GDTCOREvent *)event
|
|
|
57 |
onComplete:(void (^_Nullable)(BOOL wasWritten, NSError *_Nullable error))completion {
|
|
|
58 |
GDTCORAssert(event.qosTier != GDTCOREventQoSTelemetry, @"Use -sendTelemetryEvent, please.");
|
|
|
59 |
[self sendEvent:event onComplete:completion];
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
- (void)sendTelemetryEvent:(GDTCOREvent *)event {
|
|
|
63 |
[self sendTelemetryEvent:event onComplete:nil];
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
- (void)sendDataEvent:(GDTCOREvent *)event {
|
|
|
67 |
[self sendDataEvent:event onComplete:nil];
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
- (GDTCOREvent *)eventForTransport {
|
|
|
71 |
return [[GDTCOREvent alloc] initWithMappingID:_mappingID target:_target];
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
#pragma mark - Private helper methods
|
|
|
75 |
|
|
|
76 |
/** Sends the given event through the transport pipeline.
|
|
|
77 |
*
|
|
|
78 |
* @param event The event to send.
|
|
|
79 |
* @param completion A block that will be called when the event has been written or dropped.
|
|
|
80 |
*/
|
|
|
81 |
- (void)sendEvent:(GDTCOREvent *)event
|
|
|
82 |
onComplete:(void (^_Nullable)(BOOL wasWritten, NSError *_Nullable error))completion {
|
|
|
83 |
// TODO: Determine if sending an event before registration is allowed.
|
|
|
84 |
GDTCORAssert(event, @"You can't send a nil event");
|
|
|
85 |
GDTCOREvent *copiedEvent = [event copy];
|
|
|
86 |
copiedEvent.clockSnapshot = [GDTCORClock snapshot];
|
|
|
87 |
[self.transformerInstance transformEvent:copiedEvent
|
|
|
88 |
withTransformers:_transformers
|
|
|
89 |
onComplete:completion];
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
@end
|