X-Git-Url: https://git.rvb.name/vpproxy.git/blobdiff_plain/1074f7e8729c2ff065d73960c5450c9356c2fd11..1aebc74d991c35b7eb82395434d543d44f536089:/clientcounter.py diff --git a/clientcounter.py b/clientcounter.py index bcfd0b2..a106d35 100644 --- a/clientcounter.py +++ b/clientcounter.py @@ -2,37 +2,54 @@ Simple Client Counter for VLC VLM ''' +import gevent +import logging class ClientCounter(object): def __init__(self): self.clients = dict() + self._lock = gevent.coros.RLock() self.total = 0 def get(self, id): return self.clients.get(id, (False,))[0] - def add(self, id, ip): + def add(self, id, ip, unic): + self._lock.acquire() + logger = logging.getLogger('clientcounter_Add') if self.clients.has_key(id): self.clients[id][0] += 1 - self.clients[id][1].append(ip) + self.clients[id][1].append([ip,unic]) + logger.info('counter for %s incremented to %s' % (id,self.clients[id][0])) else: - self.clients[id] = [1, [ip]] + self.clients[id] = [1, [[ip,unic]]] + logger.info('counter for %s started (1)' % id) self.total += 1 + logger.info('total count = %s' % self.total) + + self._lock.release() return self.clients[id][0] - def delete(self, id, ip): + def delete(self, id, ip, unic): + self._lock.acquire() + logger = logging.getLogger('clientcounter_Del') if self.clients.has_key(id): self.total -= 1 + logger.info('total count = %s' % self.total) if self.clients[id][0] == 1: del self.clients[id] + logger.info('counter for %s decremented to zero' % id ) + self._lock.release() return False else: self.clients[id][0] -= 1 - self.clients[id][1].remove(ip) + self.clients[id][1].remove([ip,unic]) + logger.info('counter for %s decremented to %s' % (id,self.clients[id][0])) else: + self._lock.release() return False + self._lock.release() return self.clients[id][0] -