2017-02-09 11:53:33 +01:00
|
|
|
import pickle
|
2017-02-08 13:41:56 +01:00
|
|
|
import threading
|
|
|
|
|
2017-06-24 12:17:01 +02:00
|
|
|
from bmconfigparser import BMConfigParser
|
2017-02-09 11:53:33 +01:00
|
|
|
import state
|
|
|
|
|
2017-02-08 13:41:56 +01:00
|
|
|
knownNodesLock = threading.Lock()
|
|
|
|
knownNodes = {}
|
|
|
|
|
2017-02-27 23:31:12 +01:00
|
|
|
knownNodesTrimAmount = 2000
|
|
|
|
|
2017-02-09 11:53:33 +01:00
|
|
|
def saveKnownNodes(dirName = None):
|
|
|
|
if dirName is None:
|
|
|
|
dirName = state.appdata
|
|
|
|
with knownNodesLock:
|
|
|
|
with open(dirName + 'knownnodes.dat', 'wb') as output:
|
|
|
|
pickle.dump(knownNodes, output)
|
2017-02-27 23:31:12 +01:00
|
|
|
|
|
|
|
def trimKnownNodes(recAddrStream = 1):
|
2017-06-24 12:17:01 +02:00
|
|
|
if len(knownNodes[recAddrStream]) < BMConfigParser().get("knownnodes", "maxnodes"):
|
2017-02-27 23:31:12 +01:00
|
|
|
return
|
|
|
|
with knownNodesLock:
|
2017-02-27 23:37:15 +01:00
|
|
|
oldestList = sorted(knownNodes[recAddrStream], key=knownNodes[recAddrStream].get)[:knownNodesTrimAmount]
|
2017-02-27 23:31:12 +01:00
|
|
|
for oldest in oldestList:
|
|
|
|
del knownNodes[recAddrStream][oldest]
|