diff --git a/agkyra/agkyra/gui.py b/agkyra/agkyra/gui.py index ade74b3653df3374c437463a0caddc906c88ee0c..fc1ead525c942c54cffb6bffb8c96ce707d8c733 100644 --- a/agkyra/agkyra/gui.py +++ b/agkyra/agkyra/gui.py @@ -13,18 +13,11 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -from wsgiref.simple_server import make_server -# from ws4py.websocket import EchoWebSocket -from agkyra.protocol import WebSocketProtocol -from ws4py.server.wsgirefserver import WSGIServer, WebSocketWSGIRequestHandler -from ws4py.server.wsgiutils import WebSocketWSGIApplication from ws4py.client import WebSocketBaseClient +from agkyra.protocol import HelperServer from tempfile import NamedTemporaryFile import subprocess import json -from os.path import abspath -from threading import Thread -from hashlib import sha1 import os import logging @@ -68,40 +61,10 @@ class GUI(WebSocketBaseClient): self.close() -class HelperServer(object): - """Agkyra Helper Server sets a WebSocket server with the Helper protocol - It also provided methods for running and killing the Helper server - :param gui_id: Only the GUI with this ID is allowed to chat with the Helper - """ - - def __init__(self, port=0): - """Setup the helper server""" - self.gui_id = sha1(os.urandom(128)).hexdigest() - WebSocketProtocol.gui_id = self.gui_id - server = make_server( - '', port, - server_class=WSGIServer, - handler_class=WebSocketWSGIRequestHandler, - app=WebSocketWSGIApplication(handler_cls=WebSocketProtocol)) - server.initialize_websockets_manager() - self.server, self.port = server, server.server_port - - def start(self): - """Start the helper server in a thread""" - Thread(target=self.server.serve_forever).start() - - def shutdown(self): - """Shutdown the server (needs another thread) and join threads""" - t = Thread(target=self.server.shutdown) - t.start() - t.join() - - def run(): """Prepare helper and GUI and run them in the proper order""" server = HelperServer() - addr = 'ws://localhost:%s' % server.port - gui = GUI(addr, server.gui_id) + gui = GUI(server.addr, server.gui_id) LOG.info('Start helper server') server.start() @@ -117,4 +80,4 @@ def run(): if __name__ == '__main__': logging.basicConfig(filename='agkyra.log', level=logging.DEBUG) - run(abspath('gui/app')) + run(os.path.abspath('gui/app')) diff --git a/agkyra/agkyra/protocol.py b/agkyra/agkyra/protocol.py index 7c579fb18ae5e7e4fa18343ebf3e6184780a4b1d..0f55e9b30991d2409c2f1e1d066800702c9b67c5 100644 --- a/agkyra/agkyra/protocol.py +++ b/agkyra/agkyra/protocol.py @@ -13,10 +13,15 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +from wsgiref.simple_server import make_server from ws4py.websocket import WebSocket +from ws4py.server.wsgiutils import WebSocketWSGIApplication +from ws4py.server.wsgirefserver import WSGIServer, WebSocketWSGIRequestHandler +from hashlib import sha1 +from threading import Thread +import os import json import logging -from os.path import abspath from agkyra.syncer import ( syncer, setup, pithos_client, localfs_client, messaging) from agkyra.config import AgkyraConfig, AGKYRA_DIR @@ -25,6 +30,37 @@ from agkyra.config import AgkyraConfig, AGKYRA_DIR LOG = logging.getLogger(__name__) +class HelperServer(object): + """Agkyra Helper Server sets a WebSocket server with the Helper protocol + It also provided methods for running and killing the Helper server + :param gui_id: Only the GUI with this ID is allowed to chat with the Helper + """ + + def __init__(self, port=0): + """Setup the helper server""" + self.gui_id = sha1(os.urandom(128)).hexdigest() + WebSocketProtocol.gui_id = self.gui_id + server = make_server( + '', port, + server_class=WSGIServer, + handler_class=WebSocketWSGIRequestHandler, + app=WebSocketWSGIApplication(handler_cls=WebSocketProtocol)) + server.initialize_websockets_manager() + self.addr = 'ws://%s:%s' % (server.server_name, server.server_port) + print self.addr + self.server = server + + def start(self): + """Start the helper server in a thread""" + Thread(target=self.server.serve_forever).start() + + def shutdown(self): + """Shutdown the server (needs another thread) and join threads""" + t = Thread(target=self.server.shutdown) + t.start() + t.join() + + class WebSocketProtocol(WebSocket): """Helper-side WebSocket protocol for communication with GUI: