put peerprofiles in its own file

master
Kevin Froman 2019-07-12 12:51:35 -05:00
parent f5e613134b
commit 1ba26541b2
3 changed files with 64 additions and 63 deletions

View File

@ -1,64 +1,4 @@
''' from . import scoresortedpeerlist, peercleanup, peerprofiles
Onionr - Private P2P Communication
This file contains both the PeerProfiles class for network profiling of Onionr nodes
'''
'''
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 3 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, see <https://www.gnu.org/licenses/>.
'''
from . import scoresortedpeerlist, peercleanup
get_score_sorted_peer_list = scoresortedpeerlist.get_score_sorted_peer_list get_score_sorted_peer_list = scoresortedpeerlist.get_score_sorted_peer_list
peer_cleanup = peercleanup.peer_cleanup peer_cleanup = peercleanup.peer_cleanup
PeerProfiles = peerprofiles.PeerProfiles
class PeerProfiles:
'''
PeerProfiles
'''
def __init__(self, address, coreInst):
self.address = address # node address
self.score = None
self.friendSigCount = 0
self.success = 0
self.failure = 0
self.connectTime = None
self.coreInst = coreInst
self.loadScore()
self.getConnectTime()
return
def loadScore(self):
'''Load the node's score from the database'''
try:
self.success = int(self.coreInst.getAddressInfo(self.address, 'success'))
except (TypeError, ValueError) as e:
self.success = 0
self.score = self.success
def getConnectTime(self):
try:
self.connectTime = int(self.coreInst.getAddressInfo(self.address, 'lastConnect'))
except (KeyError, ValueError, TypeError) as e:
pass
def saveScore(self):
'''Save the node's score to the database'''
self.coreInst.setAddressInfo(self.address, 'success', self.score)
return
def addScore(self, toAdd):
'''Add to the peer's score (can add negative)'''
self.score += toAdd
self.saveScore()

View File

@ -0,0 +1,60 @@
'''
Onionr - Private P2P Communication
This file contains both the PeerProfiles class for network profiling of Onionr nodes
'''
'''
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 3 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, see <https://www.gnu.org/licenses/>.
'''
class PeerProfiles:
'''
PeerProfiles
'''
def __init__(self, address, coreInst):
self.address = address # node address
self.score = None
self.friendSigCount = 0
self.success = 0
self.failure = 0
self.connectTime = None
self.coreInst = coreInst
self.loadScore()
self.getConnectTime()
return
def loadScore(self):
'''Load the node's score from the database'''
try:
self.success = int(self.coreInst.getAddressInfo(self.address, 'success'))
except (TypeError, ValueError) as e:
self.success = 0
self.score = self.success
def getConnectTime(self):
try:
self.connectTime = int(self.coreInst.getAddressInfo(self.address, 'lastConnect'))
except (KeyError, ValueError, TypeError) as e:
pass
def saveScore(self):
'''Save the node's score to the database'''
self.coreInst.setAddressInfo(self.address, 'success', self.score)
return
def addScore(self, toAdd):
'''Add to the peer's score (can add negative)'''
self.score += toAdd
self.saveScore()

View File

@ -17,6 +17,7 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
from . import peerprofiles
def get_score_sorted_peer_list(coreInst): def get_score_sorted_peer_list(coreInst):
peer_list = coreInst.listAdders() peer_list = coreInst.listAdders()
peer_scores = {} peer_scores = {}
@ -24,7 +25,7 @@ def get_score_sorted_peer_list(coreInst):
for address in peer_list: for address in peer_list:
# Load peer's profiles into a list # Load peer's profiles into a list
profile = PeerProfiles(address, coreInst) profile = peerprofiles.PeerProfiles(address, coreInst)
peer_scores[address] = profile.score peer_scores[address] = profile.score
if not isinstance(profile.connectTime, type(None)): if not isinstance(profile.connectTime, type(None)):
peer_times[address] = profile.connectTime peer_times[address] = profile.connectTime