Exemplo n.º 1
0
    def lfu(cls):
        from src.bus import Bus
        smallest_timestamp = {'timestamp': 9999999999999999}
        for instruct in cls.cache:
            if instruct['timestamp'] < smallest_timestamp['timestamp']:
                smallest_timestamp = instruct

        Bus.update_ram(smallest_timestamp['address'], smallest_timestamp)

        index = cls.cache.index(smallest_timestamp)
        del cls.cache[index]
Exemplo n.º 2
0
 def save_instruction(cls, bt_arr: list):
     cls.instruction = bt_arr
     b = Bus()
     print(str(bt_arr[1]))
     if re.match(r'0x(\w+)', bt_arr[1].decode('utf-8')):
         address = bt_arr[1].decode('utf-8')
     else:
         address = random.choice(string.ascii_uppercase) + str(
             random.randint(0, 9))
     print('address: ', address)
     b.set_address_bus(address, cls.control)
     b.set_data_bus(cls.instruction, cls.control)
     b.send_to_ram(cls.control)
     b.send_interruption(address, cls.control)
     cls.control += 1
Exemplo n.º 3
0
 def check_cache(cls, ram_address, control):
     instruction = next(
         (instruct for instruct in cls.cache if ram_address in cls.cache),
         None)
     if instruction:
         return instruction
     else:
         from src.bus import Bus
         instruction = Bus.fetch_from_ram(ram_address, control)
         if len(cls.cache) >= 10:
             cls.lfu()
         cls.cache.append({
             ram_address: instruction,
             'timestamp': time.time()
         })
         return instruction
Exemplo n.º 4
0
 def setUp(self):
     self.bus = Bus(22, "Ocean Terminal")
Exemplo n.º 5
0
class TestBus(unittest.TestCase):
    def setUp(self):
        self.bus = Bus(22, "Ocean Terminal")

    #@unittest.skip("Delete this line to run the test")
    def test_has_route_number(self):
        self.assertEqual(22, self.bus.route_number)

    #@unittest.skip("Delete this line to run the test")
    def test_has_destination(self):
        self.assertEqual("Ocean Terminal", self.bus.destination)

    #@unittest.skip("Delete this line to run the test")
    def test_can_drive(self):
        self.assertEqual("Brum brum", self.bus.drive())

    #@unittest.skip("Delete this line to run the test")
    def test_starts_with_no_passengers(self):
        self.assertEqual(0, self.bus.passenger_count())

    #@unittest.skip("Delete this line to run the test")
    def test_can_pick_up_passenger(self):
        person = Person("Guido van Rossum", 64)
        self.bus.pick_up(person)
        self.assertEqual(1, self.bus.passenger_count())

    #@unittest.skip("Delete this line to run the test")
    def test_can_drop_off_passenger(self):
        person = Person("Guido van Rossum", 64)
        self.bus.pick_up(person)
        self.bus.drop_off(person)
        self.assertEqual(0, self.bus.passenger_count())

    #@unittest.skip("Delete this line to run the test")
    def test_can_empty_bus(self):
        person = Person("Guido van Rossum", 64)
        self.bus.pick_up(person)
        self.bus.empty()
        self.assertEqual(0, self.bus.passenger_count())

    #@unittest.skip("Delete this line to run the test")
    def test_can_pick_up_passenger_from_bus_stop(self):
        person_1 = Person("Guido van Rossum", 64)
        person_2 = Person("Carol Willing", 50)
        bus_stop = BusStop("Waverly Station")
        bus_stop.add_to_queue(person_1)
        bus_stop.add_to_queue(person_2)
        self.bus.pick_up_from_stop(bus_stop)
        self.assertEqual(2, self.bus.passenger_count())
        self.assertEqual(0, bus_stop.queue_length())
Exemplo n.º 6
0
 def ask_for_ram(address, control):
     from src.bus import Bus
     print('cpu address: ', address)
     addr = Bus.fetch_from_ram(address, control)
     CPU.process_instruction(addr['instruction'])
     time.sleep(0.1)
Exemplo n.º 7
0
from typing import Optional, List
import asyncio

from fastapi import FastAPI
from pydantic import BaseModel
from starlette.websockets import WebSocket

from src.bus import Bus
from src.redis_app import get_subscriber, redis_send

CHANNEL = "main"
app = FastAPI()
websockets: List[WebSocket] = []
bus = Bus()


class Item(BaseModel):
    name: str
    price: float
    is_offer: Optional[bool] = None


# in-memory
@app.post("/ws/write")
async def post_ws(item: Item):
    global websockets
    for ws in websockets:
        await ws.send_text(item.json())
    return {"item": item}