def pong_handler(event, source=None): cmd = Message('PING', next(pyons.get_model().command_index)) pyons.get_model().num_pings_generated += 1 pyons.create_timeout(pyons.get_model().next_ping_timeout, cmd) pyons.info("received {}, sending {} in {} sec.".format( event, cmd, pyons.get_model().next_ping_timeout), sender="pong_handler")
def _handle_generate_vehicle(self, event, source): msg, lane = event assert source is self and msg == 'generate vehicle' model = pyons.get_model() vehicle = Factory().build_vehicle(lane, model.channel) model.add_vehicle(vehicle) pyons.create_timeout(self.params.vehicle_generation_interval(), ('generate vehicle', lane)) self.n_generated += 1 if self.n_generated % 10 == 0: print("{:4.1f} [GENERATOR] created {} vehicles".format( systime.time() - self.t_started, self.n_generated))
def handle_update_position(event, source): assert event == UPDATE_POSITION_EVENT and source is None model = pyons.get_model() for tag in model.tags: assert isinstance(tag, Tag) tag.update_position() tag.update_received_power() j = journal.Journal() if j.channel_state_logging_enabled: rec = build_channel_state_record(model.reader, tag, model.channel) j.write_channel_state(rec) pyons.create_timeout(model.position_update_interval, UPDATE_POSITION_EVENT)
def handle_response(self, event, source): if not isinstance(source, Server): raise RuntimeError("expected events from server only") self.num_responses_received += 1 pyons.info("received {} from the server".format(event), self) self.next_request_timeout_id = pyons.create_timeout( self.request_interval, "timeout")
def receive_finished(self, frame, rx_power, snr=None, ber=None): if frame is None: # pyons.fine("receive error {snr}{ber}".format( # snr=('' if snr is None else ' snr={:.2f}dB'.format(snr)), # ber=('' if ber is None else ' ber={:.2f}'.format(ber))), # sender=self.name) self._reply_timeout_id = pyons.create_timeout( gen2.max_t2(self.blf), Reader.REPLY_TIMEOUT_EVENT) return assert isinstance(frame, gen2.TagFrame) # pyons.fine("received: {reply}, power={power}dBm{snr}{ber}".format( # reply=frame.reply, power=rx_power, # snr=('' if snr is None else ' snr={:.2f}dB'.format(snr)), # ber=('' if ber is None else ' ber={:.2f}'.format(ber))), # sender=self.name) self._cancel_reply_timeout() self._cancel_delayed_send() self._last_rx_end = pyons.time() reply = frame.reply if isinstance(reply, gen2.Rn16Reply): self._handle_rn16_reply(reply) elif isinstance(reply, gen2.AckReply): self._handle_ack_reply(reply) elif isinstance(reply, gen2.ReqRnReply): self._handle_reqrn_reply(reply) elif isinstance(reply, gen2.ReadReply): self._handle_read_reply(reply) else: raise RuntimeError("reader doesn't support reply {}".format( type(reply)))
def generate(self, event, source): assert source is self and event == "timeout" client_id = self.next_client_id pyons.info("building a new client #{}".format(client_id), self) client = Client(client_id, self.server, self.max_requests, self.request_interval) pyons.add_entity(client) self.next_timeout_id = pyons.create_timeout(self.timeout, "timeout") self.next_client_id += 1
def _signal_transmission_started(self): if self._state != Signal.State.INIT: return if self.sender is None: raise pyons.errors.MissingFieldError(self.__class__.__name__, 'sender') if self.receiver is None: raise pyons.errors.MissingFieldError(self.__class__.__name__, 'receiver') if self.frame is None: raise pyons.errors.MissingFieldError(self.__class__.__name__, 'frame') self.update_transmitter_power(self.initial_power) distance = vectors.length(self.receiver.node.antenna.position - self.sender.node.antenna.position) delay = distance / SPEED_OF_LIGHT duration = self.frame.duration self._begin_timeout_id = pyons.create_timeout(delay, 'rx-begin') self._end_timeout_id = pyons.create_timeout(duration + delay, 'rx-end') self._state = Signal.State.STARTED
def power_on(self): if self.is_powered_on: return # pyons.fine("powered on", sender=self.name) self._cancel_power_timeout() self._power = self.max_tx_power self._last_powered_on = pyons.time() self._inventory_flag = gen2.InventoryFlag.A self.transceiver.set_power(self._power) for transceiver in self.channel.passive_transceivers: transceiver.node.update_received_power() if self.power_on_interval is not None: self._power_off_timeout_id = pyons.create_timeout( self.power_on_interval, Reader.POWER_OFF_TIMEOUT_EVENT) self._handle_round_start()
def power_off(self): if not self.is_powered_on: return # pyons.fine("powered off", sender=self.name) self._power = None self._last_powered_off = pyons.time() self._inventory_flag = None self._cancel_power_timeout() self._cancel_delayed_send() self._cancel_reply_timeout() self.transceiver.set_power(None) self.transceiver.clear() for transceiver in self.channel.passive_transceivers: transceiver.node.update_received_power() if self.power_off_interval is not None: self._power_on_timeout_id = pyons.create_timeout( self.power_off_interval, Reader.POWER_ON_TIMEOUT_EVENT)
def send(self, frame): assert isinstance(self.channel, Channel) if len(self._tx_signals) > 0: raise RuntimeError("send() while another TX is running") peers = self.channel.get_peers(self) for peer in peers: if peer.node.turned_on: signal = Signal(sender=self, receiver=peer, frame=frame, power=self.power, channel=self.channel) self._tx_signals.append(signal) pyons.add_entity(signal) self._tx_end_timeout = pyons.create_timeout(frame.duration, 'tx-end') # If start sending, nothing would be received anyway for signal in self._rx_signals: if signal.state != Signal.State.FINISHED: signal.broken = True
def _send(self, cmd): if isinstance(cmd, gen2.Query): preamble = gen2.ReaderFrame.Preamble(tari=self.tari, rtcal=self.rtcal, trcal=self.trcal) else: preamble = gen2.ReaderFrame.Sync(tari=self.tari, rtcal=self.rtcal) frame = gen2.ReaderFrame(preamble=preamble, cmd=cmd) # pyons.fine("send: {cmd}, duration={duration:.2f}us".format( # cmd=cmd, duration=frame.duration*1e6), sender=self.name) self._cancel_delayed_send() t2 = gen2.min_t2(self.blf) time_from_rx_end = pyons.time() - self._last_rx_end if time_from_rx_end >= t2: self.transceiver.send(frame) else: dt = t2 - time_from_rx_end self._delayed_send_timeout_id = pyons.create_timeout( dt, (Reader.DELAYED_SEND_EVENT, frame))
def _create_command_timeout(self): self._cancel_command_timeout() self._command_timeout_id = pyons.create_timeout( gen2.max_t2(self.blf), Tag.COMMAND_TIMEOUT_EVENT)
def ping_handler(event, source=None): reply = Message('PONG', event.index) pyons.get_model().num_pongs_generated += 1 pyons.create_timeout(pyons.get_model().reply_timeout, reply) pyons.info("received {}, sending {} in {} sec.".format( event, reply, pyons.get_model().reply_timeout), sender="ping_handler")
def initialize(): pyons.info("PingPong is initializing", sender="initialize") cmd = Message('PING', next(pyons.get_model().command_index)) pyons.get_model().num_pings_generated += 1 pyons.create_timeout(0, cmd)
def _initializer(self): self.n_generated = 0 self.t_started = systime.time() for lane in range(0, self.params.lanes_number): pyons.create_timeout(self.params.vehicle_generation_interval(), ('generate vehicle', lane))
def initialize(self): self.next_timeout_id = pyons.create_timeout(0, "timeout")
def send_finished(self): self._last_tx_end = pyons.time() dt = gen2.max_t1(rtcal=self.rtcal, blf=self.blf) + gen2.t3() self._reply_timeout_id = pyons.create_timeout( dt, Reader.REPLY_TIMEOUT_EVENT)
def init_mobility_manager(): model = pyons.get_model() pyons.create_timeout(model.position_update_interval, UPDATE_POSITION_EVENT)