def sendPkts(n): for i in range(0, n): network.sendtoNetwork(packet.Packet(globals.PKT_LEN, "udp", 0, 0))
def generate_beacon(self, beacon_announce_time, channel_status, max_data_size): # This function is called to generate the RAWs according to the STAs need to check whether they have alarm report, # the STAs need to collect their alarm report (i.e., these STAs have checked and the AP find they have alarm report) # and the blocks that need to be check whether they are affected by the event # Input: # beacon_announce_time--when will the beacon be announced # Output: # beacon--the result beacon frames need to be announced. import packet self.trigger_RAWs, self.collect_RAWs, self.check_RAWs = [], [], [] if self.STAs_to_check: # construct Trigger RAWs to check if each STA has the alarm report new_RAW = RAW("Trigger", True) for each_STA in self.STAs_to_check: # page all the STAs invovled new_RAW.page_STA(each_STA) self.trigger_RAWs.append(new_RAW) if self.STAs_to_poll: #construct General RAWs to collect the data from STAs new_RAW = RAW("General", True) for each_STA in self.STAs_to_poll: new_RAW.page_STA(each_STA) self.collect_RAWs.append(new_RAW) if self.blocks_to_check: #construct General RAWs to check if alarm report exist in this block for each_block in self.blocks_to_check: new_RAW = RAW_for_blocks() self.check_RAWs.append(new_RAW) self.RAWs = self.trigger_RAWs + self.collect_RAWs + self.check_RAWs # set the parameters of all the raws in this polling round beacons = [] beacon = packet.BeaconFrame(self.RAWs, self.timer, self.AP, self.STA_list) if channel_status == "Busy": # need multiple beacon frame to ensure the beacon is correctly received import math new_RAW = RAW("General", False) new_beacon = packet.BeaconFrame([new_RAW], self.timer, self.AP, self.STA_list) transmission_finish_time = packet.Packet( self.timer, "Data", size=max_data_size).transmission_delay() duration_for_beacon = self.timer.SIFS + new_beacon.transmission_delay( ) number_of_beacons = math.ceil(transmission_finish_time / duration_for_beacon) end_time_for_clearance = number_of_beacons * duration_for_beacon - self.timer.SIFS + beacon_announce_time for i in range( 1, number_of_beacons + 1): # general the required beacons to clear the channel start_time_of_RAW = beacon_announce_time + duration_for_beacon * i - self.timer.SIFS new_RAW = RAW("General", False) new_RAW.parameter_setting( start_time_of_RAW, end_time_for_clearance - start_time_of_RAW, 1, self.STA_list) new_beacon = packet.BeaconFrame([new_RAW], self.timer, self.AP, self.STA_list) beacons.append(new_beacon) start_time = end_time_for_clearance + self.timer.SIFS + beacon.transmission_delay( ) + 1 else: start_time = beacon_announce_time + beacon.transmission_delay() + 1 beacons.append(beacon) if self.trigger_RAWs: self.trigger_RAWs[0].parameter_setting( start_time, self.trigger_slot_duration * self.STAs_to_check.__len__(), self.STAs_to_check.__len__(), self.STA_list) # set the parameters of the trigger RAW start_time = self.trigger_RAWs[0].end_time if self.collect_RAWs: self.collect_RAWs[0].parameter_setting( start_time, self.data_slot_duration * self.STAs_to_poll.__len__(), self.STAs_to_poll.__len__(), self.STA_list) # set the parameters of the collect RAW start_time = self.collect_RAWs[0].end_time for i in range(self.blocks_to_check.__len__()): block = self.blocks_to_check[i] self.check_RAWs[i].parameter_setting(start_time, self.data_slot_duration, block, self.STA_list) # set the parameters of check RAW start_time = self.check_RAWs[i].end_time self.end_time = max(x.end_time for x in beacon.RAWs) return beacons
def generate_packet(): return packet.Packet(generate_service_time())
else: print('file is ' + file_name) file = open(file_name, 'r') next_value = 0 exitFlag = False buffer = "" sock_in.settimeout(1) p_counter = 0 while True: buffer = file.read(512) n = len(buffer) if n == 0: d_packet = packet.Packet(0x497E, 0, next_value, 0, "") exitFlag = True if n > 0: d_packet = packet.Packet(0x497E, 0, next_value, n, buffer) packetBuffer = pickle.dumps(d_packet) while True: print("sending '" + d_packet.data + "' to " + str(cport_num_sin) + ", packet counter is " + str(p_counter)) sock_out.sendto(packetBuffer, (HOST, cport_num_sin)) p_counter += 1 try: rcvd, addr = sock_in.recvfrom(512) rcvd = pickle.loads(rcvd) except socket.timeout: continue
print("Listening for packets...") while True: # find which sockets have data waiting to be received ready, a, b = select.select(listening, [], []) for soc in ready: if soc == c_s_in: # packet recieved from sender print("Packet recieved from sender") obj = soc.recv(packet_size) magicno, packet_type, dataLen, seqno = struct.unpack( 'iiii', obj[:16]) pack = packet.Packet(magicno, packet_type, dataLen, seqno) if validate_packet(pack): print_packet(pack) if not lose_packet(): #relay the packet to receiver print("Relaying to reciever") try: c_r_out.send(obj) except: close_sockets() sys.exit("Connection failed, reciever may be offline.") else: #drop the packet
def received_packet(self, payload_string, rssi = None): pay_load = payload.Payload().loads(payload_string) pkt = packet.Packet(self.eui, rssi, pay_load, pay_load.time) return pkt