Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

web: web server on its way :)

  • Loading branch information...
commit 52bd12f4d8d96bc9b7cdf8ddc76833f29d22df5a 1 parent 1ea0751
Boris Faure billiob authored
2  amsn2/ui/front_ends/web/__init__.py
View
@@ -8,7 +8,6 @@
# so the guimanager can access its classes
def load():
try:
- import cherrypy
import amsn2.ui.front_ends.web._web
except ImportError:
etype, value, trace = sys.exc_info()
@@ -20,7 +19,6 @@ def load():
# dependency then register it to the guimanager
try:
import imp
- imp.find_module("cherrypy")
aMSNUserInterfaceManager.register_frontend("web", sys.modules[__name__])
except ImportError:
142 amsn2/ui/front_ends/web/bend.py
View
@@ -1,16 +1,138 @@
-import cherrypy
-import thread
import os
+import socket
+import errno
import logging
-import threading
-import Queue
+import urlparse
+import gobject
+
+READ_CHUNK_SIZE = 4096
+BASEPATH="amsn2/ui/front_ends/web/static"
+
+class TinyHTTPServer(object):
+ def __init__(self, backend, socket, peer):
+ self._backend = backend
+ self._socket = socket
+ self._peer = peer
+
+ self._rbuf = ""
+ self._wbuf = ""
+ self._read = 0
+ self._written = 0
+
+ self._read_delimiter = "\r\n\r\n"
+ self._rcb = self.on_headers
+
+ self._is_alive = True
+
+ print socket
+ print self
+ gobject.io_add_watch(socket, gobject.IO_IN, self.on_read)
+
+ def close(self):
+ if self._is_alive:
+ self._is_alive = False
+ self._socket.close()
+ print "closing %s" % (self)
+ self._socket = None
+
+ def write(self, data):
+ if self._is_alive:
+ self._wbuf += data
+ gobject.io_add_watch(self._socket, gobject.IO_OUT, self.on_write)
+ self.on_write(self._socket, gobject.IO_OUT)
+
+ def on_headers(self, headers):
+ eol = headers.find("\r\n")
+ start_line = headers[:eol]
+ method, uri, version = start_line.split(" ")
+ print "method=%s, uri=%s, version=%s" % (method, uri, version)
+ if not version.startswith("HTTP/"):
+ self.close()
+ return
+ print uri
+ if uri == "/" and method == "GET":
+ self.send_file("amsn2.html")
+ return
+ scheme, netloc, path, query, fragment = urlparse.urlsplit(uri)
+ print "scheme=%s, netloc=%s, path=%s, query=%s, fragment=%s" % (scheme, netloc, path, query, fragment)
+
+ def on_read(self, s, c):
+ try:
+ chunk = self._socket.recv(READ_CHUNK_SIZE)
+ except socket.error, e:
+ if e[0] in (errno.EWOULDBLOCK, errno.EAGAIN):
+ return self._is_alive
+ else:
+ logging.warning("Read error on %d: %s",
+ self._socket.fileno(), e)
+ self.close()
+ return self._is_alive
+ if not chunk:
+ self.close()
+ return self._is_alive
+
+ self._rbuf += chunk
+ self._read += len(chunk)
+ if self._read >= 16777216:
+ logging.error("Reached maximum read buffer size")
+ self.close()
+ return self._is_alive
+
+ pos = self._rbuf.find(self._read_delimiter)
+ if pos != -1:
+ pos += len(self._read_delimiter)
+ r = self._rbuf[:pos]
+ self._rbuf = self._rbuf[pos:]
+ self._rcb(r)
+ return self._is_alive
+
+ def on_write(self, s, c):
+ print self
+ while self._wbuf:
+ try:
+ b = self._socket.send(self._wbuf)
+ self._wbuf = self._wbuf[b:]
+ except socket.error, e:
+ if e[0] in (errno.EWOULDBLOCK, errno.EAGAIN):
+ break
+ else:
+ logging.warning("Write error on %d: %s",
+ self._socket.fileno(), e)
+ self.close()
+ return self._is_alive
+ return self._is_alive
+
+ def send_file(self, path):
+ f = open(os.path.join(BASEPATH, path), "r")
+ r = f.read()
+ f.close()
+ self.write("HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s"
+ % (len(r), r))
+ self.close()
+
+
class Backend(object):
"""
This is the main comunication module,
all comunication to the JS frontend will be issued from here
"""
- def __init__(self):
+ def __init__(self, core):
+ self._core = core
+ self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ self._socket.setblocking(0)
+ self._socket.bind(("127.0.0.1", 8080))
+ self._socket.listen(1)
+
+ gobject.io_add_watch(self._socket, gobject.IO_IN, self.on_accept)
+
+ def on_accept(self, s, c):
+ w = s.accept()
+ TinyHTTPServer(self, *w)
+ return True
+
+
+ """
self.listeners = {}
self._outq = Queue.Queue(0)
self._inq = Queue.Queue(0)
@@ -53,17 +175,22 @@ def out(self):
t = threading.Thread(target=worker, args=[self._inq, self._outq])
t.daemon = True
t.start()
+ """
def add_listener(self, event, listener):
+ return
+ """
if not self.listeners.has_key(event):
self.listeners[event] = []
self.listeners[event].append(listener)
+ """
def del_listener(self, event, listener):
#TODO
pass
def check_event(self):
+ """
# This function is called to check for events
while True:
try:
@@ -72,15 +199,18 @@ def check_event(self):
except Queue.Empty:
break;
# Return true to continue checking events
+ """
return True
def emit_event(self, event, *args, **kwargs):
+ """
if event in self.listeners.keys():
for func in self.listeners[event]:
try:
func(*args, **kwargs)
except:
pass
+ """
def send(self, event, *args, **kwargs):
# The backend sent a message to the JS client
@@ -89,5 +219,5 @@ def send(self, event, *args, **kwargs):
for value in args:
call += "'" + str(value).encode('string_escape') + "',"
call = call.rstrip(",") + "]);"
- self._outq.put_nowait(call)
+ #self._outq.put_nowait(call)
print call
2  amsn2/ui/front_ends/web/main.py
View
@@ -5,7 +5,7 @@
class aMSNMainWindow(base.aMSNMainWindow, Backend):
def __init__(self, amsn_core):
- Backend.__init__(self)
+ Backend.__init__(self, amsn_core)
self._amsn_core = amsn_core
self._amsn_core.timer_add(750, self.check_event)
1  amsn2/ui/front_ends/web/main_loop.py
View
@@ -4,6 +4,7 @@
class aMSNMainLoop(base.aMSNMainLoop):
def __init__(self, amsn_core):
self._amsn_core = amsn_core
+ self._mainloop = None
def run(self):
self._mainloop = gobject.MainLoop(is_running=True)
Please sign in to comment.
Something went wrong with that request. Please try again.