#after connection lost revert back to idle and ask for input


#function for gather input
def input():
    reqType = raw_input("Get or Put?")
    fileName = raw_input)"Which File?")
    

#define the header

# Create test header
testhdr = Header()
testhdr.datatype = "PUT"
testhdr.filesize = "X"
testhdr.numberofpackets = "1"
testhdr.sequencenumber = "1"
testhdr.packetsize = "100"
testhdr.timetolive = "5"
testhdr.options = "charlie_test.py"
head = testhdr.Write()

print repr(head)

#open file to read
fp = open(testhdr.options, "r")
#read the file and store into dict with index
putDict = {}
index=0
while True:
    putDict[index] = fp.read(msgSize)
Exemplo n.º 2
0
def filePut(reqHead):
    #define global variables
    global state
    global fileDict
    global putFile
    global messageSize
    global serverAddr
    global emptyhdr
    
    fileDict = {}
    #send request to server
    messageSend = reqHead
    print "header built. Sending PUT request to server..."
    clientSocket.sendto(messageSend, serverAddr)
    #wait for ACK regarding request
    #check and read the socket
    message=sockReadPut(messageSend)
    #if message is recieved split message to hader and data to check ACK
    data, index, filename, messageType = parseMessage(message)
    #check for ack
    if messageType == "ACK":
        #open file and break it up into fileDict
        print "Server received request. Preparing file for transport..."
        #read file and populate dictionary with it
    elif (len(message) == 0):
        print "Server Timed out. Closing application."
        return
    
    
    #brians way of doing things---------------------------------
        
    #open file and get relevant data out of it for header
    print putFile
    FILE = open(putFile, "rb")
    fileData = FILE.read()
    
    filebytes = len(fileData)
    filepackets = filebytes/messageSize
    packetsize = 100
    FILE.close()


    # STOP-AND-WAIT needed here: Send file parts
    #set up sequence number for sending data
    seqNumber = 1
    #for i (start at 0) to end of file (i increments by message size
    for i in range(0, filebytes+1, messageSize):
        print ('Building packet [%d]' % i)
        #Create data header
        dataHDR = Header()
        dataHDR.datatype = "DATA"
        dataHDR.filesize = str(filebytes)
        dataHDR.numberofpackets = str(filepackets)
        dataHDR.packetsize = str(packetsize)
        dataHDR.timetolive = "5"
        dataHDR.options = putFile
        dataHDR.sequencenumber = str(seqNumber)
        dataheader = dataHDR.Write()
        #match data types just incase using dataheader doesnt work
        headerBytes = bytearray(dataheader)
        
        # Create the message sequence and join with the header
        #fileOBJ = open(filepath)
        #open the file > step fid in the file where i is (0 then 60 then 120....)
        with open(putFile, "rb") as binary_file:
            #Seek to specified position
            binary_file.seek(i)
            #create message with dataheader and reading msgSize bytes from file
            messageSend = dataheader + binary_file.read(messageSize)
                        
            # Send message sequence
            print ('sending packet [%d]...' % seqNumber)
            clientSocket.sendto(messageSend, serverAddr)
                    
            #STOP-AND-WAIT
            print ('checking for ACK for packet [%d]' % i)
            serverMessage = sockReadPut(messageSend)
            #check if it is an ACK - parse message first
            data, index, filename, messageType = parseMessage(message)
            #if ack is for correct sequence number increment seq num and start the loop over again
            if messageType == "ACK":
                seqNumber = seqNumber + 1
                print ('Packet [%d] succesfully sent!' % i)
            #if not ack for correct sequence number restart loop at current sequence number
            #if there was a total timeoutleave this loop - check again in the for loop
            elif len(serverMessage) == 0:
                break
                #file closes automatically with with loop type
    #if there was a total timeout leave the for loop        
		if len(serverMessage) == 0:
			break                  
    #Send EOF (empty data message with dummt header
  
    sentEOF = clientSocket.sendto(emptyhdr.Write() + "", serverAddr)