Esempio n. 1
0
    def serve_client(self, client, address):
        total_time = datetime.now()
        pkt = self.wait_for_request(client, address)
        if not pkt:
            return 1
        file = SERVER_FOLDER + pkt.__get__('file')
        if os.path.isfile(file):  # if file is found on the server
            pkt = Packet(status='found')
            client.send(pkt.__dumb__())
            # Logic goes here
            seq_num = 0
            bits = 0
            f = open(file=file, mode='rb')
            data = f.read(CHUNK_SIZE)
            while data:
                bits += 8 * len(data)
                # Build packet
                pkt = Packet(data=data, seq_num=seq_num)
                # Send and check for success
                if not self.send_packet(pkt, client):
                    break
                seq_num += 1
                data = f.read(CHUNK_SIZE)

            total_time = (datetime.now() - total_time).total_seconds()
            print('Sent ' + str(bits) + ' bits, in ' +
                  str(total_time) + ' secs')

            with open('log.txt', 'a') as log:
                run_metrics = '\'\'^||^\'\'' + '\n'
                run_metrics += 'THROUGHPUT=' + str(bits / total_time) + '\n'
                log.write(run_metrics)

        else:  # if file not found, send not found packet
            pkt = Packet(status='not_found')
            client.send(pkt.__dumb__())

        print('Client disconnected, address: ', address)
        client.close()