def __send_message(self, socket: zmq.Socket, msg: Union[SeedMsg, FuzzerCtrlMsg]) -> None: if type(msg) == FuzzerCtrlMsg: msg_type = _MessageType.CONTROL.value elif type(msg) == SeedMsg: msg_type = _MessageType.SEED.value else: raise ValueError(f"Unknown message type: {type(msg)}") socket.send_multipart((msg_type, msg.SerializeToString()))
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)
def send_msg(message_parts: List[bytes], socket: zmq.Socket): """ Send multipart message over socket Parameters ---------- message_parts messages to send socket socket to send over """ socket.send_multipart([message for message in message_parts])
async def periodic_send(socket: zmq.Socket): rate = 100.0 while True: bid = await get_rate(rate) ask = await get_rate(rate) if bid > ask: tmp = bid bid = ask ask = tmp rateobj = {"ask": ask, "bid": bid} print(rateobj) socket.send_multipart([b"TEST", json.dumps(rateobj).encode('utf-8')]) await asyncio.sleep(1.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]))])
def _publish_data(publish_socket: zmq.Socket, machine_name: str, machine_data: Dict[Any, str]): """Publishes machine data over publish socket Parameters ---------- publish_socket : zmq.Socket socket to publish machine_name : str name of machine interface, used as topic for publishing machine_data : Dict[Any, str] machine data dictionary to publish """ publish_socket.send_multipart( [bytes(machine_name, "utf-8"), machine_data])
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)
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))
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")
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]))])
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)
def _shutdown(self, backend: zmq.Socket): for network in self.network_identities: backend.send_multipart( [network, msgpack.dumps([NetworkWorker.SHUTDOWN])])