forked from couchbaselabs/CouchDraw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseManager.cs
137 lines (117 loc) · 4.17 KB
/
DatabaseManager.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
using System;
using Couchbase.Lite;
using Couchbase.Lite.Sync;
namespace CouchDraw.Repositories
{
public class DatabaseManager : IDisposable
{
// Note: User 'localhost' or '127.0.0.1' when using a simulator
readonly Uri _remoteSyncUrl = new Uri("ws://localhost:4984");
// Note: Use '10.0.2.2' when using an emulator
//readonly Uri _remoteSyncUrl = new Uri("ws://10.0.2.2:4984");
readonly string _databaseName;
Replicator _replicator;
ListenerToken _replicatorListenerToken;
Database _database;
public Database Database
{
get
{
if (_database == null)
{
_database = new Database(_databaseName);
}
return _database;
}
}
public DatabaseManager(string databaseName)
{
_databaseName = databaseName;
}
public void StartReplication()
{
try
{
var targetUrlEndpoint = new URLEndpoint(new Uri(_remoteSyncUrl, _databaseName));
var configuration = new ReplicatorConfiguration(Database, targetUrlEndpoint)
{
ReplicatorType = ReplicatorType.PushAndPull,
Continuous = true
};
_replicator = new Replicator(configuration);
_replicatorListenerToken = _replicator.AddChangeListener(OnReplicatorUpdate);
_replicator.Start();
}
catch(Exception ex)
{
// We don't want replication errors to prevent us from
// using the app, but we do want to know about them.
Console.WriteLine($"Replication Exception - {ex.Message}");
}
}
void OnReplicatorUpdate(object sender, ReplicatorStatusChangedEventArgs e)
{
var status = e.Status;
switch (status.Activity)
{
case ReplicatorActivityLevel.Busy:
Console.WriteLine("Busy transferring data.");
break;
case ReplicatorActivityLevel.Connecting:
Console.WriteLine("Connecting to Sync Gateway.");
break;
case ReplicatorActivityLevel.Idle:
Console.WriteLine("Replicator in idle state.");
break;
case ReplicatorActivityLevel.Offline:
Console.WriteLine("Replicator in offline state.");
break;
case ReplicatorActivityLevel.Stopped:
Console.WriteLine("Completed syncing documents.");
break;
}
if (status.Progress.Completed == status.Progress.Total)
{
Console.WriteLine("All documents synced.");
}
else
{
Console.WriteLine($"Documents {status.Progress.Total - status.Progress.Completed} still pending sync");
}
}
public void StopReplication()
{
if (_replicator != null)
{
_replicator.RemoveChangeListener(_replicatorListenerToken);
_replicator.Stop();
}
}
public void Dispose()
{
try
{
if (_replicator != null)
{
StopReplication();
// Because the 'Stop' method for a Replicator instance is asynchronous
// we must wait for the status activity to be stopped before closing the database.
while (true)
{
if (_replicator.Status.Activity == ReplicatorActivityLevel.Stopped)
{
break;
}
}
_replicator.Dispose();
}
_database.Close();
_database = null;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}