def receiveFromSender(): while True: print(15 * '-') # Receive the frame from the sender stored_frame = sockSenderReceive.recv(1024).decode() print('Frame received from sender ' + stored_frame) # Insert error and other stuffs here # Send frame with a probability p p = random.randint(0, probas) if (p >= randSendF or len(stored_frame) < 8): # Ending marker should always be sent # Introduce error here with a probability p2 = random.randint(0, probas) if (p2 <= randErrF and len(stored_frame) >= 8): print("Introducing error") stored_frame = co.ins_error(stored_frame, [1]) # Send the frame to the receiver print('Sending frame to receiver ' + stored_frame) # Add sleep here # time.sleep(7) co.send_frame(stored_frame, receiverSend) print('Sent frame ' + stored_frame) if (stored_frame == '#'): break # Dont Send the frame else: print('Not sending frame') print(15 * '-')
def send_frame(frames_list): i = 0 while (True): # Sense the channel and check if flag is 1 then dont send if (busy_channel == 0): # Channel is free # Send the frame with a probability p pr = random.randint(0, p_prob) if (pr <= p): # Send the frame print('Sending frame ' + str(i)) co.send_frame(frames_list[i], s_send) if (frames_list[i] != '_'): i = i + 1 else: i = 0 time.sleep(3) else: print('Waiting for ' + str(timeSlot)) time.sleep(timeSlot) continue else: # Channel is busy print('Channel : Busy') time.sleep(2) continue
def receiveAckFromReceiver(): while True: print(15 * '=') # Wait ack from receiver ack = sockReceiverReceive.recv(1024).decode() print('Ack received from receiver ' + ack) if (ack == '#'): break # Insert error and other stuffs here # send the frame with a probability p = random.randint(0, probas) if (p >= randSendAck): # Introduce error here with a probability p2 = random.randint(0, probas) if (p2 <= randErrAck): print("Introducing error") ack = co.ins_error(ack, [1]) print('Sending ack to sender ' + ack) # Add sleep here # time.sleep(7) # Send the ack to the sender co.send_frame(ack, senderSend) else: print('Not sending acknowledgement') print(15 * '=')
def sendFrame(list_of_frames): global sn global stored_buffer global sw i = 0 while True: # Store_frame(sn) stored_frame = list_of_frames[i] print('sn: ' + str(sn) + ' sf: ' + str(sf) + ' sw: ' + str(sw)) if (stored_frame != '#'): stored_frame = co.prepare_frame_gbn(list_of_frames[i], (i % (co.window_size + 1))) # Else send the blank frame # If window size reached wait if ((sn - sf) < sw): # Sendframe(sn) stored_buffer[sn % sw] = stored_frame print('Sending frame ' + str(i) + ' ' + stored_frame) co.send_frame(stored_frame, sender) sn = (sn + 1) if (i < len(list_of_frames) - 1): i = i + 1 else: print('Window Size full') print(stored_buffer) time.sleep(5)
def receive(): # Establish connection global buffer global bufferStatus global maxLen global gCounter sockRec = co.createSocket(co.portReceiverSend) c, addr = co.allowConn(sockRec) sockSend = co.createConn(co.portReceiverReceive) print('Connected to channel') # Connection established rn = 0 while True: # Wait till frame received print(15 * '-') frame = sockSend.recv(1024).decode() print('Frame received ' + frame) if (frame != '#' and not isValid( frame, rn)): # wrong frame no received send ack for prev print('Invalid frame.Not sending ack') elif (frame != '#'): ackno = frame[0:3] ackNo = int(ackno, 2) buffer[ackNo] = frame[3:7] bufferStatus[ackNo] = gCounter # Send an acknowledgement ack = co.generateAck_gbn(ackno) time.sleep(5) # Send the ack print('Sending ack ' + ack) co.send_frame(ack, c) print("DEBUG:", sum(bufferStatus), maxLen * gCounter) if (sum(bufferStatus) >= maxLen * gCounter): file = open("output.txt", "a") print("".join(buffer), end='', file=file) file.close() rn = (rn + 1) % (co.window_size + 1) buffer = co.window_size * [''] gCounter = gCounter + 1 else: # For valid frame maxLen = maxLen - 1 ack = '#' time.sleep(5) # Send the ack print('Sending ack ' + ack) co.send_frame(ack, c) print(str(bufferStatus)) print(str(buffer)) print(15 * '-') # Close the sockets sockSend.close() sockRec.close()
def send_signal(s, saddr): signal = 0 while (True): if (len(co.shared_buffer) >= 1): # Channel is busy signal = 1 else: # Channel not busy signal = 0 # Send the signal via socket co.send_frame(str(signal), s)
def send_to_receiver(receive): while (True): # If buffer not empty send the frame and clear buffer if (len(co.shared_buffer) > 0): # Send the frame print('Sending frame to receiver ') time.sleep(4) co.send_frame(co.shared_buffer[0], receive) del co.shared_buffer[0] else: continue
def resendFrameAfterTimeout(list_of_frames): global sn global sf global stored_buffer # Resend frame temp=sf print('Resending frames') while(temp<sn): if(stored_buffer[temp%sw]!=''): print('Resending frame '+str(temp%(sw))+' '+stored_buffer[temp%sw]) co.send_frame(stored_buffer[temp%sw], sender) time.sleep(5) temp=(temp+1)
def receive(): # Establish connection sockRec = co.createSocket(co.portReceiverSend) c, addr = co.allowConn(sockRec) sockSend = co.createConn(co.portReceiverReceive) print('Connected to channel') # Connection established rn = 0 while True: # Wait till frame received print(15 * '-') frame = sockSend.recv(1024).decode() print('Frame received ' + frame) if (frame == '#'): ack = '#' else: if (isValid(frame, rn) == 0): # wrong frame no received send ack for prev print('Invalid frame') print('Sending ack for previous frame') ackno = (rn) % 2 elif (isValid(frame, rn) == 1): print('Error in frame') continue else: # Valid frame ackno = (rn + 1) % 2 # For valid data frame rn = (rn + 1) % 2 # Send an acknowledgement ack = co.generateAck(ackno) # Send the ack print('Sending ack ' + ack) co.send_frame(ack, c) if (frame == '#'): # Means end frame break print(15 * '-') # Close the sockets sockSend.close() sockRec.close()
def send_frame(list_of_frames): i=0 k=0 while(True): # Sense the channel and check if flag is 1 then dont send if(isBusyChannel==0): # Channel is free # Send the frame with a probability p pr=random.randint(0,probab) if(pr<=p): # Send the frame print('Sending frame '+str(i)) co.send_frame(list_of_frames[i], sockSend) # Wait for signal whether transmission successfull or collision isCollision=sockSend.recv(1024).decode() if(isCollision=='0'): # No collision successfull transmission print('Sent successfully') if(list_of_frames[i]!='#'): i=i+1 k=0 else: # If collision detected print('Frame collision detected..resending') k=k+1 # If max number of attempts exceeded if(k>kmax): print('Max attempts for resending exceeded discarding frame') i=i+1 k=0 continue r=random.randint(0,2**k-1) waitTime=r*timeSlot time.sleep(waitTime) time.sleep(3) else: print('Waiting '+str(timeSlot)) time.sleep(timeSlot) continue else: # Channel is busy print('Channel busy') time.sleep(2) continue
def send_all(list_of_frames): sockSend = co.createSocket( co.portSenderSend) # sender socket to send data to channel c, addr = co.allowConn(sockSend) sockRec = co.createConn( co.portSenderReceive) # Socket to receive data from channel sockRec.settimeout(timeoutTime) print('Connected to channel') # implementing stop and wait arq sn = 0 i = 0 while (i < (len(list_of_frames))): # While there are frames send print(15 * '-') canSend = True sn = (i) % 2 stored_frame = list_of_frames[i] if (stored_frame != '#'): stored_frame = co.prepare_frame(list_of_frames[i], sn) print('Sending frame ' + str(i) + ' ' + stored_frame) co.send_frame(stored_frame, c) canSend = False try: ack = sockRec.recv(1024).decode() except Exception as e: # Resend so repeat this iteration of loop print('Timeout.. Resending') continue if (ack == '#'): break print('Ack received ' + ack) if (ack and isValid(ack, sn)): # Wrong acknowledgement print('Correct ack received') canSend = True i = i + 1 elif (not isValid(ack, sn)): # invalid ack so resend print('Wrong ack.. resending') print(15 * '-') # Close the sockets sockSend.close() sockRec.close()
def send_frame(frames_list): i = 0 while (True): # Sense the channel and check if flag is 1 then dont send time.sleep(2) if (busy_channel == 0): # Channel is free print('Sending frame ' + str(i)) co.send_frame(frames_list[i], s_send) if (frames_list[i] != '_'): i = i + 1 else: i = 0 else: # Channel is busy print('Channel : Busy State') continue
def send_frame(list_of_frames): i = 0 while (True): # Sense the channel and check if flag is 1 then dont send time.sleep(2) if (isBusyChannel == 0): # Channel is free print('Sending frame ' + str(i)) co.send_frame(list_of_frames[i], sockSend) if (list_of_frames[i] != '_'): i = i + 1 else: i = 0 else: # Channel is busy print('Channel busy') continue
def send_frame(list_of_frames): i = 0 while (True): # Sense the channel and check if flag is 1 then dont send time.sleep(2) if (isBusyChannel == 0): # Channel is free print('Sending frame ' + str(i)) co.send_frame(list_of_frames[i], sockSend) if (list_of_frames[i] != '_'): i = i + 1 else: i = 0 else: # Channel is busy idealTime = random.randint(1, 5) print('Channel busy: Sleeping for {} seconds'.format(idealTime)) time.sleep(idealTime) continue
def receive_from_sender(c, addr): # Receive data from the sender and keep it in stored buffer print('Started new connection to ' + str(addr)) while (True): # Receive data from sender frame = c.recv(1024).decode() threadLock.acquire() co.shared_buffer.append(frame) print(co.shared_buffer) threadLock.release() time.sleep(10) if (len(co.shared_buffer) > 1): # Channel is busy co.send_frame('1', c) # Clear the list co.shared_buffer.clear() else: # Channel not busy co.send_frame('0', c)
def send_frame(list_of_frames): i = 0 while (True): # Sense the channel and check if flag is 1 then dont send if (isBusyChannel == 0): # Channel is free # Send the frame with a probability p pr = random.randint(0, probab) if (pr <= p): # Send the frame print('Sending frame ' + str(i)) co.send_frame(list_of_frames[i], sockSend) if (list_of_frames[i] != '#'): i = i + 1 time.sleep(3) else: print('Waiting ' + str(timeSlot)) time.sleep(timeSlot) continue else: # Channel is busy print('Channel busy') time.sleep(2) continue
def send_frame(list_of_frames): i = 0 global persistent_type while (True): # Sense the channel and check if flag is 1 then dont send if persistent_type == '1': if (isBusyChannel == 0): print('Sending frame ' + str(i)) co.send_frame(list_of_frames[i], sockSend) if (list_of_frames[i] != '#'): i = i + 1 else: break time.sleep(3) else: print('Channel busy') continue elif persistent_type == 'Non': if (isBusyChannel == 1): t = random.randint(1, 4) print("Waiting ", t) time.sleep(t) else: print('Sending frame ' + str(i)) co.send_frame(list_of_frames[i], sockSend) if (list_of_frames[i] != '#'): i = i + 1 else: break time.sleep(3) else: if (isBusyChannel == 0): # Channel is free # Send the frame with a probability p pr = random.randint(0, probab) * 0.01 if (pr <= p): # Send the frame print('Sending frame ' + str(i)) co.send_frame(list_of_frames[i], sockSend) if (list_of_frames[i] != '#'): i = i + 1 else: break time.sleep(3) else: print('Waiting ' + str(timeSlot)) time.sleep(timeSlot) continue else: # Channel is busy print('Channel busy') time.sleep(2) continue
# Send frame with a probability p p = random.randint(0, probas) print(p) if (p >= randSendF or len(stored_frame) < 8): # Ending marker should always be sent # Introduce error here with a probability p2 = random.randint(0, probas) if (p2 <= randErrF and len(stored_frame) >= 8): print("Introducing error") stored_frame = co.ins_error(stored_frame, [1]) # Send the frame to the receiver print('Sending frame to receiver ' + stored_frame) # Add sleep here time.sleep(2) co.send_frame(stored_frame, receiverSend) print('Sent frame ' + stored_frame) # Dont Send the frame else: print('Not sending frame') continue try: # Wait ack from receiver ack = sockReceiverReceive.recv(1024).decode() print('Ack received from receiver ' + ack) except Exception as e: # Resend so repeat this iteration of loop print('Timeout.. waiting for sender to send') continue