Skip to content

Commit

Permalink
Allow restart/quit to work from the Windows service. Not yet tested.
Browse files Browse the repository at this point in the history
  • Loading branch information
wmcbrine committed Feb 12, 2012
1 parent 228fa62 commit babbc80
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
1 change: 1 addition & 0 deletions httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
class TivoHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
def __init__(self, server_address, RequestHandlerClass):
self.containers = {}
self.stop = False
self.restart = False
self.logger = logging.getLogger('pyTivo')
BaseHTTPServer.HTTPServer.__init__(self, server_address,
Expand Down
19 changes: 11 additions & 8 deletions plugins/settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,28 @@ class Settings(Plugin):
CONTENT_TYPE = 'text/html'

def Quit(self, handler, query):
if (hasattr(handler.server, 'shutdown') and
not handler.server.in_service):
if hasattr(handler.server, 'shutdown'):
handler.send_response(200)
handler.send_header('Content-Type', 'text/plain')
handler.send_header('Content-Length', len(GOODBYE_MSG))
handler.send_header('Connection', 'close')
handler.end_headers()
handler.wfile.write(GOODBYE_MSG)
handler.server.shutdown()
if handler.server.in_service:
handler.server.stop = True
else:
handler.server.shutdown()
else:
handler.send_error(501)

def Restart(self, handler, query):
if (hasattr(handler.server, 'shutdown') and
not handler.server.in_service):
if hasattr(handler.server, 'shutdown'):
handler.redir(RESTART_MSG, 10)
handler.server.restart = True
handler.server.shutdown()
if handler.server.in_service:
handler.server.stop = True
else:
handler.server.shutdown()
else:
handler.send_error(501)

Expand Down Expand Up @@ -100,8 +104,7 @@ def Settings(self, handler, query):
and not section.startswith('_tivo_HD')]
t.tivos_known = buildhelp.getknown('tivos')
t.help_list = buildhelp.gethelp()
t.has_shutdown = (hasattr(handler.server, 'shutdown') and
not handler.server.in_service)
t.has_shutdown = hasattr(handler.server, 'shutdown')
handler.send_response(200)
handler.send_header('Content-Type', 'text/html; charset=utf-8')
handler.send_header('Expires', '0')
Expand Down
25 changes: 16 additions & 9 deletions pyTivoService.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import select
import sys
import time
import win32event
import win32service
import win32serviceutil
Expand All @@ -15,13 +16,7 @@ def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.stop_event = win32event.CreateEvent(None, 0, 0, None)

def SvcDoRun(self):
p = os.path.dirname(__file__)

f = open(os.path.join(p, 'log.txt'), 'w')
sys.stdout = f
sys.stderr = f

def mainloop(self):
httpd = pyTivo.setup(True)

while True:
Expand All @@ -30,10 +25,22 @@ def SvcDoRun(self):
for sck in rx:
sck.handle_request()
rc = win32event.WaitForSingleObject(self.stop_event, 5)
if rc == win32event.WAIT_OBJECT_0:
httpd.beacon.stop()
if rc == win32event.WAIT_OBJECT_0 or httpd.stop:
break

httpd.beacon.stop()
return httpd.restart

def SvcDoRun(self):
p = os.path.dirname(__file__)

f = open(os.path.join(p, 'log.txt'), 'w')
sys.stdout = f
sys.stderr = f

while self.mainloop():
time.sleep(5)

def SvcStop(self):
win32event.SetEvent(self.stop_event)

Expand Down

0 comments on commit babbc80

Please sign in to comment.