source: ogAgent-Git/src/OGAgentUser.py @ 3910a84

python3
Last change on this file since 3910a84 was be263c6, checked in by Ramón M. Gómez <ramongomez@…>, 4 years ago

#992: Cherry-pick commit af35fd9.

ogAgent sends the session type when user logs in.

  • Property mode set to 100644
File size: 11.3 KB
Line 
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#
4# Copyright (c) 2014 Virtual Cable S.L.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without modification,
8# are permitted provided that the following conditions are met:
9#
10#    * Redistributions of source code must retain the above copyright notice,
11#      this list of conditions and the following disclaimer.
12#    * Redistributions in binary form must reproduce the above copyright notice,
13#      this list of conditions and the following disclaimer in the documentation
14#      and/or other materials provided with the distribution.
15#    * Neither the name of Virtual Cable S.L. nor the names of its contributors
16#      may be used to endorse or promote products derived from this software
17#      without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29"""
30@author: Adolfo Gómez, dkmaster at dkmon dot com
31"""
32import atexit
33import base64
34import json
35import sys
36import time
37from PyQt5 import QtCore, QtGui, QtWidgets
38
39from about_dialog_ui import Ui_OGAAboutDialog
40from message_dialog_ui import Ui_OGAMessageDialog
41from opengnsys import VERSION, ipc, operations, utils
42from opengnsys.config import readConfig
43from opengnsys.loader import loadModules
44from opengnsys.log import logger
45from opengnsys.scriptThread import ScriptExecutorThread
46from opengnsys.service import IPC_PORT
47
48trayIcon = None
49
50
51def sigAtExit():
52    if trayIcon:
53        trayIcon.quit()
54
55
56# About dialog
57class OGAAboutDialog(QtWidgets.QDialog):
58    def __init__(self, parent=None):
59        QtWidgets.QDialog.__init__(self, parent)
60        self.ui = Ui_OGAAboutDialog()
61        self.ui.setupUi(self)
62        self.ui.VersionLabel.setText("Version " + VERSION)
63
64    def closeDialog(self):
65        self.hide()
66
67
68class OGAMessageDialog(QtWidgets.QDialog):
69    def __init__(self, parent=None):
70        QtWidgets.QDialog.__init__(self, parent)
71        self.ui = Ui_OGAMessageDialog()
72        self.ui.setupUi(self)
73
74    def message(self, message):
75        self.ui.message.setText(message)
76        self.show()
77
78    def closeDialog(self):
79        self.hide()
80
81
82class MessagesProcessor(QtCore.QThread):
83    logoff = QtCore.pyqtSignal(name='logoff')
84    message = QtCore.pyqtSignal(tuple, name='message')
85    script = QtCore.pyqtSignal(str, name='script')
86    exit = QtCore.pyqtSignal(name='exit')
87
88    def __init__(self, port):
89        super(self.__class__, self).__init__()
90        # Retries connection for a while
91        for _ in range(10):
92            try:
93                self.ipc = ipc.ClientIPC(port)
94                self.ipc.start()
95                break
96            except Exception:
97                logger.debug('IPC Server is not reachable')
98                self.ipc = None
99                time.sleep(2)
100
101        self.running = False
102
103    def stop(self):
104        self.running = False
105        if self.ipc:
106            self.ipc.stop()
107
108    def isAlive(self):
109        return self.ipc is not None
110
111    def sendLogin(self, user_data):
112        if self.ipc:
113            self.ipc.sendLogin(user_data)
114
115    def sendLogout(self, username):
116        if self.ipc:
117            self.ipc.sendLogout(username)
118
119    def run(self):
120        if self.ipc is None:
121            return
122        self.running = True
123
124        # Wait a bit so we ensure IPC thread is running...
125        time.sleep(2)
126
127        while self.running and self.ipc.running:
128            try:
129                msg = self.ipc.getMessage()
130                if msg is None:
131                    break
132                msg_id, data = msg
133                logger.debug('Got Message on User Space: {}:{}'.format(msg_id, data))
134                if msg_id == ipc.MSG_MESSAGE:
135                    module, message, data = data.decode('utf-8').split('\0')
136                    self.message.emit((module, message, data))
137                elif msg_id == ipc.MSG_LOGOFF:
138                    self.logoff.emit()
139                elif msg_id == ipc.MSG_SCRIPT:
140                    self.script.emit(data.decode('utf-8'))
141            except Exception as e:
142                logger.error('Got error on IPC thread {}'.format(utils.exceptionToMessage(e)))
143
144        if self.ipc.running is False and self.running is True:
145            logger.warn('Lost connection with Service, closing program')
146
147        self.exit.emit()
148
149
150class OGASystemTray(QtWidgets.QSystemTrayIcon):
151    def __init__(self, app_, parent=None):
152        self.app = app_
153        self.config = readConfig(client=True)
154        self.modules = None
155        # Get opengnsys section as dict
156        cfg = dict(self.config.items('opengnsys'))
157        # Set up log level
158        logger.setLevel(cfg.get('log', 'INFO'))
159
160        self.ipcport = int(cfg.get('ipc_port', IPC_PORT))
161
162        icon = QtGui.QIcon(':/images/img/oga.png')
163
164        QtWidgets.QSystemTrayIcon.__init__(self, icon, parent)
165        self.menu = QtWidgets.QMenu(parent)
166        exit_action = self.menu.addAction("About")
167        exit_action.triggered.connect(self.about)
168        self.setContextMenu(self.menu)
169        self.ipc = MessagesProcessor(self.ipcport)
170
171        if self.ipc.isAlive() is False:
172            raise Exception('No connection to service, exiting.')
173
174        self.timer = QtCore.QTimer()
175        self.timer.timeout.connect(self.timerFnc)
176
177        self.stopped = False
178
179        self.ipc.message.connect(self.message)
180        self.ipc.exit.connect(self.quit)
181        self.ipc.script.connect(self.executeScript)
182        self.ipc.logoff.connect(self.logoff)
183
184        self.aboutDlg = OGAAboutDialog()
185        self.msgDlg = OGAMessageDialog()
186
187        self.timer.start(1000)  # Launch idle checking every 1 seconds
188
189        self.ipc.start()
190
191    def initialize(self):
192        # Load modules and activate them
193        # Also, sends "login" event to service
194        self.modules = loadModules(self, client=True)
195        logger.debug('Modules: {}'.format(list(v.name for v in self.modules)))
196
197        # Send init to all modules
198        valid_mods = []
199        for mod in self.modules:
200            try:
201                logger.debug('Activating module {}'.format(mod.name))
202                mod.activate()
203                valid_mods.append(mod)
204            except Exception as e:
205                logger.exception()
206                logger.error("Activation of {} failed: {}".format(mod.name, utils.exceptionToMessage(e)))
207        self.modules[:] = valid_mods  # copy instead of assignment
208        # If this is running, it's because he have logged in, inform service of this fact
209        self.ipc.sendLogin((operations.getCurrentUser(), operations.getSessionLanguage(),
210                            operations.get_session_type()))
211
212    def deinitialize(self):
213        for mod in reversed(self.modules):  # Deinitialize reversed of initialization
214            try:
215                logger.debug('Deactivating module {}'.format(mod.name))
216                mod.deactivate()
217            except Exception as e:
218                logger.exception()
219                logger.error("Deactivation of {} failed: {}".format(mod.name, utils.exceptionToMessage(e)))
220
221    def timerFnc(self):
222        pass
223
224    def message(self, msg):
225        """
226        Processes the message sent asynchronously, msg is an QString
227        """
228        try:
229            logger.debug('msg: {}, {}'.format(type(msg), msg))
230            module, message, data = msg
231        except Exception as e:
232            logger.error('Got exception {} processing message {}'.format(e, msg))
233            return
234
235        for v in self.modules:
236            if v.name == module:  # Case Sensitive!!!!
237                try:
238                    logger.debug('Notifying message {} to module {} with json data {}'.format(message, v.name, data))
239                    v.processMessage(message, json.loads(data))
240                    return
241                except Exception as e:
242                    logger.error('Got exception {} processing generic message on {}'.format(e, v.name))
243
244        logger.error('Module {} not found, messsage {} not sent'.format(module, message))
245
246    def executeScript(self, script):
247        logger.debug('Executing script')
248        script = base64.b64decode(script.encode('ascii'))
249        th = ScriptExecutorThread(script)
250        th.start()
251
252    def logoff(self):
253        logger.debug('Logoff invoked')
254        operations.logoff()  # Invoke log off
255
256    def about(self):
257        self.aboutDlg.exec_()
258
259    def cleanup(self):
260        logger.debug('Quit invoked')
261        if self.stopped is False:
262            self.stopped = True
263            try:
264                self.deinitialize()
265            except Exception:
266                logger.exception()
267                logger.error('Got exception deinitializing modules')
268
269            try:
270                # If we close Client, send Logoff to Broker
271                self.ipc.sendLogout(operations.getCurrentUser())
272                time.sleep(1)
273                self.timer.stop()
274                self.ipc.stop()
275            except Exception:
276                # May we have lost connection with server, simply log and exit in that case
277                logger.exception()
278                logger.exception("Got an exception, processing quit")
279
280            try:
281                # operations.logoff()  # Uncomment this after testing to logoff user
282                pass
283            except Exception:
284                pass
285
286    def quit(self):
287        # logger.debug("Exec quit {}".format(self.stopped))
288        if self.stopped is False:
289            self.cleanup()
290            self.app.quit()
291
292    def closeEvent(self, event):
293        logger.debug("Exec closeEvent")
294        event.accept()
295        self.quit()
296
297
298if __name__ == '__main__':
299    app = QtWidgets.QApplication(sys.argv)
300
301    if not QtWidgets.QSystemTrayIcon.isSystemTrayAvailable():
302        # QtGui.QMessageBox.critical(None, "Systray", "I couldn't detect any system tray on this system.")
303        sys.exit(1)
304
305    # This is important so our app won't close on messages windows (alerts, etc...)
306    QtWidgets.QApplication.setQuitOnLastWindowClosed(False)
307
308    try:
309        trayIcon = OGASystemTray(app)
310    except Exception as e:
311        logger.exception()
312        logger.error('OGA Service is not running, or it can\'t contact with OGA Server. User Tools stopped: {}'.format(
313            utils.exceptionToMessage(e)))
314        sys.exit(1)
315
316    try:
317        trayIcon.initialize()  # Initialize modules, etc..
318    except Exception as e:
319        logger.exception()
320        logger.error('Exception initializing OpenGnsys User Agent {}'.format(utils.exceptionToMessage(e)))
321        trayIcon.quit()
322        sys.exit(1)
323
324    app.aboutToQuit.connect(trayIcon.cleanup)
325    trayIcon.show()
326
327    # Catch kill and logout user :)
328    atexit.register(sigAtExit)
329
330    res = app.exec_()
331
332    logger.debug('Exiting')
333    trayIcon.quit()
334
335    sys.exit(res)
Note: See TracBrowser for help on using the repository browser.