'''
Created on Jan 4, 2012
Modified on Feb 2, 2012
@author: Dilum Bandara
@version: 1.0
@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 OverlayMsg(object):
    '''
    Overlay message class
    '''


    def __init__(self, msgType, source, key = -1, payload = None, ttl = 0, \
                 hops = 0):
        '''
        Constructor
        @param msgType: Message type 
        @param source: Source node (peer ID)
        @param key: Overlay key relevant to message 
        @param ttl: Time to live 
        @param hops: Hops 
        '''
        self.msgType = msgType
        self.source = source
        self.key = key
        self.payload = payload
        self.ttl = ttl
        self.hops = hops
        self.nextHop = -1


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


    def setNextHop(self, NID):
        '''
        Set next hop. This is used to speed up message routing in the simulator 
        using iterative routing. A real implementation instead will use 2
        different messages
        @param NID: Node ID of node to be used as next hop
        '''
        self.nextHop = NID


    def setMsgType(self, msgType):
        '''
        Set message type
        @param msgType: Message type 
        '''
        self.msgType = msgType


    def reduceTTL(self):
        '''
        Reduce TTL
        '''
        self.ttl -= 1
        assert(self.ttl >= 0), 'TTL negative'