# 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_us2ticks(delay):
#     """Convert microseconds to timer ticks."""
#     return int(ticks_per_usec * delay)


import os
from shapy.framework.netlink.message import Attr
from .constants import *
from struct import Struct
from shapy.framework.utils import nl_us2ticks

class HTBQdiscAttr(Attr):
    """Representation of HTB qdisc options."""
    #struct tc_htb_glob {
    #    __u32 version;      /* to match HTB/TC */
    #    __u32 rate2quantum; /* bps->quantum divisor */
    #    __u32 defcls;       /* default class number */
    #    __u32 debug;        /* debug flags */
    #
    #    /* stats */
    #    __u32 direct_pkts; /* count of non shapped packets */
    #};
    
    data_format = Struct('6I')
    
    def __init__(self, defcls, r2q=10):
        data = self.data_format.pack(0x20018, 3, r2q, defcls, 0, 0)
        Attr.__init__(self, TCA_OPTIONS, data)



# 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_us2ticks(delay):
#     """Convert microseconds to timer ticks."""
#     return int(ticks_per_usec * delay)


# Path: shapy/framework/netlink/htb.py
import os
from shapy.framework.netlink.message import Attr
from .constants import *
from struct import Struct
from shapy.framework.utils import nl_us2ticks


class HTBQdiscAttr(Attr):
    """Representation of HTB qdisc options."""
    #struct tc_htb_glob {
    #    __u32 version;      /* to match HTB/TC */
    #    __u32 rate2quantum; /* bps->quantum divisor */
    #    __u32 defcls;       /* default class number */
    #    __u32 debug;        /* debug flags */
    #
    #    /* stats */
    #    __u32 direct_pkts; /* count of non shapped packets */
    #};
    
    data_format = Struct('6I')
    
    def __init__(self, defcls, r2q=10):
        data = self.data_format.pack(0x20018, 3, r2q, defcls, 0, 0)
        Attr.__init__(self, TCA_OPTIONS, data)



class HTBParms(Attr):