#!/usr/bin/env python2.7
''' The routines used for analyzing queue trace file
'''
import os

def get_enque(fname, oname):
    ''' Filter the queue trace file, only output enqueue trace 

    Args:
       fname: queue trace file 
       oname: output queue tracef file

    Returns: None
    '''
    fp = open(fname)
    data = fp.readlines()
    fp.close()
    ofp = open(oname, 'w')
    for item in data:
        if item[0] == '+':
            fp.write(item)
    fp.close()

def get_arate(fname, gran=1e-5):
    ''' Get arrival rate of a particular queue from ns2's queue trace file

    Args:
        fname: queue trace file name (just 1 queue)
        gran: sample interval when calculating throughput, in seconds

    Returns:
        A list, contains the data as: [(time, arriving rate), (time, arriving rate), ..., ]
        The arriving rate is in Mbps
    '''
    data = open(fname).readlines()
    data = [item.split() for item in data]
    etime = float(data[-1][1])
    num = int(etime/gran)
    res = [[i*gran, 0] for i in range(0, num+1)]
    for item in data:
        if item[0] != '+':
            continue
        time = float(item[1])
        size = int(item[5])
        res[int(time/gran)][1] += size
    return [(item[0], item[1]*8/gran/1000000) for item in res]

if __name__ == '__main__':
    arates = get_arate(r'ns2/que.tr')
    fp = open('ns2/throughput', 'w')
    for arate in arates:
        fp.write('%.6f %.6f\n' % (arate[0], arate[1]))
    fp.close()

@@ -86,16 +86,564 @@
 ort os%0A%0A
+def get_qlen(fname, oname):%0A    ''' Extract queue length information%0A    %0A    Extract timestamp, queue length in bytes and queue length in packets%0A    from ns2 queue length trace file.%0A%0A    Args:%0A        fname: queue trace file%0A        oname: output file%0A    ''' %0A%0A    fp = open(fname)%0A    data = fp.readlines()%0A    data = %5Bitem.split() for item in data%5D%0A    fp.close()%0A    ofp = open(oname, 'w')%0A    for item in data:%0A        ofp.write('%2512.9f %2510.3f %256.1f%5Cn' %25 %5C%0A                (float(item%5B0%5D), float(item%5B3%5D), float(item%5B4%5D)))%0A    ofp.close()%0A%0A
 def get_

add que length