コード例 #1
0
def main():
    print("Run this on two different computers.")
    mem = netmem.NetworkMemory()
    mem.connect_on_new_thread(netmem.UdpConnector(local_addr=("225.0.0.1", 9991)))

    tk1 = tk.Tk()
    lbl = tk.Label(tk1, text="Favorite operating system:")
    lbl.pack()
    txt = tk.Entry(tk1, textvariable=mem.tk_var("fav_os"))
    txt.pack()

    tk1.mainloop()
コード例 #2
0
    def __init__(self, root, connector):
        self.window = root
        root.title("TicTacToe {}".format(str(connector)))
        self.log = logging.getLogger(__name__)

        # Data
        self.netmem = netmem.NetworkMemory()

        # View
        self._grid_buttons = None  # type: [[tk.Button]
        self.create_widgets()

        # Connections
        self.netmem.connect_on_new_thread(connector)
        self.netmem.add_listener(self._netmem_changed)
        self.reset_game()
コード例 #3
0
ファイル: netmem_tk_udp.py プロジェクト: rharder/netmem
    def __init__(self, root, connector):
        self.window = root
        root.title("NetMem {}".format(str(connector)))
        self.log = logging.getLogger(__name__)

        # Data
        self.netmem = netmem.NetworkMemory()
        self.key_var = tk.StringVar()
        self.val_var = tk.StringVar()
        self.data_var = tk.StringVar()

        # View / Control
        self.create_widgets()

        # Connections
        self.netmem.add_listener(self.memory_updated)
        self.netmem.connect_on_new_thread(connector)

        self.key_var.set("pet")
        self.val_var.set("cat")
コード例 #4
0
def example_NetworkMemory():
    loop = asyncio.get_event_loop()

    mem1 = netmem.NetworkMemory()
    mem2 = netmem.NetworkMemory()
    mem3 = netmem.NetworkMemory()

    def _when_mem1_changes(var: netmem.NetworkMemory, name, old_val, new_val):
        print("mem1 '{}' changed from {} to {}: {}".format(
            name, old_val, new_val, str(var)))

    def _when_mem2_changes(var: netmem.NetworkMemory, name, old_val, new_val):
        print("mem2 '{}' changed from {} to {}: {}".format(
            name, old_val, new_val, str(var)))

    def _when_mem3_changes(var: netmem.NetworkMemory, name, old_val, new_val):
        print("mem3 '{}' changed from {} to {}: {}".format(
            name, old_val, new_val, str(var)))

    mem1.add_listener(_when_mem1_changes)
    mem2.add_listener(_when_mem2_changes)
    mem3.add_listener(_when_mem3_changes)

    # Connect just mem1 and mem2 together using multicast on local machines
    # : Uncomment below to try it
    mem1.connect(
        netmem.UdpConnector(local_addr=("225.0.0.1", 9991),
                            remote_addr=("225.0.0.2", 9992)))
    mem2.connect(
        netmem.UdpConnector(local_addr=("225.0.0.2", 9992),
                            remote_addr=("225.0.0.1", 9991)))

    # The LoggingConnector simply logs changes to the memory.  It does not listen for changes from anything.
    # : Uncomment below to try it
    mem1.connect(netmem.LoggingConnector())

    # Connect three together with websockets, one acting as the server and the other as clients.
    # The websocket connectors support two-way communication, so even though one is hosting the
    # http server, all three with be notified of changes to each other.
    # : Uncomment below to try it
    # wss = mem1.connect(netmem.WsServerConnector(port=8080))
    # mem2.connect(netmem.WsClientConnector(url=wss.url_ws_updates))
    # mem3.connect(netmem.WsClientConnector(url=wss.url_ws_updates))
    async def prompt():
        await asyncio.sleep(1)
        while True:
            key = input("Key? ")
            if key == "":
                await asyncio.sleep(0.5)
                continue
            value = input("Value? ")
            mem1.set(key, value)
            await asyncio.sleep(
                0.5
            )  # Because input() locks up the event loop, when need to yield some time

    asyncio.ensure_future(prompt())

    try:
        print("Starting loop...")
        loop.run_forever()
    except KeyboardInterrupt:
        sys.exit(1)
    loop.close()