本文整理汇总了Python中src.sim.Sim类的典型用法代码示例。如果您正苦于以下问题:Python Sim类的具体用法?Python Sim怎么用?Python Sim使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Sim类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
def run(self):
# parameters
Sim.scheduler.reset()
Sim.set_debug(True)
# setup network
net = Network('../networks/one-hop.txt')
# 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 = StopAndWait(t1,n1.get_address('n2'),1,n2.get_address('n1'),1,a)
c2 = StopAndWait(t2,n2.get_address('n1'),1,n1.get_address('n2'),1,a)
# 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()
示例2: retransmit
def retransmit(self,event):
if self.send_base >= len(self.send_buffer):
return
Sim.trace("%d's retransmission timer fired" % (self.source_address))
self.loss_detected()
packet = self.send_one_packet(self.send_base)
self.timer = Sim.scheduler.add(delay=self.timeout, event='retransmit', handler=self.retransmit)
示例3: receive_data
def receive_data(self,data):
global original_size
global received_size
Sim.trace('AppHandler',"application got %d bytes" % (len(data)))
self.f.write(data)
received_size += len(data)
self.f.flush()
示例4: handle_packet
def handle_packet(self,packet):
# handle ACK
if packet.ack_number > self.window_start and packet.ack_number <= self.sequence:
# this acks new data, so advance the window with slide_window()
Sim.trace("%d received My_RTP ACK from %d for %d" % (packet.destination_address,packet.source_address,packet.ack_number))
self.slide_window(packet.ack_number)
# handle data
if packet.length > 0:
self.pkts_rcvd += 1
Sim.trace("%d received My_RTP segment from %d for %d" % (packet.destination_address,packet.source_address,packet.sequence))
# if the packet is the one we're expecting increment our
# ack number and add the data to the receive buffer
if packet.sequence >= self.ack:
self.receive_buffer.append(packet)
if packet.sequence == self.ack:
self.receive_buffer = sorted(self.receive_buffer, key=lambda TCPPacket: TCPPacket.sequence)
while self.receive_buffer and (self.ack == self.receive_buffer[0].sequence):
pkt = self.receive_buffer.pop(0)
self.increment_ack(pkt.sequence + pkt.length)
# deliver data that is in order
self.app.handle_packet(pkt)
# always send an ACK
self.send_ack()
if packet.queueing_delay > self.pkt_q_delay_threshold:
self.queueing_delay += packet.queueing_delay
print "\n[Average Queuing Delay so far:", str(self.queueing_delay / self.pkts_rcvd) + "]"
print "\n[Total Queueing Delay so far:", str(self.queueing_delay) + "]\n"
示例5: handle_ack
def handle_ack(self, packet):
Sim.trace("%d received ReliableTransport ACK from %d for %d" % (packet.destination_address,packet.source_address,packet.ack_number))
self.unacked_packet_count -= ((packet.ack_number - self.received_ack_number) / self.mss)
self.cancel_timer()
self.timer = Sim.scheduler.add(delay=self.timeout, event='new_ack_data', handler=self.retransmit)
self.received_ack_number = packet.ack_number
self.send_if_possible()
示例6: send_one_packet
def send_one_packet(self, sequence):
if sequence >= len(self.send_buffer):
return
if sequence + self.mss > len(self.send_buffer):
body = self.send_buffer[sequence : ]
else:
body = self.send_buffer[sequence : sequence + self.mss]
# get one packet worth of data
packet = TCPPacket(source_address=self.source_address,
source_port=self.source_port,
destination_address=self.destination_address,
destination_port=self.destination_port,
body=body,
sequence=sequence,ack_number=self.ack)
# send the packet
Sim.trace("%d sending My_RTP segment to %d for %d" % (self.source_address,self.destination_address,packet.sequence))
self.transport.send_packet(packet)
# set a timer
if not self.timer_set:
self.timer = Sim.scheduler.add(delay=self.timeout, event='retransmit', handler=self.retransmit)
self.timer_set = True
return packet
示例7: run
def run(self):
# parameters
Sim.scheduler.reset()
Sim.set_debug(True)
# setup network
n1 = Node()
n2 = Node()
l = Link(address=1,startpoint=n1,endpoint=n2,queue_size=self.queue_size,bandwidth=10000000,propagation=0.01,loss=self.loss, printOut=True)
n1.add_link(l)
n1.add_forwarding_entry(address=2,link=l)
l = Link(address=2,startpoint=n2,endpoint=n1,queue_size=self.queue_size,bandwidth=10000000,propagation=0.01,loss=self.loss)
n2.add_link(l)
n2.add_forwarding_entry(address=1,link=l)
# setup transport
t1 = Transport(n1)
t2 = Transport(n2)
# setup application
a = AppHandler("2_1-" + self.filename)
# setup connection
c1 = My_RTP(t1,1,1,2,1,a)
c2 = My_RTP(t2,2,1,1,1,a)
# setup application
a = AppHandler("2_2-" + self.filename)
# setup connection
c3 = My_RTP(t1,1,2,2,2,a)
c4 = My_RTP(t2,2,2,1,2,a)
# send a file
with open(self.filename,'r') as f:
while True:
data = f.read(1000)
if not data:
break
c1.load_buffer(data)
c3.load_buffer(data)
c1.set_file_prefix("2_1")
c2.set_file_prefix("2_1")
c3.set_file_prefix("2_2")
c4.set_file_prefix("2_2")
c1.open_window_file()
c3.open_window_file()
Sim.scheduler.add(delay=0, event="window_init", handler=c1.window_init)
Sim.scheduler.add(delay=0, event="window_init", handler=c3.window_init)
# run the simulation
Sim.scheduler.run()
n1.links[0].myfile.close()
c1.close_window_file()
c3.close_window_file()
Sim.close_rate_file()
示例8: send_ack
def send_ack(self):
packet = TCPPacket(source_address=self.source_address,
source_port=self.source_port,
destination_address=self.destination_address,
destination_port=self.destination_port,
sequence=self.sequence,ack_number=self.ack)
# send the packet
Sim.trace("%d sending ReliableTransport ACK to %d for %d" % (self.source_address,self.destination_address,packet.ack_number))
self.transport.send_packet(packet)
示例9: send_ack
def send_ack(self):
packet = TCPPacket(source_address=self.source_address,
source_port=self.source_port,
destination_address=self.destination_address,
destination_port=self.destination_port,
sequence=self.sequence,ack_number=self.ack, flow_id=int(self.file_prefix.split("_")[-1]))
# send the packet
Sim.trace("%d sending My_RTP ACK to %d for %d" % (self.source_address,self.destination_address,packet.ack_number))
self.transport.send_packet(packet)
示例10: send_cumulative_ack
def send_cumulative_ack(self):
sequences = sorted(self.receive_temp_buffer)
for i in range(self.ack/self.mss, len(sequences)):
if sequences[i] == self.ack:
pkt = self.find_packet(self.ack)
self.increment_ack(pkt.sequence + pkt.length)
self.app.handle_packet(pkt)
Sim.trace_custom("%d %d" % (pkt.sequence, pkt.length), 'rate', self.source_port)
self.send_ack()
示例11: run
def run(self):
# parameters
Sim.scheduler.reset()
if "a" in self.debug:
Sim.set_debug('AppHandler')
if "t" in self.debug:
Sim.set_debug('TCP')
# setup network
networkPlotter = Plotter('out/2-flows-simple')
net = Network(config='networks/one-hop.txt',plotter=networkPlotter)
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 connection
c1 = TCP(t1,n1.get_address('n2'),1,n2.get_address('n1'),1,AppHandler(inputfile=self.inputfile,identifier="c1"),window=self.window,type=self.type,window_size_plot=True,sequence_plot=True)
c2 = TCP(t2,n2.get_address('n1'),1,n1.get_address('n2'),1,AppHandler(inputfile=self.inputfile,plot=True,identifier="c2"),window=self.window,type=self.type,receiver_flow_plot=True)
c3 = TCP(t1,n1.get_address('n2'),2,n2.get_address('n1'),2,AppHandler(inputfile=self.inputfile,identifier="c3"),window=self.window,type=self.type,window_size_plot=True,sequence_plot=True)
c4 = TCP(t2,n2.get_address('n1'),2,n1.get_address('n2'),2,AppHandler(inputfile=self.inputfile,plot=True,identifier="c4"),window=self.window,type=self.type,receiver_flow_plot=True)
global tcps
tcps = [c1, c2, c3, c4]
global original_size
f = open(self.inputfile, "rb")
try:
data = f.read(1000)
while data != "":
original_size += len(data)
Sim.scheduler.add(delay=0, event=data, handler=c1.send)
Sim.scheduler.add(delay=0, event=data, handler=c3.send)
data = f.read(1000)
finally:
f.close()
# run the simulation
global decisecondEvent
decisecondEvent = Sim.scheduler.add(delay=0.1, event=Sim, handler=self.decisecond)
Sim.scheduler.run()
networkPlotter.plot(self.sequencefile)
plotter.plot(self.sequencefile);
示例12: send_ack
def send_ack(self):
packet = TCPPacket(source_address=self.source_address,
source_port=self.source_port,
destination_address=self.destination_address,
destination_port=self.destination_port,
sequence=self.next_sequence_num,ack_number=self.ack)
# since we are sending ACK, we're not waiting to send cumulative ACK anymore
self.next_inorder_seg_pending = False
# send the packet
Sim.trace("SEND ACK: %d ===> %d : %d" % (self.source_address,self.destination_address,packet.ack_number))
self.transport.send_packet(packet)
示例13: handle_packet
def handle_packet(self,packet):
# handle ACK (Sender)
# if the ACK number is greater than our send base...
# update the acknowledged segment counter, restart timer and send if possible
if packet.ack_number > self.send_base:
Sim.trace("ACK RCVD: %d <=== %d : %d" % (packet.destination_address,packet.source_address,packet.ack_number))
self.not_yet_acknowledged_segments -= ((packet.ack_number - self.send_base) / self.mss)
self.cancel_timer()
self.timer = Sim.scheduler.add(delay=self.timeout, event='new_ack_data', handler=self.retransmit)
self.send_base = packet.ack_number
self.send_if_possible()
else:
self.triple_dup_ack_counter+=1
if self.triple_dup_ack_counter == 3:
Sim.trace("FAST RETRANSMIT: Duplicate ACK: %d"%(packet.ack_number))
Sim.trace("Outstanding acks: %d" % self.not_yet_acknowledged_segments)
self.send_one_packet(packet.ack_number)
self.triple_dup_ack_counter = 0
# handle data (Receiver)
if packet.length > 0:
Sim.trace("SEGMENT RCVD: %d <=== %d : %d" % (packet.destination_address,packet.source_address,packet.sequence))
self.receive_temp_buffer.add(packet.sequence)
self.receive_packet_buffer.append(packet)
self.send_cumulative_ack()
示例14: retransmit
def retransmit(self,event):
self.timer_set = False
if self.packets_outstanding <= 0:
return
self.ssthresh = int(max(math.ceil(self.window_size / 2.0), 1))
self.window_size = 1
self.bytes_acked = 0
self.packets_outstanding = 1
Sim.trace("%d retransmission timer fired" % (self.source_address))
packet = self.send_one_packet(self.window_start)
if packet:
self.sequence = self.window_start + packet.length
self.window_file.write(str(Sim.scheduler.current_time()) + " " + str(self.window_size) + "\n")
示例15: handle_ack
def handle_ack(self, packet):
print >> self.ack_file, Sim.scheduler.current_time(), packet.ack_number, packet.length
Sim.trace("%d received TcpTahoe ACK from %d for %d" % (packet.destination_address,packet.source_address,packet.ack_number))
new_bytes = packet.ack_number - self.received_ack_number
self.unacked_packet_count -= new_bytes
if self.cwnd >= self.threshold:
self.cwnd += (self.mss*new_bytes)/self.cwnd
self.threshold = self.cwnd
else:
self.cwnd += new_bytes
self.cancel_timer()
self.timer = Sim.scheduler.add(delay=self.timeout, event='new_ack_data', handler=self.retransmit)
self.send_if_possible()
self.received_ack_number = packet.ack_number