~lucio.torre/ubuntuone-client/windows-port

« back to all changes in this revision

Viewing changes to ubuntuone/syncdaemon/main.py

  • Committer: Tarmac
  • Author(s): facundo at com
  • Date: 2010-02-17 02:50:33 UTC
  • mfrom: (375.1.1 main-aq-lr)
  • Revision ID: dobey@gnome.org-20100217025033-e8f0sqosutr3xqb6
Separated the LR call to a method, and instantiated States after having AQ.

Show diffs side-by-side

added added

removed removed

Lines of Context:
111
111
        self.event_q = event_queue.EventQueue(self.fs)
112
112
        self.fs.register_eq(self.event_q)
113
113
        self.oauth_client = OAuthClient(self.realm)
114
 
        self.state = SyncDaemonStateManager(self, handshake_timeout,
115
 
                                            max_handshake_timeouts)
 
114
 
116
115
        # subscribe VM to EQ
117
116
        self.event_q.subscribe(self.vm)
118
117
        self.vm.init_root()
 
118
 
119
119
        # we don't have the oauth tokens yet, we 'll get them later
120
120
        self.action_q = action_queue.ActionQueue(self.event_q, self,
121
121
                                                 host, port,
126
126
        self.hash_q = hash_queue.HashQueue(self.event_q)
127
127
        events_nanny.DownloadFinishedNanny(self.fs, self.event_q, self.hash_q)
128
128
 
 
129
        # call StateManager after having AQ
 
130
        self.state = SyncDaemonStateManager(self, handshake_timeout,
 
131
                                            max_handshake_timeouts)
 
132
 
129
133
        self.sync = sync.Sync(self)
130
134
        self.lr = local_rescan.LocalRescan(self.vm, self.fs,
131
135
                                           self.event_q, self.action_q)
197
201
        self.event_q.push('SYS_WAIT_FOR_LOCAL_RESCAN')
198
202
        self.event_q.inotify_add_watch(self.root_dir)
199
203
 
200
 
        # do the local rescan
201
 
        self.logger.note("Local rescan starting...")
202
 
        d = self.lr.start()
203
 
 
204
 
        def _wait_for_hashq():
205
 
            """
206
 
            Keep on calling this until the hash_q finishes.
207
 
            """
208
 
            if len(self.hash_q):
209
 
                self.logger.info("hash queue pending. Waiting for it...")
210
 
                reactor.callLater(.1, _wait_for_hashq)
211
 
            else:
212
 
                self.logger.info("hash queue empty. We are ready!")
213
 
                # nudge the action queue into action
214
 
                self.event_q.push('SYS_LOCAL_RESCAN_DONE')
215
 
 
216
 
        def local_rescan_done(_):
217
 
            """After local rescan finished."""
218
 
            self.logger.note("Local rescan finished!")
219
 
            _wait_for_hashq()
220
 
 
221
 
        def stop_the_press(failure):
222
 
            """Something went wrong in LR, can't continue."""
223
 
            self.logger.error("Local rescan finished with error: %s",
224
 
                                                failure.getBriefTraceback())
225
 
            self.event_q.push('SYS_UNKNOWN_ERROR')
226
 
 
227
 
        d.addCallbacks(local_rescan_done, stop_the_press)
 
204
        d = self.local_rescan()
228
205
        return d
229
206
 
230
207
    def shutdown(self, with_restart=False):
231
 
        """ shutdown the daemon; optionally restart it """
 
208
        """Shutdown the daemon; optionally restart it."""
232
209
        self.event_q.push('SYS_DISCONNECT')
233
210
        self.event_q.shutdown()
234
211
        self.hash_q.shutdown()
236
213
        self.mark.stop()
237
214
 
238
215
    def restart(self):
239
 
        """
240
 
        Restart the daemon.
 
216
        """Restart the daemon.
241
217
 
242
218
        This ultimately shuts down the daemon and asks dbus to start one again.
243
219
        """
244
220
        self.quit(exit_value=42, with_restart=True)
245
221
 
246
222
    def get_root(self, root_mdid):
247
 
        """
248
 
        Ask que AQ for our root's uuid
249
 
        """
 
223
        """Ask que AQ for our root's uuid."""
250
224
        def _worker():
251
 
            """
252
 
            Actually do the asking
253
 
            """
 
225
            """Actually do the asking."""
254
226
            d = self.action_q.get_root(root_mdid)
255
227
            def root_node_cb(root):
256
 
                """ root node fetched callback. """
 
228
                """Root node fetched callback."""
257
229
                root_mdid = self.vm.on_server_root(root)
258
230
                self.action_q.uuid_map.set(root_mdid, root)
259
231
            d.addCallback(root_node_cb)
266
238
            return _worker()
267
239
 
268
240
    def check_version(self):
269
 
        """
270
 
        Check the client protocol version matches that of the server.
271
 
        """
 
241
        """Check the client protocol version matches that of the server."""
272
242
        self.action_q.check_version()
273
243
 
274
244
    def authenticate(self):
275
 
        """
276
 
        Do the OAuth dance.
277
 
        """
 
245
        """Do the OAuth dance."""
278
246
        self.action_q.authenticate(self.oauth_client.consumer)
279
247
 
280
248
    def set_capabilities(self):
283
251
        self.action_q.set_capabilities(syncdaemon.REQUIRED_CAPS)
284
252
 
285
253
    def server_rescan(self):
286
 
        """
287
 
        Do the server rescan
288
 
        """
 
254
        """Do the server rescan."""
289
255
        self.action_q.server_rescan(self.fs.get_data_for_server_rescan)
290
256
 
291
257
    def set_oauth_token(self, key, secret):
293
259
        self.token = oauth.OAuthToken(key, secret)
294
260
 
295
261
    def get_access_token(self):
296
 
        """Return the access token or a new one"""
 
262
        """Return the access token or a new one."""
297
263
        if self.token:
298
264
            return self.token
299
265
        else:
300
266
            return self.oauth_client.get_access_token()
301
267
 
302
268
    def get_rootdir(self):
303
 
        """ Returns the base dir/mount point"""
 
269
        """Return the base dir/mount point."""
304
270
        return self.root_dir
305
271
 
306
272
    def quit(self, exit_value=0, with_restart=False):
307
 
        """ shutdown and stop the reactor. """
 
273
        """Shutdown and stop the reactor."""
308
274
        self.shutdown(with_restart)
309
275
        if reactor.running:
310
276
            reactor.stop()
311
277
        else:
312
278
            sys.exit(exit_value)
313
279
 
 
280
    def local_rescan(self):
 
281
        """Do the local rescan."""
 
282
        self.logger.note("Local rescan starting...")
 
283
        d = self.lr.start()
 
284
 
 
285
        def _wait_for_hashq():
 
286
            """Keep on calling this until the hash_q finishes."""
 
287
            if len(self.hash_q):
 
288
                self.logger.info("hash queue pending. Waiting for it...")
 
289
                reactor.callLater(.1, _wait_for_hashq)
 
290
            else:
 
291
                self.logger.info("hash queue empty. We are ready!")
 
292
                # nudge the action queue into action
 
293
                self.event_q.push('SYS_LOCAL_RESCAN_DONE')
 
294
 
 
295
        def local_rescan_done(_):
 
296
            """After local rescan finished."""
 
297
            self.logger.note("Local rescan finished!")
 
298
            _wait_for_hashq()
 
299
 
 
300
        def stop_the_press(failure):
 
301
            """Something went wrong in LR, can't continue."""
 
302
            self.logger.error("Local rescan finished with error: %s",
 
303
                                                failure.getBriefTraceback())
 
304
            self.event_q.push('SYS_UNKNOWN_ERROR')
 
305
 
 
306
        d.addCallbacks(local_rescan_done, stop_the_press)
 
307
        return d
314
308
 
315
309
class NoAccessToken(Exception):
316
310
    """No access token available."""