Esempio n. 1
0
 def add_events(self, evts):
     """PrtEvent objects should be contained in a iterable container."""
     self.list.extend(evts)
     heapq.heapify(self.list)
     # If adding events during the sim,
     if self.active() or Sim.now() > 0:
         Sim.reactivate(self, prior=True)
Esempio n. 2
0
 def embark(self, vehicle, passengers, cmd_msg, cmd_msg_id):
     """If ordering matters, note that passengers at the end of the list
     are serviced first."""
     assert not self._busy
     self._action = Berth.EMBARK
     self._fnc_args = (vehicle, passengers, cmd_msg, cmd_msg_id)
     if self.passive:
         Sim.reactivate(self, prior=True)
Esempio n. 3
0
    def send(self, msg_type, msg):
        """Send an outgoing message.
        Every message sent is prefixed with:
               msg seperator: 2 bytes, signed -32123
               msg type: 2 bytes, signed
               msg ID: 4 bytes, signed
               msg time: 4 bytes, signed
               msg size: 4 bytes, signed
        The message seperator is always -32123
        The msg type is one of the values found in CtrlMsgType or SimMsgType
        The msg size is the length (in bytes) of the serialized message.
        These values are transmitted in network byte order (Big Endian).

        TEMP: Transmits to all clients (no filtering)
        """
        msgID = common.msg_id_widget.next_id()
        msg_time = ms_now()
        msg_str = msg.SerializeToString()
        msg_size = len(msg_str)
        # big_endian, *std size* and align: short, short, int, int, int
        # short = 2 bytes, int = 4 bytes
        header = struct.pack('>hhiii', api.MSG_SEP, msg_type, msgID,
                             msg_time, msg_size)
        message = header + msg_str

        # TODO: Only send to consumers that are subscribed to that message type
        # and/or message target.
        for q in self.sendQs:
            q.put(message)

        if self.passive() and not common.sim_ended:
            # Reset the resume_list to all False
            self.resume_list[:] = [False] * len(self.resume_list)
            # Reactivate the ControlInterface, placing it at the end of the
            # list of events to run.
            Sim.reactivate(self, prior=False)

        if self.log != os.devnull:
            self.log.write("Now:%s, SENT, %s, MsgType:%d, MsgID:%d, MsgTime:%d\n" %\
                  (time.time(), msg.DESCRIPTOR.name, msg_type, msgID, msg_time))
            text_format.PrintMessage(msg, out=self.log)
            self.log.write("--\n")
Esempio n. 4
0
    def run(self):
        station = common.stations[self.station_id]
        while True:
            # Unloading
            if self._unload:
                for pax in reversed(self.vehicle.passengers):
                    self._unload = False
                    self._busy = True
                    yield Sim.hold, self, pax.unload_delay
                    del self.vehicle.passengers[-1] # pax that left vehicle
                    pax.loc = self.station
                    pax.trip_end = Sim.now()
                    if self.station_id == pax.dest_station.ID:
                        pax.trip_success = True
                        common.delivered_pax.add(pax)
                        logging.info("T=%4.3f %s delivered to %s by %s. Unloaded in berth %s",
                                      Sim.now(), pax, self.station_id, self.vehicle, self.label)

                self._busy = False

                if station.passive():
                    Sim.reactivate(station, prior = True)

            # Loading
            elif self._load:
                for pax in self._pax:
                    self._load = False
                    self._busy = True
                    s_notify = api.SimNotifyPassengerLoadStart()
                    s_notify.vID = self.vehicle.ID
                    s_notify.sID = self.station_id
                    s_notify.pID = pax.ID
                    common.interface.send(api.SIM_NOTIFY_PASSENGER_LOAD_START,
                                          s_notify)
                    yield Sim.hold, self, pax.load_delay
                    self.vehicle.passengers.append(pax)
                    pax.trip_boarded = Sim.now()
                    pax.loc = self.vehicle
                    logging.info("T=%4.3f %s loaded into %s at station %s",
                                 Sim.now(), pax, self.vehicle, self.station_id)
                    e_notify = api.SimNotifyPassengerLoadEnd()
                    e_notify.vID = self.vehicle.ID
                    e_notify.sID = self.station_id
                    e_notify.pID = pax.ID
                    common.interface.send(api.SIM_NOTIFY_PASSENGER_LOAD_END,
                                          e_notify)
                    # If using the LOBBY policy, notify that passenger load command
                    # has completed.
                    if self._load_msgID:
                        cmd_notify = api.SimCompletePassengerLoadVehicle()
                        cmd_notify.msgID = self._msgID
                        cmd_notify.vID = self.vehicle.ID
                        cmd_notify.sID = self.station_id
                        cmd_notify.pID = pax.ID
                        common.interface.send(api.SIM_COMPLETE_PASSENGER_LOAD_VEHICLE,
                                          cmd_notify)
                        self._load_msgID = None

                self._busy = False
                if station.passive():
                    Sim.reactivate(station, prior = True)

            else:
                assert not self._busy
                yield Sim.passivate, self
Esempio n. 5
0
 def load(self, msg_id, passengers):
     self._load = True
     self._msg_id = msg_id
     self._pax = passengers
     if self.passive:
         Sim.reactivate(self, prior=True)
Esempio n. 6
0
 def add_event(self, evt):
     """Add a single PrtEvent object"""
     heapq.heappush(self.list, evt)
     if self.active() or Sim.now() > 0:
         Sim.reactivate(self, prior=True)
Esempio n. 7
0
 def delayed_msg(msg):
     if msg:
         interface.receiveQ.put(msg)
     if interface.passive():
         Sim.reactivate(interface, prior=False)
Esempio n. 8
0
 def exit_storage(self, position, model_name, cmd_msg, cmd_msg_id):
     assert not self._busy
     self._action = Berth.EXIT_STORAGE
     self._fnc_args = (position, model_name, cmd_msg, cmd_msg_id)
     if self.passive:
         Sim.reactivate(self, prior=True)
Esempio n. 9
0
 def enter_storage(self, vehicle, cmd_msg, cmd_msg_id):
     assert not self._busy
     self._action = Berth.ENTER_STORAGE
     self._fnc_args = (vehicle, cmd_msg, cmd_msg_id)
     if self.passive:
         Sim.reactivate(self, prior=True)