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/GDTCOREvent.h"
|
|
|
18 |
|
|
|
19 |
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORAssert.h"
|
|
|
20 |
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORPlatform.h"
|
|
|
21 |
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORStorageProtocol.h"
|
|
|
22 |
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORClock.h"
|
|
|
23 |
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORConsoleLogger.h"
|
|
|
24 |
|
|
|
25 |
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCOREvent_Private.h"
|
|
|
26 |
|
|
|
27 |
@implementation GDTCOREvent
|
|
|
28 |
|
|
|
29 |
+ (NSString *)nextEventID {
|
|
|
30 |
// Replace special non-alphanumeric characters to avoid potential conflicts with storage logic.
|
|
|
31 |
return [[NSUUID UUID].UUIDString stringByReplacingOccurrencesOfString:@"-" withString:@""];
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
- (nullable instancetype)initWithMappingID:(NSString *)mappingID target:(GDTCORTarget)target {
|
|
|
35 |
GDTCORAssert(mappingID.length > 0, @"Please give a valid mapping ID");
|
|
|
36 |
GDTCORAssert(target > 0, @"A target cannot be negative or 0");
|
|
|
37 |
if (mappingID.length == 0 || target <= 0) {
|
|
|
38 |
return nil;
|
|
|
39 |
}
|
|
|
40 |
self = [super init];
|
|
|
41 |
if (self) {
|
|
|
42 |
_eventID = [GDTCOREvent nextEventID];
|
|
|
43 |
_mappingID = mappingID;
|
|
|
44 |
_target = target;
|
|
|
45 |
_qosTier = GDTCOREventQosDefault;
|
|
|
46 |
_expirationDate = [NSDate dateWithTimeIntervalSinceNow:604800]; // 7 days.
|
|
|
47 |
|
|
|
48 |
GDTCORLogDebug(@"Event %@ created. ID:%@ mappingID: %@ target:%ld", self, _eventID, mappingID,
|
|
|
49 |
(long)target);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
return self;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
- (instancetype)copy {
|
|
|
56 |
GDTCOREvent *copy = [[GDTCOREvent alloc] initWithMappingID:_mappingID target:_target];
|
|
|
57 |
copy->_eventID = _eventID;
|
|
|
58 |
copy.dataObject = _dataObject;
|
|
|
59 |
copy.qosTier = _qosTier;
|
|
|
60 |
copy.clockSnapshot = _clockSnapshot;
|
|
|
61 |
copy.customBytes = _customBytes;
|
|
|
62 |
GDTCORLogDebug(@"Copying event %@ to event %@", self, copy);
|
|
|
63 |
return copy;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
- (NSUInteger)hash {
|
|
|
67 |
// This loses some precision, but it's probably fine.
|
|
|
68 |
NSUInteger eventIDHash = [_eventID hash];
|
|
|
69 |
NSUInteger mappingIDHash = [_mappingID hash];
|
|
|
70 |
NSUInteger timeHash = [_clockSnapshot hash];
|
|
|
71 |
NSInteger serializedBytesHash = [_serializedDataObjectBytes hash];
|
|
|
72 |
|
|
|
73 |
return eventIDHash ^ mappingIDHash ^ _target ^ _qosTier ^ timeHash ^ serializedBytesHash;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
- (BOOL)isEqual:(id)object {
|
|
|
77 |
return [self hash] == [object hash];
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
#pragma mark - Property overrides
|
|
|
81 |
|
|
|
82 |
- (void)setDataObject:(id<GDTCOREventDataObject>)dataObject {
|
|
|
83 |
// If you're looking here because of a performance issue in -transportBytes slowing the assignment
|
|
|
84 |
// of -dataObject, one way to address this is to add a queue to this class,
|
|
|
85 |
// dispatch_(barrier_ if concurrent)async here, and implement the getter with a dispatch_sync.
|
|
|
86 |
if (dataObject != _dataObject) {
|
|
|
87 |
_dataObject = dataObject;
|
|
|
88 |
}
|
|
|
89 |
self->_serializedDataObjectBytes = [dataObject transportBytes];
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
#pragma mark - NSSecureCoding and NSCoding Protocols
|
|
|
93 |
|
|
|
94 |
/** NSCoding key for eventID property. */
|
|
|
95 |
static NSString *kEventIDKey = @"GDTCOREventEventIDKey";
|
|
|
96 |
|
|
|
97 |
/** NSCoding key for mappingID property. */
|
|
|
98 |
static NSString *kMappingIDKey = @"GDTCOREventMappingIDKey";
|
|
|
99 |
|
|
|
100 |
/** NSCoding key for target property. */
|
|
|
101 |
static NSString *kTargetKey = @"GDTCOREventTargetKey";
|
|
|
102 |
|
|
|
103 |
/** NSCoding key for qosTier property. */
|
|
|
104 |
static NSString *kQoSTierKey = @"GDTCOREventQoSTierKey";
|
|
|
105 |
|
|
|
106 |
/** NSCoding key for clockSnapshot property. */
|
|
|
107 |
static NSString *kClockSnapshotKey = @"GDTCOREventClockSnapshotKey";
|
|
|
108 |
|
|
|
109 |
/** NSCoding key for expirationDate property. */
|
|
|
110 |
static NSString *kExpirationDateKey = @"GDTCOREventExpirationDateKey";
|
|
|
111 |
|
|
|
112 |
/** NSCoding key for serializedDataObjectBytes property. */
|
|
|
113 |
static NSString *kSerializedDataObjectBytes = @"GDTCOREventSerializedDataObjectBytesKey";
|
|
|
114 |
|
|
|
115 |
/** NSCoding key for customData property. */
|
|
|
116 |
static NSString *kCustomDataKey = @"GDTCOREventCustomDataKey";
|
|
|
117 |
|
|
|
118 |
+ (BOOL)supportsSecureCoding {
|
|
|
119 |
return YES;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
- (id)initWithCoder:(NSCoder *)aDecoder {
|
|
|
123 |
self = [self init];
|
|
|
124 |
if (self) {
|
|
|
125 |
_mappingID = [aDecoder decodeObjectOfClass:[NSString class] forKey:kMappingIDKey];
|
|
|
126 |
_target = [aDecoder decodeIntegerForKey:kTargetKey];
|
|
|
127 |
_eventID = [aDecoder decodeObjectOfClass:[NSString class] forKey:kEventIDKey]
|
|
|
128 |
?: [GDTCOREvent nextEventID];
|
|
|
129 |
_qosTier = [aDecoder decodeIntegerForKey:kQoSTierKey];
|
|
|
130 |
_clockSnapshot = [aDecoder decodeObjectOfClass:[GDTCORClock class] forKey:kClockSnapshotKey];
|
|
|
131 |
_customBytes = [aDecoder decodeObjectOfClass:[NSData class] forKey:kCustomDataKey];
|
|
|
132 |
_expirationDate = [aDecoder decodeObjectOfClass:[NSDate class] forKey:kExpirationDateKey];
|
|
|
133 |
_serializedDataObjectBytes = [aDecoder decodeObjectOfClass:[NSData class]
|
|
|
134 |
forKey:kSerializedDataObjectBytes];
|
|
|
135 |
if (!_serializedDataObjectBytes) {
|
|
|
136 |
return nil;
|
|
|
137 |
}
|
|
|
138 |
}
|
|
|
139 |
return self;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
- (void)encodeWithCoder:(NSCoder *)aCoder {
|
|
|
143 |
[aCoder encodeObject:_eventID forKey:kEventIDKey];
|
|
|
144 |
[aCoder encodeObject:_mappingID forKey:kMappingIDKey];
|
|
|
145 |
[aCoder encodeInteger:_target forKey:kTargetKey];
|
|
|
146 |
[aCoder encodeInteger:_qosTier forKey:kQoSTierKey];
|
|
|
147 |
[aCoder encodeObject:_clockSnapshot forKey:kClockSnapshotKey];
|
|
|
148 |
[aCoder encodeObject:_customBytes forKey:kCustomDataKey];
|
|
|
149 |
[aCoder encodeObject:_expirationDate forKey:kExpirationDateKey];
|
|
|
150 |
[aCoder encodeObject:self.serializedDataObjectBytes forKey:kSerializedDataObjectBytes];
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
@end
|