-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathservermanager.py
529 lines (371 loc) · 17 KB
/
servermanager.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# -*- coding: utf-8 -*-
#------------------------------------------------------------------------------
# servermanager - Manages GUI functions related to servers
# Copyright (c) 2012 Gavin Langdon <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#------------------------------------------------------------------------------
from gi.repository import Gtk, GObject
from twisted.internet import threads
from twisted.internet import reactor
import twisted.internet.error
import time # If only we had more time
import cPickle
import serverlogger
import utils
import recentcommands
import pinggraph
import addserverdialog
import statusmanager
import SourceLib
# wait 2 seconds between queries (default max queries per second per ip is 3, so let's reduce
# the probability that we'll hit that)
QUERY_DELAY = 2000
class ServerTextLog(object):
def __init__(self, tagtable):
self.log_buffer = Gtk.TextBuffer(tag_table=tagtable)
self.pretty_buffer = Gtk.TextBuffer(tag_table=tagtable)
class ServerManager(object):
'''
The server manager does all the gui stuff for the servers, and manages
query threads and other deferred events, as well as the text buffers
'''
def __init__(self, builder, dic, gui):
def cb(ret_tuple, server):
success, errval = ret_tuple
self.add_server_item(server, success)
self.init_query_timer(server)
#--------------------------------------------------
self.bd = builder
try:
prefs = open("servers.pkl", "r")
self.servers = cPickle.load(prefs)
prefs.close()
except:
self.servers = {}
print("No pickle found. Will create on close")
self.recentcmdmgr = recentcommands.RecentCommandManager(self.bd.get_object("recent_commands"))
self.cur_server = None
self.textlogs = {}
self.loggers = {}
self.text_tags = gui.text_tags
for s in self.servers.values():
d = self.init_server(s)
d.addCallback(cb, s)
d = { 'on_rcon_password_icon_press' : self.on_rcon_password_icon_press,
'on_act_remove_server_activate' : self.on_act_remove_server_activate,
'on_save_rcon_toggle_toggled' : self.on_save_rcon_toggle_toggled,
'on_rcon_input_activate' : self.on_rcon_input_activate,
'on_rcon_input_icon_press' : self.on_rcon_input_icon_press,
'on_rcon_input_changed' : self.on_rcon_input_changed,
'on_rcon_password_changed' : self.on_rcon_password_changed,
'on_server_list_cursor_changed' : self.on_server_list_cursor_changed,
'on_show_rcon_toggle_toggled' : self.on_show_rcon_toggle_toggled,
'on_stick_to_bottom_toggled' : self.on_stick_to_bottom_toggled,
'on_expand_player_list_clicked' : self.on_expand_player_list_clicked,
'on_act_toggle_logging_toggled' : self.on_act_toggle_logging_toggled,
'on_rcon_password_activate' : self.on_rcon_password_activate,
}
self.addserverdialog = addserverdialog.AddServerDialog(self, builder, dic)
dic.update(d)
self.pinggraph = pinggraph.PingGraph(dic, self.bd.get_object("ping_graph"))
def add_server(self, server):
'''Returns a Defer if the server has been added, False otherwise'''
sid = self.get_server_sid(server)
if sid in self.servers:
return False
self.servers[sid] = server
return self.init_server(server)
def init_server(self, server):
self.textlogs[server] = ServerTextLog(self.text_tags)
return self.query_server(server)
def on_expand_player_list_clicked(self, expand_player_list):
pl = self.bd.get_object("players_window")
self.bd.get_object("player_expander_arrow").set(Gtk.ArrowType.LEFT if pl.get_visible() else Gtk.ArrowType.RIGHT, Gtk.ShadowType.ETCHED_IN)
pl.set_visible(not pl.get_visible())
def log_rcon(self, server, command, threaded=True):
def cb(ret_tuple):
success, response = ret_tuple
response = str(response)
if self.bd.get_object("show_rcons").get_active():
self.append_to_log(server, command, 'local')
self.append_to_log(server, response, 'remote')
rcon_password = self.bd.get_object("rcon_password")
if success:
rcon_password.set_property("secondary-icon-stock", Gtk.STOCK_OK)
else:
rcon_password.set_property("secondary-icon-stock", Gtk.STOCK_DIALOG_ERROR)
if not "Unknown command" in response:
self.recentcmdmgr.addcmd(command)
return ret_tuple
#--------------------------------------------------
if not self.cur_server.rcon_connected():
self.cur_server.set_rcon(self.bd.get_object("rcon_password").get_text())
if threaded:
d = threads.deferToThread(self.cur_server.rcon_cmd, command)
d.addCallback(cb)
return d
else:
return cb(self.cur_server.rcon_cmd(command))
def append_to_log(self, server, text, tag):
if not text:
return
if text[-1] != '\n':
text += '\n'
self.textlogs[server].log_buffer.insert_with_tags(self.textlogs[server].log_buffer.get_end_iter(), text, self.textlogs[server].log_buffer.get_tag_table().lookup(tag))
if server is self.cur_server:
self.stick_check()
def quit(self):
prefs = open("servers.pkl", "w")
cPickle.dump(self.servers, prefs, 2)
prefs.close()
self.recentcmdmgr.quit()
def on_rcon_password_changed(self, rcon_password):
enabled = rcon_password.get_text() != ""
self.bd.get_object("rcon_notebook").set_show_tabs(enabled)
self.bd.get_object("act_toggle_logging").set_sensitive(enabled)
rcon_password.set_property("secondary-icon-sensitive", enabled)
rcon_password.set_property("secondary-icon-stock", Gtk.STOCK_DIALOG_AUTHENTICATION)
self.cur_server.set_rcon(rcon_password.get_text())
def on_rcon_password_icon_press(self, rcon_password, pos, event):
self.test_rcon(self.cur_server)
def test_rcon(self, server):
def cb(ret_tuple):
success, response = ret_tuple
if success:
statusmanager.push_status("RCON Password test succeeded.")
else:
statusmanager.push_status("RCON Password test failed.")
return ret_tuple
#--------------------------------------------------
if server:
server.set_rcon(self.bd.get_object("rcon_password").get_text())
d = self.log_rcon(server, "version")
d.addCallback(cb)
return d
return False
def on_rcon_password_activate(self, rcon_password):
self.test_rcon(self.cur_server)
def on_query_timer(self, server):
if server not in self.servers.values():
return False
self.query_server(server)
return True
def init_query_timer(self, server):
GObject.timeout_add(QUERY_DELAY, self.on_query_timer, server)
def get_server_sid(self, server):
return self.get_sid(server.ip, server.port)
def get_sid(self, ip, port):
return ip + ":" + str(port)
def on_act_remove_server_activate(self, act_remove_server):
if self.cur_server:
sid = self.get_server_sid(self.cur_server)
self.delete_sid(sid)
statusmanager.push_status("Deleted server "+sid+".")
def delete_server(self, server):
self.delete_sid(self.get_server_sid(server))
def delete_sid(self, sid):
if sid in self.servers:
server = self.servers[sid]
if server is self.cur_server:
self.cur_server = None
self.set_logging(server, False)
server.cleanup()
if server in self.textlogs:
del self.textlogs[server]
if server in self.loggers:
del self.loggers[server]
del self.servers[sid]
else:
msg = "Server with sid '"+sid+"' not found to delete."
print(msg)
statusmanager.push_status(msg)
store = self.bd.get_object("servers_liststore")
for row in store:
if store.get_value(row.iter, 0) == sid:
store.remove(row.iter)
break
def query_server(self, server):
def cb(ret_tuple):
'''Called when the query thread gets info about a server'''
success, errmsg = ret_tuple
# update ping graph whether or not this query worked
server.add_ping_history()
self.pinggraph.queue_draw()
store = self.bd.get_object("servers_liststore")
for row in store:
if store.get_value(row.iter, 0) == self.get_server_sid(server):
store.set(row.iter, 1, server.info['hostname'], 2, self.format_numplayers(server.info), 3, server.info['ping'], 4, server.info['map'], 5, Gtk.STOCK_CONNECT if success else Gtk.STOCK_DISCONNECT)
break
if success and self.cur_server and server is self.cur_server:
self.populate_query_data(server.info, server.player)
return ret_tuple
#--------------------------------------------------
d = threads.deferToThread(server.query)
d.addCallback(cb)
return d
def on_rcon_input_activate(self, rcon_input):
text = rcon_input.get_text()
if text == "" or not self.cur_server:
return
self.log_rcon(self.cur_server, text)
rcon_input.set_text("")
def on_rcon_input_changed(self, rcon_input):
rcon_input.set_icon_sensitive(1, rcon_input.get_text())
def on_rcon_input_icon_press(self, rcon_input, pos, event):
if rcon_input.get_text():
self.log_rcon(self.cur_server, 'find ' + rcon_input.get_text())
def on_act_toggle_logging_toggled(self, act_toggle_logging):
if not self.cur_server:
return
self.set_logging(self.cur_server, act_toggle_logging.get_active())
act_toggle_logging.set_stock_id(Gtk.STOCK_CONNECT if act_toggle_logging.get_active() else Gtk.STOCK_DISCONNECT)
if act_toggle_logging.get_active():
self.bd.get_object("rcon_notebook").set_current_page(1)
def set_logging(self, server, enabled):
def cb(ret_tuple):
# FIXME: This is not run in a thread, so it hangs the system if connecting takes a while.
success, response = ret_tuple
if not success:
self.bd.get_object("act_toggle_logging").set_active(False)
return
s, r = self.log_rcon(server, 'log', False)
if 'not currently logging' in r:
self.log_rcon(server, 'log on', False)
def after_fetch_ip(ip):
if not ip:
return
for port in xrange(27020,27100):
try:
self.loggers[server] = reactor.listenUDP(port, SourceLib.SourceLog.SourceLogListener(server.ip, server.port, serverlogger.GameserverLogger(self, server)))
for line in response.split('\n'):
if ip in line and str(port) not in line:
self.log_rcon(server, 'logaddress_del ' + line, False)
self.log_rcon(server, 'logaddress_add ' + self.get_sid(ip, port), False)
statusmanager.push_status("Enabled logging from "+self.get_server_sid(server)+".")
break
except twisted.internet.error.CannotListenError:
print("Could not bind to port "+ str(port) + ", trying "+str(port+1))
ret, thread = utils.whatismyip()
if thread:
ret.addCallback(after_fetch_ip)
else:
after_fetch_ip(ret)
#--------------------------------------------------
if enabled == server.logging:
return
server.logging = enabled
if enabled:
d = self.log_rcon(server, "logaddress_list")
d.addCallback(cb)
elif server in self.loggers:
self.loggers[server].stopListening()
del self.loggers[server]
statusmanager.push_status("Disabled logging from "+self.get_server_sid(server)+".")
def stick_check(self):
'''If we try to stick now, we'll scroll to the pre-calculated 'bottom' of the buffer, which is the
end of the previous line. So we just defer the stick until the next loop so it'll get taken care of properly.
Also, we're not using the 'insert mark' that many online have suggested, because if the text is selected it
sticks to that part of the buffer instead of the bottom. I have yet to find a nice elegant way to do this.
This way makes for a slight flicker, but it's way better than anything else I've tried'''
def cb():
log_tv = self.bd.get_object("server_log_textview")
if self.bd.get_object("stick_to_bottom").get_active():
adj = log_tv.get_vadjustment()
adj.set_value(adj.get_upper())
#--------------------------------------------------
GObject.idle_add(cb)
def on_stick_to_bottom_toggled(self, stick_to_bottom):
if self.cur_server and stick_to_bottom.get_active():
self.stick_check()
def populate_selected(self):
'''update the gui for the selected server'''
tv = self.bd.get_object("server_log_textview")
buf = self.textlogs[self.cur_server].log_buffer
tv.set_buffer(buf)
self.stick_check()
self.pinggraph.set_data(self.cur_server.pings)
self.bd.get_object("save_rcon_toggle").set_active(self.cur_server.save_rcon)
tv2 = self.bd.get_object("pretty_log_textview")
tv2.set_buffer(self.textlogs[self.cur_server].pretty_buffer)
self.set_show_rcon(self.cur_server.rcon_visible)
if self.cur_server.rcon_password:
self.bd.get_object("rcon_password").set_text(self.cur_server.rcon_password)
else:
self.bd.get_object("rcon_password").set_text("")
self.bd.get_object("act_toggle_logging").set_active(self.cur_server.logging)
def format_numplayers(self, info):
return str(info['numplayers']) + "/" + str(info['maxplayers'])
def populate_query_data(self, info=[], players=[]):
def get_play_time_string(seconds):
return time.strftime('%H:%M:%S', time.gmtime(seconds))
labels = ['hostname', 'ip', 'port', 'map', 'gamedesc', 'ping']
for l in labels:
self.bd.get_object(l+"_label").set_text(str(info[l]) if l in info else "")
self.bd.get_object("players_label").set_text(self.format_numplayers(info) if info else "")
store = self.bd.get_object("players_liststore")
# handle the case where we have players already in the list
# by setting instead of deleting and adding, we should avoid deselection or messiness
for row in store:
p_index = store.get_value(row.iter, 0) - 1
if p_index < len(players):
plr = players[p_index]
# time is in seconds, convert to h:m:s (this mods by one day, oh noes)
store.set(row.iter, 1, plr['name'] if plr['name'] else '< Connecting... >', 2, plr['kills'], 3, get_play_time_string(plr['time']))
else:
# handle the case where a player on the list has left
store.remove(row.iter)
# handle the case where a player has joined
if len(players) > len(store):
for i in xrange(len(store), len(players)):
player = players[i]
store.append([i+1, player['name'] if player['name'] else '< Connecting... >', player['kills'], get_play_time_string(player['time'])])
def on_show_rcon_toggle_toggled(self, show_rcon_toggle):
self.set_show_rcon(show_rcon_toggle.get_active())
def set_show_rcon(self, enabled):
self.bd.get_object("rcon_password").set_visibility(enabled)
self.bd.get_object("show_rcon_toggle").set_active(enabled)
if self.cur_server:
self.cur_server.set_rcon_visibility(enabled)
def on_save_rcon_toggle_toggled(self, save_rcon_toggle):
if self.cur_server:
self.cur_server.save_rcon = save_rcon_toggle.get_active()
def on_server_list_cursor_changed(self, server_list):
sel = server_list.get_selection()
if sel:
model, itr = sel.get_selected()
if itr:
sid = model.get_value(itr, 0)
if sid in self.servers:
self.cur_server = self.servers[sid]
self.bd.get_object("rcon_notebook").set_sensitive(True)
self.query_server(self.cur_server)
self.populate_selected()
if not self.cur_server.rcon_connected():
self.bd.get_object("rcon_notebook").set_current_page(0)
self.bd.get_object("act_remove_server").set_sensitive(True)
return
self.clear_server_display()
def clear_server_display(self):
self.bd.get_object("rcon_notebook").set_sensitive(False)
self.populate_query_data()
self.pinggraph.set_data(None)
self.bd.get_object("act_toggle_logging").set_sensitive(False)
self.bd.get_object("act_remove_server").set_sensitive(False)
self.bd.get_object("save_rcon_toggle").set_active(True)
def add_server_item(self, server, connected=True):
store = self.bd.get_object("servers_liststore")
iter = store.append([self.get_server_sid(server), server.info['hostname'],str(server.info['numplayers']) + "/" + str(server.info['maxplayers']), server.info['ping'], server.info['map'], Gtk.STOCK_CONNECT if connected else Gtk.STOCK_DISCONNECT])
self.bd.get_object("server_list").set_cursor(store.get_path(iter))