'''
Created on Jan 4, 2012
Modified on Mar 21, 2012
@author: Dilum Bandara
@version: 0.1
@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.
'''

import Config
import md5

class PeerID(object):
    '''
    Peer ID class
    '''


    def __init__(self, NID, globalKey, localKey = Config.MAX_KEY, \
                                                            localStartKey = 0):
        '''
        Constructor
        @param NID: Node ID
        @param globalKey: Overlay key (end of key range node is responsible for)
        @param localKey: Local key (end of range) - If not provided set to 
        largest key value
        @param localStartKey: Start local range (use to simplify implementation)
        If not provided set to 0 
        '''
        self.NID = NID
        self.gKey = globalKey
        self.lKey = localKey #Local key within cluster
        self.lStartKey = localStartKey#Start of local key range. Hack to speedup
        self.NIDHash = int(md5.new(str(NID)).hexdigest(), 16) % \
                                (Config.MAX_KEY + 1) #Round based on overlay key