home *** CD-ROM | disk | FTP | other *** search
/ Clickx 47 / Clickx 47.iso / assets / software / Miro_Installer.exe / xulrunner / python / dl_daemon / command.py < prev    next >
Encoding:
Python Source  |  2008-01-10  |  6.1 KB  |  187 lines

  1. import time
  2.  
  3. import random
  4. import socket
  5. import eventloop
  6. import logging
  7.  
  8. DAEMONIC_THREAD_TIMEOUT = 2
  9. # amount of time to wait for daemonic threads to quit.  Right now, the only
  10. # thing we use Daemonic threads for is to send HTTP requests to BitTorrent
  11. # trackers.
  12.  
  13. class Command:
  14.     def __init__(self, daemon, *args, **kws):
  15.         self.id = "cmd%08d" % random.randint(0,99999999)
  16.         self.orig = True
  17.         self.args = args
  18.         self.kws = kws
  19.         self.daemon = daemon
  20.  
  21.     def setDaemon(self, daemon):
  22.         self.daemon = daemon
  23.  
  24.     def send(self, callback=None):
  25.         if self.daemon.shutdown:
  26.             return
  27.         eventloop.addIdle(lambda : self.daemon.send(self, callback), "sending command %s" % repr(self))
  28.  
  29.     def setReturnValue(self, ret):
  30.         self.orig = False
  31.         self.ret = ret
  32.  
  33.     def getReturnValue(self):
  34.         return self.ret
  35.  
  36.     def action(self):
  37.         logging.warning ("no action defined for command %s", self.id)
  38.         #for overrriding
  39.  
  40.     def __getstate__(self):
  41.         out = {"id":self.id, "args":self.args, "kws":self.kws, "orig":self.orig}
  42.         try:
  43.             out["ret"] = self.ret
  44.         except AttributeError:
  45.             pass
  46.         return out
  47.  
  48.     def __setstate__(self, data):
  49.         self.id = data["id"]
  50.         self.kws = data["kws"]
  51.         self.args = data["args"]
  52.         self.orig = data["orig"]
  53.         try:
  54.             self.ret = data["ret"]
  55.         except KeyError:
  56.             pass
  57.  
  58. #############################################################################
  59. #  Downloader to App commands                                               #
  60. #############################################################################
  61. class FindHTTPAuthCommand(Command):
  62.     def action(self):
  63.         import httpauth
  64.         id, args = self.args[0], self.args[1:]
  65.         def callback(authHeader):
  66.             c = GotHTTPAuthCommand(self.daemon, id, authHeader)
  67.             c.send()
  68.         httpauth.findHTTPAuth(callback, *args)
  69.  
  70. class AskForHTTPAuthCommand(Command):
  71.     def action(self):
  72.         import httpauth
  73.         id, args = self.args[0], self.args[1:]
  74.         def callback(authHeader):
  75.             c = GotHTTPAuthCommand(self.daemon, id, authHeader)
  76.             c.send()
  77.         httpauth.askForHTTPAuth(callback, *args)
  78.  
  79. class UpdateDownloadStatus(Command):
  80.     def action(self):
  81.         from downloader import RemoteDownloader
  82.         return RemoteDownloader.updateStatus(*self.args, **self.kws)
  83.  
  84. class BatchUpdateDownloadStatus(Command):
  85.     def action(self):
  86.         from downloader import RemoteDownloader
  87.         for status in self.args[0]:
  88.             RemoteDownloader.updateStatus(status)
  89.  
  90. class DownloaderErrorCommand(Command):
  91.     def action(self):
  92.         import util
  93.         util.failed("In Downloader process", details=self.args[0])
  94.  
  95. class ShutDownResponseCommand(Command):
  96.     def action(self):
  97.         self.daemon.shutdownResponse()
  98.  
  99. #############################################################################
  100. #  App to Downloader commands                                               #
  101. #############################################################################
  102. class InitialConfigCommand(Command):
  103.     def action(self):
  104.         import config
  105.         from dl_daemon import download
  106.         config.setDictionary(*self.args, **self.kws)
  107.         download.configReceived()
  108.  
  109. class UpdateConfigCommand(Command):
  110.     def action(self):
  111.         import config
  112.         config.updateDictionary(*self.args, **self.kws)
  113.  
  114. class StartNewDownloadCommand(Command):
  115.     def action(self):
  116.         from dl_daemon import download
  117.         return download.startNewDownload(*self.args, **self.kws)
  118.  
  119. class StartDownloadCommand(Command):
  120.     def action(self):
  121.         from dl_daemon import download
  122.         return download.startDownload(*self.args, **self.kws)
  123.  
  124. class PauseDownloadCommand(Command):
  125.     def action(self):
  126.         from dl_daemon import download
  127.         return download.pauseDownload(*self.args, **self.kws)
  128.  
  129. class StopDownloadCommand(Command):
  130.     def action(self):
  131.         from dl_daemon import download
  132.         return download.stopDownload(*self.args, **self.kws)
  133.  
  134. class StopUploadCommand(Command):
  135.     def action(self):
  136.         from dl_daemon import download
  137.         return download.stopUpload(*self.args, **self.kws)
  138.  
  139. class GetDownloadStatusCommand(Command):
  140.     def action(self):
  141.         from dl_daemon import download
  142.         return download.getDownloadStatus(*self.args, **self.kws)
  143.  
  144. class RestoreDownloaderCommand(Command):
  145.     def action(self):
  146.         from dl_daemon import download
  147.         return download.restoreDownloader(*self.args, **self.kws)
  148.  
  149. class MigrateDownloadCommand(Command):
  150.     def action(self):
  151.         from dl_daemon import download
  152.         return download.migrateDownload(*self.args, **self.kws)
  153.  
  154. class GotHTTPAuthCommand(Command):
  155.     def action(self):
  156.         id, authHeader = self.args
  157.         import httpauth 
  158.         # note since we're in the downloader process here, httpauth is
  159.         # dl_daemon/private/httpauth.py
  160.         httpauth.handleHTTPAuthResponse(id, authHeader)
  161.  
  162. class ShutDownCommand(Command):
  163.     def response_sent(self):
  164.         import eventloop
  165.         eventloop.quit()
  166.         logging.info ("Shutdown complete")
  167.  
  168.     def action(self):
  169.         starttime = time.time()
  170.         from dl_daemon import download
  171.         download.shutDown()
  172.         import threading
  173.         eventloop.threadPoolQuit()
  174.         for thread in threading.enumerate():
  175.             if thread != threading.currentThread() and not thread.isDaemon():
  176.                 thread.join()
  177.         endtime = starttime + DAEMONIC_THREAD_TIMEOUT
  178.         for thread in threading.enumerate():
  179.             if thread != threading.currentThread():
  180.                 timeout = endtime - time.time()
  181.                 if timeout <= 0:
  182.                     break
  183.                 thread.join(timeout)
  184.         c = ShutDownResponseCommand(self.daemon)
  185.         c.send(callback=self.response_sent)
  186.         self.daemon.shutdown = True
  187.