예제 #1
0
async def cast_address(sock: Socket, device_name: str, addr: str) -> None:
    await sock.send_multipart([
        Frame(b"device.cast_address"),
        Frame(bytes(device_name, "utf8")),
        Frame(bytes(addr, 'utf8'))
    ])
    result: bytes = await sock.recv()
    assert result[0] == 0
예제 #2
0
async def ping(sock: Socket, device_name: str) -> str:
    await sock.send_multipart(
        [Frame(b"ping"),
         Frame(bytes(device_name, encoding='utf8'))])
    frames: List[Frame] = await sock.recv_multipart(copy=False)
    command_frame = frames.pop(0)
    assert (command_frame.bytes == b"pong")
    addr_frame = frames.pop(0)
    return str(addr_frame, encoding='utf8')
예제 #3
0
 def _distribute(self, *parts):
     drop = set()
     empty = Frame(b'')
     frames = [empty, empty, Frame(b'VIP1'), empty, empty]
     frames.extend(Frame(f) for f in parts)
     for peer in self._peers:
         frames[0] = peer
         drop.update(self._send(frames))
     for peer in drop:
         self._drop_peer(peer)
예제 #4
0
def casting_address_handler(store: DirectoryServerStore, sock: Socket,
                            argframes: List[Frame], id_frame: Frame) -> None:
    device_name = str(argframes.pop(0).bytes, encoding='utf8')
    address = str(argframes.pop(0).bytes, encoding='utf8')
    if device_name not in store.devices:
        store.devices[device_name] = Device(device_name, [])
    device = store.devices[device_name]
    if address not in device.cast_addresses:
        device.cast_addresses.append(address)
        print("Device {} casted entry point {}".format(device_name, address))
    reply = [id_frame, Frame(), Frame(bytes([0]))]
    sock.send_multipart(reply)
예제 #5
0
def file_get_handler(store: DirectoryServerStore, sock: Socket,
                     argframes: List[Frame], id_frame: Frame) -> None:
    filename = str(argframes.pop(0).bytes, encoding='utf8')
    if filename in store.files:
        vfile = store.files[filename]
        device_names = list(map(lambda d: d.name, vfile.declared_devices))
        sock.send_multipart([
            id_frame,
            Frame(),
            Frame(bytes([0])),
            Frame(bytes(json.dumps(device_names), 'utf8'))
        ])
    else:
        sock.send_multipart([id_frame, Frame(), Frame(bytes([1]))])
예제 #6
0
def get_addresses_handler(store: DirectoryServerStore, sock: Socket,
                          argframes: List[Frame], id_frame: Frame) -> None:
    device_name = str(argframes.pop(0).bytes, encoding='utf8')
    if device_name in store.devices:
        device = store.devices[device_name]
        playload = json.dumps(device.cast_addresses)
        reply = [
            id_frame,
            Frame(),
            Frame(bytes([0])),
            Frame(bytes(playload, 'utf8'))
        ]
        sock.send_multipart(reply)
    else:
        reply = [id_frame, Frame(), Frame(1)]
        sock.send_multipart(reply)
예제 #7
0
def ping_handler(store: DirectoryServerStore, sock: Socket,
                 argframes: List[Frame], id_frame: Frame) -> None:
    device_name_frame = argframes.pop(0)
    device_name = str(device_name_frame.bytes, encoding='utf8')
    peer_addr = device_name_frame.get("Peer-Address")
    print("Ping from {}".format(peer_addr))
    reply = [
        id_frame,
        Frame(),
        Frame(b"pong"),
        Frame(bytes(peer_addr, encoding='utf8'))
    ]
    sock.send_multipart(reply)
    if device_name not in store.devices:
        store.devices[device_name] = Device(device_name, [])
        print("New device {} is created".format(device_name))
예제 #8
0
async def read_file_handler(store: StorageServerStore, argframes: List[Frame],
                            sock: Socket, id_frame: Frame):
    filename = str(argframes.pop(0).bytes, 'utf8')
    vfile = store.files.get(filename, None)
    if vfile:
        sock.send_multipart([id_frame, Frame(), bytes([0]), vfile.content])
    else:
        sock.send(bytes([0]))
예제 #9
0
def file_disown_handler(store: DirectoryServerStore, sock: Socket,
                        argframes: List[Frame], id_frame: Frame,
                        changes_pub: Socket) -> None:
    device_name = str(argframes.pop(0).bytes, encoding='utf8')
    filename = str(argframes.pop(0).bytes, encoding='utf8')
    if (filename in store.files) and (device_name in store.devices):
        vfile = store.files[filename]
        device = store.devices[device_name]
        if device in vfile.declared_devices:
            vfile.declared_devices.remove(device)
            if not device_name.startswith("storage"):
                vfile.refcount -= 1
        if vfile.refcount == 0:
            store.files.pop(filename)
            print("File {} is deleted".format(filename))
            changes_pub.send_multipart(
                [b"fs.delete_file", bytes(filename, 'utf8')])
            print("fs.delete_file is sent")
        sock.send_multipart([id_frame, Frame(), Frame(bytes([0]))])
    else:
        sock.send_multipart([id_frame, Frame(), Frame(bytes([1]))])
예제 #10
0
def file_declare_handler(store: DirectoryServerStore, sock: Socket,
                         argframes: List[Frame], id_frame: Frame,
                         changes_pub: Socket) -> None:
    device_name = str(argframes.pop(0).bytes, encoding='utf8')
    filename = str(argframes.pop(0).bytes, encoding='utf8')
    new_file_flag = False
    if filename not in store.files:
        store.files[filename] = VirtualFile(filename, [], 0)
        print("New file {} created".format(filename))
        new_file_flag = True
    vfile = store.files[filename]
    device = store.devices.get(device_name, None)
    if device and (device not in vfile.declared_devices):
        vfile.declared_devices.append(device)
        if not device_name.startswith(
                "storage"
        ):  # In reality we may use another way to identify if we need to count reference for the device
            vfile.refcount += 1
    sock.send_multipart([id_frame, Frame(), Frame(bytes([0]))])
    if new_file_flag:
        changes_pub.send_multipart(
            [Frame(b"fs.new_file"),
             Frame(bytes(filename, 'utf8'))])
        print("fs.new_file is sent")
예제 #11
0
def file_list_handler(store: DirectoryServerStore, sock: Socket,
                      id_frame: Frame) -> None:
    file_list = store.files.keys()
    reply = [id_frame, Frame(), Frame(0), Frame(json.dumps(file_list))]
    sock.send_multipart(reply)