gui.py 3.13 KB
Newer Older
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Copyright (C) 2015 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

16
from ws4py.client import WebSocketBaseClient
17
18
from agkyra.protocol import SessionHelper
from agkyra.config import AGKYRA_DIR
19
20
import subprocess
import os
21
22
import stat
import json
23
24
import logging

25
CURPATH = os.path.dirname(os.path.abspath(__file__))
26
27
28
29
LOG = logging.getLogger(__name__)


class GUI(WebSocketBaseClient):
30
    """Launch the GUI when the SessionHelper server is ready"""
31

32
33
34
35
36
37
38
    def __init__(self, session, **kwargs):
        """Initialize the GUI Launcher
        :param session: session dict(ui_id=..., address=...) instance
        """
        super(GUI, self).__init__(session['address'])
        self.session_file = kwargs.get(
            'session_file', os.path.join(AGKYRA_DIR, 'session.info'))
39
        self.start = self.connect
40
41
42
        self.nw = kwargs.get(
            'nw', os.path.join(os.path.join(CURPATH, 'nwjs'), 'nw'))
        self.gui_code = kwargs.get('gui_code', os.path.join(CURPATH, 'gui.nw'))
43
44
45
        assert not self._gui_running(session), (
            'Failed to initialize GUI, because another GUI is running')
        self._dump_session_file(session)
46

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    def _gui_running(self, session):
        """Check if a session file with the same credentials already exists"""
        try:
            with open(self.session_file) as f:
                return session == json.load(f)
        except Exception:
            return False

    def _dump_session_file(self, session):
        """Create (overwrite) the session file for GUI use"""
        flags = os.O_CREAT | os.O_WRONLY
        mode = stat.S_IREAD | stat.S_IWRITE
        f = os.open(self.session_file, flags, mode)
        os.write(f, json.dumps(session))
        os.close(f)
62
63

    def handshake_ok(self):
64
65
66
67
        """If handshake OK is, SessionHelper UP goes, so GUI launched can be"""
        LOG.debug('Protocol server is UP, running GUI')
        subprocess.call([self.nw, self.gui_code, self.session_file])
        LOG.debug('GUI finished, close GUI wrapper connection')
68
69
70
        self.close()


71
def run():
72
73
74
    """Prepare SessionHelper and GUI and run them in the proper order"""
    helper = SessionHelper()
    gui = GUI(helper.session)
75

76
77
    LOG.info('Start SessionHelper session')
    helper.start()
78
79
80
81
82
83
84

    try:
        LOG.info('Start GUI')
        gui.start()
    except KeyboardInterrupt:
        LOG.info('Shutdown GUI')
        gui.close()
85
86
    LOG.info('Shutdown SessionHelper server')
    helper.shutdown()
87
88
89

if __name__ == '__main__':
    logging.basicConfig(filename='agkyra.log', level=logging.DEBUG)
90
    run(os.path.abspath('gui/app'))