Merge lp:~mandel/ubuntuone-windows-installer/add_timer_execution into lp:ubuntuone-windows-installer/beta

Proposed by Manuel de la Peña
Status: Merged
Approved by: Manuel de la Peña
Approved revision: 121
Merged at revision: 109
Proposed branch: lp:~mandel/ubuntuone-windows-installer/add_timer_execution
Merge into: lp:ubuntuone-windows-installer/beta
Prerequisite: lp:~mandel/ubuntuone-windows-installer/update_nunit_net_4
Diff against target: 280 lines (+195/-10)
2 files modified
src/Canonical.UbuntuOne.Common/OperationContracts/ISyncDaemon.cs (+19/-0)
src/Canonical.UbuntuOne.ProcessDispatcher/SyncDaemonService.cs (+176/-10)
To merge this branch: bzr merge lp:~mandel/ubuntuone-windows-installer/add_timer_execution
Reviewer Review Type Date Requested Status
Ubuntu One hackers Pending
Review via email: mp+38246@code.launchpad.net

Description of the change

Adds a new method in the WCF service to set up a thread that will execute the manual sync every x seconds.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/Canonical.UbuntuOne.Common/OperationContracts/ISyncDaemon.cs'
2--- src/Canonical.UbuntuOne.Common/OperationContracts/ISyncDaemon.cs 2010-10-12 16:18:43 +0000
3+++ src/Canonical.UbuntuOne.Common/OperationContracts/ISyncDaemon.cs 2010-10-12 16:18:44 +0000
4@@ -41,6 +41,25 @@
5 [OperationContract]
6 void ManualSync(string path, string token, string tokenSecret, string consumerKey, string consumerSecret);
7
8+
9+ /// <summary>
10+ /// Allows to set the manual sync operation to be performed every number of seconds.
11+ /// </summary>
12+ /// <param name="path">The path to which the root of the Ubuntu One folder will be sync.</param>
13+ /// <param name="token">The token from oauth.</param>
14+ /// <param name="tokenSecret">The token secret from oauth.</param>
15+ /// <param name="consumerKey">The consumer key from oauth.</param>
16+ /// <param name="consumerSecret">The consumer secret from oauth.</param>
17+ /// <param name="numberOfSeconds">The number of seconds that the timer will wiat everytime it executes the sync.</param>
18+ [OperationContract]
19+ void EnableManualSyncAutoExecution(string path, string token, string tokenSecret, string consumerKey, string consumerSecret, int numberOfSeconds);
20+
21+ /// <summary>
22+ /// Allows to disable the auto execution of the manual sync execution.
23+ /// </summary>
24+ [OperationContract]
25+ void DisableManualSyncAutoExecution();
26+
27 /// <summary>
28 /// Allows to start the daemon by a client.
29 /// </summary>
30
31=== modified file 'src/Canonical.UbuntuOne.ProcessDispatcher/SyncDaemonService.cs'
32--- src/Canonical.UbuntuOne.ProcessDispatcher/SyncDaemonService.cs 2010-10-12 16:18:43 +0000
33+++ src/Canonical.UbuntuOne.ProcessDispatcher/SyncDaemonService.cs 2010-10-12 16:18:44 +0000
34@@ -22,12 +22,14 @@
35 using System.Diagnostics;
36 using System.IO;
37 using System.ServiceModel;
38+using System.Threading;
39 using Canonical.UbuntuOne.Common;
40 using Canonical.UbuntuOne.Common.Aop;
41 using Canonical.UbuntuOne.Common.Security;
42 using Canonical.UbuntuOne.Common.Utils;
43 using Canonical.UbuntuOne.Common.Validation;
44 using log4net;
45+using Timer = System.Timers.Timer;
46
47 namespace Canonical.UbuntuOne.ProcessDispatcher
48 {
49@@ -36,6 +38,79 @@
50 /// </summary>
51 internal class SyncDaemonService : ISyncDaemon
52 {
53+ #region HelperStruct
54+
55+ /// <summary>
56+ /// Provides the state of the auto sync to be performed.
57+ /// </summary>
58+ private struct AutoSyncState
59+ {
60+ #region Variables
61+
62+ private string _path;
63+ private string _token;
64+ private string _tokenSecret;
65+ private string _consumerKey;
66+ private string _consumerSecret;
67+ private int _seconds;
68+
69+ #endregion
70+
71+ #region Properties
72+
73+ /// <summary>
74+ /// Gets the path to auto sync.
75+ /// </summary>
76+ public string Path { get { return _path; } }
77+
78+ /// <summary>
79+ /// Gets the ouath token.
80+ /// </summary>
81+ public string Token { get { return _token; } }
82+
83+ /// <summary>
84+ /// Gets the oauth token secret.
85+ /// </summary>
86+ public string TokenSecret { get { return _tokenSecret; } }
87+
88+ /// <summary>
89+ /// Gets the oauth consumer key.
90+ /// </summary>
91+ public string ConsumerKey { get { return _consumerKey; } }
92+
93+ /// <summary>
94+ /// Gets the oauth consuer secret.
95+ /// </summary>
96+ public string ConsumerSecret { get { return _consumerSecret; } }
97+
98+ public int Seconds { get { return _seconds; } }
99+ #endregion
100+
101+ #region Constructors
102+
103+ /// <summary>
104+ /// Creates a new state that will contain the passed data for the state.
105+ /// </summary>
106+ /// <param name="path">The path that represents the root folder to be used by the sync.</param>
107+ /// <param name="token">The ouath token.</param>
108+ /// <param name="tokenSecret">The oauth token secret.</param>
109+ /// <param name="consumerKey">The oauth consumer key.</param>
110+ /// <param name="consumerSecret">The oauth consumer secret.</param>
111+ /// <param name="seconds">The interval to use in seconds.</param>
112+ public AutoSyncState(string path, string token, string tokenSecret, string consumerKey, string consumerSecret, int seconds)
113+ {
114+ _path = path;
115+ _token = token;
116+ _tokenSecret = tokenSecret;
117+ _consumerKey = consumerKey;
118+ _consumerSecret = consumerSecret;
119+ _seconds = seconds;
120+ }
121+
122+ #endregion
123+
124+ }
125+ #endregion
126
127 #region Variables
128
129@@ -43,6 +118,10 @@
130 private readonly IDictionary<string, IProcessManager> _processesMap;
131 private readonly object _loggerLock = new object();
132 private ILog _logger;
133+ private readonly object _autoManualSyncThreadLock = new object();
134+ private Thread _autoManualSyncThread;
135+ private bool _isAutoManualSync;
136+ private readonly object _isAutoManualSyncLock;
137
138 #endregion
139
140@@ -71,6 +150,36 @@
141 }
142
143 /// <summary>
144+ /// Gets and sets the thread that is used to automatically run the manual sync.
145+ /// </summary>
146+ private Thread AutoManualSyncThread
147+ {
148+ get { return _autoManualSyncThread; }
149+ set
150+ {
151+ lock (_autoManualSyncThreadLock)
152+ {
153+ _autoManualSyncThread = value;
154+ }
155+ }
156+ }
157+
158+ /// <summary>
159+ /// Gets and sets a bool stating that we are performing the manual sync.
160+ /// </summary>
161+ private bool IsAutoManualSync
162+ {
163+ get { return _isAutoManualSync; }
164+ set
165+ {
166+ lock (_isAutoManualSyncLock)
167+ {
168+ _isAutoManualSync = value;
169+ }
170+ }
171+ }
172+
173+ /// <summary>
174 /// Allows to get and set the manager that will manage the sync daemon process.
175 /// </summary>
176 internal IProcessManagerFactory ProcessManagerFactory { get; set; }
177@@ -92,7 +201,40 @@
178 public SyncDaemonService()
179 {
180 _processesMap = new Dictionary<string, IProcessManager>();
181- }
182+
183+ }
184+
185+
186+ #region Helpers
187+
188+
189+ private void AutoManualSync(object o)
190+ {
191+ var state = (AutoSyncState) o;
192+ // TODO: Doing this might be an issue later in the project. In this case the aouth info is kept in the thread and
193+ // if there are changes, we will not know it. Is better to use the SSO lib to get the data and then perform the sync.
194+
195+ // little trick, we use the using statement to ensure that the timer is kept alive while
196+ // we needed but that it will be disposed when we stop the operation
197+ using (var manualSyncTimer = new Timer(10000))
198+ {
199+ // use a lambda as the callback to use
200+ manualSyncTimer.Elapsed +=
201+ (sender, args) => ManualSync(state.Path, state.Token, state.TokenSecret, state.ConsumerKey, state.ConsumerSecret);
202+
203+ // we got the number of seconds, therefor multiple by 1000 the value
204+ manualSyncTimer.Interval = state.Seconds*1000;
205+ //stay here forever until we are killed
206+ while (IsAutoManualSync)
207+ {
208+ //wait
209+ }
210+ //disable the timer
211+ manualSyncTimer.Enabled = false;
212+ }
213+ }
214+
215+ #endregion
216
217 /// <summary>
218 /// Allows to start the daemon by a client.
219@@ -207,14 +349,6 @@
220 }
221
222 /// <summary>
223- /// Allows the user to perform a manual sync of his machine with the ubuntu one server.
224- /// </summary>
225- public void ManualSync()
226- {
227- throw new NotImplementedException();
228- }
229-
230- /// <summary>
231 /// Allows to perform a manual sync of the root Ubuntu One directory in the provided path using the oauth information
232 /// passed to the service.
233 /// </summary>
234@@ -290,13 +424,45 @@
235 proc.Start();
236 // we wait, we are in a diff thread in a diff process this will not block the user UI
237 proc.WaitForExit();
238- var error = proc.StandardError.ReadToEnd();
239 Logger.InfoFormat("StandarOutput from u1sync is: {0}", proc.StandardOutput.ReadToEnd());
240 Logger.ErrorFormat("StandardError from u1sync is: {0}", proc.StandardError.ReadToEnd());
241 proc.Close();
242 }
243
244 /// <summary>
245+ /// Allows to set the manual sync operation to be performed every number of seconds.
246+ /// </summary>
247+ /// <param name="path">The path to which the root of the Ubuntu One folder will be sync.</param>
248+ /// <param name="token">The token from oauth.</param>
249+ /// <param name="tokenSecret">The token secret from oauth.</param>
250+ /// <param name="consumerKey">The consumer key from oauth.</param>
251+ /// <param name="consumerSecret">The consumer secret from oauth.</param>
252+ /// <param name="numberOfSeconds">The number of seconds that the timer will wiat everytime it executes the sync.</param>
253+ public void EnableManualSyncAutoExecution(string path, string token, string tokenSecret, string consumerKey, string consumerSecret, int numberOfSeconds)
254+ {
255+ if(AutoManualSyncThread != null)
256+ // we are already doing it, ignore
257+ return;
258+ Logger.InfoFormat("Enabling auto sync every {0} seconds", numberOfSeconds);
259+ var state = new AutoSyncState(path, token, tokenSecret, consumerKey, consumerSecret, numberOfSeconds);
260+ // we set var to tell that we are going to perform the auto execution
261+ IsAutoManualSync = true;
262+ // we create the tread
263+ AutoManualSyncThread = new Thread(AutoManualSync);
264+ AutoManualSyncThread.Start(state);
265+ }
266+
267+ /// <summary>
268+ /// Allows to disable the auto execution of the manual sync execution.
269+ /// </summary>
270+ public void DisableManualSyncAutoExecution()
271+ {
272+ Logger.InfoFormat("Disabling autosync.");
273+ IsAutoManualSync = false;
274+ AutoManualSyncThread = null;
275+ }
276+
277+ /// <summary>
278 /// Returns the port number of the socket that the synd daemo is listening to for communication.
279 /// </summary>
280 /// <returns></returns>

Subscribers

People subscribed via source and target branches

to all changes: