def __init__(self, name, root, datacenter, bt_start_port, webip, webport, webdir, loglevel, lazy, create=False): self.name = name # cluster name self.bj_name = self.name+'__'+uuid.uuid4().hex # mDNS name if webport is not None: self.httpd = WebUIServer((webip, webport), WebUIHandler) print 'WebUIServer listening on: http://%s:%d/' % (webip, webport) else: self.httpd = type('blank', (object,), {})() self.httpd.api = {'webdir':webdir, 'webip':webip, 'webport':str(webport), 'lazy':str(lazy), 'name':name, 'btport':str(bt_start_port), 'root':root, 'servicename':self.bj_name, 'hostname':socket.gethostname(), 'datacenter':datacenter, 'mount':'-', 'freespace': 0, 'cluster_freespace': 0, 'version':'v%s' % APP_VERSION } self.httpd.bt_handles = {} self.httpd.peers = {} self.httpd.nametoaddr = {} self.bootstrapping = True self.LOGLEVEL = loglevel self.lazy = lazy self.root = os.path.realpath(root) self.indexdir = os.path.join(self.root, 'meta', 'index').encode(FS_ENCODE) self.metadir = os.path.join(self.root, 'meta').encode(FS_ENCODE) self.chunksdir = os.path.join(self.root, 'chunks').encode(FS_ENCODE) self.tmp = os.path.join(self.root, 'tmp').encode(FS_ENCODE) if not os.path.isdir(self.root): os.mkdir(self.root) self.next_time_to_check_for_undermirrored_files = datetime.datetime.now() + datetime.timedelta(0,10+random.randint(0,30)) self.last_read_file = {} self.bt_in_progress = set() self.bt_session = libtorrent.session() self.bt_session.listen_on(bt_start_port, bt_start_port+10) pe_settings = libtorrent.pe_settings() pe_enc_policy = {0:libtorrent.enc_policy.forced, 1:libtorrent.enc_policy.enabled, 2:libtorrent.enc_policy.disabled} pe_settings.out_enc_policy = libtorrent.enc_policy(pe_enc_policy[0]) pe_settings.in_enc_policy = libtorrent.enc_policy(pe_enc_policy[0]) pe_enc_level = {0:libtorrent.enc_level.plaintext, 1:libtorrent.enc_level.rc4, 2:libtorrent.enc_level.both} pe_settings.allowed_enc_level = libtorrent.enc_level(pe_enc_level[1]) self.bt_session.set_pe_settings(pe_settings) self.bt_port = self.bt_session.listen_port() self.httpd.api['btport'] = self.bt_port # no libtorrent lsd for private if we use h.connect_peer self.bt_session.start_lsd() # self.bt_session.stop_lsd() # no libtorrent dht for private if we use h.connect_peer # self.bt_session.start_dht() self.bt_session.stop_dht() print 'libtorrent listening on:', self.bt_port # self.bt_session.add_dht_router('localhost', 10670) print '...dht_state()', self.bt_session.dht_state() thread = Thread(target=self.__start_webui) thread.daemon = True thread.start() thread = Thread(target=self.__bonjour_start_listening) thread.daemon = True thread.start() print 'give me a sec to look for other peers...' time.sleep(2) cnfn = os.path.join(self.metadir, '__delugefs__', 'cluster_name').encode(FS_ENCODE) if create: if os.listdir(self.root): files = [x for x in os.listdir(self.root) if x!=".git.meta" and x!="chunks" and x!="meta" and x!="tmp" ] if files: raise Exception('--create specified, but %s is not empty' % self.root) if self.httpd.peers: raise Exception('--create specified, but i found %i peer%s using --id "%s" already' % (len(self.httpd.peers), 's' if len(self.httpd.peers)>1 else '', self.name)) if not os.path.isdir(self.metadir): os.makedirs(self.metadir) if not os.path.isdir(os.path.dirname(cnfn)): os.mkdir(os.path.dirname(cnfn)) with open(cnfn, 'w') as f: f.write(self.name) if not os.path.isdir(self.indexdir): os.makedirs(self.indexdir) with open(os.path.join(self.indexdir, '.__delugefs_dir__').encode(FS_ENCODE),'w') as f: f.write("git doesn't track empty dirs, so we add this file.") else: if os.path.isfile(cnfn): with open(cnfn, 'r') as f: existing_cluster_name = f.read().strip() if existing_cluster_name != self.name: raise Exception('a cluster root exists at %s, but its name is "%s", not "%s"' % (self.root, existing_cluster_name, self.name)) else: if os.listdir(self.root): raise Exception('root %s is not empty, but no cluster was found' % self.root) if not self.httpd.peers: raise Exception('--create not specified, no repo exists at %s and no peers of cluster "%s" found' % (self.root, self.name)) if not os.path.isdir(self.metadir): raise Exception('no repo exists at %s' % self.metadir) prune_empty_dirs(self.metadir) print '='*80 if not os.path.isdir(self.tmp): os.makedirs(self.tmp) for fn in os.listdir(self.tmp): os.remove(os.path.join(self.tmp,fn).encode(FS_ENCODE)) if not os.path.isdir(self.chunksdir): os.makedirs(self.chunksdir) self.rwlock = Lock() self.open_files = {} # used to track opened files except READONLY self.bootstrapping = False thread = Thread(target=self.__bonjour_register, args=()) thread.daemon = True thread.start() thread = Thread(target=self.__load_local_torrents) thread.daemon = True thread.start() thread = Thread(target=self.__monitor) thread.daemon = True thread.start()