__init__.py :  » Network » Torrent-Swapper » swapper » Swapper » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Torrent Swapper 
Torrent Swapper » swapper » Swapper » __init__.py
# Written by Jie Yang, Arno Bakker
# see LICENSE.txt for license information

## Arno: FIXME _idprefix is also defined in BitTornado.__init__ and that's the one
## actually used in connections, so make sure they are defined in one place
## (here) and correct.
##

from BitTornado.__init__ import version_id,version_short,product_name,version,report_email

from types import StringType
from sha import sha
from time import time,clock
from string import strip
import socket
import sys
try:
    from os import getpid
except ImportError:
    def getpid():
        return 1
import Overlay.permid as permid
import CacheDB.cachedb as cachedb
import CacheDB.superpeer as superpeer
from base64 import decodestring
import CacheDB.friends as friends
import guessip    

mapbase64 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-'

## Global initialization

# Arno: looking at Azureus BTPeerIDByteDecoder this letter is free
# 'T' is BitTornado, 'A' is ABC, 'TR' is Transmission
_idprefix = 'R' 
#for subver in version_short[2:].split('.'):
for subver in version_short.split('-')[1].split('.'):
    try:
        subver = int(subver)
    except:
        subver = 0
    _idprefix += mapbase64[subver]
_idprefix += ('-' * (6-len(_idprefix)))

_idrandom = [None]

class GLOBAL:
    do_cache = 1
    do_overlay = 1
    do_buddycast = 1
    do_download_help = 1
    do_superpeer = 0
    do_das_test = 0
    do_buddycast_interval = 15
    do_torrent_collecting = 1
    overlay_log = ''
    config_dir = '.'+product_name
    
myinfo = {}

def load_myinfo(myinfo):    # TODO: load more personal infomation
    my_permid = str(permid._ec_keypair.pub().get_der())
    name = socket.gethostname()
    my_ip = get_my_ip(name)
    myinfo['permid'] = my_permid
    myinfo['ip'] = my_ip
    myinfo['name'] = name

def get_my_ip(name):
    ip = guessip.get_my_wan_ip()
    if ip is None:
        host = socket.gethostbyname_ex(name)
        ipaddrlist = host[2]
        for ip in ipaddrlist:
            return ip
        return '127.0.0.1'
    else:
        return ip

def resetPeerIDs():
    try:
        f = open('/dev/urandom', 'rb')
        x = f.read(20)
        f.close()
    except:
        x = ''

    l1 = 0
    t = clock()
    while t == clock():
        l1 += 1
    l2 = 0
    t = long(time()*100)
    while t == long(time()*100):
        l2 += 1
    l3 = 0
    if l2 < 1000:
        t = long(time()*10)
        while t == long(clock()*10):
            l3 += 1
    x += ( repr(time()) + '/' + str(time()) + '/'
           + str(l1) + '/' + str(l2) + '/' + str(l3) + '/'
           + str(getpid()) )

    s = ''
    for i in sha(x).digest()[-11:]:
        s += mapbase64[ord(i) & 0x3F]
    _idrandom[0] = s

def createPeerID(ins = '---'):
    if type(ins) != StringType:
        raise Exception, "swapper__init__: createPeerID"
    if len(ins) != 3:
        raise Exception, "swapper__init__: createPeerID"
    return _idprefix + ins + _idrandom[0]


def swapper_init(config_dir = None, install_dir = None):
    global myinfo
    if config_dir:
        GLOBAL.config_dir = config_dir
    resetPeerIDs()
    permid.init(config_dir)
    load_myinfo(myinfo)
    # roee88 says we need to revert to encoded here for the databases
    cachedb.init(config_dir.encode(sys.getfilesystemencoding()),myinfo)
    superpeer.init(install_dir)
    friends.init(config_dir)

def swapper_done(config_dir = None):
    friends.done(config_dir)
    cachedb.done(config_dir)
    
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.