Advertisement
Guest User

Untitled

a guest
Mar 16th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.15 KB | None | 0 0
  1. //
  2. // ReplicationTest.cs
  3. //
  4. // Author:
  5. //     Zachary Gramana  <zack@xamarin.com>
  6. //
  7. // Copyright (c) 2014 Xamarin Inc
  8. // Copyright (c) 2014 .NET Foundation
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. //
  30. // Copyright (c) 2014 Couchbase, Inc. All rights reserved.
  31. //
  32. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  33. // except in compliance with the License. You may obtain a copy of the License at
  34. //
  35. // http://www.apache.org/licenses/LICENSE-2.0
  36. //
  37. // Unless required by applicable law or agreed to in writing, software distributed under the
  38. // License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  39. // either express or implied. See the License for the specific language governing permissions
  40. // and limitations under the License.
  41. //
  42.  
  43. using System;
  44. using Couchbase.Lite.Util;
  45. using System.Threading;
  46.  
  47. namespace Couchbase.Lite
  48. {
  49.     public class ReplicationObserver
  50.     {
  51.         const string Tag = "ReplicationObserver";
  52.  
  53.         private bool replicationFinished = false;
  54.         private readonly CountdownEvent doneSignal;
  55.  
  56.         internal ReplicationObserver(CountdownEvent doneSignal)
  57.         {
  58.             this.doneSignal = doneSignal;
  59.         }
  60.  
  61.         public void Changed(object sender, ReplicationChangeEventArgs args)
  62.         {
  63.             var replicator = args.Source;
  64.             Log.D(Tag, replicator + " changed: " + replicator.CompletedChangesCount + " / " + replicator.ChangesCount);
  65.  
  66.             if (replicator.CompletedChangesCount < 0)
  67.             {
  68.                 string msg = replicator + ": replicator.CompletedChangesCount < 0";
  69.                 Log.D(Tag, msg);
  70.                 throw new Exception(msg);
  71.             }
  72.  
  73.             if (replicator.ChangesCount < 0)
  74.             {
  75.                 string msg = replicator + ": replicator.ChangesCount < 0";
  76.                 Log.E(Tag, msg);
  77.                 throw new Exception(msg);
  78.             }
  79.  
  80.             if (replicator.CompletedChangesCount > replicator.ChangesCount)
  81.             {
  82.                 string msgStr = "replicator.CompletedChangesCount : " + replicator.CompletedChangesCount + " > replicator.ChangesCount : " + replicator.ChangesCount;
  83.                 Log.E(Tag, msgStr);
  84.                 throw new Exception(msgStr);
  85.             }
  86.  
  87.             if (!replicator.IsRunning)
  88.             {
  89.                 this.replicationFinished = true;
  90.                 string msg = "ReplicationFinishedObserver.changed called, set replicationFinished to true";
  91.                 Log.D(Tag, msg);
  92.                 try
  93.                 {
  94.                     if (doneSignal.CurrentCount > 0)
  95.                         this.doneSignal.Signal();
  96.                 }
  97.                 catch { }
  98.  
  99.             }
  100.             else
  101.             {
  102.                 string msg = string.Format("ReplicationFinishedObserver.changed called, but replicator still running, so ignore it");
  103.                 Log.D(Tag, msg);
  104.             }
  105.         }
  106.  
  107.         internal virtual bool IsReplicationFinished()
  108.         {
  109.             return this.replicationFinished;
  110.         }
  111.     }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement