2016-09-27 19:52:44 +02:00
|
|
|
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"
|
|
|
|
|
2016-09-27 19:52:44 +02:00
|
|
|
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:
|
2016-09-27 19:52:44 +02:00
|
|
|
self.accept()
|
2016-09-30 06:12:17 +02:00
|
|
|
elif offer == Offer.RATE_TOO_LOW:
|
2016-09-27 19:52:44 +02:00
|
|
|
self.reject()
|
2016-10-03 08:44:58 +02:00
|
|
|
else:
|
|
|
|
self.unset()
|
2016-09-27 19:52:44 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def accepted(self):
|
2016-09-30 06:12:17 +02:00
|
|
|
return self._state is Offer.RATE_ACCEPTED
|
2016-09-27 19:52:44 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def too_low(self):
|
2016-09-30 06:12:17 +02:00
|
|
|
return self._state is Offer.RATE_TOO_LOW
|
2016-09-27 19:52:44 +02:00
|
|
|
|
2016-10-03 08:44:58 +02:00
|
|
|
@property
|
|
|
|
def is_unset(self):
|
|
|
|
return self._state is Offer.RATE_UNSET
|
|
|
|
|
2016-09-27 19:52:44 +02:00
|
|
|
@property
|
|
|
|
def message(self):
|
|
|
|
if self.accepted:
|
2016-09-30 06:12:17 +02:00
|
|
|
return Offer.RATE_ACCEPTED
|
2016-09-27 19:52:44 +02:00
|
|
|
elif self.too_low:
|
2016-09-30 06:12:17 +02:00
|
|
|
return Offer.RATE_TOO_LOW
|
2016-10-03 08:44:58 +02:00
|
|
|
elif self.is_unset:
|
2016-09-30 06:12:17 +02:00
|
|
|
return Offer.RATE_UNSET
|
2016-10-03 08:44:58 +02:00
|
|
|
return None
|
2016-09-27 19:52:44 +02:00
|
|
|
|
|
|
|
def accept(self):
|
2016-10-03 08:44:58 +02:00
|
|
|
if self._state is None or self.is_unset:
|
2016-09-30 06:12:17 +02:00
|
|
|
self._state = Offer.RATE_ACCEPTED
|
2016-09-27 19:52:44 +02:00
|
|
|
|
|
|
|
def reject(self):
|
2016-10-03 08:44:58 +02:00
|
|
|
if self._state is None or self.is_unset:
|
2016-09-30 06:12:17 +02:00
|
|
|
self._state = Offer.RATE_TOO_LOW
|
2016-10-03 08:44:58 +02:00
|
|
|
|
|
|
|
def unset(self):
|
|
|
|
self._state = Offer.RATE_UNSET
|