DCXferListener.py :  » Development » pyDC » pydc » pyDClib » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » pyDC 
pyDC » pydc » pyDClib » DCXferListener.py
# This file is part of pyDC software
# Copyright 2002 Anakim Border <aborder@users.sourceforge.net>
#
# pyDC is released under the terms of GPL licence.

from AsyncSocket import *
from EventGenerator import *
from Job import *
from DCQueue import *
import DCWorker
from DCXfer import DCXfer
from DCDownload import DCDownload
from DCUpload import DCUpload
from threading import Lock
from socket import *
from copy import copy
import os
import errno

class DCXferListener(object, Job, EventGenerator):
  def __init__(self, address, port):
    EventGenerator.__init__(self)

    if port == 0:
      self.sock = None
    else:
      self.sock = socket(AF_INET, SOCK_STREAM)
      self.sock.bind((address, port))
      self.sock.listen(3)
      self.sock.setblocking(0)

    self._xfers = []
    self._uploadLimit = 0
    self.uploadBps = [0, 0]
    self.status = 0
    self.opLock = Lock()

  def poll(self):
    if self.status == 0:
      if self.sock != None:
        try:
          conn, addr = self.sock.accept()
          DCWorker.getWorker().addJob(DCXfer(AsyncSocket(conn)))
        except error: pass

    elif self.status == 1:
      if self.sock != None:
        self.sock.close()
        
      self.status = 2
      return 1

    else:
      return 1
  
    return 0

  def stop(self):
    self.status = 1

  def isAlive(self):
    return self.status == 0

  def getXfers(self):
    self.opLock.acquire()
    xfers = copy(self._xfers)
    self.opLock.release()

    return xfers
    
  def stopXfer(self, item, source=None):
    self.opLock.acquire()
    for xfer in self._xfers:
      if xfer.item == item:
        if source == None or xfer.remoteNick == source.nick:
          xfer.stop()
          break
    self.opLock.release()
    
  def createDownload(self, xfer):
    DCWorker.getWorker().addJob(DCDownload(xfer))
    
  def createUpload(self, xfer):
    DCWorker.getWorker().addJob(DCUpload(xfer))

  def setUploadLimit(self, bps):
    # TODO: check bps
    self._uploadLimit = bps
    self.setUploadBps()

  def getUploadLimit(self):
    return self._uploadLimit

  def getUploadBps(self):
    if self._uploadLimit == 0: return 0

    self.opLock.acquire()
    bps = min(self.uploadBps[0] + self.uploadBps[1], self._uploadLimit)
    self.opLock.release()
    return bps

  def setUnusedUploadBps(self, bps):
    self.uploadBps[1] = max(bps, 0)

  def onNewXfer(self, xfer):
    self.opLock.acquire()
    self._xfers.append(xfer)
    self.setUploadBps()
    self.opLock.release()

    self.lock.acquire()
    for listener in self.listeners:
      listener.onNewXfer(xfer)
    self.lock.release()

  def onXferUpdate(self, xfer):
    self.lock.acquire()
    for listener in self.listeners:
      listener.onXferUpdate(xfer)
    self.lock.release()

  def onXferClosed(self, xfer):
    self.opLock.acquire()
    self._xfers.remove(xfer)
    self.setUploadBps()
    self.opLock.release()
    
    self.lock.acquire()
    for listener in self.listeners:
      listener.onXferClosed(xfer)
    self.lock.release()

  #=================#
  # Private section #
  #=================#

  def setUploadBps(self):
    uploadNum = 0
    for xfer in self._xfers:
      if xfer.getType() == DCXfer.TYPE_UPLOAD:
        uploadNum += 1

    self.uploadBps[0] = max(float(self._uploadLimit) / max(uploadNum, 1), 1)

  def isWouldBlock(self, code):
    if os.name == "posix":
      if code == errno.EAGAIN or code == errno.EALREADY or code == errno.EINPROGRESS:
        return 1
    elif os.name == "nt":
      if code == errno.WSAEWOULDBLOCK:
        return 1
        
  xfers = property(getXfers, None, None, None)
  uploadLimit = property(getUploadLimit, setUploadLimit, None, None)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.