-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
119 lines (108 loc) · 4.32 KB
/
client.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
112
113
114
115
116
117
118
119
#!/usr/bin/python
"""
Created on Thu Dec 1 15:41:34 2016
@author: pavla kratochvilova
"""
# Imports----------------------------------------------------------------------
from argparse import ArgumentParser
import threading
import Queue
import logging
import pika
# Custom imports --------------------------------------------------------------
import common
from windows import thread_printing
from windows.server import ServerWindow
from windows.lobby import LobbyWindow
from windows.game import GameWindow
# Setup Python logging --------------------------------------------------------
FORMAT = '%(asctime)-15s %(levelname)s %(message)s'
logging.basicConfig(level=logging.INFO, format=FORMAT)
LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)
# Constants -------------------------------------------------------------------
___NAME = 'Battleship Game Client'
___VER = '0.1.0.0'
___DESC = 'Battleship Game Client'
___BUILT = '2016-11-10'
# Functions -------------------------------------------------------------------
def __info():
return '%s version %s (%s)' % (___NAME, ___VER, ___BUILT)
def window_control(events, server_window, lobby_window, game_window):
'''Get events from queue and show windows accordingly. In case of close
event, all windows are closed, and disconnect request is sent if necessary.
@param events: Queue of events
@param server_window: ServerWindow
@param lobby_window: LobbyWindow
@param game_window: GameWindow
'''
while True:
try:
event, old_thread, arguments = events.get(timeout=5)
LOG.debug('Got window event: %s', event)
if old_thread is not None:
old_thread.join()
# Showing windows
if event == 'server':
server_window.show()
elif event == 'lobby':
lobby_window.show(arguments)
elif event == 'game':
game_window.show(arguments)
else:
LOG.debug('Unknown event for window control: %s', event)
except Queue.Empty:
pass
# Main method -----------------------------------------------------------------
if __name__ == '__main__':
# Parsing arguments
parser = ArgumentParser()
parser.add_argument(
'-H', '--host',
help='Address of the RabbitMQ server, defaults to %s' %
common.DEFAULT_SERVER_INET_ADDR,
default=common.DEFAULT_SERVER_INET_ADDR
)
parser.add_argument(
'-p', '--port',
help='Port of the RabbitMQ server, defaults to %d' %
common.DEFAULT_SERVER_PORT,
default=common.DEFAULT_SERVER_PORT
)
args = parser.parse_args()
# Connection
connection = pika.BlockingConnection(pika.ConnectionParameters(
host=args.host, port=args.port))
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs', type='direct')
# Queues
client_queue = channel.queue_declare(exclusive=True).method.queue
server_advertisements = channel.queue_declare(exclusive=True).method.queue
game_advertisements = channel.queue_declare(exclusive=True).method.queue
events_queue = channel.queue_declare(exclusive=True).method.queue
# Queue for window control
events = Queue.Queue()
# Application windows
server_window = ServerWindow(channel, server_advertisements,
client_queue, events)
lobby_window = LobbyWindow(channel, game_advertisements,
client_queue, events, server_window)
game_window = GameWindow(channel, client_queue, events_queue, events,
server_window)
server_window.lobby_window = lobby_window
lobby_window.game_window = game_window
lobby_window.server_window = server_window
game_window.lobby_window = lobby_window
# Controling which windows are shown and which are hidden
t_control = threading.Thread(target=window_control,
args=(events, server_window,
lobby_window, game_window),
name='Window control')
t_control.setDaemon(True)
t_control.start()
#thread_printing()
try:
server_window.root.mainloop()
except KeyboardInterrupt as e:
LOG.debug('Crtrl+C issued ...')
LOG.info('Terminating client ...')