lbry-sdk/lbrynet/core/Offer.py

47 lines
1.1 KiB
Python
Raw Normal View History

from lbrynet.core.Error import NegotiationError
class Offer(object):
"""
A rate offer to download blobs from a host
"""
2016-09-30 06:12:17 +02:00
RATE_ACCEPTED = "RATE_ACCEPTED"
RATE_TOO_LOW = "RATE_TOO_LOW"
RATE_UNSET = "RATE_UNSET"
def __init__(self, offer):
self._state = None
self.rate = None
if isinstance(offer, float):
self.rate = round(offer, 5)
2016-09-30 06:12:17 +02:00
elif offer == Offer.RATE_ACCEPTED:
self.accept()
2016-09-30 06:12:17 +02:00
elif offer == Offer.RATE_TOO_LOW:
self.reject()
@property
def accepted(self):
2016-09-30 06:12:17 +02:00
return self._state is Offer.RATE_ACCEPTED
@property
def too_low(self):
2016-09-30 06:12:17 +02:00
return self._state is Offer.RATE_TOO_LOW
@property
def message(self):
if self.accepted:
2016-09-30 06:12:17 +02:00
return Offer.RATE_ACCEPTED
elif self.too_low:
2016-09-30 06:12:17 +02:00
return Offer.RATE_TOO_LOW
elif self.rate is None:
2016-09-30 06:12:17 +02:00
return Offer.RATE_UNSET
def accept(self):
if self._state is None:
2016-09-30 06:12:17 +02:00
self._state = Offer.RATE_ACCEPTED
def reject(self):
if self._state is None:
2016-09-30 06:12:17 +02:00
self._state = Offer.RATE_TOO_LOW