'''
Created on Mar 9, 2012
Modified on May 18, 2012
@version: 0.1
@author: Dilum Bandara
@email: dilumb@engr.colostate.edu
@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 PSubscription:
    '''
    Proxy subscription class.
    '''
    def __init__(self, name, currentTime, validTime, interval, userID, query, \
                 minLoc, maxLoc, app):
        '''
        Constructor
        @param name: Name of interest message
        @param currentTime: Current time 
        @param validTIme: Data need to be newer than this time
        @param interval: Subscription interval
        @param userID: User ID
        @param query: Original query issued by user
        @param minLoc: Lower left corner location
        @param maxLoc: Upper right corner location
        @param app: Application type           
        '''
        self.name = name
        self.nextQueryTime = currentTime + interval 
        self.interval = interval
        self.validDelay = currentTime - validTime
        self.validTime = validTime
        self.nextValidTime = validTime + interval
        self.users = {userID:query}
        self.minLoc = minLoc
        self.maxLoc = maxLoc
        self.app = app
        self.pendingValidTime = [] #Pending valid times for 1st round


    def addUser(self, userID, query):
        '''
        Add user to this subscription
        @param userID: User ID
        @param query: Original query issued by user        
        '''
        self.users[userID] = query


    def setNextValidTime(self):
        '''
        Set valid time for next round
        '''
        self.validTime = self.nextValidTime 
        self.nextValidTime += self.interval