summaryrefslogtreecommitdiff
path: root/server/controller.py
blob: 273f33fab04b4b33dcdd8745ae8d0eab46a78fa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'''
    A intermediary class for general purpose
    flow control operations throughout the
    program.

    Verified: 2021 February 7
    * Follows PEP8
    * Tested Platforms
        * Windows 10
    * Third Party Modules
        * eel
'''

from server.state import ServerStatic, Dynamic
from server.console import Console

import socket
import time

if not ServerStatic.TERMINAL:
    import eel


class Controller:

    @staticmethod
    def message(ret_msg, *args, **kwargs):
        Dynamic.MESSAGES.append((ret_msg, args, kwargs))

        # NOTE : Calling this method with ret_msg
        # enabled at the same time twice will cause
        # erroneous output & should only be called
        # from the GUI, as it requires return data
        if ret_msg:
            try:
                while Dynamic.RESULT is None:
                    time.sleep(.1)
                else:
                    return Dynamic.RESULT
            finally:
                Dynamic.RESULT = None

    @staticmethod
    def exit_program():
        if Dynamic.CLIENTS:
            for unique_id in Dynamic.CLIENTS.copy():
                Controller.delete_client(unique_id, False)

        raise SystemExit

    @staticmethod
    def add_session(unique_id, allow_raise=True):
        try:
            assert unique_id in Dynamic.CLIENTS, 'Client Not Found'
            assert unique_id not in Dynamic.SESSION, \
                'Client Already In Session'

            Dynamic.SESSION.add(unique_id)

            if not ServerStatic.TERMINAL:
                if eel._websockets != []:
                    eel.sessionAddEel(unique_id)
        except Exception:
            if allow_raise:
                raise

    @staticmethod
    def remove_session(unique_id, allow_raise=True):
        try:
            Dynamic.SESSION.remove(unique_id)

            if not ServerStatic.TERMINAL:
                if eel._websockets != []:
                    eel.sessionRemoveEel(unique_id)
        except Exception:
            if allow_raise:
                raise

    @staticmethod
    def delete_client(unique_id, allow_raise=True):
        try:
            client = Dynamic.CLIENTS[unique_id]
            del Dynamic.CLIENTS[unique_id]

            try:
                client.socket.shutdown(socket.SHUT_RDWR)
            except Exception:
                pass

            client.socket.close()
            Controller.remove_session(unique_id, False)

            if not ServerStatic.TERMINAL:
                if eel._websockets != []:
                    eel.clientRemoveEel(unique_id)

            Console.log('Disconnected Client [{} {} {}]\n'.format(
                client.connect_ip.pure, ServerStatic.SEPERATOR,
                unique_id), log_type=ServerStatic.ACTION)
        except Exception:
            if allow_raise:
                raise