fragmentation.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2012, Willow Garage, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 from rosbridge_library.capability import Capability
34 import math
35 
36 
38  """ The Fragmentation capability doesn't define any incoming operation
39  handlers, but provides methods to fragment outgoing messages """
40 
41  fragmentation_seed = 0
42 
43  def __init__(self, protocol):
44  # Call superclass constructor
45  Capability.__init__(self, protocol)
46 
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.
50 
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
53 
54  Since fragmentation is typically only used for very large messages,
55  this method returns a generator for fragments rather than a list
56 
57  Keyword Arguments
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.
62 
63  Returns a generator of message dict objects representing the fragments
64  """
65  # All fragmented messages need an ID so they can be reconstructed
66  if mid is None:
67  mid = self.fragmentation_seed
69 
70  serialized = self.protocol.serialize(message, mid)
71 
72  if serialized is None:
73  return []
74 
75  message_length = len(serialized)
76  if message_length <= fragment_size:
77  return [message]
78 
79  msg_id = message.get("id", None)
80 
81  expected_duration = int(math.ceil(math.ceil(message_length / float(fragment_size))) * self.protocol.delay_between_messages)
82 
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]"
84  self.protocol.log("info", log_msg)
85 
86  return self._fragment_generator(serialized, fragment_size, mid)
87 
88  def _fragment_generator(self, msg, size, mid):
89  """ Returns a generator of fragment messages """
90  total = ((len(msg)-1) / size) + 1
91  n = 0
92  for i in range(0, len(msg), size):
93  fragment = msg[i:i+size]
94  yield self._create_fragment(fragment, n, total, mid)
95  n = n + 1
96 
97  def _create_fragment(self, fragment, num, total, mid):
98  """ Given a string fragment of the original message, creates
99  the appropriate fragment message """
100  return {
101  "op": "fragment",
102  "id": mid,
103  "data": fragment,
104  "num": num,
105  "total": total
106  }
def fragment(self, message, fragment_size, mid=None)
def _create_fragment(self, fragment, num, total, mid)


rosbridge_library
Author(s): Jonathan Mace
autogenerated on Fri Oct 21 2022 02:45:18