Пример #1
0
    def stop(self):
        if not self.running:
            return

        if self.app.chat.running:
            self.display_message("waiting for overnet client to finish...\n", "grey")

        def timeout(self=self):
            result = os.waitpid(self.pid, os.WNOHANG)
            if result[0] == self.pid:
                self.lock.acquire()
                self.pid = None
                self.lock.release()
                return 0
            return 1

        utility.schedule_mainthread(250.0, timeout)

        while self.pid != None:
            self.tell("q\n")
            time.sleep(0.3)
            self.tell("y\n")
            time.sleep(0.3)

        os.close(self.shell_master)
        os.close(self.shell_slave)

        self.connected = 0
        self.running = 0
        if self.app.chat.running:
            self.display_message("overnet client stopped.\n", "grey")

        for downloader in self.app.file_server.downloaders:
            if downloader.node == self:
                downloader.stop()

        Task_manager.stop(self)
Пример #2
0
    def __init__(self, app):

        Task_manager.__init__(self)
        self.node = app.node
        self.app = app
        self.pid = None
        self.results = []
        self.key = None
        self.downloads = []
        self.running = 0
        self.connected = 0
        self.connecting = 0
        self.reading_message = 0
        self.message = ""
        self.exit = 0

        self.reading_results = 0
        self.reading_downloads = 0
        self.reading_status = 0
        self.waiting_results = 0

        self.tempdir = os.path.join(self.app.config["download"], ".edonkey_temp/")
        self.client_file = os.path.join(utility.config_dir, "overnet" + overnet_version)
        self.connected = 1
Пример #3
0
    def start(self):
        def poll_downloads(self):

            if not self.reading_results and not self.reading_status and not self.reading_downloads:
                self.lock.acquire()  #  otherwise downloads get written in results...
                self.vd()
                self.reading_downloads = 1
                self.lock.release()

            return self.running

        def read_messages_task(self):
            buffer = ""
            while self.running:
                try:
                    str = ""
                    c = ""
                    while c != "\n":
                        c = os.read(self.shell_master, 1)
                        str = str + c
                except:
                    self.running = 0
                    return

                str = string.replace(str, "\r", "")
                str = string.replace(str, "> ", "")
                str = string.replace(str, chr(7), "")

                str = unicode(str, "latin-1")
                # print "overnet:",str

                if not self.reading_downloads:
                    if self.reading_message:
                        self.message = self.message + str

                if self.reading_status:
                    self.status += str
                    if string.find(str, "Users") != -1:
                        self.reading_status = 0

                if string.find(str, "Invalid Result number") != -1:
                    raise Error("eDonkey error: " + str)

                if string.find(str, "Connected to:") != -1:
                    self.display_message("overnet client c" + str[1:], "grey")
                    if self.connecting:
                        self.reading_message = 1
                        self.message = ""
                    else:
                        self.reading_message = 0
                    self.connecting = 0
                    self.connected = 1

                elif string.find(str, "Connecting to") != -1 and self.connecting:
                    self.display_message("overnet client c" + str[1:], "grey")
                elif string.find(str, "Disconnected") != -1:
                    self.display_message("overnet client disconnected.\n", "red")
                    self.connected = 0

                if self.waiting_results:
                    if str[0:16] == "Results returned" or str[0:11] == "Got results":
                        self.waiting_results = 0

                if self.reading_results:
                    if str[0:1] == "(":
                        self.results.append(str)
                    else:
                        self.tell(" ")

                    # if str[0:11] == 'press space':
                    #    print "overnet: pressing space"
                    #    self.lock.acquire()
                    #    self.tell(' ')
                    #    self.lock.release()
                    #    #time.sleep(0.01)

                if self.reading_downloads:
                    if string.find(str, "File Name") != -1:
                        self.downloads = []

                    if string.find(str, "Total") != -1:
                        self.lock.acquire()
                        self.reading_downloads = 0
                        self.lock.release()

                        for downloader in self.app.file_server.downloaders:
                            if downloader.node == self and not downloader.neverseen:
                                downloader.lock.acquire()
                                downloader.success = 1
                                downloader.lock.release()

                        for item in self.downloads:
                            number = self.downloads.index(item)
                            has_downloader = 0
                            for downloader in self.app.file_server.downloaders:
                                if string.find(item, downloader.data["filename"]) == -1:
                                    continue
                                else:
                                    downloader.lock.acquire()
                                    downloader.number = number
                                    downloader.success = 0
                                    downloader.neverseen = 0
                                    has_downloader = 1
                                    try:
                                        downloader.bytes_downloaded = (
                                            int(self.downloads[number + 1].split("\t")[4][:-1]) * 1024
                                        )
                                        downloader.speed_kBs = float(self.downloads[number + 1].split("\t")[5])
                                        downloader.availability = self.downloads[number + 1].split("\t")[6]
                                        downloader.status = self.downloads[number + 1].split("\t")[2]
                                        downloader.lock.release()
                                    except:
                                        break

                            if not has_downloader:
                                try:
                                    data = {
                                        "keywords": ["?"],
                                        "length": int(self.downloads[number + 1].split("\t")[3][:-1]) * 1024,
                                        "type": "file",
                                        "name": "file:?",
                                        "index": "?",
                                        "filename": self.downloads[number].split(" ", 1)[1][:-2],
                                    }
                                except:
                                    data = None
                                if data:
                                    downloader = file_server.Donkey_Downloader(self, data, self.app.config["download"])
                                    downloader.lock.acquire()
                                    downloader.number = number
                                    downloader.success = 0
                                    downloader.neverseen = 0
                                    downloader.lock.release()
                                    utility.mainthread_call(self.app.download_manager.assert_visible)
                                    self.app.file_server.downloaders.append(downloader)
                                    utility.mainthread_call(downloader.start)

                    if str.find("Downloading:") != -1:
                        str = None
                    if str:
                        self.downloads.append(str)

        if self.running:
            return
        Task_manager.start(self)
        self.running = 1
        self.results = []
        self.downloads = []
        self.reading_results = 0
        self.reading_downloads = 0
        self.waiting_results = 0

        self.shell_master, self.shell_slave = os.openpty()

        import termios

        try:
            termios.ECHO
            TERMIOS = termios
        except:
            import TERMIOS

        attr = termios.tcgetattr(self.shell_master)
        attr[3] = attr[3] & ~TERMIOS.ECHO
        termios.tcsetattr(self.shell_master, TERMIOS.TCSANOW, attr)

        attr = termios.tcgetattr(self.shell_slave)
        attr[3] = attr[3] & ~TERMIOS.ECHO
        termios.tcsetattr(self.shell_slave, TERMIOS.TCSANOW, attr)

        try:
            self.pid = os.fork()
        except:
            raise Error("eDonkey : cannot fork")
            return

        if not self.pid:
            time.sleep(1)
            os.chdir(utility.config_dir)
            os.dup2(self.shell_slave, 0)
            os.dup2(self.shell_slave, 1)
            os.dup2(self.shell_slave, 2)

            os.execlp("/bin/sh", "/bin/sh", "-c", self.client_file)
            os._exit(0)

        self.set_options()
        self.connect()
        self.display_message("overnet client running.\n", "grey")

        utility.schedule_mainthread(500.0, poll_downloads, self)
        utility.Task(read_messages_task, self).start()