added incoming directory support and made the server not use 100% cpu busy waiting

This commit is contained in:
jchu 2004-12-13 03:12:12 +00:00
parent e69d7b1356
commit c0b9a256aa

View file

@ -27,15 +27,18 @@ import MySQLdb.connections
import ConfigParser import ConfigParser
import getopt import getopt
import os.path import os.path
import os
import time
CACHEDIR = '/var/cache/tupkgs/' CACHEDIR = '/var/cache/tupkgs/'
INCOMINGDIR = '/var/cache/tupkgs/incomplete/'
CONFIGFILE = '/etc/tupkgs.conf' CONFIGFILE = '/etc/tupkgs.conf'
config = ConfigParser.ConfigParser() config = ConfigParser.ConfigParser()
class ClientFile: class ClientFile:
def __init__(self, filename, actual_size, actual_md5): def __init__(self, filename, actual_size, actual_md5):
self.pathname = CACHEDIR + filename self.pathname = INCOMINGDIR + filename
self.filename = filename self.filename = filename
self.fd = open(self.pathname, "w+b") self.fd = open(self.pathname, "w+b")
self.actual_size = actual_size self.actual_size = actual_size
@ -57,6 +60,13 @@ class ClientFile:
self.fd.seek(cur) self.fd.seek(cur)
self.md5 = md5sum.hexdigest() self.md5 = md5sum.hexdigest()
def finishDownload(self):
self.fd.close();
newpathname = CACHEDIR + self.filename
os.rename(self.pathname, newpathname)
self.pathname = newpathname
self.fd = open(self.pathname, "a+b")
class ClientSocket(threading.Thread): class ClientSocket(threading.Thread):
def __init__(self, sock, db, **other): def __init__(self, sock, db, **other):
threading.Thread.__init__(self, *other) threading.Thread.__init__(self, *other)
@ -142,6 +152,7 @@ class ClientSocket(threading.Thread):
i.fd.write(self.reliableRead(1024)) i.fd.write(self.reliableRead(1024))
count += 1024 count += 1024
i.fd.flush() i.fd.flush()
i.finishDownload()
reply = {'numpkgs': len(self.files)} reply = {'numpkgs': len(self.files)}
for i, v in enumerate(self.files): for i, v in enumerate(self.files):
v.makeMd5() v.makeMd5()
@ -246,6 +257,14 @@ def main(argv=None):
passwd=config.get('mysql', 'password'), passwd=config.get('mysql', 'password'),
db=config.get('mysql', 'db')) db=config.get('mysql', 'db'))
print "Verifying "+CACHEDIR+" and "+INCOMINGDIR+" exist"
if not os.path.isdir(CACHEDIR):
print "Creating "+CACHEDIR
os.mkdir(CACHEDIR, 0755)
if not os.path.isdir(INCOMINGDIR):
print "Creating "+INCOMINGDIR
os.mkdir(INCOMINGDIR, 0755)
print "Starting ServerSocket" print "Starting ServerSocket"
servsock = ServerSocket(dbconn, confdict['port'], confdict['maxqueue']) servsock = ServerSocket(dbconn, confdict['port'], confdict['maxqueue'])
servsock.start() servsock.start()
@ -253,7 +272,7 @@ def main(argv=None):
try: try:
while running: while running:
# Maybe do stuff here? # Maybe do stuff here?
pass time.sleep(10)
except KeyboardInterrupt: except KeyboardInterrupt:
running = 0 running = 0