"""
ajaxWM - the free web based window manager for remote terminals
Copyright (C) 2007 Dennis Felsing, Andreas Waidler

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

$Id: Loop.py 76 2007-12-15 11:26:24Z r0q $
"""

#{{{ Imports
import time
import threading

import Server
import Session
#}}}
class Loop(threading.Thread):#{{{
    """
Loop class for checking every TIMEOUT seconds if the session 
it was called for did do something. If the session was idling all the
time, which in most cases means the client holding the session has been
disconnected, it is killed.

If the session already got killed somewhere else the object of this class
is destroyed so it doesn't delete another session that took the place of
the old one.
    """
    def __init__(self, sessionId):#{{{
        threading.Thread.__init__(self)
        
        self.sessionId = sessionId
        self._wantAbort = False
        
        self.newTime()
    #}}}
    def run(self):#{{{
        while True:
            oldTime = time.time()
            for i in range(Session.Session.TIMEOUT):
                # Check every second if we have been aborted.
                time.sleep(1)
                if self._wantAbort:
                    # abort-Method has been run. That means someone else
                    # already killed the session this object is watching over.
                    return
            if time.time() - self._time > Session.Session.TIMEOUT:
                # Commit suicide
                Server.Server.sessions[self.sessionId] = None
                return
    #}}}
    def newTime(self):#{{{
        """Save the current time."""
        self._time = time.time()
    #}}}
    def abort(self):#{{{
        """Make the loop leave and the thread die."""
        self._wantAbort = True
    #}}}
#}}}

#------------------------------------------------------------------------------
# vim: fdm=marker ts=4 sw=4 expandtab 

