main.py 6.32 KB
Newer Older
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse
import sys
import signal

from .tcpliveplot import TcpLivePlot
from .info_registry import InfoRegistry


# constants
TCP_PLOT_VERSION = "0.2.0"
TCP_LOG_FORMAT_VERSION_MIN = "2"

DEFAULT_INPUT_BACKEND = "socket"

# default values
DEFAULT_SOCKETSERVER_PORT = 11337
DEFAULT_SOCKETSERVER_LOCATION = 'localhost:' + str(DEFAULT_SOCKETSERVER_PORT)
DEFAULT_LINES_TO_SHOW = ['cwnd']
DEFAULT_FILTER_PORT = 5001

# move
VALUES_TO_PLOT = ['cwnd', 'sst', 'rtt', 'bw'] # (only values for Y-axis)

# main prog
def main():
    inputBackend = None
    guiBackend = None
    infoRegistry = InfoRegistry()
    options = parse_options()
    startupSelfCheck(options)

    if(options.showVersion):
        print("TCPlivePLOT: " + TCP_PLOT_VERSION)
        sys.exit(0)

    options.guiBackend = "live"

    # init input backend
    if(options.inputBackend == "socket"):
        from .backends.input.socket import SocketInput
        inputBackend = SocketInput(options, infoRegistry)
    elif(options.inputBackend == "file"):
        from .backends.input.file import FileInput
        inputBackend = FileInput(options, infoRegistry)
    # elif(options.inputBackend == "stdin"):
    #     from .backends.input.stdin import StdinInput
    #     inputBackend = StdinInput(options, info)

    if(inputBackend is None):
        print("No valid input backend selected. Exiting...")
        sys.exit(1)

    # init guiBackend
    if(options.guiBackend == "live"):
        from .backends.gui.live import LiveGui
        guiBackend = LiveGui(options, infoRegistry)
    else:
        pass

    if(guiBackend is None):
        print("No valid GUI backend selected. Exiting...")
        sys.exit(1)

    if(inputBackend is not None):
        inputBackend.startupCheck()
        inputBackend.startUp()

    if(guiBackend is not None):
        guiBackend.startupCheck()
        guiBackend.startUp()

    try:
        tcpLivePlot = TcpLivePlot(inputBackend, guiBackend, options, infoRegistry)
        signal.signal(signal.SIGINT, tcpLivePlot.handleSignals)
        signal.signal(signal.SIGTERM, tcpLivePlot.handleSignals)
        tcpLivePlot.run()
        # ^-- starts the main programme
    except Exception as e:
        # utility.eprint(str(e))
        print(str(e))
        raise e
    finally:
        if(not options.quiet):
            print("Goodbye cruel world!")
        sys.exit(0)


def parse_options():
    parser = argparse.ArgumentParser()

    parser.add_argument(
            "-i",
            "--input-backend",
            help="Available input backends: \"socket\" and \"file\" (default: " + DEFAULT_INPUT_BACKEND + ")",
            dest="inputBackend",
            default=DEFAULT_INPUT_BACKEND)

    parser.add_argument(
            "-b",
            "--buffer", help="Length of preload buffer (in seconds, default: 1, 0 to deactivate preload buffer)",
            type=float,
            dest="preloadBuffer",
            default=1)

    parser.add_argument(
            "-ib",
            "--interimbuffer", help="Activate interim buffering.",
            action="store_true",
            dest="interimBuffering",
            default=False)

    parser.add_argument(
            "-ps",
            "--playback-speed", help="Playback speed (factor, default: 1)",
            type=float,
            dest="playbackSpeed",
            default=1)
    parser.add_argument(
            "-aps",
            "--adaptive-playback-speed", help="Enable adaptive playback speed (default: false)",
            action="store_true",
            dest="adaptivePlaybackSpeed",
            default=False)

    parser.add_argument(
            "--buffer-size", help="Number of elements to buffer from socket per filter (5000)",
            dest="bufferLength",
            default=5000)


    parser.add_argument(
            "-z",
            "--blit", help="Deactivate blitting",
            action="store_false",
            dest="blitting",
            default=True)

    logTypeGroup = parser.add_mutually_exclusive_group()

    logTypeGroup.add_argument(
            "-s",
            "--server",
            help="IP and Port of socket-logging server (" + DEFAULT_SOCKETSERVER_LOCATION + ")",
            dest="logServer",
            default=DEFAULT_SOCKETSERVER_LOCATION)
    logTypeGroup.add_argument(
            "-f",
            "--filepath",
            help="Path where the log file is stored",
            type=str,
            dest="logFilePath")

    parser.add_argument(
            "-df",
            help="Number of FPS to draw",
            dest="drawFps",
            type=int,
            default=60)
    parser.add_argument(
            "-di",
            help="Draw intervall",
            dest="drawIntervall",
            type=int,
            default=30)

    parser.add_argument(
            "-x",
            help="Seconds to plot (20)",
            dest="xDelta",
            type=int,
            default=20)
    parser.add_argument(
            "-r",
            "--resolution",
            help="Plot resolution (in seconds, default: 0.001)",
            dest="plotResolution",
            type=float,
            default=0.01)

    # Filter
    parser.add_argument(
            "-p",
            "--port",
            help="Filter by port. Multiple occurrences possible (" + str(DEFAULT_FILTER_PORT) + ")",
            dest="filterPorts",
            action='append',
            type=int,
            default=[])
    parser.add_argument(
            "-l",
            "--line",
            help="Plot lines to show initially. Multiple occurrences possible. Possible values: " + ', '.join(VALUES_TO_PLOT),
            dest="linesToShow",
            action='append',
            default=[])


    parser.add_argument(
            "-d",
            "--debug", help="Debug mode - ignores \"-q\"",
            action="store_true",
            dest="debug",
            default=False)

    parser.add_argument(
            "-q",
            "--quiet", help="Whether to ouput anything at all (false)",
            action="store_true",
            dest="quiet",
            default=False)

    parser.add_argument(
            "--version",
            help="Print version information",
            action="store_true",
            dest="showVersion",
            default=False)

    options = parser.parse_args()

    return options

def startupSelfCheck(options):
    pass

if __name__ == "__main__":
    main()