Example #1
0
	def _shutdown(self):
		"""
		shut down node server
		"""
		mylogger.warn('[_shutdown]: shutdown server...')
		# 1)on shutdown,save urls on thread-save
		thread_save = SaveIPsThread('Thread-save',self._save)
		thread_save.start()
		# 2) inform other nodes that myself is offline
		self.offline()
		# 3) shutdown server
		self.local_server.shutdown()
Example #2
0
    def _shutdown(self):
        """
		shut down node server
		"""
        mylogger.warn('[_shutdown]: shutdown server...')
        # 1)on shutdown,save urls on thread-save
        thread_save = SaveIPsThread('Thread-save', self._save)
        thread_save.start()
        # 2) inform other nodes that myself is offline
        self.offline()
        # 3) shutdown server
        self.local_server.shutdown()
Example #3
0
	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))
Example #4
0
    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))
Example #5
0
	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))
Example #6
0
	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))
Example #7
0
    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))
Example #8
0
    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))
Example #9
0
    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...')
Example #10
0
    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)
Example #11
0
	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)
Example #12
0
	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...')
Example #13
0
class Node:
    """
	a simple node class
	"""
    def __init__(self, url, dirname, secret, ipsfile, event_running):
        self.url = url
        self.dirname = dirname
        self.secret = secret
        # ipsfile for storing all available nodes
        self.ipsfile = ipsfile
        # store all known urls in set (including self)
        self.known = set()
        # inform client node server is running
        self.event_running = event_running
        # inform client to update local or remote list
        self.event_update_local = Event()
        self.event_update_remote = Event()

        # New variables
        # store local node server for later shutdown
        self.local_server = None

        # NEW variables
        self.local_files = []
        # url---[f1,f2,f3...]
        self.remote_files = {}

    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 _save(self):
        """
		save urls to ipsfile
		"""
        mylogger.info('[_save]: saving urls ... ')
        save_urls(self.known, self.ipsfile)
        mylogger.info('[_save]: saving urls finiehed')

    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...')
            # event_running must be false
        except Exception, e:
            mylogger.warn(e)
            mylogger.warn('[_start]: except')
            mylogger.warn('[_start]: Server stopped at {0}'.format(self.url))
Example #14
0
                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)
                #mylogger.warn('[broadcast]: <knows>: {0}'.format(self.known))
                #mylogger.warn("[broadcast]: <history>: {0}".format(history))
            except Exception, e:
                mylogger.warn(e)
                mylogger.warn("[broadcast]: Exception")
        mylogger.info('[broadcast] not found')
        return NOT_EXIST, None

    """
	node that we can list all available files in dirname
	"""

    def is_local_updated(self):
        """
		whether local updated
		"""
        return self.event_update_local.is_set()

    def is_remote_updated(self):
Example #15
0
				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)
				#mylogger.warn('[broadcast]: <knows>: {0}'.format(self.known))
				#mylogger.warn("[broadcast]: <history>: {0}".format(history))
			except Exception, e:
				mylogger.warn(e)
				mylogger.warn("[broadcast]: Exception")
		mylogger.info('[broadcast] not found')
		return NOT_EXIST,None

	"""
	node that we can list all available files in dirname
	"""
	def is_local_updated(self):
		"""
		whether local updated
		"""
		return self.event_update_local.is_set()

	def is_remote_updated(self):
		"""