fragmentation.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 from rosbridge_library.capability import Capability
00034 
00035 
00036 class Fragmentation(Capability):
00037     """ The Fragmentation capability doesn't define any incoming operation
00038     handlers, but provides methods to fragment outgoing messages """
00039     
00040     fragmentation_seed = 0
00041 
00042     def __init__(self, protocol):
00043         # Call superclass constructor
00044         Capability.__init__(self, protocol)
00045 
00046     def fragment(self, message, fragment_size, mid=None):
00047         """ Serializes the provided message, then splits the serialized
00048         message according to fragment_size, then sends the fragments.
00049         
00050         If the size of the message is less than the fragment size, then
00051         the original message is returned rather than a single fragment
00052         
00053         Since fragmentation is typically only used for very large messages,
00054         this method returns a generator for fragments rather than a list
00055         
00056         Keyword Arguments
00057         message       -- the message dict object to be fragmented
00058         fragment_size -- the max size for the fragments
00059         mid           -- (optional) if provided, the fragment messages
00060         will be given this id.  Otherwise an id will be auto-generated.
00061 
00062         Returns a generator of message dict objects representing the fragments
00063         """
00064         # All fragmented messages need an ID so they can be reconstructed
00065         if mid is None:
00066             mid = self.fragmentation_seed
00067             self.fragmentation_seed = self.fragmentation_seed + 1
00068             
00069         serialized = self.protocol.serialize(message, mid)
00070         
00071         if serialized is None:
00072             return []
00073         
00074         message_length = len(serialized)
00075         if message_length <= fragment_size:
00076             return [message]
00077         
00078         return self._fragment_generator(serialized, fragment_size, mid)
00079     
00080     def _fragment_generator(self, msg, size, mid):
00081         """ Returns a generator of fragment messages """
00082         total = ((len(msg)-1) / size) + 1
00083         n = 0
00084         for i in range(0, len(msg), size):
00085             fragment = msg[i:i+size]
00086             yield self._create_fragment(fragment, n, total, mid)
00087             n = n + 1
00088     
00089     def _create_fragment(self, fragment, num, total, mid):
00090         """ Given a string fragment of the original message, creates
00091         the appropriate fragment message """
00092         return {
00093             "op": "fragment",
00094             "id": mid,
00095             "data": fragment,
00096             "num": num,
00097             "total": total
00098         }


rosbridge_library
Author(s): Jonathan Mace
autogenerated on Thu Jan 2 2014 11:53:35