forked from Tribler/tribler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tribler_main.py
1168 lines (935 loc) · 50.8 KB
/
tribler_main.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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
#
#
# Author : Choopan RATTANAPOKA, Jie Yang, Arno Bakker
#
# Description : Main ABC [Yet Another Bittorrent Client] python script.
# you can run from source code by using
# >python abc.py
# need Python, WxPython in order to run from source code.
#
# see LICENSE.txt for license information
#
import sys
import logging
from Tribler.Main.Utility.compat import (convertSessionConfig, convertMainConfig, convertDefaultDownloadConfig,
convertDownloadCheckpoints)
from Tribler.Core.version import version_id, commit_id, build_date
from Tribler.Core.osutils import get_free_space
logger = logging.getLogger(__name__)
# Arno: M2Crypto overrides the method for https:// in the
# standard Python libraries. This causes msnlib to fail and makes Tribler
# freakout when "http://www.tribler.org/version" is redirected to
# "https://www.tribler.org/version/" (which happened during our website
# changeover) Until M2Crypto 0.16 is patched I'll restore the method to the
# original, as follows.
#
# This must be done in the first python file that is started.
#
import urllib
original_open_https = urllib.URLopener.open_https
import M2Crypto # Not a useless import! See above.
urllib.URLopener.open_https = original_open_https
# modify the sys.stderr and sys.stdout for safe output
import Tribler.Debug.console
import os
from Tribler.Main.Utility.GuiDBHandler import startWorker, GUIDBProducer
from Tribler.dispersy.util import attach_profiler, call_on_reactor_thread
from Tribler.community.bartercast3.community import MASTER_MEMBER_PUBLIC_KEY_DIGEST as BARTER_MASTER_MEMBER_PUBLIC_KEY_DIGEST
from Tribler.Core.CacheDB.Notifier import Notifier
import traceback
from random import randint
try:
prctlimported = True
import prctl
except ImportError as e:
prctlimported = False
# Arno, 2008-03-21: see what happens when we disable this locale thing. Gives
# errors on Vista in "Regional and Language Settings Options" different from
# "English[United Kingdom]"
# import locale
import wx
from Tribler.Main.vwxGUI.gaugesplash import GaugeSplash
from Tribler.Main.vwxGUI.MainFrame import FileDropTarget
from Tribler.Main.Dialogs.FeedbackWindow import FeedbackWindow
# import hotshot
from collections import defaultdict
from traceback import print_exc
import urllib2
import tempfile
from Tribler.Main.vwxGUI.TriblerUpgradeDialog import TriblerUpgradeDialog
from Tribler.Main.vwxGUI.MainFrame import MainFrame # py2exe needs this import
from Tribler.Main.vwxGUI.GuiUtility import GUIUtility, forceWxThread
from Tribler.Main.vwxGUI.MainVideoFrame import VideoDummyFrame
from Tribler.Main.vwxGUI.GuiImageManager import GuiImageManager
from Tribler.Main.Dialogs.GUITaskQueue import GUITaskQueue
from Tribler.Main.globals import DefaultDownloadStartupConfig
from Tribler.Main.Utility.utility import Utility
from Tribler.Main.Utility.Feeds.rssparser import RssParser
from Tribler.Category.Category import Category
from Tribler.Policies.RateManager import UserDefinedMaxAlwaysOtherwiseDividedOverActiveSwarmsRateManager
from Tribler.Policies.SeedingManager import GlobalSeedingManager
from Tribler.Utilities.Instance2Instance import (Instance2InstanceClient, Instance2InstanceServer,
InstanceConnectionHandler)
from Tribler.Utilities.SingleInstanceChecker import SingleInstanceChecker
from Tribler.Core.simpledefs import (UPLOAD, DOWNLOAD, NTFY_MODIFIED, NTFY_INSERT, NTFY_REACHABLE, NTFY_ACTIVITIES,
NTFY_UPDATE, NTFY_CREATE, NTFY_CHANNELCAST, NTFY_STATE, NTFY_VOTECAST,
NTFY_MYPREFERENCES, NTFY_TORRENTS, NTFY_COMMENTS, NTFY_PLAYLISTS, NTFY_DELETE,
NTFY_MODIFICATIONS, NTFY_MODERATIONS, NTFY_PEERS, NTFY_MARKINGS, NTFY_FINISHED,
NTFY_MAGNET_GOT_PEERS, NTFY_MAGNET_PROGRESS, NTFY_MAGNET_STARTED,
NTFY_MAGNET_CLOSE, STATEDIR_TORRENTCOLL_DIR, dlstatus_strings,
DLSTATUS_STOPPED_ON_ERROR, DLSTATUS_DOWNLOADING, DLSTATUS_SEEDING,
DLSTATUS_STOPPED, NTFY_DISPERSY, NTFY_STARTED)
from Tribler.Core.Session import Session
from Tribler.Core.SessionConfig import SessionStartupConfig
from Tribler.Core.DownloadConfig import get_default_dest_dir, get_default_dscfg_filename
from Tribler.Core.Statistics.Status.Status import get_status_holder, delete_status_holders
from Tribler.Core.Statistics.Status.NullReporter import NullReporter
from Tribler.Core.Video.VideoPlayer import return_feasible_playback_modes, PLAYBACKMODE_INTERNAL
# Arno, 2012-06-20: h4x0t DHT import for py2...
import Tribler.Core.DecentralizedTracking.pymdht.core
import Tribler.Core.DecentralizedTracking.pymdht.core.identifier
import Tribler.Core.DecentralizedTracking.pymdht.core.message
import Tribler.Core.DecentralizedTracking.pymdht.core.node
import Tribler.Core.DecentralizedTracking.pymdht.core.ptime
import Tribler.Core.DecentralizedTracking.pymdht.core.routing_table
# Boudewijn: keep this import BELOW the imports from Tribler.xxx.* as
# one of those modules imports time as a module.
from time import time, sleep
from twisted.python.threadable import isInIOThread
from twisted.internet import reactor
SESSION_CHECKPOINT_INTERVAL = 900.0 # 15 minutes
CHANNELMODE_REFRESH_INTERVAL = 5.0
FREE_SPACE_CHECK_INTERVAL = 300.0
DEBUG = False
DEBUG_DOWNLOADS = False
ALLOW_MULTIPLE = os.environ.get("TRIBLER_ALLOW_MULTIPLE", "False").lower() == "true"
SKIP_TUNNEL_DIALOG = os.environ.get("TRIBLER_SKIP_OPTIN_DLG", "False") == "True"
# used by the anon tunnel tests as there's no way to mess with the Session before running the test ATM.
FORCE_ENABLE_TUNNEL_COMMUNITY = False
#
#
# Class : ABCApp
#
# Main ABC application class that contains ABCFrame Object
#
#
class ABCApp(object):
def __init__(self, params, installdir, autoload_discovery=True):
assert not isInIOThread(), "isInIOThread() seems to not be working correctly"
self._logger = logging.getLogger(self.__class__.__name__)
self.params = params
self.installdir = installdir
self.state_dir = None
self.error = None
self.last_update = 0
self.ready = False
self.done = False
self.frame = None
self.guiserver = GUITaskQueue.getInstance()
self.said_start_playback = False
self.decodeprogress = 0
self.old_reputation = 0
# DISPERSY will be set when available
self.dispersy = None
# BARTER_COMMUNITY will be set when both Dispersy and the EffortCommunity are available
self.barter_community = None
self.tunnel_community = None
self.seedingmanager = None
self.i2is = None
self.torrentfeed = None
self.webUI = None
self.utility = None
# Stage 1 start
session = self.InitStage1(installdir, autoload_discovery=autoload_discovery)
self.splash = None
try:
bm = self.gui_image_manager.getImage(u'splash.png')
self.splash = GaugeSplash(bm, "Loading...")
self.splash.setTicks(13)
self.splash.Show()
self._logger.info('Client Starting Up.')
self._logger.info("Tribler is using %s as working directory", self.installdir)
# Stage 2: show the splash window and start the session
self.splash.tick('Starting API')
s = self.startAPI(session, self.splash.tick)
self.utility = Utility(self.installdir, s.get_state_dir())
self.utility.app = self
self.utility.session = s
self.guiUtility = GUIUtility.getInstance(self.utility, self.params, self)
GUIDBProducer.getInstance()
self._logger.info('Tribler Version: %s Build: %s', version_id, commit_id)
version_info = self.utility.read_config('version_info')
if version_info.get('version_id', None) != version_id:
# First run of a different version
version_info['first_run'] = int(time())
version_info['version_id'] = version_id
self.utility.write_config('version_info', version_info)
self.splash.tick('Starting session and upgrading database (it may take a while)')
s.start()
self.dispersy = s.lm.dispersy
self.splash.tick('Loading userdownloadchoice')
from Tribler.Main.vwxGUI.UserDownloadChoice import UserDownloadChoice
UserDownloadChoice.get_singleton().set_utility(self.utility)
self.splash.tick('Initializing Family Filter')
cat = Category.getInstance()
state = self.utility.read_config('family_filter')
if state in (1, 0):
cat.set_family_filter(state == 1)
else:
self.utility.write_config('family_filter', 1)
self.utility.flush_config()
cat.set_family_filter(True)
# Create global rate limiter
self.splash.tick('Setting up ratelimiters')
self.ratelimiter = UserDefinedMaxAlwaysOtherwiseDividedOverActiveSwarmsRateManager()
# Counter to suppress some event from occurring
self.ratestatecallbackcount = 0
# So we know if we asked for peer details last cycle
self.lastwantpeers = []
maxup = self.utility.read_config('maxuploadrate')
self.ratelimiter.set_global_max_speed(UPLOAD, maxup)
maxdown = self.utility.read_config('maxdownloadrate')
self.ratelimiter.set_global_max_speed(DOWNLOAD, maxdown)
self.seedingmanager = GlobalSeedingManager(self.utility.read_config)
# Only allow updates to come in after we defined ratelimiter
self.prevActiveDownloads = []
s.set_download_states_callback(self.sesscb_states_callback)
# Schedule task for checkpointing Session, to avoid hash checks after
# crashes.
self.guiserver.add_task(self.guiservthread_checkpoint_timer, SESSION_CHECKPOINT_INTERVAL)
if not ALLOW_MULTIPLE:
# Put it here so an error is shown in the startup-error popup
# Start server for instance2instance communication
self.i2iconnhandler = InstanceConnectionHandler(self.i2ithread_readlinecallback)
self.i2is = Instance2InstanceServer(self.utility.read_config('i2ilistenport'), self.i2iconnhandler)
self.i2is.start()
self.splash.tick('GUIUtility register')
self.guiUtility.register()
channel_only = os.path.exists(os.path.join(self.installdir, 'joinchannel'))
if channel_only:
f = open(os.path.join(self.installdir, 'joinchannel'), 'rb')
channel_only = f.readline()
f.close()
self.frame = MainFrame(None, channel_only, PLAYBACKMODE_INTERNAL in return_feasible_playback_modes(), self.splash.tick)
self.frame.SetIcon(wx.Icon(os.path.join(self.installdir, 'Tribler', 'Main', 'vwxGUI', 'images', 'tribler.ico'), wx.BITMAP_TYPE_ICO))
# Arno, 2011-06-15: VLC 1.1.10 pops up separate win, don't have two.
self.frame.videoframe = None
if PLAYBACKMODE_INTERNAL in return_feasible_playback_modes():
vlcwrap = s.lm.videoplayer.get_vlcwrap()
wx.CallLater(3000, vlcwrap._init_vlc)
self.frame.videoframe = VideoDummyFrame(self.frame.videoparentpanel, self.utility, vlcwrap)
if sys.platform == 'win32':
wx.CallAfter(self.frame.top_bg.Refresh)
wx.CallAfter(self.frame.top_bg.Layout)
else:
self.frame.top_bg.Layout()
# Arno, 2007-05-03: wxWidgets 2.8.3.0 and earlier have the MIME-type for .bmp
# files set to 'image/x-bmp' whereas 'image/bmp' is the official one.
try:
bmphand = None
hands = wx.Image.GetHandlers()
for hand in hands:
# print "Handler",hand.GetExtension(),hand.GetType(),hand.GetMimeType()
if hand.GetMimeType() == 'image/x-bmp':
bmphand = hand
break
# wx.Image.AddHandler()
if bmphand is not None:
bmphand.SetMimeType('image/bmp')
except:
# wx < 2.7 don't like wx.Image.GetHandlers()
print_exc()
self.splash.Destroy()
self.frame.Show(True)
self.guiserver.add_task(self.guiservthread_free_space_check, 0)
self.torrentfeed = RssParser.getInstance()
self.webUI = None
if self.utility.read_config('use_webui'):
try:
from Tribler.Main.webUI.webUI import WebUI
self.webUI = WebUI.getInstance(self.guiUtility.library_manager, self.guiUtility.torrentsearch_manager, self.utility.read_config('webui_port'))
self.webUI.start()
except Exception:
print_exc()
wx.CallAfter(self.PostInit2)
# 08/02/10 Boudewijn: Working from home though console
# doesn't allow me to press close. The statement below
# gracefully closes Tribler after 120 seconds.
# wx.CallLater(120*1000, wx.GetApp().Exit)
status = get_status_holder("LivingLab")
status.add_reporter(NullReporter("Periodically remove all events", 0))
# TODO(emilon): can we delete this?
# status.add_reporter(LivingLabPeriodicReporter("Living lab CS reporter", 300, "Tribler client")) # Report every 5 minutes
# status.add_reporter(LivingLabPeriodicReporter("Living lab CS reporter", 30, "Tribler client")) # Report every 30 seconds - ONLY FOR TESTING
# report client version
status.create_and_add_event("client-startup-version", [version_id])
status.create_and_add_event("client-startup-build", [commit_id])
status.create_and_add_event("client-startup-build-date", [build_date])
self.ready = True
except Exception as e:
if self.splash:
self.splash.Destroy()
self.onError(e)
def InitStage1(self, installdir, autoload_discovery=True):
""" Stage 1 start: pre-start the session to handle upgrade.
"""
self.gui_image_manager = GuiImageManager.getInstance(installdir)
# Start Tribler Session
defaultConfig = SessionStartupConfig()
state_dir = defaultConfig.get_state_dir()
if not state_dir:
state_dir = Session.get_default_state_dir()
cfgfilename = Session.get_default_config_filename(state_dir)
self._logger.debug(u"Session config %s", cfgfilename)
try:
self.sconfig = SessionStartupConfig.load(cfgfilename)
except:
try:
self.sconfig = convertSessionConfig(os.path.join(state_dir, 'sessconfig.pickle'), cfgfilename)
convertMainConfig(state_dir, os.path.join(state_dir, 'abc.conf'), os.path.join(state_dir, 'tribler.conf'))
except:
self.sconfig = SessionStartupConfig()
self.sconfig.set_state_dir(state_dir)
self.sconfig.set_install_dir(self.installdir)
# Arno, 2010-03-31: Hard upgrade to 50000 torrents collected
self.sconfig.set_torrent_collecting_max_torrents(50000)
dlcfgfilename = get_default_dscfg_filename(self.sconfig.get_state_dir())
self._logger.debug("main: Download config %s", dlcfgfilename)
try:
defaultDLConfig = DefaultDownloadStartupConfig.load(dlcfgfilename)
except:
try:
defaultDLConfig = convertDefaultDownloadConfig(os.path.join(state_dir, 'dlconfig.pickle'), dlcfgfilename)
except:
defaultDLConfig = DefaultDownloadStartupConfig.getInstance()
if not defaultDLConfig.get_dest_dir():
defaultDLConfig.set_dest_dir(get_default_dest_dir())
if not os.path.isdir(defaultDLConfig.get_dest_dir()):
try:
os.makedirs(defaultDLConfig.get_dest_dir())
except:
# Could not create directory, ask user to select a different location
dlg = wx.DirDialog(None, "Could not find download directory, please select a new location to store your downloads", style=wx.DEFAULT_DIALOG_STYLE)
dlg.SetPath(get_default_dest_dir())
if dlg.ShowModal() == wx.ID_OK:
new_dest_dir = dlg.GetPath()
defaultDLConfig.set_dest_dir(new_dest_dir)
defaultDLConfig.save(dlcfgfilename)
self.sconfig.set_torrent_collecting_dir(os.path.join(new_dest_dir, STATEDIR_TORRENTCOLL_DIR))
self.sconfig.save(cfgfilename)
else:
# Quit
self.onError = lambda e: self._logger.error("tribler: quitting due to non-existing destination directory")
raise Exception()
# Setting torrent collection dir based on default download dir
if not self.sconfig.get_torrent_collecting_dir():
self.sconfig.set_torrent_collecting_dir(os.path.join(defaultDLConfig.get_dest_dir(), STATEDIR_TORRENTCOLL_DIR))
if FORCE_ENABLE_TUNNEL_COMMUNITY:
self.sconfig.set_tunnel_community_enabled(True)
if not self.sconfig.get_tunnel_community_optin_dialog_shown() and not SKIP_TUNNEL_DIALOG:
optin_dialog = wx.MessageDialog(None,
'If you are not familiar with proxy technology, please opt-out.\n\n'
'This experimental anonymity feature using Tor-inspired onion routing '
'and multi-layered encryption.'
'You will become an exit node for other users downloads which could get you in '
'trouble in various countries.\n'
'This privacy enhancement will not protect you against spooks or '
'government agencies.\n'
'We are a torrent client and aim to protect you against lawyer-based '
'attacks and censorship.\n'
'With help from many volunteers we are continuously evolving and improving.'
'\n\nI you aren\'t sure, press Cancel to disable the \n'
'experimental anonymity feature',
'Do you want to use the experimental anonymity feature?',
wx.ICON_WARNING | wx.OK | wx.CANCEL)
enable_tunnel_community = optin_dialog.ShowModal() == wx.ID_OK
self.sconfig.set_tunnel_community_enabled(enable_tunnel_community)
self.sconfig.set_tunnel_community_optin_dialog_shown(True)
optin_dialog.Destroy()
del optin_dialog
session = Session(self.sconfig, autoload_discovery=autoload_discovery)
# check and upgrade
upgrader = session.prestart()
if not upgrader.is_done:
upgrade_dialog = TriblerUpgradeDialog(self.gui_image_manager, upgrader)
failed = upgrade_dialog.ShowModal()
upgrade_dialog.Destroy()
if failed:
wx.MessageDialog(None, "Failed to upgrade the on disk data.\n\n"
"Tribler has backed up the old data and will now start from scratch.\n\n"
"Get in contact with the Tribler team if you want to help debugging this issue.\n\n"
"Error was: %s" % upgrader.current_status,
"Data format upgrade failed", wx.OK | wx.CENTRE | wx.ICON_EXCLAMATION).ShowModal()
return session
def _frame_and_ready(self):
return self.ready and self.frame and self.frame.ready
def PostInit2(self):
self.frame.Raise()
self.startWithRightView()
self.set_reputation()
s = self.utility.session
s.add_observer(self.sesscb_ntfy_reachable, NTFY_REACHABLE, [NTFY_INSERT])
s.add_observer(self.sesscb_ntfy_activities, NTFY_ACTIVITIES, [NTFY_INSERT], cache=10)
s.add_observer(self.sesscb_ntfy_channelupdates, NTFY_CHANNELCAST, [NTFY_INSERT, NTFY_UPDATE, NTFY_CREATE, NTFY_STATE, NTFY_MODIFIED], cache=10)
s.add_observer(self.sesscb_ntfy_channelupdates, NTFY_VOTECAST, [NTFY_UPDATE], cache=10)
s.add_observer(self.sesscb_ntfy_myprefupdates, NTFY_MYPREFERENCES, [NTFY_INSERT, NTFY_UPDATE, NTFY_DELETE])
s.add_observer(self.sesscb_ntfy_torrentupdates, NTFY_TORRENTS, [NTFY_UPDATE, NTFY_INSERT], cache=10)
s.add_observer(self.sesscb_ntfy_playlistupdates, NTFY_PLAYLISTS, [NTFY_INSERT, NTFY_UPDATE])
s.add_observer(self.sesscb_ntfy_commentupdates, NTFY_COMMENTS, [NTFY_INSERT, NTFY_DELETE])
s.add_observer(self.sesscb_ntfy_modificationupdates, NTFY_MODIFICATIONS, [NTFY_INSERT])
s.add_observer(self.sesscb_ntfy_moderationupdats, NTFY_MODERATIONS, [NTFY_INSERT])
s.add_observer(self.sesscb_ntfy_markingupdates, NTFY_MARKINGS, [NTFY_INSERT])
s.add_observer(self.sesscb_ntfy_torrentfinished, NTFY_TORRENTS, [NTFY_FINISHED])
s.add_observer(self.sesscb_ntfy_magnet, NTFY_TORRENTS, [NTFY_MAGNET_GOT_PEERS, NTFY_MAGNET_PROGRESS, NTFY_MAGNET_STARTED, NTFY_MAGNET_CLOSE])
# TODO(emilon): Use the LogObserver I already implemented
# self.dispersy.callback.attach_exception_handler(self.frame.exceptionHandler)
startWorker(None, self.loadSessionCheckpoint, delay=5.0, workerType="guiTaskQueue")
# initialize the torrent feed thread
channelcast = s.open_dbhandler(NTFY_CHANNELCAST)
def db_thread():
return channelcast.getMyChannelId()
def wx_thread(delayedResult):
my_channel = delayedResult.get()
if my_channel:
self.torrentfeed.register(self.utility.session, my_channel)
self.torrentfeed.addCallback(my_channel, self.guiUtility.channelsearch_manager.createTorrentFromDef)
# self.torrentfeed.addCallback(my_channel, self.guiUtility.torrentsearch_manager.createMetadataModificationFromDef)
startWorker(wx_thread, db_thread, delay=5.0)
def startAPI(self, session, progress):
@call_on_reactor_thread
def define_communities(*args):
assert isInIOThread()
from Tribler.community.search.community import SearchCommunity
from Tribler.community.allchannel.community import AllChannelCommunity
from Tribler.community.channel.community import ChannelCommunity
from Tribler.community.channel.preview import PreviewChannelCommunity
from Tribler.community.metadata.community import MetadataCommunity
from Tribler.community.tunnel.tunnel_community import TunnelSettings
from Tribler.community.tunnel.hidden_community import HiddenTunnelCommunity
from Tribler.community.bartercast4.community import BarterCommunity
# make sure this is only called once
session.remove_observer(define_communities)
dispersy = session.get_dispersy_instance()
self._logger.info("tribler: Preparing communities...")
now = time()
dispersy.attach_progress_handler(self.progressHandler)
default_kwargs = {'tribler_session': session}
# must be called on the Dispersy thread
dispersy.define_auto_load(SearchCommunity, session.dispersy_member, load=True, kargs=default_kwargs)
dispersy.define_auto_load(AllChannelCommunity, session.dispersy_member, load=True, kargs=default_kwargs)
dispersy.define_auto_load(BarterCommunity, session.dispersy_member, load=True)
# load metadata community
# dispersy.define_auto_load(MetadataCommunity, session.dispersy_member, load=True, kargs=default_kwargs)
dispersy.define_auto_load(ChannelCommunity, session.dispersy_member, load=True, kargs=default_kwargs)
dispersy.define_auto_load(PreviewChannelCommunity, session.dispersy_member, kargs=default_kwargs)
if self.sconfig.get_tunnel_community_enabled():
keypair = dispersy.crypto.generate_key(u"curve25519")
dispersy_member = dispersy.get_member(private_key=dispersy.crypto.key_to_bin(keypair),)
settings = TunnelSettings(session.get_install_dir())
tunnel_kwargs = {'tribler_session': session, 'settings': settings}
self.tunnel_community = dispersy.define_auto_load(HiddenTunnelCommunity, dispersy_member, load=True,
kargs=tunnel_kwargs)[0]
session.set_anon_proxy_settings(2, ("127.0.0.1", session.get_tunnel_community_socks5_listen_ports()))
diff = time() - now
self._logger.info("tribler: communities are ready in %.2f seconds", diff)
session.add_observer(define_communities, NTFY_DISPERSY, [NTFY_STARTED])
return session
@staticmethod
def determine_install_dir():
# Niels, 2011-03-03: Working dir sometimes set to a browsers working dir
# only seen on windows
# apply trick to obtain the executable location
# see http://www.py2exe.org/index.cgi/WhereAmI
# Niels, 2012-01-31: py2exe should only apply to windows
if sys.platform == 'win32':
def we_are_frozen():
"""Returns whether we are frozen via py2exe.
This will affect how we find out where we are located."""
return hasattr(sys, "frozen")
def module_path():
""" This will get us the program's directory,
even if we are frozen using py2exe"""
if we_are_frozen():
return os.path.dirname(unicode(sys.executable, sys.getfilesystemencoding()))
filedir = os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
return os.path.abspath(os.path.join(filedir, '..', '..'))
return module_path()
return os.getcwdu()
@forceWxThread
def sesscb_ntfy_myprefupdates(self, subject, changeType, objectID, *args):
if self._frame_and_ready():
if changeType in [NTFY_INSERT, NTFY_UPDATE]:
if changeType == NTFY_INSERT:
if self.frame.searchlist:
manager = self.frame.searchlist.GetManager()
manager.downloadStarted(objectID)
manager = self.frame.selectedchannellist.GetManager()
manager.downloadStarted(objectID)
manager = self.frame.librarylist.GetManager()
manager.downloadStarted(objectID)
elif changeType == NTFY_DELETE:
self.guiUtility.frame.librarylist.RemoveItem(objectID)
if self.guiUtility.frame.librarylist.IsShownOnScreen() and \
self.guiUtility.frame.librarydetailspanel.torrent and \
self.guiUtility.frame.librarydetailspanel.torrent.infohash == objectID:
self.guiUtility.frame.librarylist.ResetBottomWindow()
self.guiUtility.frame.top_bg.ClearButtonHandlers()
if self.guiUtility.frame.librarylist.list.IsEmpty():
self.guiUtility.frame.librarylist.SetData([])
def progressHandler(self, title, message, maximum):
from Tribler.Main.Dialogs.ThreadSafeProgressDialog import ThreadSafeProgressDialog
return ThreadSafeProgressDialog(title, message, maximum, None, wx.PD_APP_MODAL | wx.PD_ELAPSED_TIME | wx.PD_ESTIMATED_TIME | wx.PD_REMAINING_TIME | wx.PD_AUTO_HIDE)
def set_reputation(self):
def do_db():
nr_connections = 0
nr_channel_connections = 0
if self.dispersy:
for community in self.dispersy.get_communities():
from Tribler.community.search.community import SearchCommunity
from Tribler.community.allchannel.community import AllChannelCommunity
if isinstance(community, SearchCommunity):
nr_connections = community.get_nr_connections()
elif isinstance(community, AllChannelCommunity):
nr_channel_connections = community.get_nr_connections()
return nr_connections, nr_channel_connections
def do_wx(delayedResult):
if not self.frame:
return
nr_connections, nr_channel_connections = delayedResult.get()
# self.frame.SRstatusbar.set_reputation(myRep, total_down, total_up)
# bitmap is 16px wide, -> but first and last pixel do not add anything.
percentage = min(1.0, (nr_connections + 1) / 16.0)
self.frame.SRstatusbar.SetConnections(percentage, nr_connections, nr_channel_connections)
""" set the reputation in the GUI"""
if self._frame_and_ready():
startWorker(do_wx, do_db, uId=u"tribler.set_reputation")
startWorker(None, self.set_reputation, delay=5.0, workerType="guiTaskQueue")
def _dispersy_get_barter_community(self):
try:
return self.dispersy.get_community(BARTER_MASTER_MEMBER_PUBLIC_KEY_DIGEST, load=False, auto_load=False)
except KeyError:
return None
def sesscb_states_callback(self, dslist):
if not self.ready:
return 5.0, []
wantpeers = []
self.ratestatecallbackcount += 1
if DEBUG:
torrentdb = self.utility.session.open_dbhandler(NTFY_TORRENTS)
peerdb = self.utility.session.open_dbhandler(NTFY_PEERS)
self._logger.debug(u"main: Stats: Total torrents found %s peers %s",
repr(torrentdb.size()), repr(peerdb.size()))
try:
# Print stats on Console
if self.ratestatecallbackcount % 5 == 0:
for ds in dslist:
safename = repr(ds.get_download().get_def().get_name())
self._logger.debug("%s %s %.1f%% dl %.1f ul %.1f n %d", safename, dlstatus_strings[ds.get_status()], 100.0 * ds.get_progress(), ds.get_current_speed(DOWNLOAD), ds.get_current_speed(UPLOAD), ds.get_num_peers())
# print >>sys.stderr,"main: Infohash:",`ds.get_download().get_def().get_infohash()`
if ds.get_status() == DLSTATUS_STOPPED_ON_ERROR:
self._logger.error("main: Error: %s", repr(ds.get_error()))
# show error dialog
dlg = wx.MessageDialog(self.frame,
"Download stopped on error: %s" % repr(ds.get_error()),
"Download Error",
wx.OK | wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()
# Pass DownloadStates to libaryView
no_collected_list = []
try:
coldir = os.path.basename(os.path.abspath(self.utility.session.get_torrent_collecting_dir()))
for ds in dslist:
destdir = os.path.basename(ds.get_download().get_dest_dir())
if destdir != coldir:
no_collected_list.append(ds)
# Arno, 2012-07-17: Retrieving peerlist for the DownloadStates takes CPU
# so only do it when needed for display.
wantpeers.extend(self.guiUtility.library_manager.download_state_callback(no_collected_list))
except:
print_exc()
# Check to see if a download has finished
newActiveDownloads = []
doCheckpoint = False
for ds in dslist:
state = ds.get_status()
tdef = ds.get_download().get_def()
safename = tdef.get_name_as_unicode()
if state == DLSTATUS_DOWNLOADING:
newActiveDownloads.append(safename)
elif state == DLSTATUS_SEEDING:
if safename in self.prevActiveDownloads:
download = ds.get_download()
tdef = download.get_def()
coldir = os.path.basename(os.path.abspath(self.utility.session.get_torrent_collecting_dir()))
destdir = os.path.basename(download.get_dest_dir())
if destdir != coldir:
infohash = tdef.get_infohash()
notifier = Notifier.getInstance()
notifier.notify(NTFY_TORRENTS, NTFY_FINISHED, infohash, safename)
doCheckpoint = True
self.prevActiveDownloads = newActiveDownloads
if doCheckpoint:
self.utility.session.checkpoint()
self.seedingmanager.apply_seeding_policy(no_collected_list)
# Adjust speeds and call TunnelCommunity.monitor_downloads once every 4 seconds
adjustspeeds = False
if self.ratestatecallbackcount % 4 == 0:
adjustspeeds = True
if adjustspeeds:
self.ratelimiter.adjust_speeds()
if DEBUG_DOWNLOADS:
for ds in dslist:
tdef = ds.get_download().get_def()
state = ds.get_status()
self._logger.debug(u"tribler: BT %s %s %s", dlstatus_strings[state], tdef.get_name(),
ds.get_current_speed(UPLOAD))
if self.tunnel_community:
self.tunnel_community.monitor_downloads(dslist)
except:
print_exc()
self.lastwantpeers = wantpeers
return 1.0, wantpeers
def loadSessionCheckpoint(self):
# Niels: first remove all "swift" torrent collect checkpoints
dir = self.utility.session.get_downloads_pstate_dir()
coldir = os.path.basename(os.path.abspath(self.utility.session.get_torrent_collecting_dir()))
filelist = os.listdir(dir)
if any([filename.endswith('.pickle') for filename in filelist]):
convertDownloadCheckpoints(dir)
filelist = os.listdir(dir)
filelist = [os.path.join(dir, filename) for filename in filelist if filename.endswith('.state')]
for file in filelist:
try:
pstate = self.utility.session.lm.load_download_pstate(file)
saveas = pstate.get('downloadconfig', 'saveas')
if saveas:
destdir = os.path.basename(saveas)
if destdir == coldir:
os.remove(file)
except:
pass
from Tribler.Main.vwxGUI.UserDownloadChoice import UserDownloadChoice
user_download_choice = UserDownloadChoice.get_singleton()
initialdlstatus_dict = {}
for id, state in user_download_choice.get_download_states().iteritems():
if state == 'stop':
initialdlstatus_dict[id] = DLSTATUS_STOPPED
self.utility.session.load_checkpoint(initialdlstatus_dict=initialdlstatus_dict)
def guiservthread_free_space_check(self):
free_space = get_free_space(DefaultDownloadStartupConfig.getInstance().get_dest_dir())
self.frame.SRstatusbar.RefreshFreeSpace(free_space)
storage_locations = defaultdict(list)
for download in self.utility.session.get_downloads():
if download.get_status() == DLSTATUS_DOWNLOADING:
storage_locations[download.get_dest_dir()].append(download)
show_message = False
low_on_space = [path for path in storage_locations.keys() if 0 < get_free_space(path) < self.utility.read_config('free_space_threshold')]
for path in low_on_space:
for download in storage_locations[path]:
download.stop()
show_message = True
if show_message:
wx.CallAfter(wx.MessageBox, "Tribler has detected low disk space. Related downloads have been stopped.", "Error")
self.guiserver.add_task(self.guiservthread_free_space_check, FREE_SPACE_CHECK_INTERVAL)
def guiservthread_checkpoint_timer(self):
""" Periodically checkpoint Session """
if self.done:
return
try:
self._logger.info("main: Checkpointing Session")
self.utility.session.checkpoint()
self.guiserver.add_task(self.guiservthread_checkpoint_timer, SESSION_CHECKPOINT_INTERVAL)
except:
print_exc()
@forceWxThread
def sesscb_ntfy_activities(self, events):
if self._frame_and_ready():
for args in events:
objectID = args[2]
args = args[3:]
self.frame.setActivity(objectID, *args)
@forceWxThread
def sesscb_ntfy_reachable(self, subject, changeType, objectID, msg):
if self._frame_and_ready():
self.frame.SRstatusbar.onReachable()
@forceWxThread
def sesscb_ntfy_channelupdates(self, events):
if self._frame_and_ready():
for args in events:
subject = args[0]
changeType = args[1]
objectID = args[2]
if self.frame.channellist:
if len(args) > 3:
myvote = args[3]
else:
myvote = False
manager = self.frame.channellist.GetManager()
manager.channelUpdated(objectID, subject == NTFY_VOTECAST, myvote=myvote)
manager = self.frame.selectedchannellist.GetManager()
manager.channelUpdated(objectID, stateChanged=changeType == NTFY_STATE, modified=changeType == NTFY_MODIFIED)
if changeType == NTFY_CREATE:
if self.frame.channellist:
self.frame.channellist.SetMyChannelId(objectID)
self.torrentfeed.register(self.utility.session, objectID)
self.torrentfeed.addCallback(objectID, self.guiUtility.channelsearch_manager.createTorrentFromDef)
# self.torrentfeed.addCallback(objectID, self.guiUtility.torrentsearch_manager.createMetadataModificationFromDef)
self.frame.managechannel.channelUpdated(objectID, created=changeType == NTFY_CREATE, modified=changeType == NTFY_MODIFIED)
@forceWxThread
def sesscb_ntfy_torrentupdates(self, events):
if self._frame_and_ready():
infohashes = [args[2] for args in events]
if self.frame.searchlist:
manager = self.frame.searchlist.GetManager()
manager.torrentsUpdated(infohashes)
manager = self.frame.selectedchannellist.GetManager()
manager.torrentsUpdated(infohashes)
manager = self.frame.playlist.GetManager()
manager.torrentsUpdated(infohashes)
manager = self.frame.librarylist.GetManager()
manager.torrentsUpdated(infohashes)
from Tribler.Main.Utility.GuiDBTuples import CollectedTorrent
if self.frame.torrentdetailspanel.torrent and self.frame.torrentdetailspanel.torrent.infohash in infohashes:
# If an updated torrent is being shown in the detailspanel, make sure the information gets refreshed.
t = self.frame.torrentdetailspanel.torrent
torrent = t.torrent if isinstance(t, CollectedTorrent) else t
self.frame.torrentdetailspanel.setTorrent(torrent)
if self.frame.librarydetailspanel.torrent and self.frame.librarydetailspanel.torrent.infohash in infohashes:
t = self.frame.librarydetailspanel.torrent
torrent = t.torrent if isinstance(t, CollectedTorrent) else t
self.frame.librarydetailspanel.setTorrent(torrent)
def sesscb_ntfy_torrentfinished(self, subject, changeType, objectID, *args):
self.guiUtility.Notify("Download Completed", "Torrent '%s' has finished downloading. Now seeding." % args[0], icon='seed')
if self._frame_and_ready():
self.guiUtility.torrentstate_manager.torrentFinished(objectID)
def sesscb_ntfy_magnet(self, subject, changetype, objectID, *args):
if changetype == NTFY_MAGNET_STARTED:
self.guiUtility.library_manager.magnet_started(objectID)
elif changetype == NTFY_MAGNET_GOT_PEERS:
self.guiUtility.library_manager.magnet_got_peers(objectID, args[0])
elif changetype == NTFY_MAGNET_PROGRESS:
self.guiUtility.library_manager.magnet_got_piece(objectID, args[0])
elif changetype == NTFY_MAGNET_CLOSE:
self.guiUtility.library_manager.magnet_close(objectID)
@forceWxThread
def sesscb_ntfy_playlistupdates(self, subject, changeType, objectID, *args):
if self._frame_and_ready():
if changeType == NTFY_INSERT:
self.frame.managechannel.playlistCreated(objectID)
manager = self.frame.selectedchannellist.GetManager()
manager.playlistCreated(objectID)
else:
self.frame.managechannel.playlistUpdated(objectID, modified=changeType == NTFY_MODIFIED)
if len(args) > 0:
infohash = args[0]
else:
infohash = False
manager = self.frame.selectedchannellist.GetManager()
manager.playlistUpdated(objectID, infohash, modified=changeType == NTFY_MODIFIED)
manager = self.frame.playlist.GetManager()
manager.playlistUpdated(objectID, modified=changeType == NTFY_MODIFIED)
@forceWxThread
def sesscb_ntfy_commentupdates(self, subject, changeType, objectID, *args):
if self._frame_and_ready():
self.frame.selectedchannellist.OnCommentCreated(objectID)
self.frame.playlist.OnCommentCreated(objectID)
@forceWxThread
def sesscb_ntfy_modificationupdates(self, subject, changeType, objectID, *args):
if self._frame_and_ready():
self.frame.selectedchannellist.OnModificationCreated(objectID)
self.frame.playlist.OnModificationCreated(objectID)
@forceWxThread
def sesscb_ntfy_moderationupdats(self, subject, changeType, objectID, *args):
if self._frame_and_ready():
self.frame.selectedchannellist.OnModerationCreated(objectID)
self.frame.playlist.OnModerationCreated(objectID)
@forceWxThread
def sesscb_ntfy_markingupdates(self, subject, changeType, objectID, *args):
if self._frame_and_ready():
self.frame.selectedchannellist.OnMarkingCreated(objectID)
self.frame.playlist.OnModerationCreated(objectID)
@forceWxThread
def onError(self, e):
print_exc()
type, value, stack = sys.exc_info()
backtrace = traceback.format_exception(type, value, stack)
win = FeedbackWindow("Unfortunately, Tribler ran into an internal error")
win.CreateOutputWindow('')
for line in backtrace:
win.write(line)
win.ShowModal()
def MacOpenFile(self, filename):
self._logger.info(repr(filename))
target = FileDropTarget(self.frame)
target.OnDropFiles(None, None, [filename])
@forceWxThread
def OnExit(self):
bm = self.gui_image_manager.getImage(u'closescreen.png')
self.closewindow = GaugeSplash(bm, "Closing...")
self.closewindow.setTicks(6)
self.closewindow.Show()
self._logger.info("main: ONEXIT")
self.ready = False
self.done = True
# Remove anonymous test download
self.closewindow.tick('Remove anonymous test download')
for download in self.utility.session.get_downloads():
tdef = download.get_def()
if not tdef.is_anonymous() and download.get_anon_mode() and \
os.path.basename(download.get_dest_dir()) == "anon_test":
self.utility.session.remove_download(download)
# write all persistent data to disk
self.closewindow.tick('Write all persistent data to disk')
if self.i2is:
self.i2is.shutdown()
if self.torrentfeed:
self.torrentfeed.shutdown()
self.torrentfeed.delInstance()
if self.webUI:
self.webUI.stop()
self.webUI.delInstance()
if self.guiserver:
self.guiserver.shutdown(True)
self.guiserver.delInstance()
delete_status_holders()
if self.frame:
self.frame.Destroy()
self.frame = None
# Don't checkpoint, interferes with current way of saving Preferences,
# see Tribler/Main/Dialogs/abcoption.py