MCPcopy Index your code
hub / github.com/Bitmessage/PyBitmessage / chooseConnection

Function chooseConnection

src/network/connectionchooser.py:21–58  ·  view source on GitHub ↗
(stream)

Source from the content-addressed store, hash-verified

19 return peer
20
21def chooseConnection(stream):
22 haveOnion = BMConfigParser().safeGet("bitmessagesettings", "socksproxytype")[0:5] == 'SOCKS'
23 if state.trustedPeer:
24 return state.trustedPeer
25 try:
26 retval = portCheckerQueue.get(False)
27 portCheckerQueue.task_done()
28 return retval
29 except Queue.Empty:
30 pass
31 # with a probability of 0.5, connect to a discovered peer
32 if random.choice((False, True)) and not haveOnion:
33 # discovered peers are already filtered by allowed streams
34 return getDiscoveredPeer()
35 for _ in range(50):
36 peer = random.choice(knownnodes.knownNodes[stream].keys())
37 try:
38 rating = knownnodes.knownNodes[stream][peer]["rating"]
39 except TypeError:
40 print "Error in %s" % (peer)
41 rating = 0
42 if haveOnion:
43 # onion addresses have a higher priority when SOCKS
44 if peer.host.endswith('.onion') and rating > 0:
45 rating = 1
46 else:
47 encodedAddr = protocol.encodeHost(peer.host)
48 # don't connect to local IPs when using SOCKS
49 if not protocol.checkIPAddress(encodedAddr, False):
50 continue
51 if rating > 1:
52 rating = 1
53 try:
54 if 0.05/(1.0-rating) > random.random():
55 return peer
56 except ZeroDivisionError:
57 return peer
58 raise ValueError

Callers 1

loopMethod · 0.90

Calls 5

BMConfigParserClass · 0.90
getDiscoveredPeerFunction · 0.85
safeGetMethod · 0.80
task_doneMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected