"""This file provides two classes, TalkServer and TalkHandler. They
work as a team, as they are subclasses of ThreadingTCPServer and
BaseRequestHandler, which are designed to work as a pair anyway. They
really just do the grunt work here, quickly passing control over to a
Session object from sessions.py."""
from constants import *
import SocketServer,socket,struct
import users
import sessions
class TalkServer(SocketServer.ThreadingTCPServer):
allow_reuse_address = 1
def __init__(self,port):
addr = ('0.0.0.0',port) # 0.0.0.0 is the current machine
SocketServer.ThreadingTCPServer.__init__(self,addr,TalkHandler)
# the following line doesn't work - it's supposed to allow
# a C-c shutdown of the talker even with users logged in
self.socket.setsockopt(socket.SOL_SOCKET,socket.SO_LINGER,
struct.pack("ii",0,0))
print "Server up and running!"
class TalkHandler(SocketServer.BaseRequestHandler):
def handle(self):
session = sessions.Session(self)
while session.logged_in:
# lose the user if they're not logged in
msg = session.get_input()
if not msg: break # this means their client has disconnected
processed = session.process(msg)
session.user_logout()
del(session)