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:
parent
985810317a
commit
bca6369b41
6 changed files with 118 additions and 36 deletions
|
@ -6,6 +6,9 @@
|
||||||
from SessionManager import Session
|
from SessionManager import Session
|
||||||
|
|
||||||
class DomainManager:
|
class DomainManager:
|
||||||
|
def __init__(self, dbconn):
|
||||||
|
self._dbconn = dbconn
|
||||||
|
|
||||||
def listDomains(self, session):
|
def listDomains(self, session):
|
||||||
if isinstance(session, Session):
|
if isinstance(session, Session):
|
||||||
return 'a,b,c'
|
return 'a,b,c'
|
||||||
|
|
|
@ -7,13 +7,33 @@ from SessionManager import *
|
||||||
from DomainManager import *
|
from DomainManager import *
|
||||||
|
|
||||||
class ServiceFacade:
|
class ServiceFacade:
|
||||||
def __init__(self):
|
|
||||||
self.sessionManager = SessionManager()
|
|
||||||
self.domainManager = DomainManager()
|
|
||||||
"""
|
"""
|
||||||
This class implements the facade to the services provided by the
|
This class implements the facade to the services provided by the
|
||||||
gnuviech admin backend.
|
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):
|
def login(self, login, password):
|
||||||
"""
|
"""
|
||||||
Logs in the user specified by the given login and password.
|
Logs in the user specified by the given login and password.
|
||||||
|
@ -26,12 +46,12 @@ class ServiceFacade:
|
||||||
except InvalidLoginError, ile:
|
except InvalidLoginError, ile:
|
||||||
return 0
|
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.
|
Lists the domains the given session may see.
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
session = self.sessionManager.getSession(sessionid)
|
|
||||||
return self.domainManager.listDomains(session)
|
return self.domainManager.listDomains(session)
|
||||||
except InvalidSessionError, ise:
|
|
||||||
return ""
|
|
||||||
|
|
|
@ -1,37 +1,81 @@
|
||||||
#
|
"""
|
||||||
# Session manager class for gnuviech-admin tool backend
|
Session manager class for gnuviech-admin tool backend
|
||||||
# (c) 2006 Jan Dittberner <jan@dittberner.info>
|
(c) 2006 Jan Dittberner <jan@dittberner.info>
|
||||||
# $Id$
|
$Id$
|
||||||
#
|
"""
|
||||||
import Settings
|
import Settings
|
||||||
import os
|
import os, sha, time, logging
|
||||||
|
from threading import Timer
|
||||||
|
|
||||||
|
SESSIONTIMEOUT=120 # 2 minutes
|
||||||
|
|
||||||
class InvalidLoginError(Exception):
|
class InvalidLoginError(Exception):
|
||||||
|
"""
|
||||||
|
Exception class for invalid logins.
|
||||||
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class InvalidSessionError(Exception):
|
class InvalidSessionError(Exception):
|
||||||
|
"""
|
||||||
|
Exception class for invalid sessions.
|
||||||
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class Session:
|
class Session:
|
||||||
def __init__(self, id, login, rights, uid, gid):
|
def __init__(self, id, login):
|
||||||
self.id = id
|
self.id = id
|
||||||
self.login = login
|
self.login = login
|
||||||
self.right = rights
|
self._timeoutTimer = None
|
||||||
self.uid = uid
|
|
||||||
self.gid = gid
|
def settimeoutTimer(self, timeoutTimer):
|
||||||
|
self._timeoutTimer = timeoutTimer
|
||||||
|
self._timeoutTimer.start()
|
||||||
|
|
||||||
|
def gettimeoutTimer(self):
|
||||||
|
return self._timeoutTimer
|
||||||
|
|
||||||
class SessionManager:
|
class SessionManager:
|
||||||
def _getSessionFile(self, sessionid):
|
def __init__(self, dbconn):
|
||||||
if os.path.exists(Settings.SESSIONDIR) and \
|
self._sessions = {}
|
||||||
os.path.isdir(Settings.SESSIONDIR):
|
self._dbconn = dbconn
|
||||||
return file(os.path.join(SETTINGS.SESSIONDIR, sessionid))
|
self._hashobj = sha.new(str(time.time()))
|
||||||
return None
|
self.logger = logging.getLogger('SessionManager')
|
||||||
|
|
||||||
|
def listSessions(self):
|
||||||
|
return self._sessions.keys()
|
||||||
|
|
||||||
def newSession(self, login, password):
|
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):
|
def getSession(self, sessionid):
|
||||||
|
if self._sessions.has_key(sessionid):
|
||||||
|
return self._sessions[sessionid]
|
||||||
raise InvalidSessionError()
|
raise InvalidSessionError()
|
||||||
|
|
||||||
def deleteSession(self, sessionid):
|
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()))
|
||||||
|
|
|
@ -19,4 +19,3 @@ WEBHOME = ALLPREFIX + '/home/www/'
|
||||||
USERPREFIX = 'usr'
|
USERPREFIX = 'usr'
|
||||||
|
|
||||||
GNUVIECHADMINDIR = ALLPREFIX + '/var/lib/gnuviechadmin'
|
GNUVIECHADMINDIR = ALLPREFIX + '/var/lib/gnuviechadmin'
|
||||||
SESSIONDIR = GNUVIECHADMINDIR + '/sessions'
|
|
||||||
|
|
|
@ -6,9 +6,11 @@
|
||||||
#
|
#
|
||||||
from DocXMLRPCServer import DocXMLRPCServer
|
from DocXMLRPCServer import DocXMLRPCServer
|
||||||
from GnuviechAdmin import ServiceFacade
|
from GnuviechAdmin import ServiceFacade
|
||||||
|
import logging
|
||||||
|
|
||||||
def startRPCServer():
|
def startRPCServer():
|
||||||
server = DocXMLRPCServer(("localhost", 8080))
|
address = ('localhost', 8080)
|
||||||
|
server = DocXMLRPCServer(address)
|
||||||
server.register_introspection_functions()
|
server.register_introspection_functions()
|
||||||
server.register_instance(ServiceFacade())
|
server.register_instance(ServiceFacade())
|
||||||
|
|
||||||
|
@ -18,4 +20,10 @@ def startRPCServer():
|
||||||
server.server_close()
|
server.server_close()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
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()
|
startRPCServer()
|
||||||
|
|
|
@ -11,11 +11,19 @@ if __name__ == '__main__':
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger()
|
||||||
logging.basicConfig()
|
logging.basicConfig()
|
||||||
|
|
||||||
|
sessionid = None
|
||||||
try:
|
try:
|
||||||
sessionid = server.login('jan', 'heyyou97')
|
sessionid = server.login('jan', 'heyyou97')
|
||||||
if sessionid:
|
if sessionid:
|
||||||
server.listDomains(sessionid)
|
print "Session %s" % sessionid
|
||||||
|
print server.listdomains(sessionid)
|
||||||
|
server.logout(sessionid)
|
||||||
else:
|
else:
|
||||||
print "login failed"
|
print "login failed"
|
||||||
except Exception, v:
|
except Exception, v:
|
||||||
logger.exception(v)
|
logger.exception(v)
|
||||||
|
if sessionid is not None:
|
||||||
|
try:
|
||||||
|
server.logout(sessionid)
|
||||||
|
except Exception, vn:
|
||||||
|
logger.exception(vn)
|
||||||
|
|
Loading…
Reference in a new issue