Skip to content

Commit

Permalink
web: this should work better, but cherrypy doesn't want to run in it'…
Browse files Browse the repository at this point in the history
…s own thread
  • Loading branch information
borisfaure committed Feb 22, 2010
1 parent 0e1d859 commit 73110ad
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 53 deletions.
96 changes: 47 additions & 49 deletions amsn2/ui/front_ends/web/bend.py
Expand Up @@ -2,6 +2,8 @@
import thread
import os
import logging
import threading
import Queue

class Backend(object):
"""
Expand All @@ -10,39 +12,49 @@ class Backend(object):
"""
def __init__(self):
self.listeners = {}
self.out_stack = []
self._outq = Queue.Queue(0)
self._inq = Queue.Queue(0)

class Root(object):
def __init__(self, backend):
self._backend = backend
@cherrypy.expose
def index(self):
raise cherrypy.HTTPRedirect("static/amsn2.html")

@cherrypy.expose
def signin(self, u=None, p=None):
self._backend.emit_event("signin", u, p)
print self._backend.listeners
def worker(inq, outq):
class Root(object):
def __init__(self, inq, outq):
self._inq = inq
self._outq = outq

@cherrypy.expose
def out(self):
l = self._backend.out_stack
self._backend.out_stack = []
return l
@cherrypy.expose
def index(self):
raise cherrypy.HTTPRedirect("static/amsn2.html")

current_dir = os.path.dirname(os.path.abspath(__file__))
cherrypy.config.update({'log.error_file': 'amsn2-web-error.log',
'log.access_file': 'amsn2-web-access.log',
'log.screen': False})
@cherrypy.expose
def signin(self, u=None, p=None):
self._inq.put_nowait(["signin", u, p])

conf = {'/static': {'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.join(current_dir, 'static')},
}
@cherrypy.expose
def out(self):
l = []
while True:
try:
l.append(self._outq.get_nowait())
except Queue.Empty:
break;
logging.error("OOOOOO")
return l

self._web = thread.start_new_thread(
cherrypy.quickstart(Root(self), '/', config=conf))
current_dir = os.path.dirname(os.path.abspath(__file__))
cherrypy.config.update({'log.error_file': 'amsn2-web-error.log',
'log.access_file': 'amsn2-web-access.log',
'log.screen': False})

def set_listener(self, event, listener):
conf = {'/static': {'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.join(current_dir, 'static')},
}
cherrypy.quickstart(Root(inq, outq), '/', config=conf)
t = threading.Thread(target=worker, args=[self._inq, self._outq])
t.daemon = True
t.start()

def add_listener(self, event, listener):
if not self.listeners.has_key(event):
self.listeners[event] = []
self.listeners[event].append(listener)
Expand All @@ -51,28 +63,14 @@ def del_listener(self, event, listener):
#TODO
pass

def checkEvent(self):
# This function is called to check for events in the file
try:
# one event per line, divided by columns divided by tab
# the first column is the event, the next columns are the arguments
eventDesc = self._in.readline()
while len(eventDesc) > 0:
try:
eventDesc = eventDesc.strip().split("\t")
eventName = eventDesc.pop(0)
realValues = []
for value in eventDesc:
realValues.append(str(value).decode('string_escape'))
if eventName is not "":
self.event(eventName, realValues)
except:
# event problem.. probably a badly encoded string
break
eventDesc = self._in.readline()
except:
# problem with lines (maybe empty file)
pass
def check_event(self):
# This function is called to check for events
while True:
try:
e = self._inq.get_nowait()
self.emit_event(e[0], e[1:])
except Queue.Empty:
break;
# Return true to continue checking events
return True

Expand All @@ -91,5 +89,5 @@ def send(self, event, *args, **kwargs):
for value in args:
call += "'" + str(value).encode('string_escape') + "',"
call = call.rstrip(",") + "]);"
self.out_stack.append(call)
self._outq.put_nowait(call)
print call
5 changes: 3 additions & 2 deletions amsn2/ui/front_ends/web/login.py
Expand Up @@ -7,7 +7,7 @@ def __del__(self):
self._main.del_listener("signin", self.signin)

def show(self):
self._main.set_listener("signin", self.signin)
self._main.add_listener("signin", self.signin)

def hide(self):
self._main.send("hideLogin");
Expand All @@ -32,7 +32,8 @@ def signin(self, u, p, *args, **kwargs):
accv.save_password = False
accv.autologin = False

print "signing in"
logging.error("signing in")
self._amsn_core.quit()
self._amsn_core.signin_to_account(self, accv)

def on_connecting(self, progress, msg):
Expand Down
2 changes: 1 addition & 1 deletion amsn2/ui/front_ends/web/main.py
Expand Up @@ -7,7 +7,7 @@ class aMSNMainWindow(base.aMSNMainWindow, Backend):
def __init__(self, amsn_core):
Backend.__init__(self)
self._amsn_core = amsn_core
self._amsn_core.timer_add(1, self.checkEvent)
self._amsn_core.timer_add(750, self.check_event)

def show(self):
self.send("showMainWindow", [])
Expand Down
2 changes: 1 addition & 1 deletion amsn2/ui/front_ends/web/static/backend.js
Expand Up @@ -466,5 +466,5 @@ $(document).ready(function()
});
showMainWindow();
showLogin();
Listening();
//Listening();
});

0 comments on commit 73110ad

Please sign in to comment.