Browse Source

add BigString amp argument to accommodate huge offer list

master
Adam Gibson 9 years ago
parent
commit
55a747059f
No known key found for this signature in database
GPG Key ID: B3AE09F1E9A3197A
  1. 64
      jmbase/bigstring.py
  2. 3
      jmbase/commands.py

64
jmbase/bigstring.py

@ -0,0 +1,64 @@
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from itertools import count
from twisted.protocols import amp
CHUNK_MAX = 0xffff
class BigString(amp.Argument):
"""
A byte-string amp.Argument with no 65,535 length limit.
Each value for a key/value pair in an AMP box may not
exceed 65,535 bytes in length. So if we *really* want to
send potentially larger values, this class will implicitly
encode/decode them to/from an arbitrary additional
number of key/value pairs that are given automatic key
names by prefixing this Argument's key name to a counter.
"""
def fromBox(self, name, strings, objects, proto):
value = StringIO()
value.write(strings.get(name))
for counter in count(2):
chunk = strings.get("%s.%d" % (name, counter))
if chunk is None:
break
value.write(chunk)
objects[name] = self.buildvalue(value.getvalue())
def buildvalue(self, value):
return value
def toBox(self, name, strings, objects, proto):
value = StringIO(self.fromvalue(objects[name]))
firstChunk = value.read(CHUNK_MAX)
strings[name] = firstChunk
counter = 2
while True:
nextChunk = value.read(CHUNK_MAX)
if not nextChunk:
break
strings["%s.%d" % (name, counter)] = nextChunk
counter += 1
def fromvalue(self, value):
return value
class BigUnicode(BigString):
"""
A unicode-string amp.Argument with no 65,535 length limit.
Each value for a key/value pair in an AMP box may not
exceed 65,535 bytes in length. So if we *really* want to
send potentially larger values, this class will implicitly
encode/decode them to/from an arbitrary additional
number of key/value pairs that are given automatic key
names by prefixing this Argument's key name to a counter.
"""
def buildvalue(self, value):
return value.decode('utf-8')
def fromvalue(self, value):
return value.encode('utf-8')

3
jmbase/commands.py

@ -5,6 +5,7 @@ messaging protocol (*not* Joinmarket p2p protocol).
Used for AMP asynchronous messages.
"""
from twisted.protocols.amp import Integer, String, Unicode, Boolean, Command
from bigstring import BigString
class DaemonNotReady(Exception):
pass
@ -78,7 +79,7 @@ class JMSetupDone(JMCommand):
arguments = []
class JMOffers(JMCommand):
arguments = [('orderbook', String())]
arguments = [('orderbook', BigString())]
class JMFillResponse(JMCommand):
arguments = [('success', Boolean()),

Loading…
Cancel
Save