def fileReceiver(): serverSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) serverSocket.bind(('', 10080)) Pbuf = MyPacket.PacketBuffer(0) rhandler = MyPacket.RecvHandler(Pbuf, serverSocket) rhandler.recv(writePkt, writeAck, writeEnd, 2048) rhandler.write()
def fileSender(recvAddr, windowSize, srcFilename, dstFilename): clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) clientSocket.connect((recvAddr, 10080)) logfile = open(dstFilename + '_sending_log.txt', 'w') Pbuf = MyPacket.PacketBuffer(windowSize) shandler = MyPacket.SendHandler(Pbuf, clientSocket, logfile) thSend = Thread(target=shandler.sendto, args=((recvAddr, 10080), writePkt)) thAck = Thread(target=shandler.check_acked, args=(writeAck, )) thTimer = Thread(target=shandler.timer, args=(writeAck, writeEnd)) firstpacket = MyPacket.Packet((dstFilename + ':' + shandler.myaddr[0] + ';' + str(shandler.myaddr[1])).encode(), False, 0) shandler.buf.append(firstpacket) filedata = open(srcFilename, 'rb') ackNum = 1 retdata = None while True: retdata = filedata.read(1024) if not retdata: shandler.buf[-1].end = True break shandler.buf.append(MyPacket.Packet(retdata, False, ackNum)) ackNum += 1 filedata.close() thSend.start() thAck.start() thTimer.start() thSend.join() thAck.join() thTimer.join() i = 0 totalRTT = 0 length = shandler.length print("[SENDER] goodput : %f, avgRTT : %f" % (shandler.goodput, shandler.avgRTT)) writeEnd(logfile, shandler.goodput, shandler.avgRTT * 1000) logfile.close() clientSocket.close()
def client_bgp(threadName, neighbor): while True: cs = socket.socket(socket.AF_INET, socket.SOCK_STREAM) d("Connecting to %s..." % neighbor["ip"]) try: cs.connect((neighbor["ip"], PORT_NUM)) d("Connected to : %s" % neighbor["ip"]) neighbor.update({"socket": cs}) while(True): pkt = MyPacket.encode(KEY_TYPE_REQUEST, thisNet, thisSub, [autoSys], neighbor["linkCost"]) #d("sending:" + ByteToHex(pkt)) cs.send(pkt) time.sleep(INTERVAL) except socket.error, e: print "Socket Error: %s" % str(e) cs.close()
def sendEot(connection, seqNum): print "Sending FIN" eot = MyPacket.mypacket(3, seqNum, None, config.windowSize, None) eot = pickle.dumps(eot) connection.sendall(eot)
cachePacket = [] while True: # loops through the window if eotSent == False: for i in xrange(config.windowSize): if resend: # temp = pickle.loads(cachePacket[i]) # print "resending packets: %s" % (temp.seqNum) time.sleep(0.01) socketT.sock.sendall(cachePacket[i]) else: read_data = f.read(1) # pack data in packet print 'sending packet. Sequence Number: %s. Type: %s' % (seqNum, 'Data') packet = MyPacket.mypacket(2, seqNum, read_data, config.windowSize, None) seqNum += 1 # encode packet packet = pickle.dumps(packet) # put packets in the cache array cachePacket.append(packet) # send encoded packet socketT.sock.sendall(packet) # reset variable after for loop resend = False try: # set socket timeout for receiving ack socketT.sock.settimeout(0.01) # wait and receive ack
while True: recvBuffer = conn.recv(config.BUFFER_SIZE) if not recvBuffer: break # if there are data if recvBuffer: # decode data recvPacket = pickle.loads(recvBuffer) print "Packet type: ", recvPacket.type # if eot received if recvPacket.type == 3: print "FIN received" # send FIN ACK to transmitter finAck = pickle.dumps(MyPacket.mypacket(3, 1, None, config.windowSize, ackNum+1)) print "sending FIN ACK" conn.sendall(finAck) print "connection closed" # close connection conn.close() break # assign variables data = recvPacket.data seqNum = recvPacket.seqNum ackNum = seqNum + 1 print "packet %s received" % (seqNum) # this packet is not in the buffer window if seqNumInArray(buffWin, seqNum) == False:
def server_bgp(threadName, conn, addr): while True: #buf = bytearray(BUFFER_SIZE) #recved = conn.recv_into(buf, BUFFER_SIZE) buf = None try: buf = conn.recv(BUFFER_SIZE) except socket.error, e: print "Socket Error: %s" % str(e) #d("Received: %s" % ByteToHex(buf)) recved = 0 if(buf is not None): recved = len(buf) if(recved > 19): (dataType, network, subnet, linkCost, pathVector) = MyPacket.decode(buf) if dataType == KEY_TYPE_REQUEST: neighbor = findNeighborByIp(addr[0]) if neighbor is None: neighbors.append({"ip" : addr[0], "age": AGE_LIFE, "socket": conn}) else: neighbor.update({"age" : AGE_LIFE}) if determineLoop(pathVector): d("Loop detected. Ignoring...") continue print "Received: net:%s sub:%s path:%s" %(network, subnet, pathVector) # Update Routing Table! forward = "" if(pathVector[0] == pathVector[-1]): forward = "Direct" else: forward = "Routed" routingRow = {"network":network, "subnet":subnet, "AS":pathVector[-1], "neighbor": pathVector[0], "linkCost": linkCost, "forward": forward} if(len(routingTable) == 0): routingTable.append(routingRow) else: newcomer = True for route in routingTable: if(route["network"] == network): newcomer = False #Updates the route only if the cost is smaller to make Shortest path! if(route["linkCost"] > linkCost): route["linkCost"] = linkCost route.update(routingRow) d("Link cost is updated with smaller one") if(newcomer): routingTable.append(routingRow) # Finished! Displaying Routing Table displayRoutingTable() #Now we need to add this AS first line and send it to other neighbors #Only floods to the other neighbors if it is set flooding or #the network is not in excluding list!! if(flooding and network not in excluding): pathVector.insert(0, autoSys) for neighbor in neighbors: if(neighbor["ip"] == addr[0]): continue elif "socket" in neighbor: s = neighbor["socket"] pkt = MyPacket.encode(KEY_TYPE_REQUEST, network, subnet, pathVector, linkCost) s.send(pkt) if(recved == 0): break