First naive implementation of methods
This commit is contained in:
		@@ -1,6 +1,6 @@
 | 
				
			|||||||
#!/usr/bin/env python
 | 
					#!/usr/bin/env python
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#IDE JÖNNEK IMPORTOK
 | 
					import datetime
 | 
				
			||||||
 | 
					
 | 
				
			||||||
"""
 | 
					"""
 | 
				
			||||||
Main Flask RESTful API
 | 
					Main Flask RESTful API
 | 
				
			||||||
@@ -15,36 +15,70 @@ __version__text__ = "1"
 | 
				
			|||||||
class ConsumerLocator:
 | 
					class ConsumerLocator:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self):
 | 
					    def __init__(self):
 | 
				
			||||||
        self.consumerList = ["KnownHost"]
 | 
					        self.consumerList = [{"Host": "KnownHost", "State": True, "LastOk": datetime.datetime.now()}]
 | 
				
			||||||
        self.currentConsumer = self.consumerList[0]
 | 
					        self.currentConsumer = self.consumerList[0]["Host"]
 | 
				
			||||||
        pass
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def learnConsumerList(self):
 | 
					    def learnConsumerList(self):
 | 
				
			||||||
        """"
 | 
					        """"
 | 
				
			||||||
        Learns the list of consumers.
 | 
					        Learns the list of consumers.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        pass
 | 
					        recievedConsumerList = communicator.getConsumerList()
 | 
				
			||||||
 | 
					        for consumer in recievedConsumerList:
 | 
				
			||||||
 | 
					            self.consumerList.append({"Host": consumer, "State": True, "LastOk": datetime.datetime.now()})
 | 
				
			||||||
 | 
					        self.updateConsumerList()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def updateConsumerList(self):
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        Updates the consumer list based on their availability.
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        removeList = []
 | 
				
			||||||
 | 
					        for consumer in self.consumerList:
 | 
				
			||||||
 | 
					            if not communicator.ping(consumer["Host"]):
 | 
				
			||||||
 | 
					                consumer["State"] = False
 | 
				
			||||||
 | 
					                if datetime.datetime.now() - consumer["LastOk"] > datetime.timedelta(hours=1):
 | 
				
			||||||
 | 
					                    removeList.append(consumer)
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                consumer["LastOk"] = datetime.datetime.now()
 | 
				
			||||||
 | 
					        for rem in removeList:
 | 
				
			||||||
 | 
					            self.consumerList.remove(rem)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def updateConsumer(self):
 | 
					    def updateConsumer(self):
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        Updates the current consumer.
 | 
					        Checks all the consumers in the list and updates the current consumer if necessary.
 | 
				
			||||||
        :return:
 | 
					        :return: the current consumer
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        self.currentConsumer = self.consumerList[0]
 | 
					
 | 
				
			||||||
 | 
					        self.updateConsumerList()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if not self.checkConsumer():
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            newCurrentConsumer = None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            for consumer in self.consumerList:
 | 
				
			||||||
 | 
					                if consumer["State"]:
 | 
				
			||||||
 | 
					                    newCurrentConsumer = consumer
 | 
				
			||||||
 | 
					                    break
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            self.currentConsumer = newCurrentConsumer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if self.currentConsumer is not None:
 | 
				
			||||||
 | 
					            return self.currentConsumer["Host"]
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            return None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def getCurrentConsumer(self):
 | 
					    def getCurrentConsumer(self):
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        Returns the currently selected consumer.
 | 
					        Returns the currently selected consumer.
 | 
				
			||||||
        :return: the current consumer
 | 
					        :return: the current consumer
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        return self.currentConsumer
 | 
					        return self.currentConsumer["Host"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def checkConsumer(self):
 | 
					    def checkConsumer(self):
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        Check the consumers health.
 | 
					        Check the consumers health.
 | 
				
			||||||
        :return: True if OK, False if fail
 | 
					        :return: True if OK, False if fail
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        if communicator.ping(self.currentConsumer):
 | 
					        if communicator.ping(self.currentConsumer["Host"]):
 | 
				
			||||||
            return True
 | 
					            return True
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            return False
 | 
					            return False
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user