# Path: shapy/framework/netlink/message.py
# class Attr(object):
#     """Represents a single attribute."""
#     #struct rtattr {
#     #   unsigned short rta_len;    /* Length of option */
#     #   unsigned short rta_type;   /* Type of option */
#     #   /* Data follows */
#     #};
#     
#     rtattr = Struct('HH')
#     
#     @classmethod
#     def unpack(cls, data):
#         length, type = cls.rtattr.unpack(data[:cls.rtattr.size])
#         data_len = length-cls.rtattr.size
#         attr_data = struct.unpack('{0}s'.format(data_len),
#                              data[cls.rtattr.size:cls.rtattr.size+data_len])[0]
#         return cls(type, attr_data), data[align(length):]
#     
#     def __init__(self, rta_type, payload):
#         self.rta_type = rta_type
#         self.payload = payload
#         self.data = self.unpack_data()
#         
#     def pack(self):
#         data = struct.pack('{0}s'.format(len(self.payload)), self.payload)
#         rta_len = self.rtattr.size+len(data)
#         return struct.pack('{0}{1}s'.format(self.rtattr.format, align(len(data))),
#                            rta_len, self.rta_type, data)
#     
#     def unpack_data(self):
#         if hasattr(self, 'data_format') and hasattr(self, 'data_struct'):
#             return self.data_struct._make(self.data_format.unpack(self.payload))
#         return None
#     
#     def __repr__(self):
#         return """<rtattr datalen=%d rta_type=%d data="%s">""" % (
#             align(len(self.payload)), self.rta_type,
#             self.payload.encode('string_escape'))
# 
# Path: shapy/framework/utils.py
# def nl_ticks2us(ticks):
#     """Convert ticks to microseconds."""
#     return ticks / ticks_per_usec
# 
# def nl_us2ticks(delay):
#     """Convert microseconds to timer ticks."""
#     return int(ticks_per_usec * delay)


from struct import Struct
from collections import namedtuple
from shapy.framework.netlink.message import Attr
from shapy.framework.utils import nl_ticks2us, nl_us2ticks
from shapy.framework.netlink.constants import *





class NetemOptions(Attr):
    #struct tc_netem_qopt {
    #    __u32   latency;    /* added delay (us) */
    #    __u32   limit;      /* fifo limit (packets) */
    #    __u32   loss;       /* random packet loss (0=none ~0=100%) */
    #    __u32   gap;        /* re-ordering gap (0 for none) */
    #    __u32   duplicate;  /* random packet dup  (0=none ~0=100%) */
    #    __u32   jitter;     /* random jitter in latency (us) */
    #};
    
    data_format = Struct("6I")
    data_struct = namedtuple('tc_netem_qopt',
                             "latency limit loss gap duplicate jitter")
    
    @classmethod
    def unpack(cls, data):
        attr, rest = Attr.unpack(data)
        opts = cls.data_struct._make(cls.data_format.unpack(attr.payload))
        opts = opts._replace(latency=nl_ticks2us(opts.latency))
        return cls(*opts), rest
    
    def __init__(self, latency, limit=1000, loss=0, gap=0, duplicate=0, jitter=0):
        """Latency is in microseconds [us]"""
        latency_ticks = nl_us2ticks(latency)

# Path: shapy/framework/netlink/message.py
# class Attr(object):
#     """Represents a single attribute."""
#     #struct rtattr {
#     #   unsigned short rta_len;    /* Length of option */
#     #   unsigned short rta_type;   /* Type of option */
#     #   /* Data follows */
#     #};
#     
#     rtattr = Struct('HH')
#     
#     @classmethod
#     def unpack(cls, data):
#         length, type = cls.rtattr.unpack(data[:cls.rtattr.size])
#         data_len = length-cls.rtattr.size
#         attr_data = struct.unpack('{0}s'.format(data_len),
#                              data[cls.rtattr.size:cls.rtattr.size+data_len])[0]
#         return cls(type, attr_data), data[align(length):]
#     
#     def __init__(self, rta_type, payload):
#         self.rta_type = rta_type
#         self.payload = payload
#         self.data = self.unpack_data()
#         
#     def pack(self):
#         data = struct.pack('{0}s'.format(len(self.payload)), self.payload)
#         rta_len = self.rtattr.size+len(data)
#         return struct.pack('{0}{1}s'.format(self.rtattr.format, align(len(data))),
#                            rta_len, self.rta_type, data)
#     
#     def unpack_data(self):
#         if hasattr(self, 'data_format') and hasattr(self, 'data_struct'):
#             return self.data_struct._make(self.data_format.unpack(self.payload))
#         return None
#     
#     def __repr__(self):
#         return """<rtattr datalen=%d rta_type=%d data="%s">""" % (
#             align(len(self.payload)), self.rta_type,
#             self.payload.encode('string_escape'))
# 
# Path: shapy/framework/utils.py
# def nl_ticks2us(ticks):
#     """Convert ticks to microseconds."""
#     return ticks / ticks_per_usec
# 
# def nl_us2ticks(delay):
#     """Convert microseconds to timer ticks."""
#     return int(ticks_per_usec * delay)


# Path: shapy/framework/netlink/netem.py
from struct import Struct
from collections import namedtuple
from shapy.framework.netlink.message import Attr
from shapy.framework.utils import nl_ticks2us, nl_us2ticks
from shapy.framework.netlink.constants import *






class NetemOptions(Attr):
    #struct tc_netem_qopt {
    #    __u32   latency;    /* added delay (us) */
    #    __u32   limit;      /* fifo limit (packets) */
    #    __u32   loss;       /* random packet loss (0=none ~0=100%) */
    #    __u32   gap;        /* re-ordering gap (0 for none) */
    #    __u32   duplicate;  /* random packet dup  (0=none ~0=100%) */
    #    __u32   jitter;     /* random jitter in latency (us) */
    #};
    
    data_format = Struct("6I")
    data_struct = namedtuple('tc_netem_qopt',
                             "latency limit loss gap duplicate jitter")
    
    @classmethod
    def unpack(cls, data):
        attr, rest = Attr.unpack(data)
        opts = cls.data_struct._make(cls.data_format.unpack(attr.payload))
        opts = opts._replace(latency=nl_ticks2us(opts.latency))
        return cls(*opts), rest
    
    def __init__(self, latency, limit=1000, loss=0, gap=0, duplicate=0, jitter=0):
        """Latency is in microseconds [us]"""
        latency_ticks = nl_us2ticks(latency)

        jitter_ticks = nl_us2ticks(jitter)