1
0
Fork 0

SessionManager and client-server communication implemented

git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/gnuviech.info/gnuviechadmin/trunk@185 a67ec6bc-e5d5-0310-a910-815c51eb3124
This commit is contained in:
Jan Dittberner 2006-04-17 19:20:14 +00:00
parent 985810317a
commit bca6369b41
6 changed files with 118 additions and 36 deletions

View file

@ -6,6 +6,9 @@
from SessionManager import Session
class DomainManager:
def __init__(self, dbconn):
self._dbconn = dbconn
def listDomains(self, session):
if isinstance(session, Session):
return 'a,b,c'

View file

@ -7,13 +7,33 @@ from SessionManager import *
from DomainManager import *
class ServiceFacade:
def __init__(self):
self.sessionManager = SessionManager()
self.domainManager = DomainManager()
"""
This class implements the facade to the services provided by the
gnuviech admin backend.
"""
def __init__(self):
self.sessionManager = SessionManager(None)
self.domainManager = DomainManager(None)
def _dispatch(self, method, params):
try:
func = getattr(self, method)
except AttributeError:
raise Exception('method "%s" is not supported' % method)
else:
if method != 'login' and method != 'logout':
sessionid = params[0]
try:
session = self.sessionManager.getSession(sessionid)
except InvalidSessionError:
return ""
nparams = [session]
for item in params[1:]:
nparams.append(item)
params = nparams
self.sessionManager.updateSession(sessionid)
return func(*params)
def login(self, login, password):
"""
Logs in the user specified by the given login and password.
@ -26,12 +46,12 @@ class ServiceFacade:
except InvalidLoginError, ile:
return 0
def listdomains(self, sessionid):
def logout(self, sessionid):
self.sessionManager.deleteSession(sessionid)
return 0
def listdomains(self, session):
"""
Lists the domains the given session may see.
"""
try:
session = self.sessionManager.getSession(sessionid)
return self.domainManager.listDomains(session)
except InvalidSessionError, ise:
return ""
return self.domainManager.listDomains(session)

View file

@ -1,37 +1,81 @@
#
# Session manager class for gnuviech-admin tool backend
# (c) 2006 Jan Dittberner <jan@dittberner.info>
# $Id$
#
"""
Session manager class for gnuviech-admin tool backend
(c) 2006 Jan Dittberner <jan@dittberner.info>
$Id$
"""
import Settings
import os
import os, sha, time, logging
from threading import Timer
SESSIONTIMEOUT=120 # 2 minutes
class InvalidLoginError(Exception):
"""
Exception class for invalid logins.
"""
pass
class InvalidSessionError(Exception):
"""
Exception class for invalid sessions.
"""
pass
class Session:
def __init__(self, id, login, rights, uid, gid):
def __init__(self, id, login):
self.id = id
self.login = login
self.right = rights
self.uid = uid
self.gid = gid
self._timeoutTimer = None
def settimeoutTimer(self, timeoutTimer):
self._timeoutTimer = timeoutTimer
self._timeoutTimer.start()
def gettimeoutTimer(self):
return self._timeoutTimer
class SessionManager:
def _getSessionFile(self, sessionid):
if os.path.exists(Settings.SESSIONDIR) and \
os.path.isdir(Settings.SESSIONDIR):
return file(os.path.join(SETTINGS.SESSIONDIR, sessionid))
return None
def __init__(self, dbconn):
self._sessions = {}
self._dbconn = dbconn
self._hashobj = sha.new(str(time.time()))
self.logger = logging.getLogger('SessionManager')
def listSessions(self):
return self._sessions.keys()
def newSession(self, login, password):
raise InvalidLoginError()
self._hashobj.update("%s,%s" % (time.time(), login))
sessionid = self._hashobj.hexdigest()
self._sessions[sessionid] = Session(sessionid, login)
self.updateSession(sessionid)
return sessionid
def updateSession(self, sessionid):
self.logger.debug("update session %s" % sessionid)
try:
session = self.getSession(sessionid)
except InvalidSessionError, ev:
pass
else:
if session.gettimeoutTimer() is not None:
session.gettimeoutTimer().cancel()
session.settimeoutTimer(Timer(SESSIONTIMEOUT, self.deleteSession,
args=[sessionid]))
def getSession(self, sessionid):
if self._sessions.has_key(sessionid):
return self._sessions[sessionid]
raise InvalidSessionError()
def deleteSession(self, sessionid):
self._getSessionFile(sessionid)
self.logger.debug("delete session %s" % sessionid)
try:
session = self.getSession(sessionid)
except InvalidSessionError:
print "invalid session"
else:
if session.gettimeoutTimer() is not None:
session.gettimeoutTimer().cancel()
del(self._sessions[sessionid])
self.logger.debug("%d sessions remaining" % len(self.listSessions()))

View file

@ -19,4 +19,3 @@ WEBHOME = ALLPREFIX + '/home/www/'
USERPREFIX = 'usr'
GNUVIECHADMINDIR = ALLPREFIX + '/var/lib/gnuviechadmin'
SESSIONDIR = GNUVIECHADMINDIR + '/sessions'

View file

@ -6,16 +6,24 @@
#
from DocXMLRPCServer import DocXMLRPCServer
from GnuviechAdmin import ServiceFacade
import logging
def startRPCServer():
server = DocXMLRPCServer(("localhost", 8080))
server.register_introspection_functions()
server.register_instance(ServiceFacade())
address = ('localhost', 8080)
server = DocXMLRPCServer(address)
server.register_introspection_functions()
server.register_instance(ServiceFacade())
try:
server.serve_forever()
except KeyboardInterrupt:
server.server_close()
try:
server.serve_forever()
except KeyboardInterrupt:
server.server_close()
if __name__ == "__main__":
startRPCServer()
logger = logging.getLogger()
hdlr = logging.FileHandler('xmlrpcserver.log')
f = logging.Formatter('%(asctime)s %(levelname)s %(module)s: %(message)s')
hdlr.setFormatter(f)
logger.addHandler(hdlr)
logger.setLevel(logging.DEBUG)
startRPCServer()

View file

@ -11,11 +11,19 @@ if __name__ == '__main__':
logger = logging.getLogger()
logging.basicConfig()
sessionid = None
try:
sessionid = server.login('jan', 'heyyou97')
if sessionid:
server.listDomains(sessionid)
print "Session %s" % sessionid
print server.listdomains(sessionid)
server.logout(sessionid)
else:
print "login failed"
except Exception, v:
logger.exception(v)
if sessionid is not None:
try:
server.logout(sessionid)
except Exception, vn:
logger.exception(vn)