def _save(self): """ save urls to ipsfile """ mylogger.info('[_save]: saving urls ... ') save_urls(self.known,self.ipsfile) mylogger.info('[_save]: saving urls finiehed')
def __init__(self,name,filepath,data): mylogger.info('[SaveFileThread]:Init...' ) super(SaveFileThread,self).__init__() self.name = name self.daemon = True self.filepath = filepath self.data = data
def _save(self): """ save urls to ipsfile """ mylogger.info('[_save]: saving urls ... ') save_urls(self.known, self.ipsfile) mylogger.info('[_save]: saving urls finiehed')
def keyPressEvent(self,event): mylogger.info("[keyPressEvent]") if event.key() == QtCore.Qt.Key_Escape: self.close() elif event.key() == QtCore.Qt.Key_Enter: self.onFetchHandler(False) else: pass
def _start(self): """ start node server """ try: t = ('', getport(self.url)) # in both server and client set allow_none=True self.local_server = SimpleXMLRPCServer(t, allow_none=True, logRequests=False) self.local_server.register_instance(self) msg = "[_start]: Server started at {0}...".format(self.url) print(msg) mylogger.info(msg) # 1)on start up ,read urls self._read() # 2)after all urls added to known set,inform others about myself's status online self.online() mylogger.info('[_start]: event_running..') # 3)start server self.event_running.set() # set flag to true self.local_server.serve_forever() except socket.error, e: mylogger.warn(e) mylogger.warn('[_start]: socket error') mylogger.warn('[_start]: program is going to exit...')
def get_remote_urls(self): """ get remote urls of local node """ mylogger.info('[get_remote_urls]: ') copy = self.known.copy() copy.remove(self.url) return list(copy)
def __init__(self,name,interval,target): mylogger.info('[__init__]: {0}'.format(name)) super(UpdateGUIListTimer,self).__init__() self.name = name self.daemon = True self.interval = interval self.target = target # whether to stop thread self.stopped = Event() # default false
def update_local_list(self): """ update method list all files in local node """ mylogger.info('[update_local_list]: update local list') temp = self.list_local() if not list_equal(temp,self.local_files): self.local_files = temp self._trigger_update_local() return True
def update_local_list(self): """ update method list all files in local node """ mylogger.info('[update_local_list]: update local list') temp = self.list_local() if not list_equal(temp, self.local_files): self.local_files = temp self._trigger_update_local() return True
def _read(self): """ read urls from ipsfile """ mylogger.info('[_read]: reading urls ... ') urls = read_urls(self.ipsfile) # make sure self.url in urls if not self.url in urls: urls.append(self.url) for url in urls: self._add(url) mylogger.info('[_read]: reading urls finished')
def remove_node(self, other, otherfiles=[]): """ remove other from myself's known set remove otherfiles from myself's remote_files [used in offline] """ mylogger.info('[remove_node]: BYEBYE {0}'.format(other)) self.known.remove(other) if other in self.remote_files: del self.remote_files[other] self._trigger_update_remote() return True
def closeEvent(self,event): mylogger.info("[closeEvent]") # If we close the QtGui.QWidget, the QtGui.QCloseEvent is generated and closeEvent is called. reply = QtGui.QMessageBox.question(self, 'Message', "Are you sure to exit?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.Yes) if reply == QtGui.QMessageBox.Yes: msg = ('###[closeEvent]: program is going to exit... ') mylogger.info(msg) print(msg) self.stop() event.accept() else: event.ignore()
def remove_node(self,other,otherfiles=[]): """ remove other from myself's known set remove otherfiles from myself's remote_files [used in offline] """ mylogger.info('[remove_node]: BYEBYE {0}'.format(other)) self.known.remove(other) if other in self.remote_files: del self.remote_files[other] self._trigger_update_remote() return True
def __init__(self,name,url,dirname,secret,ipsfile,event_running): mylogger.info('[__init__]: {0}'.format(name)) super(NodeServerThread,self).__init__() self.name = name self.daemon = True self.url = url self.dirname = dirname self.secret = secret self.ipsfile = ipsfile self.event_running = event_running # New variables # server_node self.server_node = None
def run(self): mylogger.info('[SaveFileThread]: Starting {0}'.format(self.name)) mylogger.info('[SaveFileThread]: Saving to {0} ...'.format(self.filepath)) t1 = time.clock() files.savefile_frombinary_xmlrpc(self.filepath,self.data) mylogger.info('[SaveFileThread]: Time used {0}s'.format(time.clock()-t1)) mylogger.info('[SaveFileThread]: Exiting {0}'.format(self.name))
def add_node(self, other, otherfiles): """ add other to myself's known set add otherfiles to myself's remote_files [used in online] [used in list_other SPECIAL!!!] """ if other in self.known: mylogger.info("{0} in known set".format(other)) return mylogger.info('[add_node]: HELLO {0}'.format(other)) self.known.add(other) if len(otherfiles): self.remote_files[other] = otherfiles self._trigger_update_remote() return True
def add_node(self,other,otherfiles): """ add other to myself's known set add otherfiles to myself's remote_files [used in online] [used in list_other SPECIAL!!!] """ if other in self.known: mylogger.info("{0} in known set".format(other)) return mylogger.info('[add_node]: HELLO {0}'.format(other)) self.known.add(other) if len(otherfiles): self.remote_files[other] = otherfiles self._trigger_update_remote() return True
def offline(self): """ inform others about myself's status(off) """ mylogger.info('[offline]') for other in self.known.copy(): if other == self.url: continue s = ServerProxy(other) try: # inform other node to remove local node s.remove_node(self.url) except Fault,f: mylogger.warn(f) mylogger.warn('[offline]: {0} started but inform failed'.format(other)) except socket.error,e: mylogger.error('[offline]: {0} for {1}'.format(e,other))
def initUI(self): mylogger.info("[initUI]...") # menus toolbars statusbar # actions self.fetchAction = QtGui.QAction(QtGui.QIcon(ICON_FETCH), '&Fetch', self) self.fetchAction.setShortcut('Ctrl+F') self.fetchAction.setStatusTip('Fetch file') self.fetchAction.triggered.connect(self.onFetchHandler) self.stopAction = QtGui.QAction(QtGui.QIcon(ICON_QUIT), '&Quit', self) self.stopAction.setShortcut('Ctrl+Q') self.stopAction.setStatusTip('Quit application') self.stopAction.triggered.connect(self.close) menubar = self.menuBar() fileMenu = menubar.addMenu('&File') fileMenu.addAction(self.fetchAction) fileMenu.addAction(self.stopAction) toolbar = self.addToolBar('tool') toolbar.addAction(self.fetchAction) toolbar.addAction(self.stopAction) self.statusbar = self.statusBar() # GuiWidget self.main_widget = GuiWidget(self) self.main_widget.le.textChanged[str].connect(self.onTextChanged) self.main_widget.btn_fetch.clicked.connect(self.onFetchHandler) self.main_widget.btn_update.clicked.connect(self.onUpdateHandler) self.main_widget.list_remote.itemClicked.connect(self.onListItemClicked) # set central widget for main window self.setCentralWidget(self.main_widget) # set control states self.setFetchEnabled(False) # set list files self.setLocal() self.setRemote() # settings for window self.resize(WIN_WIDTH,WIN_HEIGHT) #self.move(200,200) self.center() self.setWindowTitle('File Sharing Client') self.setWindowIcon(QtGui.QIcon(ICON_APP)) self.show()
def updateList(self): mylogger.info('-'*50) mylogger.info("[updateList]...") # update local and remote files NodeService.update_local_list(self) NodeService.update_remote_list(self) self.setLocal() self.setRemote() mylogger.info("[updateList] finished") mylogger.info('-'*50)
def online(self): """ inform others about myself's status(on) """ mylogger.info('[online]') for other in self.known.copy(): if other == self.url: continue s = ServerProxy(other) try: # inform other node to add local node files = self.get_local_files() s.add_node(self.url,files) except Fault,f: mylogger.warn(f) mylogger.warn('[online]: {0} started but inform failed'.format(other)) except socket.error,e: mylogger.error('[online]: {0} for {1}'.format(e,other))
def offline(self): """ inform others about myself's status(off) """ mylogger.info('[offline]') for other in self.known.copy(): if other == self.url: continue s = ServerProxy(other) try: # inform other node to remove local node s.remove_node(self.url) except Fault, f: mylogger.warn(f) mylogger.warn( '[offline]: {0} started but inform failed'.format(other)) except socket.error, e: mylogger.error('[offline]: {0} for {1}'.format(e, other))
def list_other(self,other): """ list files in other node """ mylogger.info('[list_other]: list files in {0}'.format(other)) lt = [] s = ServerProxy(other) try: #mylogger.info("[list_other]: call list_local 3") # since we connect to other,introduce self.url to other # inform other node to add local node files = self.get_local_files() s.add_node(self.url,files) # introduce self.url to other lt = s.list_local() except Fault,f: mylogger.warn(f) mylogger.warn('[list_other]: {0} started but list failed'.format(other))
def online(self): """ inform others about myself's status(on) """ mylogger.info('[online]') for other in self.known.copy(): if other == self.url: continue s = ServerProxy(other) try: # inform other node to add local node files = self.get_local_files() s.add_node(self.url, files) except Fault, f: mylogger.warn(f) mylogger.warn( '[online]: {0} started but inform failed'.format(other)) except socket.error, e: mylogger.error('[online]: {0} for {1}'.format(e, other))
def list_other(self, other): """ list files in other node """ mylogger.info('[list_other]: list files in {0}'.format(other)) lt = [] s = ServerProxy(other) try: #mylogger.info("[list_other]: call list_local 3") # since we connect to other,introduce self.url to other # inform other node to add local node files = self.get_local_files() s.add_node(self.url, files) # introduce self.url to other lt = s.list_local() except Fault, f: mylogger.warn(f) mylogger.warn( '[list_other]: {0} started but list failed'.format(other))
def _add(self, url): """ add url to myself's known set at the same time, list files in url [used in _read] """ mylogger.info('[_add]: adding {0}...'.format(url)) self.known.add(url) if url == self.url: #mylogger.info("[_add]: call list_local 1") lt = self.list_local() if len(lt): self.local_files = lt self._trigger_update_local() else: #mylogger.info("[_add]: call list_other 2") lt = self.list_other(url) if len(lt): self.remote_files[url] = lt self._trigger_update_remote()
def _add(self,url): """ add url to myself's known set at the same time, list files in url [used in _read] """ mylogger.info('[_add]: adding {0}...'.format(url)) self.known.add(url) if url == self.url: #mylogger.info("[_add]: call list_local 1") lt = self.list_local() if len(lt): self.local_files = lt self._trigger_update_local() else: #mylogger.info("[_add]: call list_other 2") lt = self.list_other(url) if len(lt): self.remote_files[url] = lt self._trigger_update_remote()
def update_remote_list(self): """ update method list all files in remote nodes """ mylogger.info('[update_remote_list]: update remote list') for other in self.known.copy(): if other == self.url: continue else: lt = self.list_other(other) if other in self.remote_files: if not list_equal(lt,self.remote_files[other]): self.remote_files[other]= lt if not len(lt): del self.remote_files[other] self._trigger_update_remote() elif len(lt):# k not in dict self.remote_files[other]= lt self._trigger_update_remote() return True
def update_remote_list(self): """ update method list all files in remote nodes """ mylogger.info('[update_remote_list]: update remote list') for other in self.known.copy(): if other == self.url: continue else: lt = self.list_other(other) if other in self.remote_files: if not list_equal(lt, self.remote_files[other]): self.remote_files[other] = lt if not len(lt): del self.remote_files[other] self._trigger_update_remote() elif len(lt): # k not in dict self.remote_files[other] = lt self._trigger_update_remote() return True
def onFetchHandler(self,value): mylogger.info("[onFetchHandler]") # by default ,for a button value is False arg = str(self.main_widget.le.text()) if not arg.strip(): msg = 'Please enter query file' self.statusbar.showMessage(msg) return # add statusbar messge for fetching file msg = "Fetching [{0}].......".format(arg) mylogger.info(msg) self.statusbar.showMessage(msg) # use NodeService code = NodeService.fetch(self,arg) if code == SUCCESS: msg ="Fetch successfully for [{0}]".format(arg) self._onFetchSuccessfully(arg) elif code == ACCESS_DENIED: msg ="Access denied for [{0}]".format(arg) elif code == NOT_EXIST: msg ="Not exist for [{0}]".format(arg) else: msg = "Already exist for [{0}]".format(arg) mylogger.info(msg) self.statusbar.showMessage(msg)
def fetch(self, query, secret): """ fetch a given file from all available nodes query: filepath """ mylogger.info('-' * 60) mylogger.info('[fetch]: fetching from {0}'.format(self.url)) if secret != self.secret: return ACCESS_DENIED code, data = self.query(query, self.url, []) mylogger.info('[fetch]: query return code {0}'.format(code)) mylogger.info("[fetch]: knows: {0}".format(self.known)) if code == SUCCESS: # create a background(daemon) thread to save file thread = SaveFileThread('Thread-savefile', query, data) thread.start() return code
def fetch(self,query,secret): """ fetch a given file from all available nodes query: filepath """ mylogger.info('-'*60) mylogger.info('[fetch]: fetching from {0}'.format(self.url)) if secret != self.secret: return ACCESS_DENIED code,data = self.query(query,self.url,[]) mylogger.info('[fetch]: query return code {0}'.format(code)) mylogger.info("[fetch]: knows: {0}".format(self.known)) if code == SUCCESS: # create a background(daemon) thread to save file thread = SaveFileThread('Thread-savefile',query,data) thread.start() return code
def _start(self): """ start node server """ try: t = ('',getport(self.url)) # in both server and client set allow_none=True self.local_server = SimpleXMLRPCServer(t,allow_none=True,logRequests=False) self.local_server.register_instance(self) msg ="[_start]: Server started at {0}...".format(self.url) print(msg) mylogger.info(msg) # 1)on start up ,read urls self._read() # 2)after all urls added to known set,inform others about myself's status online self.online() mylogger.info('[_start]: event_running..') # 3)start server self.event_running.set() # set flag to true self.local_server.serve_forever() except socket.error,e: mylogger.warn(e) mylogger.warn('[_start]: socket error') mylogger.warn('[_start]: program is going to exit...')
def setLocal(self): if NodeService.is_local_updated(self): mylogger.info('[setLocal]...') self._setLocalFiles() self._setLocalLabel() self._setLocalList() mylogger.info('[setLocal] finished') # after set local list,clear local update NodeService.clear_local_update(self) # set to false else: mylogger.info('*********NO GUI UPDATE FOR local list*********')
def setRemote(self): if NodeService.is_remote_updated(self): mylogger.info('[setRemote]...') self._setRemoteFiles() self._setRemoteLabel() self._setRemoteList() mylogger.info('[setRemote] finished') # after set remote list,clear remote update NodeService.clear_remote_update(self) # set to false else: mylogger.info('*********NO GUI UPDATE FOR remote list*********')
def start(self): """ start NodeServerThread in child thread,and connect to server in main thread """ mylogger.info('[start]: NodeService starting...') # 1)start node server in child thread self.server_thread.start() # block current thread until node server is started if not self.event_running.wait(3): sys.exit() mylogger.info('[start]: NodeServerThread started') # 2) connect to server in main thread self.server = ServerProxy(self.url,allow_none=True) mylogger.info('[start]: NodeService started')
def _handle(self, query, starturl): """ handle query in local node # query like './share/11.txt' """ mylogger.info('-' * 20) mylogger.info('[handle]: begin') filepath = query # query is filepath mylogger.info('[handle]: filepath is {0}'.format(filepath)) if not isfile(filepath): mylogger.info('[handle]: not file') return NOT_EXIST, None if not inside(self.dirname, filepath): mylogger.info('[handle]: not inside') return ACCESS_DENIED, None if starturl == self.url: mylogger.info('[handle]: ******already exist******') return ALREADY_EXIST, None mylogger.info('[handle]: success') mylogger.info('[handle]: reading {0} ...'.format(filepath)) t1 = time.clock() data = readfile_asbinary_xmlrpc(filepath) mylogger.info('[handle]: reading finished'.format(filepath)) mylogger.info('[handle]: time used {0}s'.format(time.clock() - t1)) return SUCCESS, data
def list_local(self): """ list files in local node """ mylogger.info('[list_local]: list files in {0}'.format(self.url)) return list_all_files(self.dirname)
def get_url(self): """ get url of local node """ mylogger.info('[get_url]: ') return self.url
def __init__(self,name,target): mylogger.info('[__init__]: {0}'.format(name)) super(SaveIPsThread,self).__init__() self.name = name self.daemon = True self.target = target
def run(self): mylogger.info('[SaveIPsThread]: Starting {0}'.format(self.name)) # call target self.target() mylogger.info('[SaveIPsThread]: Exiting {0}'.format(self.name))
def _broadcast(self,query,starturl,history): """ broadcast to all other nodes """ mylogger.info('-'*10) mylogger.info('[broadcast]:') mylogger.info("knows: {0}".format(self.known)) mylogger.info("history: {0}".format(history)) for other in self.known.copy(): mylogger.info('[broadcast]: other is {0}'.format(other)) if other in history: continue s = ServerProxy(other) mylogger.info('[broadcast]: Connecting from {0} to {1}'.format(self.url,other)) mylogger.info('*'*80) try: code,data = s.query(query,starturl,history) mylogger.info('[broadcast]: query return code {0}'.format(code)) if code == SUCCESS: mylogger.info('[broadcast]: query SUCCESS!!!') return code,data elif code == NOT_EXIST: mylogger.info('[broadcast]: query NOT_EXIST!!!') else: mylogger.info('[broadcast]: query ACCESS_DENIED!!!') except Fault, f: # connected to server,but method does not exist(Never happen in this example) mylogger.warn(f) mylogger.warn("[broadcast]:except fault") except socket.error, e: mylogger.warn("[broadcast]:except socket error") mylogger.error('[broadcast]: {0} for {1}'.format(e,other)) # added by kzl self.known.remove(other)
def query(self, query, starturl, history=[]): """ query a given file return value:(NOT_EXIST,None)(ACCESS_DENIED,None)(ALREADY_EXIST,None)(SUCCESS,data) """ mylogger.info('-' * 40) mylogger.info('[query]: querying from {0}'.format(self.url)) code, data = self._handle(query, starturl) if code == SUCCESS: mylogger.info('[query]: success') return code, data elif code == NOT_EXIST: # history is a list containing urls from which we can not find file history = history + [self.url] if len(history) > MAX_HISTORY_LENGTH: mylogger.info('[query]: history too long') return NOT_EXIST, None mylogger.info("[query]: query for {0} NOT in {1}".format( query, history)) code, data = self._broadcast(query, starturl, history) mylogger.info("[query]: [after broadcast]: {0}".format(code)) mylogger.info("[query]: knows: {0}".format(self.known)) return code, data else: # access denied or already exist return code, data
def _broadcast(self, query, starturl, history): """ broadcast to all other nodes """ mylogger.info('-' * 10) mylogger.info('[broadcast]:') mylogger.info("knows: {0}".format(self.known)) mylogger.info("history: {0}".format(history)) for other in self.known.copy(): mylogger.info('[broadcast]: other is {0}'.format(other)) if other in history: continue s = ServerProxy(other) mylogger.info('[broadcast]: Connecting from {0} to {1}'.format( self.url, other)) mylogger.info('*' * 80) try: code, data = s.query(query, starturl, history) mylogger.info( '[broadcast]: query return code {0}'.format(code)) if code == SUCCESS: mylogger.info('[broadcast]: query SUCCESS!!!') return code, data elif code == NOT_EXIST: mylogger.info('[broadcast]: query NOT_EXIST!!!') else: mylogger.info('[broadcast]: query ACCESS_DENIED!!!') except Fault, f: # connected to server,but method does not exist(Never happen in this example) mylogger.warn(f) mylogger.warn("[broadcast]:except fault") except socket.error, e: mylogger.warn("[broadcast]:except socket error") mylogger.error('[broadcast]: {0} for {1}'.format(e, other)) # added by kzl self.known.remove(other)