Beispiel #1
0
    def callback(self, packet, sender):
        """In this method should be implemented logic of processing
        response packet from requested node

        @param packet - object of FabnetPacketResponse class
        @param sender - address of sender node.
        If sender == None then current node is operation initiator

        @return object of FabnetPacketResponse
                that should be resended to current node requestor
                or None for disabling packet resending
        """
        if sender:
            return packet

        node_address = packet.ret_parameters.get('node_address', None)
        superior_neighbours = packet.ret_parameters.get('superior_neighbours', None)
        upper_neighbours = packet.ret_parameters.get('upper_neighbours', None)
        node_name = packet.ret_parameters.get('node_name', '')

        if (node_address is None) or (superior_neighbours is None) or (upper_neighbours is None):
            raise Exception('TopologyCognition response packet is invalid! Packet: %s'%str(packet.to_dict()))

        self._lock()
        try:
            db = SafeJsonFile(os.path.join(self.home_dir, TOPOLOGY_DB))
            data = db.read()
            data[node_address] = {'node_name': node_name, 'superiors': superior_neighbours, \
                    'uppers': upper_neighbours, 'old_data': 0}
            db.write(data)
        finally:
            self._unlock()

        if packet.ret_parameters.get('need_rebalance', False):
            self.operator.smart_neighbours_rebalance(node_address, superior_neighbours, upper_neighbours)
Beispiel #2
0
 def run(self):
     for i in self.cnt:
         f = SafeJsonFile(self.filepath)
         try:
             f.write(self.obj)
             time.sleep(.1)
         except Exception, err:
             self.errors.append(str(err))
Beispiel #3
0
 def on_network_notify(self, event_type, event_provider, event_topic, event_message):
     self._lock()
     try:
         db = SafeJsonFile(os.path.join(self.home_dir, NOTIFICATIONS_DB))
         data = db.read()
         n_list = data.get('notifications', [])
         n_list.append({'event_type': event_type, 'event_provider': event_provider, \
                 'event_topic': event_topic, 'event_message': event_message, 'notify_dt': datetime.now().isoformat()})
         data['notifications'] = n_list
         db.write(data)
     finally:
         self._unlock()
Beispiel #4
0
    def before_resend(self, packet):
        """In this method should be implemented packet transformation
        for resend it to neighbours

        @params packet - object of FabnetPacketRequest class
        @return object of FabnetPacketRequest class
                or None for disabling packet resend to neigbours
        """
        if packet.sender is None:
            db = SafeJsonFile(os.path.join(self.home_dir, TOPOLOGY_DB))
            data = db.read()
            if data:
                for item in data.values():
                    item['old_data'] = 1
                db.write(data)

        return packet