summaryrefslogtreecommitdiff
path: root/FireBase/Streaming/FirebaseSubscription.cs
blob: fb0f4033e44b48d3ba8f9b8081c5b8343c061775 (plain)
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
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Firebase.Database.Query;
using Newtonsoft.Json.Linq;

namespace Firebase.Database.Streaming
{
    /// <summary>
    ///     The firebase subscription.
    /// </summary>
    /// <typeparam name="T"> Type of object to be streaming back to the called. </typeparam>
    internal class FirebaseSubscription<T> : IDisposable
    {
        private static readonly HttpClient http;
        private readonly FirebaseCache<T> cache;
        private readonly CancellationTokenSource cancel;
        private readonly FirebaseClient client;
        private readonly string elementRoot;
        private readonly IObserver<FirebaseEvent<T>> observer;
        private readonly IFirebaseQuery query;

        static FirebaseSubscription()
        {
            var handler = new HttpClientHandler
            {
                AllowAutoRedirect = true,
                MaxAutomaticRedirections = 10,
                CookieContainer = new CookieContainer()
            };

            var httpClient = new HttpClient(handler, true);

            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/event-stream"));

            http = httpClient;
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="FirebaseSubscription{T}" /> class.
        /// </summary>
        /// <param name="observer"> The observer.  </param>
        /// <param name="query"> The query.  </param>
        /// <param name="cache"> The cache. </param>
        public FirebaseSubscription(IObserver<FirebaseEvent<T>> observer, IFirebaseQuery query, string elementRoot,
            FirebaseCache<T> cache)
        {
            this.observer = observer;
            this.query = query;
            this.elementRoot = elementRoot;
            cancel = new CancellationTokenSource();
            this.cache = cache;
            client = query.Client;
        }

        public void Dispose()
        {
            cancel.Cancel();
        }

        public event EventHandler<ExceptionEventArgs<FirebaseException>> ExceptionThrown;

        public IDisposable Run()
        {
            Task.Run(() => ReceiveThread());

            return this;
        }

        private async void ReceiveThread()
        {
            while (true)
            {
                var url = string.Empty;
                var line = string.Empty;
                var statusCode = HttpStatusCode.OK;

                try
                {
                    cancel.Token.ThrowIfCancellationRequested();

                    // initialize network connection
                    url = await query.BuildUrlAsync().ConfigureAwait(false);
                    var request = new HttpRequestMessage(HttpMethod.Get, url);
                    var serverEvent = FirebaseServerEventType.KeepAlive;

                    var client = GetHttpClient();
                    var response = await client
                        .SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancel.Token)
                        .ConfigureAwait(false);

                    statusCode = response.StatusCode;
                    response.EnsureSuccessStatusCode();

                    using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
                    using (var reader = this.client.Options.SubscriptionStreamReaderFactory(stream))
                    {
                        while (true)
                        {
                            cancel.Token.ThrowIfCancellationRequested();

                            line = reader.ReadLine()?.Trim();

                            if (string.IsNullOrWhiteSpace(line)) continue;

                            var tuple = line.Split(new[] {':'}, 2, StringSplitOptions.RemoveEmptyEntries)
                                .Select(s => s.Trim()).ToArray();

                            switch (tuple[0].ToLower())
                            {
                                case "event":
                                    serverEvent = ParseServerEvent(serverEvent, tuple[1]);
                                    break;
                                case "data":
                                    ProcessServerData(url, serverEvent, tuple[1]);
                                    break;
                            }

                            if (serverEvent == FirebaseServerEventType.AuthRevoked)
                                // auth token no longer valid, reconnect
                                break;
                        }
                    }
                }
                catch (OperationCanceledException)
                {
                    break;
                }
                catch (Exception ex) when (statusCode != HttpStatusCode.OK)
                {
                    observer.OnError(new FirebaseException(url, string.Empty, line, statusCode, ex));
                    Dispose();
                    break;
                }
                catch (Exception ex)
                {
                    ExceptionThrown?.Invoke(this,
                        new ExceptionEventArgs<FirebaseException>(new FirebaseException(url, string.Empty, line,
                            statusCode, ex)));

                    await Task.Delay(2000).ConfigureAwait(false);
                }
            }
        }

        private FirebaseServerEventType ParseServerEvent(FirebaseServerEventType serverEvent, string eventName)
        {
            switch (eventName)
            {
                case "put":
                    serverEvent = FirebaseServerEventType.Put;
                    break;
                case "patch":
                    serverEvent = FirebaseServerEventType.Patch;
                    break;
                case "keep-alive":
                    serverEvent = FirebaseServerEventType.KeepAlive;
                    break;
                case "cancel":
                    serverEvent = FirebaseServerEventType.Cancel;
                    break;
                case "auth_revoked":
                    serverEvent = FirebaseServerEventType.AuthRevoked;
                    break;
            }

            return serverEvent;
        }

        private void ProcessServerData(string url, FirebaseServerEventType serverEvent, string serverData)
        {
            switch (serverEvent)
            {
                case FirebaseServerEventType.Put:
                case FirebaseServerEventType.Patch:
                    var result = JObject.Parse(serverData);
                    var path = result["path"].ToString();
                    var data = result["data"].ToString();

                    // If an elementRoot parameter is provided, but it's not in the cache, it was already deleted. So we can return an empty object.
                    if (string.IsNullOrWhiteSpace(elementRoot) || !cache.Contains(elementRoot))
                        if (path == "/" && data == string.Empty)
                        {
                            observer.OnNext(FirebaseEvent<T>.Empty(FirebaseEventSource.OnlineStream));
                            return;
                        }

                    var eventType = string.IsNullOrWhiteSpace(data)
                        ? FirebaseEventType.Delete
                        : FirebaseEventType.InsertOrUpdate;

                    var items = cache.PushData(elementRoot + path, data);

                    foreach (var i in items.ToList())
                        observer.OnNext(new FirebaseEvent<T>(i.Key, i.Object, eventType,
                            FirebaseEventSource.OnlineStream));

                    break;
                case FirebaseServerEventType.KeepAlive:
                    break;
                case FirebaseServerEventType.Cancel:
                    observer.OnError(new FirebaseException(url, string.Empty, serverData, HttpStatusCode.Unauthorized));
                    Dispose();
                    break;
            }
        }

        private HttpClient GetHttpClient()
        {
            return http;
        }
    }
}