Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGonzalo Odiard <godiard@gmail.com>2013-03-13 18:50:06 (GMT)
committer Gonzalo Odiard <godiard@gmail.com>2013-03-13 18:50:06 (GMT)
commitb46fce545ea0c631a0c85dd41616280e91b76364 (patch)
tree689ec612c76bc12884508478dd43772446228af1
parenta26a959f1bdb14986ddeb8d8a6f5ac6235cd6175 (diff)
Use a simple server to create content and transfer files
Afte trying run the server in a thread and having problems due to webkit bloking the ui, I implemented the server in another package and start it in another process with subproces. This solution solves the problem, and provide separation, but can complicate other parts in the code, like collaboration. Signed-off-by: Gonzalo Odiard <gonzalo@laptop.org>
-rw-r--r--activity.py62
-rw-r--r--server.py89
2 files changed, 86 insertions, 65 deletions
diff --git a/activity.py b/activity.py
index 0862ba1..0d2e6bd 100644
--- a/activity.py
+++ b/activity.py
@@ -14,10 +14,8 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-import logging
-from threading import Thread
+import subprocess
-from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import WebKit
@@ -25,45 +23,6 @@ from sugar3.activity import activity
from sugar3.activity.widgets import ActivityToolbarButton
from sugar3.activity.widgets import StopButton
from sugar3.graphics.toolbarbox import ToolbarBox
-from sugar3 import network
-
-
-class JournalHTTPRequestHandler(network.ChunkedGlibHTTPRequestHandler):
- """HTTP Request Handler to send data to the webview.
-
- RequestHandler class that integrates with Glib mainloop. It writes
- the specified file to the client in chunks, returning control to the
- mainloop between chunks.
-
- """
- def do_HEAD(self):
- self.send_response(200)
- self.send_header("Content-type", "text/html")
- self.end_headers()
-
- def do_GET(self):
- """Respond to a GET request."""
- logging.error('inside do_get dir(self) %s', dir(self))
- self.send_response(200)
- self.send_header("Content-type", "text/html")
- self.end_headers()
- self.wfile.write("<html><head><title>Title goes here.</title></head>")
- self.wfile.write("<body><p>This is a test.</p>")
- # If someone went to "http://something.somewhere.net/foo/bar/",
- # then s.path equals "/foo/bar/".
-
- #self.wfile.write("<p>You accessed path: %s</p>" % self.path)
- self.wfile.write("</body></html>")
-
-
-class JournalHTTPServer(network.GlibTCPServer):
- """HTTP Server for transferring document while collaborating."""
-
- def __init__(self, server_address):
- """Set up the GlibTCPServer with the JournalHTTPRequestHandler.
- """
- network.GlibTCPServer.__init__(self, server_address,
- JournalHTTPRequestHandler)
class JournalShare(activity.Activity):
@@ -72,6 +31,10 @@ class JournalShare(activity.Activity):
activity.Activity.__init__(self, handle)
+ activity_path = activity.get_bundle_path()
+ self.server_proc = subprocess.Popen(['/bin/python', 'server.py',
+ activity_path])
+
toolbar_box = ToolbarBox()
activity_button = ActivityToolbarButton(self)
@@ -91,22 +54,11 @@ class JournalShare(activity.Activity):
self.set_toolbar_box(toolbar_box)
toolbar_box.show()
- activity_path = activity.get_bundle_path()
self.view = WebKit.WebView()
- #self.view.load_uri('file://%s/web/index.html' % activity_path)
- self.view.load_uri('http://localhost:2500')
+ self.view.load_uri('http://localhost:2500/index.html')
self.view.show()
self.set_canvas(self.view)
- # TODO: set the port in a more inteligent way
- self.port = 2500
- self._server = JournalHTTPServer(("", self.port))
- server = Thread(target=self._server.serve_forever)
- server.setDaemon(True)
- logging.debug("Before start server")
- server.start()
- logging.debug("After start server")
-
def read_file(self, file_path):
pass
@@ -114,5 +66,5 @@ class JournalShare(activity.Activity):
pass
def can_close(self):
- self._server.shutdown()
+ self.server_proc.kill()
return True
diff --git a/server.py b/server.py
index aee37b2..6434a9d 100644
--- a/server.py
+++ b/server.py
@@ -14,25 +14,94 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-import SimpleHTTPServer
-import SocketServer
import os
+import sys
+import logging
-os.chdir(os.path.join(os.path.dirname(__file__), 'web'))
+from gi.repository import Gio
+from sugar3 import network
-def setup_server():
- PORT = 2810
- Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
- httpd = SocketServer.TCPServer(('', PORT), Handler)
+class JournalHTTPRequestHandler(network.ChunkedGlibHTTPRequestHandler):
+ """HTTP Request Handler to send data to the webview.
- print 'serving at port', PORT
+ RequestHandler class that integrates with Glib mainloop. It writes
+ the specified file to the client in chunks, returning control to the
+ mainloop between chunks.
- return httpd
+ """
+ def do_HEAD(self):
+ self.send_response(200)
+ self.send_header("Content-type", "text/html")
+ self.end_headers()
+
+ def do_GET(self):
+ """Respond to a GET request."""
+ #logging.error('inside do_get dir(self) %s', dir(self))
+ self.send_response(200)
+ self.send_header("Content-type", "text/html")
+ self.end_headers()
+ # If someone went to "http://something.somewhere.net/foo/bar/",
+ # then s.path equals "/foo/bar/".
+ # verify if the requested path is in the web_path directory
+
+ file_used = False
+ if self.path:
+ file_path = self.server.web_path + self.path
+ logging.error('Requested file %s', file_path)
+
+ if os.path.isfile(file_path):
+ logging.error('Opening requested file %s', file_path)
+ f = Gio.File.new_for_path(file_path)
+ _error, content, _time = f.load_contents(None)
+
+ logging.error('Closing requested file %s', file_path)
+ self.wfile.write(content)
+ file_used = True
+
+ if not file_used:
+ self.wfile.write("<html><head><title>Title ...</title></head>")
+ self.wfile.write("<body><p>This is a test.</p>")
+ self.wfile.write("<p>You accessed path: %s</p>" % self.path)
+ self.wfile.write("</body></html>")
+
+
+class JournalHTTPServer(network.GlibTCPServer):
+ """HTTP Server for transferring document while collaborating."""
+
+ def __init__(self, server_address, activity_path):
+ """Set up the GlibTCPServer with the JournalHTTPRequestHandler.
+ """
+ self.activity_path = activity_path
+ self.web_path = self.activity_path + '/web'
+ network.GlibTCPServer.__init__(self, server_address,
+ JournalHTTPRequestHandler)
+
+
+class JournalManager():
+
+ def __init__(self):
+ pass
+
+ def get_json(self, query):
+ """
+ Receive a dictionary with the query parameters and creates
+ a json with the results
+ """
+ pass
+
+
+def setup_server(activity_path):
+ # TODO: set the port in a more inteligent way
+ port = 2500
+ server = JournalHTTPServer(("", port), activity_path)
+ return server
if __name__ == "__main__":
- server = setup_server()
+ activity_path = sys.argv[1]
+ server = setup_server(activity_path)
try:
+ logging.debug("Before start server")
server.serve_forever()
except KeyboardInterrupt:
print "Shutting down server"