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]
-