Example #1
0
def filenameOUT(basename=None):
    """
    File name for outgoing stats for today or other day
    """
    if basename is None:
        basename = misc.gmtime2str("%d%m%y")
    return os.path.join(settings.BandwidthOutDir(), basename)
Example #2
0
def saveOUT(basename=None):
    """
    Writes outgoing stats for today on disk 
    """
    if basename is None:
        basename = misc.gmtime2str("%d%m%y")
    ret = os.path.isfile(filenameOUT(basename))
    dhnio._write_dict(filenameOUT(basename), getBandwidthOUT())
    if not ret:
        dhnio.Dprint(4, "bandwidth.saveOUT to new file " + basename)
    else:
        dhnio.Dprint(12, "bandwidth.saveOUT to " + basename)
    return ret
Example #3
0
def files2send():
    """
    Return a list of file names to be read and send to Central server.
    Sent files are market with ".sent" extension and skipped here.  
    """
    dhnio.Dprint(6, "bandwidth.files2send")
    listIN = []
    listOUT = []
    for filename in os.listdir(settings.BandwidthInDir()):
        # if we sent the file - skip it
        if filename.endswith(".sent"):
            continue
        # if filename is not a date - skip it
        if len(filename) != 6:
            continue
        # skip today bandwidth - it is still counting, right?
        if filename == misc.gmtime2str("%d%m%y"):
            continue
        filepath = os.path.join(settings.BandwidthInDir(), filename)
        # if filepath == filenameIN():
        #     continue
        listIN.append(filepath)
    for filename in os.listdir(settings.BandwidthOutDir()):
        if filename.endswith(".sent"):
            continue
        if len(filename) != 6:
            continue
        if filename == misc.gmtime2str("%d%m%y"):  # time.strftime('%d%m%y'):
            continue
        filepath = os.path.join(settings.BandwidthOutDir(), filename)
        # if filepath == filenameOUT():
        #     continue
        listOUT.append(filepath)
    dhnio.Dprint(6, "bandwidth.files2send listIN=%d listOUT=%d" % (len(listIN), len(listOUT)))
    for i in listIN:
        dhnio.Dprint(6, "  " + i)
    for i in listOUT:
        dhnio.Dprint(6, "  " + i)
    return listIN, listOUT
Example #4
0
def OUT(idurl, size):
    """
    Call this when need to count outgoing bandwidth.
    `size` - how many bytes sent to user with `idurl`.
    Typically called when outgoing packet were sent.
    """
    global BandOutDict
    global CountTimeOut
    if not isExistOUT():
        currentFileName = time.strftime("%d%m%y", time.localtime(CountTimeOut))
        if currentFileName != misc.gmtime2str("%d%m%y"):
            saveOUT(currentFileName)
            clear_bandwidthOUT()
            CountTimeOut = time.time()
    currentV = int(BandOutDict.get(idurl, 0))
    currentV += size
    BandOutDict[idurl] = currentV
    saveOUT()
Example #5
0
def IN(idurl, size):
    """
    Call this when need to count incoming bandwidth.
    `size` - how many incoming bytes received from user with `idurl`.
    Typically called when incoming packet arrives.
    """
    global BandInDict
    global CountTimeIn
    if not isExistIN():
        currentFileName = time.strftime("%d%m%y", time.localtime(CountTimeIn))
        if currentFileName != misc.gmtime2str("%d%m%y"):
            saveIN(currentFileName)
            clear_bandwidthIN()
            CountTimeIn = time.time()
    currentV = int(BandInDict.get(idurl, 0))
    currentV += size
    BandInDict[idurl] = currentV
    saveIN()