/
RudderIntegrationManager.cs
313 lines (292 loc) · 12.6 KB
/
RudderIntegrationManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using UnityEngine;
namespace RudderStack
{
class RudderIntegrationManager
{
private Dictionary<string, object> _rudderServerConfig = null;
private string _serverConfigJson = null;
private bool _isFactoryPrepared = false;
private Dictionary<string, RudderIntegration> _integrationOpsMap = new Dictionary<string, RudderIntegration>();
private string _writeKey = null;
private RudderConfig _config = null;
private object _lockingObj = new object();
private List<RudderMessage> _factoryDumpQueue = new List<RudderMessage>();
private string _persistedUserId = null;
private RudderTraits _persistedTraits = null;
private long _lastUpdatedTimestamp = 0;
public RudderIntegrationManager(string writeKey, RudderConfig config)
{
this._writeKey = writeKey;
this._config = config;
ServicePointManager.ServerCertificateValidationCallback = Validator;
DownloadIntegrations();
}
private void DownloadIntegrations()
{
try
{
lock (this._lockingObj)
{
this._lastUpdatedTimestamp = long.Parse(PlayerPrefs.GetString("rl_server_update_time", "0"));
RudderLogger.LogDebug("RudderIntegrationManager: downloadIntegrations: lastUpdatedTimeStamp: " + _lastUpdatedTimestamp);
this._serverConfigJson = PlayerPrefs.GetString("rl_server_config", null);
RudderLogger.LogDebug("RudderIntegrationManager: downloadIntegrations: serverConfigJson: " + this._serverConfigJson);
}
if (this._serverConfigJson == null || this._serverConfigJson.Equals("") || IsServerConfigOutdated())
{
Thread t = new Thread(DownloadConfig);
t.Start();
}
}
catch (Exception ex)
{
RudderLogger.LogError("CreateConnection ERROR: downloadConfig error");
}
}
private void PrepareFactories()
{
try
{
if (this._rudderServerConfig == null)
{
RudderLogger.LogInfo("No integrations: rudderServerConfig is null");
}
else if (this._config.factories == null)
{
RudderLogger.LogInfo("No integrations: config.factories is null");
}
else if (this._config.factories.Count == 0)
{
// no factory to initialize
RudderLogger.LogInfo("No integrations: config.factories.Count is 0");
}
else
{
RudderClient client = RudderClient.GetInstance();
Dictionary<string, object> source = this._rudderServerConfig["source"] as Dictionary<string, object>;
List<object> destinations = source["destinations"] as List<object>;
if (destinations.Count > 0)
{
Dictionary<string, object> destinationMap = new Dictionary<string, object>();
RudderLogger.LogDebug("Native SDKs enabled in Dashboard");
foreach (var destinationObj in destinations)
{
Dictionary<string, object> destination = destinationObj as Dictionary<string, object>;
Dictionary<string, object> destinationDefinition = destination["destinationDefinition"] as Dictionary<string, object>;
string destinationName = destinationDefinition["displayName"] as string;
RudderLogger.LogDebug("Extracted Native Destination information: " + destinationName);
destinationMap[destinationName] = destination;
}
foreach (RudderIntegrationFactory factory in this._config.factories)
{
string factoryKey = factory.Key();
RudderLogger.LogDebug("Initiating native destination factory: " + factoryKey);
if (destinationMap.ContainsKey(factoryKey))
{
Dictionary<string, object> serverDestination = destinationMap[factoryKey] as Dictionary<string, object>;
if (serverDestination != null)
{
bool? isDestinationEnabled = serverDestination["enabled"] as bool?;
if (isDestinationEnabled != null && isDestinationEnabled == true)
{
Dictionary<string, object> destinationConfig = serverDestination["config"] as Dictionary<string, object>;
RudderIntegration integrationOp = factory.Create(destinationConfig, client, this._config);
RudderLogger.LogDebug("Native integration initiated for " + factoryKey);
this._integrationOpsMap[factoryKey] = integrationOp;
}
}
}
}
}
}
this._isFactoryPrepared = true;
lock (this._lockingObj)
{
if (this._factoryDumpQueue.Count > 0)
{
for (int index = 0; index < this._factoryDumpQueue.Count; index++)
{
this.MakeIntegrationDump(this._factoryDumpQueue[index]);
}
this._factoryDumpQueue.Clear();
}
if (_persistedTraits != null && _persistedUserId != null)
{
this.MakeIntegrationIdentify(_persistedUserId, _persistedTraits);
this._persistedTraits = null;
this._persistedUserId = null;
}
}
}
catch (Exception ex)
{
RudderLogger.LogError(ex.Message);
}
}
private Dictionary<string, object> ParseServerConfig(string configJson)
{
if (configJson == null || configJson.Equals(""))
{
RudderLogger.LogDebug("parseServerConfig: configJson is null");
return null;
}
try
{
return MiniJSON.Json.Deserialize(configJson) as Dictionary<string, object>;
}
catch (Exception ex)
{
RudderLogger.LogError(ex.Message);
}
return null;
}
private bool IsServerConfigOutdated()
{
if (this._lastUpdatedTimestamp == 0)
{
// no config available. cold start
return true;
}
return (Stopwatch.GetTimestamp() - this._lastUpdatedTimestamp) > (_config.configRefreshInterval * 60 * 60 * 1000);
}
void DownloadConfig(object obj)
{
bool isDone = false;
int retryCount = 0;
int retryTimeOut = 10;
while (!isDone && retryCount <= 3)
{
try
{
string configEndpointUrl = _config.controlPlaneUrl + "/sourceConfig";
RudderLogger.LogDebug("configEndpontUrl: " + configEndpointUrl);
// create http request object
var http = (HttpWebRequest)WebRequest.Create(new Uri(configEndpointUrl));
http.Method = "GET";
var authKeyBytes = System.Text.Encoding.UTF8.GetBytes(_writeKey + ":");
string authHeader = System.Convert.ToBase64String(authKeyBytes);
http.Headers.Add("Authorization", "Basic " + authHeader);
// get the response
var response = http.GetResponse();
var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
// return the response as a string
string responseJson = sr.ReadToEnd();
RudderLogger.LogDebug("Config Server Response: " + responseJson);
if (responseJson != null)
{
lock (this._lockingObj)
{
this._serverConfigJson = responseJson;
}
isDone = true;
}
else
{
retryCount += 1;
Thread.Sleep(1000 * retryCount * retryTimeOut);
}
}
catch (Exception ex)
{
// RudderLogger.LogError(ex.Message);
retryCount += 1;
Thread.Sleep(1000 * retryCount * retryTimeOut);
}
}
}
public void MakeIntegrationDump(RudderMessage message)
{
// if factories are not prepared dump those in the queue
if (!this._isFactoryPrepared || this._rudderServerConfig == null)
{
lock (this._lockingObj)
{
_factoryDumpQueue.Add(message);
}
}
// make native integration calls
else
{
foreach (string key in _integrationOpsMap.Keys)
{
RudderLogger.LogDebug("Dumping " + message.eventName + " to " + key + " native SDK");
_integrationOpsMap[key].Dump(message);
}
}
}
public void MakeIntegrationIdentify(string userId, RudderTraits traits)
{
if (!this._isFactoryPrepared || this._rudderServerConfig == null)
{
lock (this._lockingObj)
{
this._persistedUserId = userId;
this._persistedTraits = traits;
}
RudderLogger.LogDebug("Factories are not prepared yet");
}
// make native integration calls
else
{
foreach (string key in _integrationOpsMap.Keys)
{
RudderLogger.LogDebug("Identify to " + key + " native SDK");
_integrationOpsMap[key].Identify(userId, traits);
}
}
}
// ssl check validator
public static bool Validator(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
public void Update()
{
if (!this._isFactoryPrepared)
{
lock (this._lockingObj)
{
if (this._serverConfigJson != null && !this._serverConfigJson.Equals(""))
{
RudderLogger.LogDebug("RudderIntegrationManager Update: serverConfigJson: " + this._serverConfigJson);
if (IsServerConfigOutdated())
{
#if !UNITY_EDITOR
PlayerPrefs.SetString("rl_server_update_time", Stopwatch.GetTimestamp().ToString());
PlayerPrefs.SetString("rl_server_config", this._serverConfigJson);
PlayerPrefs.Save();
#endif
}
this._rudderServerConfig = ParseServerConfig(this._serverConfigJson);
this.PrepareFactories();
}
}
}
}
public void Reset()
{
if (!this._isFactoryPrepared)
{
RudderLogger.LogDebug("Factories are not prepared yet");
}
// make native integration calls
else
{
foreach (string key in _integrationOpsMap.Keys)
{
RudderLogger.LogDebug("Resetting native SDK " + key);
_integrationOpsMap[key].Reset();
}
}
}
}
}