38 """ The Fragmentation capability doesn't define any incoming operation
39 handlers, but provides methods to fragment outgoing messages """
41 fragmentation_seed = 0
45 Capability.__init__(self, protocol)
47 def fragment(self, message, fragment_size, mid=None):
48 """ Serializes the provided message, then splits the serialized
49 message according to fragment_size, then sends the fragments.
51 If the size of the message is less than the fragment size, then
52 the original message is returned rather than a single fragment
54 Since fragmentation is typically only used for very large messages,
55 this method returns a generator for fragments rather than a list
58 message -- the message dict object to be fragmented
59 fragment_size -- the max size for the fragments
60 mid -- (optional) if provided, the fragment messages
61 will be given this id. Otherwise an id will be auto-generated.
63 Returns a generator of message dict objects representing the fragments
70 serialized = self.
protocol.serialize(message, mid)
72 if serialized
is None:
75 message_length = len(serialized)
76 if message_length <= fragment_size:
79 msg_id = message.get(
"id",
None)
81 expected_duration = int(math.ceil(math.ceil(message_length / float(fragment_size))) * self.
protocol.delay_between_messages)
83 log_msg =
"sending " + str(int(math.ceil(message_length / float(fragment_size)))) +
" parts [fragment size: " + str(fragment_size) +
"; expected duration: ~" + str(expected_duration) +
"s]"
89 """ Returns a generator of fragment messages """
90 total = ((len(msg)-1) / size) + 1
92 for i
in range(0, len(msg), size):
93 fragment = msg[i:i+size]
98 """ Given a string fragment of the original message, creates
99 the appropriate fragment message """