'''
Created on Jan 22, 2011
Modified on Apr 24, 2011
@author: Dilum Bandara
@version: 0.21
@copyright: Copyright(c) 2011, Dilum Bandara
@license: GPL
'''

from BaseClasses.BaseOverlay import BaseOverlay
import BaseClasses.BaseConfig as BaseConfig

class Overlay(BaseOverlay):
    '''
    Overlay class. 
    '''
    def __init__(self):
        super(Overlay, self).__init__()


    def route_new_ads(self, source, atts):
        '''
        Route resource ads to given destination & return no of overlay hops
        @param source: Source node ID
        @param atts: Attributes
        @return: Success/Failure
        '''
        result = BaseConfig.MANAGERS[0].add_to_index(source, atts)
        if result == False:
            print 'Unable to add to index @ central node'
            return []
        return [1] #1 hop to central node


    def route_old_ads(self, source, atts):
        '''
        Route resource remove msgs to given destination & return no of overlay hops
        @param source: Source node ID
        @param atts: Attributes
        @return: Success/Failure
        '''
        return [0] #No remove message routing


    def route_query(self, que_msg):
        '''
        Route query to given destination & return no of overlay hops   
        @param que_msg: Query message
        @return: Modified query message
        '''        
        result = BaseConfig.MANAGERS[0].query_index(que_msg)
        result.hops += 1 #Increase hop count
        result.hops = [result.hops]
        if result.n <= result.num_res_found: #Found enough resources
            result.success = True
        return result
