def run(self): # parameters Sim.scheduler.reset() # setup network net = Network('experiment.txt') net.loss(self.loss) # setup routes n1 = net.get_node('n1') n2 = net.get_node('n2') n1.add_forwarding_entry(address=n2.get_address('n1'), link=n1.links[0]) n2.add_forwarding_entry(address=n1.get_address('n2'), link=n2.links[0]) # setup transport t1 = Transport(n1) t2 = Transport(n2) # setup application a = AppHandler(self.filename) # setup connection c1 = TCP(t1, n1.get_address('n2'), 1, n2.get_address('n1'), 1, a, window=self.window, fast_retransmit=self.fast_retransmit, measure=True) TCP(t2, n2.get_address('n1'), 1, n1.get_address('n2'), 1, a, window=self.window, fast_retransmit=self.fast_retransmit, measure=True) # send a file with open(self.filename, 'rb') as f: while True: data = f.read() if not data: break Sim.scheduler.add(delay=0, event=data, handler=c1.send) Sim.scheduler.run() result_file = open("results.txt", "r") results = result_file.read() result_file.close() f = open("experiment.csv", "a") f.write(str(self.window) + "," + results + "\n")
def run(self): # parameters Sim.scheduler.reset() Sim.set_debug('AppHandler') Sim.set_debug('TCP') # setup network net = Network('network.txt') net.loss(self.loss) # setup routes n1 = net.get_node('n1') n2 = net.get_node('n2') n1.add_forwarding_entry(address=n2.get_address('n1'), link=n1.links[0]) n2.add_forwarding_entry(address=n1.get_address('n2'), link=n2.links[0]) # setup transport t1 = Transport(n1) t2 = Transport(n2) # setup application a = AppHandler(self.filename) # setup connection c1 = TCP(t1, n1.get_address('n2'), 1, n2.get_address('n1'), 1, a, window=self.window, fast_retransmit=self.fast_retransmit) c2 = TCP(t2, n2.get_address('n1'), 1, n1.get_address('n2'), 1, a, window=self.window, fast_retransmit=self.fast_retransmit) # send a file with open(self.filename, 'rb') as f: while True: data = f.read(10000) if not data: break Sim.scheduler.add(delay=0, event=data, handler=c1.send) # run the simulation Sim.scheduler.run() if self.print_queueing_delay: print('average queueing delay: {}'.format( c2.total_queueing_delay / c2.total_packets_received))
def __init__(self, ipAddress): self.sq = Queue() self.rq = Queue() self.struct = Struct('BBbB') #command, selector, value, checksum self.sender = TCP(isServer=False, isSender=True, host=ipAddress, port=1234, q=self.sq) self.receiver = TCP(isServer=False, isSender=False, host=ipAddress, port=5678, q=self.rq)
def run(self): # parameters Sim.scheduler.reset() Sim.set_debug('AppHandler') Sim.set_debug('TCP') Sim.set_debug('Plot') # setup network net = Network('basic.txt') net.loss(self.loss) # setup routes n1 = net.get_node('n1') n2 = net.get_node('n2') n1.add_forwarding_entry(address=n2.get_address('n1'), link=n1.links[0]) n2.add_forwarding_entry(address=n1.get_address('n2'), link=n2.links[0]) # setup transport t1 = Transport(n1) t2 = Transport(n2) # setup application a = AppHandler(self.filename) # setup connection c1 = TCP(t1, n1.get_address('n2'), 1, n2.get_address('n1'), 1, a, window=self.window) TCP(t2, n2.get_address('n1'), 1, n1.get_address('n2'), 1, a, window=self.window) # send a file with open(self.filename, 'rb') as f: while True: data = f.read() if not data: break Sim.scheduler.add(delay=0, event=data, handler=c1.send) Sim.scheduler.run()
def run(self): # parameters Sim.scheduler.reset() Sim.set_debug('AppHandler') Sim.set_debug('TCP') Sim.set_debug('Plot') # setup network net = Network('./networks/one-hop.txt') net.loss(self.loss) # setup routes n1 = net.get_node('n1') n2 = net.get_node('n2') n1.add_forwarding_entry(address=n2.get_address('n1'), link=n1.links[0]) n2.add_forwarding_entry(address=n1.get_address('n2'), link=n2.links[0]) # setup transport t1 = Transport(n1) t2 = Transport(n2) # setup application a = AppHandler(self.filename) # setup connection c1 = TCP(t1, n1.get_address('n2'), 1, n2.get_address('n1'), 1, a, window=self.window, drop=self.drops) c2 = TCP(t2, n2.get_address('n1'), 1, n1.get_address('n2'), 1, a, window=self.window, drop=self.drops) # setup fast retransmit if self.fast_retransmit: c1.set_fast_retransmit_enabled(True) c2.set_fast_retransmit_enabled(True) # send a file with open(self.filename, 'rb') as f: while True: data = f.read(1000) if not data: break Sim.scheduler.add(delay=0, event=data, handler=c1.send) # run the simulation Sim.scheduler.run()
def __init__(self, camera, pnts_pattern): self.rep = Representation() self.h = Homography(pnts_pattern) self.tcp = TCP() self.a = Angle() self.camera = camera self.hom = np.zeros((3, 3)) self.inv_hom = np.zeros((3, 3)) self.Frame = np.zeros((3, 3)) self.inv_Frame = np.zeros((3, 3)) self.hom_final_camera = np.zeros((3, 3))
def main(url): parsed = urlparse(url) # exit on unsupported application protocol if parsed.scheme.lower() != 'http': print '"' + parsed.scheme + '"' + ' scheme address is not supported' sys.exit() host = parsed.netloc filename = getFilename(parsed.path) request = getRequest(parsed.path, host) print filename print request # Initiate the download startTime = time.time() sock = TCP() sock.connect(host) sock.sendGet(request) # Start the retrieve filedata = sock.transmission() print filedata # if found it is a non-200 packet, exit if not filedata.startswith("HTTP/1.1 200"): print ('Non-200 HTTP status code is not supported') sys.exit(0) # remove the HTTP header: pos = filedata.find("\r\n\r\n") if pos > -1: pos += 4 filedata = filedata[pos:] endTime = time.time() # save file to disk saveFile(filename, filedata) print len(filedata), print 'Bytes received' #timecost = '%.2g' % (endTime - startTime) #print timecost, #print 'seconds passed. Average speed:', #throughput = '%f' % (len(filedata) / float(timecost) / 1000) #print throughput, #print 'KBps' sock.sock.close()
print('Ethernet Frame:') print(TAB_1 + 'Destination: {}, Source: {}, Protocol: {}'.format( eth.dest_mac, eth.src_mac, eth.proto)) # IPv4 if eth.proto == 8: ipv4 = IPv4(eth.data) print(TAB_1 + 'IPv4 Packet:') print(TAB_2 + 'Version: {}, Header Length: {}, TTL: {},'.format( ipv4.version, ipv4.header_length, ipv4.ttl)) print(TAB_2 + 'Protocol: {}, Source: {}, Target: {}'.format( ipv4.proto, ipv4.src, ipv4.target)) # TCP if ipv4.proto == 6: tcp = TCP(ipv4.data) print(TAB_1 + 'TCP Segment:') print(TAB_2 + 'Source Port: {}, Destination Port: {}'.format( tcp.src_port, tcp.dest_port)) print(TAB_2 + 'Sequence: {}, Acknowledgment: {}'.format( tcp.sequence, tcp.acknowledgment)) print(TAB_2 + 'Flags:') print(TAB_3 + 'URG: {}, ACK: {}, PSH: {}'.format( tcp.flag_urg, tcp.flag_ack, tcp.flag_psh)) print(TAB_3 + 'RST: {}, SYN: {}, FIN:{}'.format( tcp.flag_rst, tcp.flag_syn, tcp.flag_fin)) if len(tcp.data) > 0: # HTTP if tcp.src_port == 80 or tcp.dest_port == 80: print(TAB_2 + 'HTTP Data:')
def run(self): # parameters Sim.scheduler.reset() Sim.set_debug('AppHandler') #Sim.set_debug('TCP') Sim.set_debug('Link') net = Network('networks/one-hop-queue.txt') net.loss(self.loss) # setup routes n1 = net.get_node('n1') n2 = net.get_node('n2') n1.add_forwarding_entry(address=n2.get_address('n1'), link=n1.links[0]) n2.add_forwarding_entry(address=n1.get_address('n2'), link=n2.links[0]) # setup transport t1 = Transport(n1) t2 = Transport(n2) # setup application a = AppHandler(self.filename) # setup connection c1 = TCP(t1, n1.get_address('n2'), 1, n2.get_address('n1'), 1, a, window=self.window, threshold=self.threshold, fast_recovery=self.fast_recovery) c2 = TCP(t2, n2.get_address('n1'), 1, n1.get_address('n2'), 1, a, window=self.window, threshold=self.threshold, fast_recovery=self.fast_recovery) # send a file with open(self.filename, 'r') as f: while True: data = f.read(1000) if not data: break Sim.scheduler.add(delay=0, event=data, handler=c1.send) # run the simulation Sim.scheduler.run() # print some results print print "========== Overall results ==========" time = Sim.scheduler.current_time() print "Total time: %f seconds" % time avg = numpy.mean(c2.queueing_delay_list) print "Average queueing delay: %f" % avg max = numpy.max(c2.queueing_delay_list) print "Max queueing delay: %f" % max file_size = os.path.getsize(self.filename) print "File size: %i" % file_size throughput = file_size / time print "Throughput: %f" % throughput plotter = Plotter() #print "Saving the sequence plot" #plotter.create_sequence_plot(c1.x, c1.y, c1.dropX, c1.dropY, c1.ackX, c1.ackY) self.c2 = c2 self.c1 = c1 self.t1 = t1 self.t2 = t2 self.net = net plotter.rateTimePlot(c2.packets_received, Sim.scheduler.current_time(), 'one/rateTime.png')
def run(self): # parameters Sim.scheduler.reset() #Sim.set_debug('AppHandler') #Sim.set_debug('TCP') Sim.set_debug('Link') net = Network('networks/two.txt') net.loss(self.loss) # setup routes n1 = net.get_node('n1') n2 = net.get_node('n2') n1.add_forwarding_entry(address=n2.get_address('n1'), link=n1.links[0]) n2.add_forwarding_entry(address=n1.get_address('n2'), link=n2.links[0]) # setup transport t1 = Transport(n1) t2 = Transport(n2) # setup application a1 = AppHandler('test1.txt') a2 = AppHandler('test2.txt') # setup connection c1 = TCP(t1, n1.get_address('n2'), 1, n2.get_address('n1'), 1, a1, window=self.window, threshold=self.threshold, fast_recovery=self.fast_recovery) c2 = TCP(t2, n2.get_address('n1'), 1, n1.get_address('n2'), 1, a1, window=self.window, threshold=self.threshold, fast_recovery=self.fast_recovery) c3 = TCP(t1, n1.get_address('n2'), 2, n2.get_address('n1'), 2, a2, window=self.window, threshold=self.threshold, fast_recovery=self.fast_recovery) c4 = TCP(t2, n2.get_address('n1'), 2, n1.get_address('n2'), 2, a2, window=self.window, threshold=self.threshold, fast_recovery=self.fast_recovery) # send a file with open('test1.txt', 'r') as f: while True: data = f.read(1000) if not data: break Sim.scheduler.add(delay=0, event=data, handler=c1.send) with open('test2.txt', 'r') as f: while True: data = f.read(1000) if not data: break Sim.scheduler.add(delay=0, event=data, handler=c3.send) # run the simulation Sim.scheduler.run() # print some results print print "========== Overall results ==========" time = Sim.scheduler.current_time() print "Total time: %f seconds" % time avg = numpy.mean(c2.queueing_delay_list) print "Average queueing delay: %f" % avg max = numpy.max(c2.queueing_delay_list) print "Max queueing delay: %f" % max file_size = os.path.getsize(self.filename) print "File size: %i" % file_size throughput = file_size / time print "Throughput: %f" % throughput # Variables for debugging self.c3 = c3 self.c4 = c4 self.c2 = c2 self.c1 = c1 self.t1 = t1 self.t2 = t2 self.net = net linkab = n1.links[0] self.linkab = linkab l = linkab # Plotting plotter = Plotter() # Plot sequence charts plotter.clear() plotter.create_sequence_plot(c1.x, c1.y, c1.dropX, c1.dropY, c1.ackX, c1.ackY, chart_name='two/sequence1.png') plotter.clear() plotter.create_sequence_plot(c3.x, c3.y, c3.dropX, c3.dropY, c3.ackX, c3.ackY, chart_name='two/sequence2.png') # Plot receiver rate plotter.clear() plotter.rateTimePlot(c2.packets_received, Sim.scheduler.current_time(), chart_name=None) plotter.rateTimePlot(c4.packets_received, Sim.scheduler.current_time(), chart_name='two/rateTime.png') # Plot queue size plotter.clear() plotter.queueSizePlot(l.queue_log_x, l.queue_log_y, l.dropped_packets_x, l.dropped_packets_y, chart_name='two/queueSize.png') # Plot congestion window plotter.clear() plotter.windowSizePlot(c1.window_x, c1.window_y, chart_name="two/windowSize1.png") plotter.clear() plotter.windowSizePlot(c3.window_x, c3.window_y, chart_name="two/windowSize2.png")
def fn(x, y): tcp = TCP([x, y], arms=2, armlengths=armlengths) tcp.computeTcp() return tcp.tcp # should be list of length 3
def run(self, size): # parameters Sim.scheduler.reset() logging.getLogger('app').setLevel(logging.INFO) logging.getLogger('bene.link.queue').setLevel(logging.DEBUG) logging.getLogger('bene.tcp.sequence').setLevel(logging.DEBUG) logging.getLogger('bene.tcp.cwnd').setLevel(logging.DEBUG) if self.debug: logging.getLogger('bene.tcp').setLevel(logging.DEBUG) logging.getLogger('bene.tcp.sender').setLevel(logging.DEBUG) logging.getLogger('bene.tcp.receiver').setLevel(logging.DEBUG) # setup network net = Network('networks/one-hop-q10.txt') net.loss(self.loss) # setup routes n1 = net.get_node('n1') n2 = net.get_node('n2') n1.add_forwarding_entry(n2.get_address('n1'), n1.links[0]) n2.add_forwarding_entry(n1.get_address('n2'), n2.links[0]) # setup transport t1 = Transport(n1) t2 = Transport(n2) # setup application a = AppHandler(self.filename) window = size # setup connection drop = [] filen = ("data/queue-%i.csv" % size) fd = open(filen, "w") fd.write("Time_stamp,qDelay\n") c1 = TCP(t1, n1.get_address('n2'), 1, n2.get_address('n1'), 1, a, window=window, drop=drop, retran=self.retran) c2 = TCP(t2, n2.get_address('n1'), 1, n1.get_address('n2'), 1, a, window=window, fd=fd) # send a file time_before = Sim.scheduler.current_time() count = 0 with open(self.filename, 'rb') as f: while True: data = f.read(1000) count += len(data) # print("0",count) if not data: break Sim.scheduler.add(delay=0, event=data, handler=c1.send) Sim.scheduler.run() time_after = Sim.scheduler.current_time() Sim.scheduler.reset() fd.close() return (time_after - time_before), count
def run(self): # parameters Sim.scheduler.reset() #Sim.set_debug('AppHandler') #Sim.set_debug('TCP') Sim.set_debug('Link') net = Network('networks/one.txt') net.loss(self.loss) # setup routes n1 = net.get_node('n1') n2 = net.get_node('n2') n1.add_forwarding_entry(address=n2.get_address('n1'), link=n1.links[0]) n2.add_forwarding_entry(address=n1.get_address('n2'), link=n2.links[0]) # setup transport t1 = Transport(n1) t2 = Transport(n2) # setup application a1 = AppHandler('test.txt') # setup connection c1 = TCP(t1, n1.get_address('n2'), 1, n2.get_address('n1'), 1, a1, window=self.window, threshold=self.threshold, fast_recovery=True, aimdc=5.0 / 6.0) c2 = TCP(t2, n2.get_address('n1'), 1, n1.get_address('n2'), 1, a1, window=self.window, threshold=self.threshold, fast_recovery=True, aimdc=5.0 / 6.0) # send a file with open('test.txt', 'r') as f: while True: data = f.read(1000) if not data: break Sim.scheduler.add(delay=0, event=data, handler=c1.send) # run the simulation Sim.scheduler.run() # print some results print print "========== Overall results ==========" time = Sim.scheduler.current_time() print "Total time: %f seconds" % time avg = numpy.mean(c2.queueing_delay_list) print "Average queueing delay: %f" % avg max = numpy.max(c2.queueing_delay_list) print "Max queueing delay: %f" % max file_size = os.path.getsize(self.filename) print "File size: %i" % file_size throughput = file_size / time print "Throughput: %f" % throughput plotter = Plotter() print "Saving the sequence plot" plotter.create_sequence_plot(c1.x, c1.y, c1.dropX, c1.dropY, c1.ackX, c1.ackY, chart_name='advanced/aimd/sequence.png') plotter.clear() plotter.rateTimePlot(c2.packets_received, Sim.scheduler.current_time(), 'advanced/aimd/rateTime1.png') linkab = n1.links[0] self.linkab = linkab plotter.clear() plotter.queueSizePlot(linkab.queue_log_x, linkab.queue_log_y, linkab.dropped_packets_x, linkab.dropped_packets_y, chart_name='advanced/aimd/queueSize.png') plotter.clear() plotter.windowSizePlot(c1.window_x, c1.window_y, chart_name="advanced/aimd/windowSize1.png")
class Cal_camera(): def __init__(self, camera, pnts_pattern): self.rep = Representation() self.h = Homography(pnts_pattern) self.tcp = TCP() self.a = Angle() self.camera = camera self.hom = np.zeros((3, 3)) self.inv_hom = np.zeros((3, 3)) self.Frame = np.zeros((3, 3)) self.inv_Frame = np.zeros((3, 3)) self.hom_final_camera = np.zeros((3, 3)) def get_pxls_homography(self, image, scale=1): pts_image = self.h.read_image(image, scale) return pts_image def calculate_homography(self, pxls_pattern): self.hom = self.h.calculate(pxls_pattern) self.inv_hom = linalg.inv(self.hom) def get_pxl_origin(self, folder, scale=1): pxl_pnts = [] for f in sorted(folder): im_uEye = cv2.imread(f) pxl_pnts.append(self.tcp.read_image(im_uEye, scale)) return pxl_pnts def get_pxl_orientation(self, folder, scale=1): for f in sorted(folder): name = basename(f) if name == "move_x.jpg": im_uEye = cv2.imread(f) pxl_pnts_x = self.tcp.read_image(im_uEye, scale) elif name == "move_y.jpg": im_uEye = cv2.imread(f) pxl_pnts_y = self.tcp.read_image(im_uEye, scale) elif name == "move_o.jpg": im_uEye = cv2.imread(f) pxl_pnts_origin = self.tcp.read_image(im_uEye, scale) return pxl_pnts_origin, pxl_pnts_x, pxl_pnts_y def calculate_TCP_orientarion(self, pxl_pnts, pxl_pnts_origin, pxl_pnts_x, pxl_pnts_y, dx, dy): pxl_TCP = self.tcp.calculate_origin(pxl_pnts) print "TCP :", pxl_TCP factor, angle_y, angle_x = self.tcp.calculate_orientation(pxl_pnts_origin, pxl_pnts_x, pxl_pnts_y, dx, dy) return pxl_TCP, factor, angle_y, angle_x def calculate_angle_TCP(self, origin, axis_x, pattern): pnt_origin = self.rep.transform(self.hom, origin) pnt_x = self.rep.transform(self.hom, axis_x) pnt_pattern = self.rep.transform(self.hom, pattern) l_1 = np.float32([pnt_origin[0], pnt_x[0]]) l_2 = pnt_pattern[0:2] vd1 = self.a.director_vector(l_1[0], l_1[1]) vd2 = self.a.director_vector(l_2[0], l_2[1]) angle = self.a.calculate_angle(vd1, vd2) return angle def calculate_frame(self, pxl_TCP, angle): pnts_TCP = self.rep.transform(self.hom, pxl_TCP)[0] a = np.deg2rad(angle) self.Frame = np.float32([[np.cos(a), -np.sin(a), pnts_TCP[0]], [np.sin(a), np.cos(a), pnts_TCP[1]], [0, 0, 1]]) self.Orientation = np.float32([[np.cos(a), -np.sin(a), 0], [np.sin(a), np.cos(a), 0], [0, 0, 1]]) self.inv_Orientation = linalg.inv(self.Orientation) self.inv_Frame = linalg.inv(self.Frame) def calculate_hom_final(self, img, pnts, corners, pnts_final): # pxls_camera = self.rep.transform(self.inv_hom, pnts) im_measures = self.rep.define_camera(img.copy(), self.hom) #------------------ Data in (c)mm image_axis = self.rep.draw_axis_camera(im_measures, pnts) #------------------ pnts_Frame = pnts pnts_camera = self.rep.transform(self.Frame, pnts_Frame) image_axis_tcp = self.rep.draw_axis_camera(image_axis, pnts_camera) #------------------ corners_Frame = corners corners_camera = self.rep.transform(self.Frame, corners_Frame) img = self.rep.draw_points(image_axis_tcp, corners_camera) #------------------ pxls_corner = self.rep.transform(self.inv_hom, corners_camera) self.hom_final_camera, status = cv2.findHomography(pxls_corner.copy(), pnts_final) self.write_config_file() return self.hom_final_camera def write_config_file(self): hom_vis = self.hom_final_camera hom_TCP = np.dot(self.inv_hom, self.Frame) inv_hom_TCP = np.dot(self.inv_Frame, self.hom) data = dict( hom_vis=hom_vis.tolist(), hom=hom_TCP.tolist(), inv_hom=inv_hom_TCP.tolist(), ) filename = '../../config/' + self.camera + '_config.yaml' with open(filename, 'w') as outfile: yaml.dump(data, outfile) print data def visualize_data(self): print "Homography: " print self.hom print "Frame: " print self.Frame print "Final homography: " print self.hom_final_camera def draw_pattern_axis(self, pnts, img): pnts_axis_pattern = pnts pxls_axis_pattern = self.rep.transform(self.inv_hom, pnts_axis_pattern) pnt_axis_pattern_final = self.rep.transform(self.hom_final_camera, pxls_axis_pattern) img_pattern_NIT = self.rep.draw_axis_camera(img, pnt_axis_pattern_final) return img_pattern_NIT def draw_TCP_axis(self, pnts, img): pnts_axis = pnts pnts_axis_TCP = self.rep.transform(self.Frame, pnts_axis) pxls_axis_TCP = self.rep.transform(self.inv_hom, pnts_axis_TCP) pnt_axis_TCP_final = self.rep.transform(self.hom_final_camera, pxls_axis_TCP) img_final_axis = self.rep.draw_axis_camera(img, pnt_axis_TCP_final) return img_final_axis def draw_axis(self, pnts, img): img_TCP = self.draw_pattern_axis(pnts, img) img_final = self.draw_TCP_axis(pnts, img_TCP) return img_final
def test_format_message(): test_body = "This is a test!" expected_message = "b'\\x00\\x00\\x00\\x0f'This is a test!" assert TCP.format_message(message=test_body) == expected_message
def main(*args, **kwargs): start = time.time() global client, passphrase, host, port network = [] chain = BlockChain(client, passphrase) files = Filemng(client, chain) encrypt = files.Load_Both_Keys() files.Load_Month() print("Loading Logs") sensors = Sensor() sensors.Config_Sensors() print("Configued Sensors") que = queue.Queue() send_que = queue.Queue() recv_que = queue.Queue() comm = TCP(encrypt, passphrase, host, port, que, send_que, recv_que) Showdown = False while not Showdown: print("Collecting Sensors data") Collect_Sensor(chain, sensors) print("Saving Log") files.Save_Log() error = 0 transmit = False water_count = 0 print("Checking Water level/Levels") if (float(chain.chain[-1].sensor.water_level) < MAX_WATER_LEVEL and float(chain.chain[-1].sensor.water_level) != 0.00 or 0 < water_count <= 10): if (float(chain.chain[-1].sensor.water_level) < MAX_WATER_LEVEL): water_count = 0 water_count += 1 attenna_status = True #sensors.boot_antenna() while (not attenna_status and not Showdown): attenna_status = sensors.boot_antenna() error += 1 if (error >= 3): chain.Set_Network_Error(112) Shutdown(files) Showdown = True if (not comm.State): Start_Comm(comm) time.sleep(1) print("Water level High") transmit = True print("Checking Unsent Object") if (Check_Unsent_Blocks(chain) > UNSENT_SEND and not transmit): attenna_status = True #sensors.boot_antenna() while (not attenna_status and not Showdown): attenna_status = sensors.boot_antenna() error += 1 if (error >= 3): chain.Set_Network_Error(112) Shutdown(files) Showdown = True if (not comm.State): Start_Comm(comm) time.sleep(1) print("Unsent blocks High") transmit = True if (transmit): Send_Blocks(chain, send_que) time.sleep(5) Remove_blocks(chain, recv_que) print("Transmitting") else: print("Nothing to transmitt") files.Save_Log() Showdown = True if not que.empty(): network.append(que.get()) if (len(network) > 0): chain.Set_Network_Error(network[0][0]) if (network[0][0] == 111): Showdown = True time.sleep(2) print("Cycling ") print("Shutting Down") Soft_Shutdown(files, comm)
class PXI: """ PXI Class. TODO: write docstring Attributes: """ help_str = ("At any time, type... \n" + " - 'h' to see this message again \n" + " - 'r' to reset the connection to CsPy \n" + " - 'd' to toggle the server DEBUG level logging \n" + " - 'x' to print the most recently received xml to a file \n" + " - 'q' to stop the connection and close this server.") def __init__(self, address: Tuple[str, int]): self.root_logger = logging.getLogger() # root_logger self._root_logging_lvl_default = self.root_logger.level self.sh = self.root_logger.handlers[0] # stream_handler self._sh_lvl_default = self.sh.level self.logger = logging.getLogger(self.__class__.__name__) self.logger.setLevel(logging.DEBUG) fh = logging.FileHandler('spam.log') fh.setLevel(logging.DEBUG) self.logger.addHandler(fh) self._stop_connections = False self._reset_connection = False self._exit_measurement = False self.cycle_continuously = False self.return_data = b"" self.return_data_queue = b"" self.measurement_timeout = 0 self.keylisten_thread = None self.command_queue = Queue(0) # 0 indicates no maximum queue length enforced. self.element_tags = [] # for debugging self.devices = [] # instantiate the device objects self.hsdio = HSDIO(self) self.tcp = TCP(self, address) self.analog_input = AnalogInput(self) self.analog_output = AnalogOutput(self) self.ttl = TTLInput(self) # self.daqmx_do = DAQmxDO(self) self.hamamatsu = Hamamatsu(self) self.counters = Counters(self) @property def stop_connections(self) -> bool: return self._stop_connections @stop_connections.setter def stop_connections(self, value): self._stop_connections = value @property def reset_connection(self) -> bool: return self._reset_connection @reset_connection.setter def reset_connection(self, value): self._reset_connection = value @property def exit_measurement(self) -> bool: return self._exit_measurement @exit_measurement.setter def exit_measurement(self, value): self._exit_measurement = value @property def active_devices(self): """ Number of devices that were successfully initialized """ return sum(dev.is_initialized for dev in self.devices) @property def root_logging_lvl_default(self): """ The default root logging level for this server """ return self._root_logging_lvl_default @property def sh_lvl_default(self): """ The default stream handler level for this server """ return self._sh_lvl_default def queue_command(self, command): self.command_queue.put(command) def launch_network_thread(self): self.tcp.launch_network_thread() def launch_experiment_thread(self): """ Launch a thread for the main experiment loop Thread target method = self.command_loop """ self.experiment_thread = threading.Thread( target=self.command_loop, name='Experiment Thread' ) self.experiment_thread.setDaemon(False) self.experiment_thread.start() def command_loop(self): """ Update devices with xml from CsPy, and get and return data from devices Pop a command from self.command_queue on each iteration, parse the xml in that command, and update the instruments accordingly. When the queue is empty, try to receive measurements from the data if cycling continuously. This function handles the switching between updating devices and getting data from them, while the bulk of the work is done in the hierarchy of methods in self.parse_xml and self.measurement. """ while not (self.stop_connections or self.exit_measurement): try: # dequeue xml xml_str = self.command_queue.get(block=False, timeout=0) self.parse_xml(xml_str) except Empty: self.exit_measurement = False self.return_data = b"" # clear the return data if self.cycle_continuously and self.active_devices > 0: self.logger.debug("Entering cycle continously...") # This method returns the data self.return_data_queue = self.measurement() sleep(0.001) # keep while loop from hogging resources self.shutdown() self.logger.info("Exiting Experiment Thread.") def launch_keylisten_thread(self): """ Launch a KeyListener thread to get key presses in the command line """ self.keylisten_thread = KeyListener(self.on_key_press) self.keylisten_thread.setDaemon(True) self.logger.info("starting keylistener") self.keylisten_thread.start() def parse_xml(self, xml_str: str): """ Initialize the device instances and other settings from queued xml Loop over highest tier of xml tags with the root tag='LabView' in the message received from CsPy, and call the appropriate device class accordingly. the xml is popped from a queue, which updates in the network_loop whenever a valid message from CsPy is received. Args: 'xml_str': (str) xml received from CsPy in the receive_message method """ self.exit_measurement = False self.element_tags = [] # clear the list of received tags # get the xml root root = ET.fromstring(xml_str) if root.tag != "LabView": self.logger.warning("Not a valid msg for the pxi") else: # loop non-recursively over children in root to setup device # hardware and other server settings for child in root: self.element_tags.append(child) try: if child.tag == "measure": # if no data available, take one measurement. Otherwise, # use the most recent data. if self.return_data_queue == b"": self.measurement() else: self.return_data = self.return_data_queue pass elif child.tag == "pause": # TODO: set state of server to 'pause'; # i don't know if this a feature that currently gets used, # so might be able to omit this. pass elif child.tag == "run": # TODO: set state of server to 'run'; # i don't know if this a feature that currently gets used, # so might be able to omit this. pass elif child.tag == "HSDIO": # setup the HSDIO self.hsdio.load_xml(child) self.logger.info("HSDIO XML loaded") self.hsdio.init() self.logger.info("HSDIO hardware initialized") self.hsdio.update() self.logger.info("HSDIO hardware updated") elif child.tag == "TTL": self.ttl.load_xml(child) self.logger.info("TTLInput XML loaded") self.ttl.init() self.logger.info("TTLInput hardware initialized") elif child.tag == "DAQmxDO": # self.daqmx_do.load_xml(child) # self.daqmx_do.init() pass elif child.tag == "timeout": try: # get timeout in [ms] self.measurement_timeout = 1000 * float(child.text) except ValueError as e: msg = f"{e} \n {child.text} is not valid" + \ f"text for node {child.tag}" raise XMLError(self, child, message=msg) elif child.tag == "cycleContinuously": cycle = False if child.text.lower() == "true": cycle = True self.cycle_continuously = cycle elif child.tag == "camera": # set up the Hamamatsu camera self.hamamatsu.load_xml(child) # Raises ValueError self.hamamatsu.init() # Raises IMAQErrors elif child.tag == "AnalogOutput": # set up the analog_output self.analog_output.load_xml(child) self.logger.info("AnalogOutput XML loaded") self.analog_output.init() self.logger.info("AnalogOutput initialized") self.analog_output.update() self.logger.info("AnalogOutput hardware updated") elif child.tag == "AnalogInput": # set up the analog_input self.analog_input.load_xml(child) self.analog_input.init() elif child.tag == "Counters": # # TODO: implement counters class # # set up the counters self.counters.load_xml(child) self.counters.init() pass # # might implement, or might move RF generator functionality to # # CsPy based on code used by Hybrid. elif child.tag == "RF_generators": pass else: self.logger.warning(f"Node {child.tag} received is not a valid" + f"child tag under root <{root.tag}>") # I do not catch AssertionErrors. The one at the top of load_xml in every # device class can only occur if the device is passed the wrong xml node, # which can never occur in pxi.parse_xml, as we check the tag before # instantiating a device. those assertions are there in case someone down the # road does something more careless. except (XMLError, HardwareError) as e: self.handle_errors(e) # send a message back to CsPy self.tcp.send_message(self.return_data) # clear the return data self.return_data = b"" self.return_data_queue = b"" def data_to_xml(self) -> str: """ Get xml-formatted data string from device measurements Return the data as an xml string by calling the device class is_out methods. Returns: 'return_data': concatenated string of xml-formatted data """ return_data = b"" # the devices which have a method named 'data_out' which returns a str devices = [ self.hamamatsu, self.counters, #self.ttl, # self.counters, #TODO: implement self.ttl, self.analog_input # self.demo # not implemented, and debatable whether it needs to be ] for dev in devices: if dev.is_initialized: try: return_data += dev.data_out() except HardwareError as e: self.handle_errors(e) self.return_data = return_data return return_data def measurement(self) -> str: """ Return a queue of the acquired responses queried from device hardware Returns: 'return_data': string of concatenated responses received from the device classes """ if not (self.stop_connections or self.exit_measurement): # self.logger.info(f"measurement time lapse= {time()-self.trelative}") self.trelative = time() self.reset_data() self.system_checks() self.start_tasks() _is_done = False _is_error = False ## timed loop to frequently check if tasks are done tau = 10 # loop period in [ms] scl = 1e-6 # scale factor to convert ns to ms devtime = time() finished_devs = [] while not (_is_done or _is_error or self.stop_connections or self.exit_measurement): try: _is_done = self.is_done() except HardwareError as e: self.handle_errors(e) # sleep to reduce resource usage sleep(0.001) try: self.get_data() self.system_checks() self.stop_tasks() return_data = self.data_to_xml() return return_data except Exception as e: # TODO: make less general self.logger.warning(f"Error encountered {e}\nNo data returned.") self.handle_errors(e) return "" def reset_data(self): """ Resets data on devices which need to be reset. For now, only applies to TTL """ try: self.ttl.reset_data() except HardwareError as e: self.handle_errors(e) def system_checks(self): """ Check devices. For now, only applies to TTL """ try: self.ttl.check() except HardwareError as e: self.handle_errors(e) # wrap batch_method_call calls in convenience functions def start_tasks(self, handle_error=True): """ Start measurement and output tasks for relevant devices """ # devices which have a method 'start' devices = [ self.hsdio, # self.daqmx_do, # self.hamamatsu, self.analog_input, self.analog_output, #self.ttl self.counters ] self.batch_method_call(devices, 'start', handle_error) # self.reset_timeout() # TODO : Implement or discard def stop_tasks(self, handle_error=True): """ Stop measurement and output tasks for relevant devices """ # devices which have a method 'stop' devices = [ self.hsdio, # self.daqmx_do, self.analog_input, self.analog_output, #self.ttl self.counters # TODO: implement Counters.stop ] self.batch_method_call(devices, 'stop', handle_error) def close_tasks(self, handle_error=True): """ Close references to tasks for relevant devices """ # devices which have a method 'stop' devices = [ self.hsdio, # self.daqmx_do, self.hamamatsu, self.analog_input, self.analog_output, self.ttl, self.counters # TODO: implement Counters.stop ] self.batch_method_call(devices, 'close', handle_error) def get_data(self, handle_error=True): """ Get data from the devices """ if not (self.stop_connections or self.exit_measurement): # devices which have a method 'get_data' devices = [ self.hamamatsu, self.analog_input, self.counters # TODO: implement Counters.get_data ] self.batch_method_call(devices, 'get_data', handle_error) def is_done(self) -> bool: """ Check if devices running processes are done yet Loops over the device classes and calls the instance's is_done method for each device capable of running a process and breaks when a process is found to not be done. Returns: done 'done': will return True iff all the device processes are done. """ done = True if not (self.stop_connections or self.exit_measurement): # devices which have a method named 'is_done' that returns a bool devices = [ self.hsdio, self.analog_output, self.analog_input, # self.daqmx_do #self.counters ] try: for dev in devices: if dev.is_initialized: if not dev.is_done(): done = False break except HardwareError as e: self.handle_errors(e) return done return done def reset_timeout(self): """ Seems to change a global variable 'Timeout Elapses at' to the current time + timeout Will that work here? Returns: """ # TODO: Implement pass def on_key_press(self, key: str): """ Determines what happens for key presses in the command prompt. This method to be passed into the KeyListener instance to be called when keys are pressed. Args: 'key': the returned key from msvcrt.getwch(), e.g. 'h' """ if key == 'h': # show the help str self.logger.info(self.help_str) if key == 'r': # reset the connection self.logger.info("Connection reset by user.") self.reset_connection = True if key == 'x': # print most recently received xml to file try: fname = self.tcp.xml_to_file() self.logger.info("wrote xml to file "+fname) except Exception as e: self.logger.error(f"oops. failed to write to file. + \n {e}") if key == 'd': # toggle debug/info level root logging if self.root_logger.level != logging.DEBUG: self.root_logger.setLevel(logging.DEBUG) else: self.root_logger.setLevel(self.root_logging_lvl_default) self.logger.info("set the root level logging to default") if self.sh.level != logging.DEBUG: self.sh.setLevel(logging.DEBUG) else: self.sh.setLevel(self.sh_lvl_default) self.logger.info("set the stream handler level logging to default") self.logger.debug("Logging level is now DEBUG") elif key == 'q': self.logger.info("Connection stopped by user. Closing server.") self.exit_measurement = True self.stop_connections = True else: self.logger.info("Not a valid keypress. Type \'h\' for help.") def handle_errors(self, error: Exception, traceback_str: str = ""): """ Handle errors caught in the PXI instance Error handling philosophy: I. The class where the error originated should log a useful message detailing the source of the error II. If error should halt instrument activity, error should be raised to pxi class. Use logger.error(msg,exc_info=True) to log error with traceback. III. If error should not halt instrument activity, use logger.warning(msg,exc_info=t/f) (t/f = with/without traceback) IV. When error is raised to pxi class, this function should be called. A message will be logged to the terminal output as well as sent to CsPy, warning the user that an error has occurred and that the PXI settings should be updated. V. The current measurement cycle should be aborted, and restarted if self.cycle_continuously == True. Device where error occurred should be excluded from the measurement cycle until it has been reinitialized. I.e., devices which read/write signals (e.g. TTLInput, HSDIO, etc) should continue to cycle if they can VI. The errors raised in the device classes can should be classified coarsely, as many errors should result in the same handling behavior in this function (e.g. all error types pertaining to an XML initialization failure should result in a message urging the user to check the XML in CsPy and reinitialize the device). Specific error types handled here are HardwareError and XMLError, defined in pxierrors.py Args: error : The error that was raised. Maybe it's useful to keep it around traceback_str : string useful for traceback """ # NOTE: # This code is not a janitor, your mother/father, etc. If your device soiled itself, it is your # responsibility to clean up the problem where it occurred. The code that follows is only # intended to change the state the of the server and/or log a report of what happened, # in response to whatever mess was made. if self.stop_connections: return self.logger.warning("PXIError encountered. Closing the problematic tasks.") if isinstance(error, XMLError): self.logger.error(traceback_str + "\n" + error.message + "\n" + "Fix the pertinent XML in CsPy, then try again.") self.cycle_message(error.device) self.reset_exp_thread() elif isinstance(error, HardwareError): self.logger.error(traceback_str + "\n" + error.message) self.cycle_message(error.device) self.stop_tasks(handle_error=False) # stop all current measurement tasks error.device.close() # close the reference to the problematic device self.reset_exp_thread() # this stalls the program currently # If not a type of error we anticipated, raise it. else: raise error def cycle_message(self, dev: XMLLoader): """ Log a message about the status of the server cycling. Args: dev: the device instance where the problem occurred """ if self.cycle_continuously: self.logger.warning(f"The server will now cycle, but without {dev}") else: self.logger.info(f"The server is not taking data. Cycle continuously to resume, but without {dev}") def reset_exp_thread(self): """ Restart experiment thread after current measurement ends """ # self.logger.info("Waiting for the experiment thread to end...") # self.exit_measurement = True # while self.experiment_thread.is_alive(): # pass # self.experiment_thread.join() self.logger.info("Restarting current experiment thread...") self.exit_measurement = False # overwrite the existing thread ref. might be a better way to do this. self.launch_experiment_thread() self.logger.info("Experiment thread relaunched") def batch_method_call( self, device_list: List[Instrument], method: str, handle_error: bool = True ): """ Call a method common to several device classes Given a list of device instances, assumed to have method 'method' as well as bool parameter 'is_initialized', 'method' will be called for each instance. For now, it is assumed that 'method' takes no arguments, and does does not return anything. Args: device_list: list of device instances method: name of the method to be called. make sure every device in device list has a method with this name. handle_error: Should self.handle_errors() be called to deal with errors during this operation? """ # only iterate over initialized devices for dev in filter(lambda x: x.is_initialized, device_list): fun = getattr(dev, method, None) if fun is None or not callable(fun): self.logger.warning(f"{dev} does not have a method '{method}'") continue try: fun() # call the method # self.logger.info("starting the {dev.__class__.__name__}") except HardwareError as he: self.logger.info( f"Error {he} encountered while performing {dev}.{method}()" f"handle_error = {handle_error}") if handle_error: self.handle_errors(he) def shutdown(self): """ Nicely shut down this server """ if self.tcp.seeking_connection: self.tcp.abort() self.logger.info(f"Attempting to stop devices with stop method") self.stop_tasks() self.logger.info(f"Attempting to close devices with close method") self.close_tasks() self.logger.info(f"Disabling all devices") for device in self.devices: device.enable = False
class Trainer: def __init__(self, agent): self.agent = agent self.name = "Trainer" def start_training_session(self, num_of_episodes): for episode_num in range(num_of_episodes): self.headline_print("start game of episode number {}".format(episode_num + 1)) self.game_id = rest_api.create_game(self.name).content.decode("utf-8") print("Trainer created game with id: {}".format(self.game_id)) self.tcp = TCP(self.game_id, self.name, self.on_recieved) self.start_game_with_agent(self.game_id) self.headline_print("end of episode number {}".format(episode_num + 1)) def start_game_with_agent(self, game_id): env = QuoridorEnv(game_id, "Agent") cur_state = env.reset() done = False steps_num = 0 while not done: steps_num += 1 action = self.agent.act(cur_state, env) new_state, reward, done, _ = env.step(action) self.agent.remember(cur_state, action, reward, new_state, done) self.agent.replay(env) # internally iterates default (prediction) model self.agent.target_train() # iterates target model cur_state = new_state print("\n\nGAME FINISHED IN {} STEPS!\n\n".format(steps_num)) def on_recieved(self, json_message): if json_message["type"] == "NewTurnEvent": if json_message["nextPlayerToPlay"] == self.name: myLoc = json_message["currentPosition"] actions = [] for move in json_message["avialiableMoves"]: if int(move["x"]) > int(myLoc["x"]): actions.append(3) # Move Right elif int(move["x"]) < int(myLoc["x"]): actions.append(2) # Move Left elif int(move["y"]) > int(myLoc["y"]): actions.append(1) # Move Down elif int(move["y"]) < int(myLoc["y"]): actions.append(0) # Move Up actions_len = len(actions) random_i = np.random.randint(0, actions_len) act_json = utils.convert_action_to_server(actions[random_i]) self.tcp.write(act_json) elif json_message["type"] == "GameOverEvent": pass elif json_message["type"] == "RoomStateResponse": if len(json_message["players"]) == 2: rest_api.start_game(self.game_id) def headline_print(self, text): print("====================================================") print(text) print("====================================================")
class QuoridorEnv(gym.Env): """ players: { index: number, start_location: number, name: string, targets: [number] } """ def __init__(self, game_id, player_name, is_not_tcp): self.game_id = game_id self.player_name = player_name self.is_my_turn = False self.winner_status = GameWinnerStatus.NoWinner self.last_turn_illegal = False self.action_options = [] self.winning_points_dim = np.zeros(shape=(9, 9), dtype=int) # join_game(self.game_id, self.player_name) if not is_not_tcp: self.tcp = TCP(game_id, player_name, self.on_recieved) self.wait_for_my_turn() self.action_space = spaces.Discrete(Global.num_of_actions) self.observation_space = spaces.Tuple(( spaces.MultiBinary([9, 9]), spaces.MultiBinary([9, 9]), spaces.MultiBinary([9, 9]), spaces.MultiBinary([9, 9]), spaces.MultiBinary([9, 9]) )) self.seed() # Start the first game #self.reset() def step(self, action): assert self.action_space.contains(action) action = int(action) self.update_board(action) self.wait_for_my_turn() reward, done = self.calculate_reward() self.is_my_turn = False # self.print_board() return self.board, reward, done, {} def wait_for_my_turn(self): while not self.is_my_turn: pass def reset(self): # self.board = self.init_board() return self.board def calculate_reward(self): reward = -1 done = False if self.last_turn_illegal: reward = -10 self.last_turn_illegal = False if self.winner_status != GameWinnerStatus.NoWinner: done = True if self.winner_status == GameWinnerStatus.EnvWinner: reward = 200 elif self.winner_status == GameWinnerStatus.EnvLoser: reward = -200 return reward, done def update_board(self, action): operation = utils.convert_action_to_server(action) self.send_to_server(operation) # WAITING def print_board(self): os.system('cls') print('------- TEAM 600 --------') arrays = np.dsplit(self.board, 4) for y in range(9): for x in range(9): print('|', end='') if arrays[0][y][x] != 0: print(1, end='') elif arrays[1][y][x] != 0: print(2, end='') else: print(' ', end='') if arrays[3][y][x] != 0: print('|', end='') elif y != 0 & arrays[3][y - 1][x] != 0: print('|', end='') else: print(' ', end='') print('') for x_wall in range(9): if arrays[2][y][x_wall] != 0: print(' ==', end='') elif x_wall != 0 & arrays[2][y][x_wall - 1] != 0: print(' ==', end='') else: print(' __', end='') print('') def get_and_convert_board(self): board = json.loads(get_board(self.game_id).content) return convert_board(board, self.winning_points_dim) def send_to_server(self, operation): self.tcp.write(operation) def on_recieved(self, json_message): if json_message["type"] == "IllegalMove": # self.is_my_turn = True self.last_turn_illegal = True elif json_message["type"] == "NewTurnEvent": if json_message["nextPlayerToPlay"] == self.player_name: self.board = self.get_and_convert_board() self.is_my_turn = True self.update_action_options(json_message) elif json_message["type"] == "GameOverEvent": self.is_my_turn = True if json_message["winnerName"] == self.player_name: self.winner_status = GameWinnerStatus.EnvWinner else: self.winner_status = GameWinnerStatus.EnvLoser elif json_message["type"] == "StartGameMessage": self.update_winning_locations(json_message["players"]) def action_shape(self): return action_shape() def observation_shape(self): return observation_shape() def update_action_options(self, moves_json): self.action_options = [] myLoc = moves_json["currentPosition"] for move in moves_json["avialiableMoves"]: if int(move["x"]) > int(myLoc["x"]): self.action_options.append(3) # Move Right elif int(move["x"]) < int(myLoc["x"]): self.action_options.append(2) # Move Left elif int(move["y"]) > int(myLoc["y"]): self.action_options.append(1) # Move Down elif int(move["y"]) < int(myLoc["y"]): self.action_options.append(0) # Move Up for wall in moves_json["availableWalls"]: wall_action = 4 + wall["position"]["x"] + (8 * wall["position"]["y"]) if wall["wallDirection"] == "Down": wall_action += 64 self.action_options.append(wall_action) def get_action_options(self): return self.action_options def update_winning_locations(self, players): for player in players: if player["name"] == self.player_name: for loc in player["endLine"]: x = int(loc["x"]) y = int(loc["y"]) self.winning_points_dim[y, x] = 1 print(self.winning_points_dim)
class Robot(object): def __init__(self, ipAddress): self.sq = Queue() self.rq = Queue() self.struct = Struct('BBbB') #command, selector, value, checksum self.sender = TCP(isServer=False, isSender=True, host=ipAddress, port=1234, q=self.sq) self.receiver = TCP(isServer=False, isSender=False, host=ipAddress, port=5678, q=self.rq) def motor(self, device, value): self.send(1, device, value) def send(self, command, device, value): self.sq.put(self.struct.pack(command, device, value)) def connect(self): self.sender.start() self.receiver.start() def listen(self): pass def disconnect(self): while not self.sq.empty(): sleep(0.01) self.sender.running = False self.receiver.running = False self.sender.disconnect() self.receiver.disconnect() self.sender.join() self.receiver.join()
def run(self): # parameters Sim.scheduler.reset() logging.getLogger('app').setLevel(logging.INFO) logging.getLogger('bene.link.queue').setLevel(logging.DEBUG) logging.getLogger('bene.tcp.sequence').setLevel(logging.DEBUG) logging.getLogger('bene.tcp.cwnd').setLevel(logging.DEBUG) if self.debug: logging.getLogger('bene.tcp').setLevel(logging.DEBUG) logging.getLogger('bene.tcp.sender').setLevel(logging.DEBUG) logging.getLogger('bene.tcp.receiver').setLevel(logging.DEBUG) # setup network net = Network('networks/one-hop-q10.txt') net.loss(self.loss) # setup routes n1 = net.get_node('n1') n2 = net.get_node('n2') n1.add_forwarding_entry(n2.get_address('n1'), n1.links[0]) n2.add_forwarding_entry(n1.get_address('n2'), n2.links[0]) # setup transport t1 = Transport(n1) t2 = Transport(n2) # setup application a = AppHandler(self.filename) window = self.wsize # setup connection drop = [] c1 = TCP(t1, n1.get_address('n2'), 1, n2.get_address('n1'), 1, a, window=window, drop=drop, retran=self.retran) c2 = TCP(t2, n2.get_address('n1'), 1, n1.get_address('n2'), 1, a, window=window) # send a file strin = "" times = [] for i in range(5): with open(self.filename, 'rb') as f: while True: data = f.read(1000) if not data: break Sim.scheduler.add(delay=0, event=data, handler=c1.send) # break Sim.scheduler.run() times.append(Sim.scheduler.current_time()) Sim.scheduler.reset() times.append(sum(times) / len(times)) with open("report/w10000_TESTING.txt", 'w') as f2: f2.writelines(["%s\n" % item for item in times])
def run(self): # parameters Sim.scheduler.reset() logging.getLogger('app').setLevel(logging.INFO) logging.getLogger('bene.link.queue').setLevel(logging.DEBUG) logging.getLogger('bene.tcp.sequence').setLevel(logging.DEBUG) logging.getLogger('bene.tcp.cwnd').setLevel(logging.DEBUG) if self.debug: logging.getLogger('bene.tcp').setLevel(logging.DEBUG) logging.getLogger('bene.tcp.sender').setLevel(logging.DEBUG) logging.getLogger('bene.tcp.receiver').setLevel(logging.DEBUG) # setup network net = Network('../networks/one-hop.txt') net.loss(self.loss) # setup routes n1 = net.get_node('n1') n2 = net.get_node('n2') n1.add_forwarding_entry(n2.get_address('n1'), n1.links[0]) n2.add_forwarding_entry(n1.get_address('n2'), n2.links[0]) # setup transport t1 = Transport(n1) t2 = Transport(n2) # setup application a = AppHandler(self.filename) window = self.window increment = 0 # setup connection drop = [] c1 = TCP(t1, n1.get_address('n2'), 1, n2.get_address('n1'), 1, a, window=window, drop=drop, fast=self.fast, com_seq_num=self.com_seq_num, increment=increment) c2 = TCP(t2, n2.get_address('n1'), 1, n1.get_address('n2'), 1, a, window=window) # send a file with open(self.filename, 'rb') as f: while True: data = f.read(1000) if not data: break Sim.scheduler.add(delay=0, event=data, handler=c1.send) # run the simulation Sim.scheduler.run()
def checkServerMailTCP(payloadToBeChecked): patternT1 = re.compile("HELO", flags=2) # --->124 same 1 #$SMTP_SERVERS 25 destination ip and port msg:"SERVER-MAIL Exim gethostbyname heap buffer overflow attempt" patternT2 = re.compile("\\x0A", flags=2) #-->124 NOT |0A| same 1 # $SMTP_SERVERS 25 destination ip and port msg:"SERVER-MAIL Exim gethostbyname heap buffer overflow attempt" patternT3 = re.compile("EHLO", flags=2) #--->125 not patternT2 same 1 # $SMTP_SERVERS 25 destination ip and port msg:"SERVER-MAIL Exim gethostbyname heap buffer overflow attempt" patternT4 = re.compile("Content-Disposition\\x3A", flags=2) #same 2 --->145 # $SMTP_SERVERS 25 msg "SERVER-MAIL Content-Disposition attachment" patternT5 = re.compile("attachment", flags=2) #same 2 --->145 # $SMTP_SERVERS 25 msg "SERVER-MAIL Content-Disposition attachment" patternT6 = re.compile("WorldMail IMAP4 Server", flags=2) #--->148 #$HOME_NET 143 source ip then source port msg:"SERVER-MAIL Qualcomm WorldMail Server Response" patternT7 = re.compile("BM", flags=2) #-->181 # $SMTP_SERVERS 25 destination msg:"SERVER-MAIL IBM Domino BMP color palette stack buffer overflow attempt" patternT8 = re.compile("\\x00 \\x00 \\x00 \\x00", flags=2) # -->181 # $SMTP_SERVERS 25 destination msg:"SERVER-MAIL IBM Domino BMP color palette stack buffer overflow attempt" patternT9 = re.compile("\\x28 \\x00 \\x00 \\x00|", flags=2) # -->181 # $SMTP_SERVERS 25 destination msg:"SERVER-MAIL IBM Domino BMP color palette stack buffer overflow attempt" patternT10 = re.compile( "GIF89a", flags=2) # -->184,185 -->$FILE_DATA_PORTS source port #$SMTP_SERVERS 25 destination msg:"SERVER-MAIL IBM Lotus Domino Server nrouter.exe malformed GIF parsing remote exploit attempt" patternT11 = re.compile( "\\x21 \\xF9 \\x04", flags=2) # -->184,185-->$FILE_DATA_PORTS source port # $SMTP_SERVERS 25 destination msg:"SERVER-MAIL IBM Lotus Domino Server nrouter.exe malformed GIF parsing remote exploit attempt" patternT12 = re.compile( "\\x00 \\x2C", flags=2) # -->184 ,185--> $FILE_DATA_PORTS source port # $SMTP_SERVERS 25 destination msg:"SERVER-MAIL IBM Lotus Domino Server nrouter.exe malformed GIF parsing remote exploit attempt" if re.search(patternT1, payloadToBeChecked) and (not re.search( patternT2, payloadToBeChecked)) and TCP.getdestinationPort() == "25": print("Alert!!!\t", "SQL sa login failed") if re.search(patternT3, payloadToBeChecked) and TCP.getdestinationPort() == "25": print( "Alert!!!\t", "SERVER-MAIL Exim gethostbyname heap buffer overflow attempt") if re.search(patternT4, payloadToBeChecked) and re.search( patternT5, payloadToBeChecked) and TCP.getdestinationPort() == "25": print("Alert!!!\t", "SERVER-MAIL Content-Disposition attachment") if re.search(patternT6, payloadToBeChecked) and TCP.getsourcePort() == "143": print("Alert!!!\t", "SERVER-MAIL Qualcomm WorldMail Server Response") if re.search(patternT7, payloadToBeChecked) and re.search( patternT8, payloadToBeChecked) and re.search( patternT9, payloadToBeChecked) and TCP.getdestinationPort() == "25": print( "Alert!!!\t", "SERVER-MAIL IBM Domino BMP color palette stack buffer overflow attempt" ) if re.search(patternT10, payloadToBeChecked) and re.search( patternT11, payloadToBeChecked) and re.search( patternT12, payloadToBeChecked) and TCP.getdestinationPort() == "25": print( "Alert!!!\t", "SERVER-MAIL IBM Lotus Domino Server nrouter.exe malformed GIF parsing remote exploit attempt" )
def run(self): # parameters Sim.scheduler.reset() Sim.set_debug('AppHandler') Sim.set_debug('TCP') # setup network if self.use_queue: net = Network('networks/one-hop-queue.txt') else: net = Network('networks/one-hop.txt') net.loss(self.loss) # setup routes n1 = net.get_node('n1') n2 = net.get_node('n2') n1.add_forwarding_entry(address=n2.get_address('n1'), link=n1.links[0]) n2.add_forwarding_entry(address=n1.get_address('n2'), link=n2.links[0]) # setup transport t1 = Transport(n1) t2 = Transport(n2) # setup application a = AppHandler(self.filename) # setup connection c1 = TCP(t1, n1.get_address('n2'), 1, n2.get_address('n1'), 1, a, window=self.window, threshold=self.threshold, fast_recovery=self.fast_recovery) c2 = TCP(t2, n2.get_address('n1'), 1, n1.get_address('n2'), 1, a, window=self.window, threshold=self.threshold, fast_recovery=self.fast_recovery) # send a file with open(self.filename, 'r') as f: while True: data = f.read(1000) if not data: break Sim.scheduler.add(delay=0, event=data, handler=c1.send) # run the simulation Sim.scheduler.run() # print some results print print "========== Overall results ==========" time = Sim.scheduler.current_time() print "Total time: %f seconds" % time avg = numpy.mean(c2.queueing_delay_list) print "Average queueing delay: %f" % avg max = numpy.max(c2.queueing_delay_list) print "Max queueing delay: %f" % max file_size = os.path.getsize(self.filename) print "File size: %i" % file_size throughput = file_size / time print "Throughput: %f" % throughput print "%i,%f,%f,%f,%i,%f\n" % (self.window, time, avg, max, file_size, throughput) if self.loss == 0.0: print "Outputing results to experiment.csv" output_fh = open('experiment.csv', 'a') output_fh.write( "%i,%f,%f,%f,%i,%f\n" % (self.window, time, avg, max, file_size, throughput)) output_fh.close() print "Saving the sequence plot" self.create_sequence_plot(c1.x, c1.y, c1.dropX, c1.dropY, c1.ackX, c1.ackY)
def test_format_data(): test_name = "Test" test_data = "one, two, three, four" expected_message = "b'\\x00\\x00\\x00\\x04'Testb'\\x00\\x00\\x00\\x15'one, two, three, four" assert TCP.format_data(name=test_name, data=test_data) == expected_message
def __init__(self): TCP.__init__(self) self.cwnd = 1000
# This file is part of openWNS (open Wireless Network Simulator) # _____________________________________________________________________________ # # Copyright (C) 2004-2007 # Chair of Communication Networks (ComNets) # Kopernikusstr. 16, D-52074 Aachen, Germany # phone: ++49-241-80-27910, # fax: ++49-241-80-22242 # email: [email protected] # www: http://www.openwns.org # _____________________________________________________________________________ # # openWNS is free software; you can redistribute it and/or modify it under the # terms of the GNU Lesser General Public License version 2 as published by the # Free Software Foundation; # # openWNS is distributed in the hope that it will be useful, but WITHOUT ANY # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # details. # # You should have received a copy of the GNU Lesser General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # ############################################################################### import openwns import tcp.TCP openwns.simulator.OpenWNS.modules.tcp = TCP.TCP()
def checkServerOtherTCP(payloadToBeChecked): patternT1 = re.compile('edit\.action\?', flags=2) #72 #$HOME_NET $HTTP_PORTS destination (msg:"SERVER-OTHER Apache Struts2 skillName remote code execution attempt" patternT2 = re.compile("skillName=\\x7B \\x28 \\x23", flags=2) #72 patternT3 = re.compile("SOAPAction\\x3A,flags=2") #101 #$HOME_NET [$HTTP_PORTS,5555] destination (msg:"SERVER-OTHER MiniUPnPd ExecuteSoapAction buffer overflow attempt" patternT4 = re.compile("\x75", flags=2) #109 #$HOME_NET 3000 (msg:"SERVER-OTHER EMC AlphaStor Device Manager command injection attempt" patternT5 = re.compile("nsrmm", flags=2) #109 # $HOME_NET 3000 (msg:"SERVER-OTHER EMC AlphaStor Device Manager command injection attempt" patternT6 = re.compile("mmpool", flags=2) # 110 # $HOME_NET 3000 (msg:"SERVER-OTHER EMC AlphaStor Device Manager command injection attempt" patternT7 = re.compile("mmlocate", flags=2) # 111 # $HOME_NET 3000 (msg:"SERVER-OTHER EMC AlphaStor Device Manager command injection attempt" patternT8 = re.compile("nsrjb", flags=2) # 112 # $HOME_NET 3000 (msg:"SERVER-OTHER EMC AlphaStor Device Manager command injection attempt" patternT9 = re.compile("\\x18 \\x03 \\x03", flags=2) # 645 #$HOME_NET [21,25,443,465,636,992,993,995,2484] -> $EXTERNAL_NET any # (msg:"SERVER-OTHER OpenSSL TLSv1.2 large heartbeat response - possible ssl heartbleed attempt" patternT10 = re.compile("\\x18 \\x03 \\x02", flags=2) # 646 # $HOME_NET [21,25,443,465,636,992,993,995,2484] -> $EXTERNAL_NET any # (msg:"SERVER-OTHER OpenSSL TLSv1.1 large heartbeat response - possible ssl heartbleed attempt" patternT11 = re.compile("\\x18 \\x03 \\x00", flags=2) # 647 # $HOME_NET [21,25,443,465,636,992,993,995,2484] -> $EXTERNAL_NET any # (msg:"SERVER-OTHER OpenSSL TLSv1 large heartbeat response - possible ssl heartbleed attempt" patternT12 = re.compile("\\x18 \\x03 \\x01", flags=2) # 648 --->652 # $HOME_NET [21,25,443,465,636,992,993,995,2484] -> $EXTERNAL_NET any # (msg:"SERVER-OTHER OpenSSL TLSv3 large heartbeat response - possible ssl heartbleed attempt" patternT13 = re.compile("\\x18 \\x03 \\x03", flags=2) # #$EXTERNAL_NETany -> $HOME_NET[21, 25, 443, 465, 636, 992, 993, 995, 2484](msg:"SERVER-OTHER OpenSSL TLSv1.2 heartbeat read overrun attempt" patternT14 = re.compile("\\x18 \\x03 \\x02", flags=2) # # $HOME_NET 3000 (msg:"SERVER-OTHER EMC AlphaStor Device Manager command injection attempt" patternT15 = re.compile("\\x18 \\x03 \\x00", flags=2) # 112 # $HOME_NET 3000 (msg:"SERVER-OTHER EMC AlphaStor Device Manager command injection attempt" patternT16 = re.compile("\\x18 \\x03 \\x01", flags=2) # 112 # $HOME_NET 3000 (msg:"SERVER-OTHER EMC AlphaStor Device Manager command injection attempt" patternT17 = re.compile("\\x18 \\x03 \\x01", flags=2) # 112 # $HOME_NET 3000 (msg:"SERVER-OTHER EMC AlphaStor Device Manager command injection attempt" patternT18 = re.compile("\\x18 \\x03 \\x01", flags=2) # 112 # $HOME_NET 3000 (msg:"SERVER-OTHER EMC AlphaStor Device Manager command injection attempt" patternT19 = re.compile("\\x18 \\x03 \\x01", flags=2) # 112 # $HOME_NET 3000 (msg:"SERVER-OTHER EMC AlphaStor Device Manager command injection attempt" patternT20 = re.compile("\\x18 \\x03 \\x01", flags=2) # 112 # $HOME_NET 3000 (msg:"SERVER-OTHER EMC AlphaStor Device Manager command injection attempt" if ((re.search(patternT1, payloadToBeChecked)) and (re.search(patternT2, payloadToBeChecked))): print( "Alert!!!\t", "SERVER-OTHER Apache Struts2 skillName remote code execution attempt" ) if (re.search(patternT3, payloadToBeChecked)): print( "Alert!!!\t", "SERVER-OTHER MiniUPnPd ExecuteSoapAction buffer overflow attempt" ) if (re.search(patternT4, payloadToBeChecked) and (TCP.getdestinationPort() == "3000") and re.search(patternT5, payloadToBeChecked)): print( "Alert!!!\t", "SERVER-OTHER EMC AlphaStor Device Manager command injection attempt" ) if (re.search(patternT4, payloadToBeChecked) and (TCP.getdestinationPort() == "3000") and re.search(patternT6, payloadToBeChecked)): print( "Alert!!!\t", "SERVER-OTHER EMC AlphaStor Device Manager command injection attempt" ) if (re.search(patternT4, payloadToBeChecked) and (TCP.getdestinationPort() == "3000") and re.search(patternT7, payloadToBeChecked)): print( "Alert!!!\t", "SERVER-OTHER EMC AlphaStor Device Manager command injection attempt" ) if (re.search(patternT4, payloadToBeChecked) and (TCP.getdestinationPort() == "3000") and re.search(patternT8, payloadToBeChecked)): print( "Alert!!!\t", "SERVER-OTHER EMC AlphaStor Device Manager command injection attempt" ) if (re.search(patternT9, payloadToBeChecked)) and ( TCP.getsourcePort() == "21" or TCP.getsourcePort() == "443" or TCP.getsourcePort() == "25" or TCP.getsourcePort() == "993" or TCP.getsourcePort() == "992" or TCP.getsourcePort() == "636" or TCP.getsourcePort() == "465" or TCP.getsourcePort() == "995" or TCP.getsourcePort() == "2484"): print( "Alert!!!\t", "SERVER-OTHER OpenSSL TLSv1.2 large heartbeat response - possible ssl heartbleed attempt" ) if (re.search(patternT10, payloadToBeChecked)) and ( TCP.getsourcePort() == "21" or TCP.getsourcePort() == "443" or TCP.getsourcePort() == "25" or TCP.getsourcePort() == "993" or TCP.getsourcePort() == "992" or TCP.getsourcePort() == "636" or TCP.getsourcePort() == "465" or TCP.getsourcePort() == "995" or TCP.getsourcePort() == "2484"): print( "Alert!!!\t", "SERVER-OTHER OpenSSL TLSv1.1 large heartbeat response - possible ssl heartbleed attempt" ) if (re.search(patternT11, payloadToBeChecked)) and ( TCP.getsourcePort() == "21" or TCP.getsourcePort() == "443" or TCP.getsourcePort() == "25" or TCP.getsourcePort() == "993" or TCP.getsourcePort() == "992" or TCP.getsourcePort() == "636" or TCP.getsourcePort() == "465" or TCP.getsourcePort() == "995" or TCP.getsourcePort() == "2484"): print( "Alert!!!\t", "SERVER-OTHER OpenSSL TLSv1 large heartbeat response - possible ssl heartbleed attempt" ) if (re.search(patternT12, payloadToBeChecked)) and ( TCP.getsourcePort() == "21" or TCP.getsourcePort() == "443" or TCP.getsourcePort() == "25" or TCP.getsourcePort() == "993" or TCP.getsourcePort() == "992" or TCP.getsourcePort() == "636" or TCP.getsourcePort() == "465" or TCP.getsourcePort() == "995" or TCP.getsourcePort() == "2484"): print( "Alert!!!\t", "SERVER-OTHER OpenSSL TLSv3 large heartbeat response - possible ssl heartbleed attempt" ) if (re.search(patternT9, payloadToBeChecked)) and ( TCP.getdestinationPort() == "21" or TCP.getdestinationPort() == "443" or TCP.getdestinationPort() == "25" or TCP.getdestinationPort() == "993" or TCP.getdestinationPort() == "992" or TCP.getdestinationPort() == "636" or TCP.getdestinationPort() == "465" or TCP.getdestinationPort() == "995" or TCP.getdestinationPort() == "2484"): print( "Alert!!!\t", "SERVER-OTHER OpenSSL TLSv1.2 heartbeat read overrun attempt") if (re.search(patternT10, payloadToBeChecked)) and ( TCP.getdestinationPort() == "21" or TCP.getdestinationPort() == "443" or TCP.getdestinationPort() == "25" or TCP.getdestinationPort() == "993" or TCP.getdestinationPort() == "992" or TCP.getdestinationPort() == "636" or TCP.getdestinationPort() == "465" or TCP.getdestinationPort() == "995" or TCP.getdestinationPort() == "2484"): print( "Alert!!!\t", "SERVER-OTHER OpenSSL TLSv1.1 heartbeat read overrun attempt") if (re.search(patternT11, payloadToBeChecked)) and ( TCP.getdestinationPort() == "21" or TCP.getdestinationPort() == "443" or TCP.getdestinationPort() == "25" or TCP.getdestinationPort() == "993" or TCP.getdestinationPort() == "992" or TCP.getdestinationPort() == "636" or TCP.getdestinationPort() == "465" or TCP.getdestinationPort() == "995" or TCP.getdestinationPort() == "2484"): print("Alert!!!\t", "SERVER-OTHER OpenSSL TLSv1 heartbeat read overrun attempt") if (re.search(patternT12, payloadToBeChecked)) and ( TCP.getdestinationPort() == "21" or TCP.getdestinationPort() == "443" or TCP.getdestinationPort() == "25" or TCP.getdestinationPort() == "993" or TCP.getdestinationPort() == "992" or TCP.getdestinationPort() == "636" or TCP.getdestinationPort() == "465" or TCP.getdestinationPort() == "995" or TCP.getdestinationPort() == "2484"): print("Alert!!!\t", "SERVER-OTHER OpenSSL TLSv3 heartbeat read overrun attempt")