-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc__listoffers.py
executable file
·111 lines (95 loc) · 3.94 KB
/
gc__listoffers.py
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
#!/usr/bin/env python3
import importlib.util
# yapapi_loader = importlib.util.find_spec("yapapi")
from appview import AppView
from controller.local_connection import LocalConnection
from controller.remote_connection import RemoteConnection
from multiprocessing import Process, Queue, freeze_support
# other imports depend on launch state, see below
import sys
import debug
if __name__ == "__main__":
"""launch application to run independently or on network, in which case
one instance shall be a client and the other server
when running independent or as a client, the controller connects the
gui to the model (MVC)
case: independent
AppView ----via queues directly on LocalConnection
----> Controller ----> LocalConnection
notes: OfferLookup, which implements the model, may better be
encapsulated in the LocalConnection object as in run_server
case: client
AppView ---via queues directly on RemoteConnection
---> Controller ---> RemoteConnection
case: server
model.run_server()
when running as a server, there is no gui so the server runs as a
Model-Controller
"""
controller_process = None
INVALID_CLA = False
if len(sys.argv) == 1: # no arguments
# <script executable>
# configure for controller to use local connection to model
from model.offer_lookup import OfferLookup
print(f"\033[1mrunning gc__listoffers locally\033[0m")
offerLookup = OfferLookup() # implements model
appView = AppView()
# setup controller to use LocalConnection having a both a full
# duplex message queue to appView plus callback to OfferLookup functor
localConnection = LocalConnection(appView.q_in, appView.q_out, offerLookup)
controller_process = Process(target=localConnection, daemon=False)
controller_process.start()
appView()
# parse arguments
# <remote server or client ip> <remote server port>
elif len(sys.argv) >= 2: # one or more cli arguments
# <script> <client|serve> [<host>] [<port>]
if len(sys.argv) >= 3:
ip = sys.argv[2]
if ip.isdigit():
# assume user accidently skipped hostname and assign
# as localhost
ip = "localhost"
else:
ip = "localhost"
if len(sys.argv) == 4:
port = int(sys.argv[3])
else:
port = 8000
if sys.argv[1] == "serve":
from model import run_server
# from model.offer_lookup import OfferLookup
from controller.remote_connection import RemoteConnection
# _log_msg(f"[application.py] launching server on {ip}:{port}")
print(f"\033[1mlaunching server on {ip}:{port}\033[0m")
# launch model as a server
run_server(ip, port)
elif sys.argv[1] == "client":
# connect to model remotely
from controller.local_connection import LocalConnection
print(f"\033[1mlaunching client to connect with " f"{ip}:{port}\033[0m")
# configure for controller to use remote connection
appView = AppView()
# setup controller to use remoteConnection having both a
# full duplex message queue to appView and addressing info
# to client
remoteConnection = RemoteConnection(appView.q_in, appView.q_out, ip, port)
controller_process = Process(target=remoteConnection, daemon=True)
# freeze_support()
controller_process.start()
appView()
else:
INVALID_CLA = True
else:
INVALID_CLA = True
if INVALID_CLA:
print(
"usage: {} <client|serve> [<host=localhost>] [<port=8000>]".format(
sys.argv[0]
)
)
try:
controller_process.terminate()
except:
pass