예제 #1
0
def sync_file(groupname, filename, HOST, PORT):
    blocksize = settings.BLOCKSIZE
  
    s = socket (AF_INET, SOCK_STREAM)
    tryCount = 0
    debugLog("TRYING TO CONNECT")
    while tryCount < settings.TRY_COUNT:
        try:
            s.connect((HOST, PORT))
            debugLog("OK")
            break
        except:
            debugLog("FAILED")
            tryCount += 1
        time.sleep(0.1)
            
    if tryCount > 9:
        return
        
    debugLog("CONNECTED")
     
    fr = open(filename, "rb")

    #sending bllocksize
    s.send (str(blocksize))
    s.recv(1)
    
    while 1:
        # read file
        filecontent = fr.read(blocksize)

        # sending data
        s.send(filecontent)
        s.recv(1)
        
        if len(filecontent) < blocksize:
            break
    
    fr.close()
    s.close()
    
    logActivity(groupname, "addFile", filename, "remote")
예제 #2
0
def sync_receiver(groupname, fileName, HOST, PORT):
  
    s = socket(AF_INET, SOCK_STREAM)
    s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    s.bind((HOST, PORT))
    s.listen(1)
    
    try:
        conn, addr = s.accept()
        debugLog("CONNECTION OPENED")
        
        # file writer -> creates file with name <fileName> and sets it writable
        fw = file(fileName, "wb")
            
        # receiving blocksize
        blocksize = int (conn.recv(32))
        conn.send("1")  
        
        # file content puffer
        filecontent = ''
        # receiving data
        debugLog("receiving file content..")
        
        while 1:
            data = conn.recv(blocksize)
            conn.send("1")
            debugLog(data)
            filecontent += data
            if len(data) < blocksize:
                break
        
        debugLog("done")
       
        # writing data to file
        fw.write(filecontent)
        conn.close()
        fw.close()
    finally:
        s.close()
    
    logActivity(groupname, "addFile", fileName, "local")