'''
Created on Apr 09, 2011
Modified on May 31, 2012
@author: Dilum Bandara
@version: 0.11
@license: Apache License v2.0

   Copyright 2012 H. M. N. Dilum Bandara and Anura P. Jayasumana, Colorado State
   University

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
'''

class QueryMsg(object):
    '''
    Query message class. 
    '''
    def __init__(self, source, n, query, minKey = -1, maxKey = -1, \
                 attToQue = None, msgType = 'Query'):        
        '''        
        @param source: Source address
        @param n: Number of resources to query for
        @param query: List of attribute ranges ['att', min, max]
        @param minKey: Start of query range
        @param maxKey: End of query range
        @param param: Most selective attribute or attribute for sub-query
        @param msgType: Message type - also used as a reply message
        '''
        self.msgType = msgType
        self.source = source
        self.n = n
        self.query = query
        self.minKey = minKey
        self.maxKey = maxKey
        self.attToQue = attToQue
        self.hops = 0 #Number of hops
        self.resources = [] #List of nodes IDs satisfying query
        self.numResFound = 0 #Number of resources found so far
        self.success = False #Was query success
        self.fragmentsToTry = [] #Fragments to try


    def foundEnoughResources(self):
        '''
        If require number of resources are found set message as success 
        @return: True or false         
        '''
        if len(self.resources) >= self.n:
            self.success = True
            return True
        return False


    def incrementHops(self):
        '''
        Increment hops
        '''
        self.hops += 1


    def setHops(self, hops):
        '''
        Set number of hops
        @param hops: Hops 
        '''
        self.hops = hops


    def setFragmentsToTry(self, fragments):
        '''
        list of fragments to try within a cluster
        @param fragments: List of fragments 
        '''
        self.fragmentsToTry = fragments


    def hasFragmentsToTry(self):
        '''
        Is there a list of fragments to try
        @return: True or false
        '''
        if len(self.fragmentsToTry) > 0: return True
        return False


    def popFragment(self):
        '''
        Remove first entry from list of nodes to try
        '''
        if len(self.fragmentsToTry) > 0: self.fragmentsToTry.pop(0)
        else: assert(False), 'Empty list of fragments to try'


    def getFragmentToTry(self):
        '''
        Get next fragment to try
        @return: Node ID
        '''
        if len(self.fragmentsToTry) > 0: return self.fragmentsToTry[0]
        else: return None