More whitespace cleanup.

This commit is contained in:
Griatch 2012-03-30 23:57:04 +02:00
parent c0322c9eae
commit 45c5be8468
43 changed files with 1116 additions and 1131 deletions

View file

@ -1,33 +1,33 @@
"""
This module defines handlers for storing sessions when handles
sessions of users connecting to the server.
This module defines handlers for storing sessions when handles
sessions of users connecting to the server.
There are two similar but separate stores of sessions:
ServerSessionHandler - this stores generic game sessions
ServerSessionHandler - this stores generic game sessions
for the game. These sessions has no knowledge about
how they are connected to the world.
how they are connected to the world.
PortalSessionHandler - this stores sessions created by
twisted protocols. These are dumb connectors that
handle network communication but holds no game info.
"""
import time
from django.conf import settings
from django.contrib.auth.models import User
from src.server.models import ServerConfig
from src.utils import utils
from src.utils import utils
from src.commands.cmdhandler import CMD_LOGINSTART
# AMP signals
# AMP signals
PCONN = chr(1) # portal session connect
PDISCONN = chr(2) # portal session disconnect
PSYNC = chr(3) # portal session sync
SLOGIN = chr(4) # server session login
SDISCONN = chr(5) # server session disconnect
SDISCONN = chr(5) # server session disconnect
SDISCONNALL = chr(6) # server session disconnect all
SSHUTD = chr(7) # server shutdown
SSHUTD = chr(7) # server shutdown
SSYNC = chr(8) # server session sync
# i18n
@ -43,7 +43,7 @@ class SessionHandler(object):
"""
def __init__(self):
"""
Init the handler.
Init the handler.
"""
self.sessions = {}
@ -64,13 +64,13 @@ class SessionHandler(object):
def get_all_sync_data(self):
"""
Create a dictionary of sessdata dicts representing all
sessions in store.
Create a dictionary of sessdata dicts representing all
sessions in store.
"""
sessdict = {}
for sess in self.sessions.values():
# copy all relevant data from all sessions
sessdict[sess.sessid] = sess.get_sync_data()
sessdict[sess.sessid] = sess.get_sync_data()
return sessdict
#------------------------------------------------------------
@ -80,13 +80,13 @@ class SessionHandler(object):
class ServerSessionHandler(SessionHandler):
"""
This object holds the stack of sessions active in the game at
any time.
any time.
A session register with the handler in two steps, first by
registering itself with the connect() method. This indicates an
non-authenticated session. Whenever the session is authenticated
the session together with the related player is sent to the login()
method.
method.
"""
@ -94,15 +94,15 @@ class ServerSessionHandler(SessionHandler):
def __init__(self):
"""
Init the handler.
Init the handler.
"""
self.sessions = {}
self.server = None
self.server = None
self.server_data = {"servername":settings.SERVERNAME}
def portal_connect(self, sessid, session):
"""
Called by Portal when a new session has connected.
Called by Portal when a new session has connected.
Creates a new, unlogged-in game session.
"""
self.sessions[sessid] = session
@ -122,10 +122,10 @@ class ServerSessionHandler(SessionHandler):
"""
Syncing all session ids of the portal with the ones of the server. This is instantiated
by the portal when reconnecting.
sesslist is a complete list of (sessid, session) pairs, matching the list on the portal.
if session was logged in, the amp handler will have logged them in before this point.
"""
"""
for sess in self.sessions.values():
# we delete the old session to make sure to catch eventual lingering references.
del sess
@ -136,15 +136,15 @@ class ServerSessionHandler(SessionHandler):
def portal_shutdown(self):
"""
Called by server when shutting down the portal.
"""
"""
self.server.amp_protocol.call_remote_PortalAdmin(0,
operation=SSHUTD,
data="")
# server-side access methods
data="")
# server-side access methods
def disconnect(self, session, reason=""):
"""
Called from server side to remove session and inform portal
Called from server side to remove session and inform portal
of this fact.
"""
session = self.sessions.get(session.sessid, None)
@ -157,7 +157,7 @@ class ServerSessionHandler(SessionHandler):
data=reason)
self.session_count(-1)
def login(self, session):
"""
Log in the previously unloggedin session and the player we by
@ -165,22 +165,22 @@ class ServerSessionHandler(SessionHandler):
assume the session to be logged in one way or another.
"""
# prep the session with player/user info
if not ALLOW_MULTISESSION:
# disconnect previous sessions.
self.disconnect_duplicate_sessions(session)
session.logged_in = True
session.logged_in = True
self.session_count(1)
# sync the portal to this session
sessdata = session.get_sync_data()
self.server.amp_protocol.call_remote_PortalAdmin(session.sessid,
operation=SLOGIN,
data=sessdata)
def session_sync(self):
"""
This is called by the server when it reboots. It syncs all session data
to the portal.
to the portal.
"""
sessdata = self.get_all_sync_data()
self.server.amp_protocol.call_remote_PortalAdmin(0,
@ -192,7 +192,7 @@ class ServerSessionHandler(SessionHandler):
"""
Cleanly disconnect all of the connected sessions.
"""
for session in self.sessions:
del session
self.session_count(0)
@ -203,42 +203,42 @@ class ServerSessionHandler(SessionHandler):
def disconnect_duplicate_sessions(self, curr_session, reason = _("Logged in from elsewhere. Disconnecting.") ):
"""
Disconnects any existing sessions with the same game object.
Disconnects any existing sessions with the same game object.
"""
curr_char = curr_session.get_character()
doublet_sessions = [sess for sess in self.sessions
if sess.logged_in
if sess.logged_in
and sess.get_character() == curr_char
and sess != curr_session]
for sessid in doublet_sessions:
self.disconnect(session, reason)
self.disconnect(session, reason)
self.session_count(-1)
def validate_sessions(self):
"""
Check all currently connected sessions (logged in and not)
Check all currently connected sessions (logged in and not)
and see if any are dead.
"""
tcurr = time.time()
reason= _("Idle timeout exceeded, disconnecting.")
for session in (session for session in self.sessions.values()
if session.logged_in and IDLE_TIMEOUT > 0
for session in (session for session in self.sessions.values()
if session.logged_in and IDLE_TIMEOUT > 0
and (tcurr - session.cmd_last) > IDLE_TIMEOUT):
self.disconnect(session, reason=reason)
self.session_count(-1)
def session_count(self, num=None):
"""
Count up/down the number of connected, authenticated users.
Count up/down the number of connected, authenticated users.
If num is None, the current number of sessions is returned.
num can be a positive or negative value to be added to the current count.
If 0, the counter will be reset to 0.
num can be a positive or negative value to be added to the current count.
If 0, the counter will be reset to 0.
"""
if num == None:
# show the current value. This also syncs it.
return int(ServerConfig.objects.conf('nr_sessions', default=0))
# show the current value. This also syncs it.
return int(ServerConfig.objects.conf('nr_sessions', default=0))
elif num == 0:
# reset value to 0
ServerConfig.objects.conf('nr_sessions', 0)
@ -255,7 +255,7 @@ class ServerSessionHandler(SessionHandler):
Only logged-in players are counted here.
"""
return len(set(session.uid for session in self.sessions.values() if session.logged_in))
def sessions_from_player(self, player):
"""
Given a player, return any matching sessions.
@ -275,7 +275,7 @@ class ServerSessionHandler(SessionHandler):
player = character.player
if player:
return self.sessions_from_player(player)
return None
return None
def announce_all(self, message):
@ -295,15 +295,15 @@ class ServerSessionHandler(SessionHandler):
def data_in(self, sessid, string="", data=""):
"""
Data Portal -> Server
"""
session = self.sessions.get(sessid, None)
if session:
"""
session = self.sessions.get(sessid, None)
if session:
session.execute_cmd(string)
# ignore 'data' argument for now; this is otherwise the place
# to put custom effects on the server due to data input, e.g.
# from a custom client.
# from a custom client.
def oob_data_in(self, sessid, data):
"""
OOB (Out-of-band) Data Portal -> Server
@ -327,11 +327,11 @@ class PortalSessionHandler(SessionHandler):
"""
This object holds the sessions connected to the portal at any time.
It is synced with the server's equivalent SessionHandler over the AMP
connection.
connection.
Sessions register with the handler using the connect() method. This
Sessions register with the handler using the connect() method. This
will assign a new unique sessionid to the session and send that sessid
to the server using the AMP connection.
to the server using the AMP connection.
"""
@ -339,7 +339,7 @@ class PortalSessionHandler(SessionHandler):
"""
Init the handler
"""
self.portal = None
self.portal = None
self.sessions = {}
self.latest_sessid = 0
self.uptime = time.time()
@ -351,19 +351,19 @@ class PortalSessionHandler(SessionHandler):
Server. At this point, the AMP connection is already
established.
"""
self.connection_time = time.time()
self.connection_time = time.time()
def connect(self, session):
"""
Called by protocol at first connect. This adds a not-yet authenticated session
using an ever-increasing counter for sessid.
"""
using an ever-increasing counter for sessid.
"""
self.latest_sessid += 1
sessid = self.latest_sessid
session.sessid = sessid
sessdata = session.get_sync_data()
self.sessions[sessid] = session
# sync with server-side
# sync with server-side
self.portal.amp_protocol.call_remote_ServerAdmin(sessid,
operation=PCONN,
data=sessdata)
@ -374,23 +374,23 @@ class PortalSessionHandler(SessionHandler):
sessid = session.sessid
self.portal.amp_protocol.call_remote_ServerAdmin(sessid,
operation=PDISCONN)
def server_disconnect(self, sessid, reason=""):
"""
Called by server to force a disconnect by sessid
"""
session = self.sessions.get(sessid, None)
if session:
session.disconnect(reason)
del session
session.disconnect(reason)
del session
def server_disconnect_all(self, reason=""):
"""
Called by server when forcing a clean disconnect for everyone.
"""
for session in self.sessions.values():
for session in self.sessions.values():
session.disconnect(reason)
del session
del session
def count_loggedin(self, include_unloggedin=False):
@ -398,22 +398,22 @@ class PortalSessionHandler(SessionHandler):
Count loggedin connections, alternatively count all connections.
"""
return len(self.get_sessions(include_unloggedin=include_unloggedin))
def session_from_suid(self, suid):
"""
Given a session id, retrieve the session (this is primarily
intended to be called by web clients)
"""
return [sess for sess in self.get_sessions(include_unloggedin=True)
return [sess for sess in self.get_sessions(include_unloggedin=True)
if hasattr(sess, 'suid') and sess.suid == suid]
def data_in(self, session, string="", data=""):
"""
Called by portal sessions for relaying data coming
in from the protocol to the server. data is
serialized before passed on.
"""
Called by portal sessions for relaying data coming
in from the protocol to the server. data is
serialized before passed on.
"""
self.portal.amp_protocol.call_remote_MsgPortal2Server(session.sessid,
msg=string,
data=data)
@ -426,12 +426,12 @@ class PortalSessionHandler(SessionHandler):
def data_out(self, sessid, string="", data=""):
"""
Called by server for having the portal relay messages and data
to the correct session protocol.
Called by server for having the portal relay messages and data
to the correct session protocol.
"""
session = self.sessions.get(sessid, None)
if session:
session.data_out(string, data=data)
session.data_out(string, data=data)
def oob_data_in(self, session, data):
"""