Client¶
-
class
narupatools.app.client.Client(**kwargs: Any)¶ An extension of NarupaImdClient with more features.
These features include:
Callback for when a frame is received.
Exposing the shared state through the narupatools shared state API.
Fixing a bug where the client connects to frames before it has defined the needed variables to store them.
Inheritance

Methods
Create a client.
Attempt to acquire and/or free a number of locks on the shared state. :param update: A dictionary of keys to either a duration in seconds to attempt to acquire or renew a lock, or None to indicate the lock should be released if held. :return: True if the desired locks were acquired, and False otherwise.
Attempt to make a single atomic change to the shared state, blocking until a response is received. :param update: A single change to make to the shared state that will either be made in full, or ignored if some of the keys are locked by another user. :return: True if the server accepted our change, and False otherwise.
Autoconnect to the first available server discovered that at least produces frames.
Remove all selections in the system
Closes the connection with the server.
Connects the client to all services for which addresses are provided.
Connects the client to the given interactive molecular dynamics server, allowing it to start publishing interactions.
Connects the client to the given multiplayer server.
Connect to a server.
Connect to a session.
Connect to a single Narupa server running all services on the same port.
Connect to a collection of Narupa servers running at the same address but potentially different ports.
Connects the client to the given trajectory server, and begin receiving frames.
Create a particle selection with the given name.
Get the selection with the given selection id, throwing a KeyError if it is not present.
Attempts to retrieve the value for the given key in the multiplayer shared value store.
Delete the given selection
Attempts to remove the given key on the multiplayer shared value store.
Runs a command on the trajectory service, multiplayer service or imd service as appropriate.
Runs a command on the iMD service.
Runs a command on the multiplayer service.
Sends a request to pause the simulation to the trajectory service.
Sends a request to start playing the trajectory to the trajectory service.
Sends a request to reset the simulation to the trajectory service.
Sends a request to take one step to the trajectory service.
Runs a command on the trajectory service.
Attempts to set the given key/value pair on the multiplayer shared value store.
Start an interaction with the IMD server.
Stops the interaction identified with the given interaction_id on the server.
Subscribe to all multiplayer state updates.
Request all the frames from the server.
Request the latest frames at a given time interval.
Fetches an updated set of available commands from the services this client is connected to.
Updates the interaction identified with the given interaction_id on the server with parameters from the given interaction.
Applies changes to the given selection to the shared key store.
Wait until the first frame is received from the server.
-
__init__(**kwargs: Any) → None¶ Create a client.
- Parameters
kwargs – Keyword arguments to pass to NarupaImdClient.
-
attempt_update_multiplayer_locks(update: Dict[str, Optional[float]]) → bool¶ Attempt to acquire and/or free a number of locks on the shared state. :param update: A dictionary of keys to either a duration in
seconds to attempt to acquire or renew a lock, or None to indicate the lock should be released if held.
- Returns
True if the desired locks were acquired, and False otherwise.
-
attempt_update_multiplayer_state(update: narupa.utilities.change_buffers.DictionaryChange) → bool¶ Attempt to make a single atomic change to the shared state, blocking until a response is received. :param update: A single change to make to the shared state that will
either be made in full, or ignored if some of the keys are locked by another user.
- Returns
True if the server accepted our change, and False otherwise.
-
classmethod
autoconnect(search_time=2.0, discovery_address: Optional[str] = None, discovery_port: Optional[int] = None, name: Optional[str] = None)¶ Autoconnect to the first available server discovered that at least produces frames.
- Parameters
search_time – Time, in seconds, to search for.
discovery_address – IP address to search on.
discovery_port – Port upon which to listen for discovery messages.
name – If supplied, only servers with this name will be used.
- Returns
Instantiation of an iMD client connected to whatever is available at the first
-
clear_selections()¶ Remove all selections in the system
-
close(clear_frames=True)¶ Closes the connection with the server.
- Parameters
clear_frames – Whether to clear the frames received by the client, or keep them.
-
connect(*, trajectory_address: Optional[Tuple[str, int]] = None, imd_address: Optional[Tuple[str, int]] = None, multiplayer_address: Optional[Tuple[str, int]] = None)¶ Connects the client to all services for which addresses are provided.
-
connect_imd(address: Tuple[str, int])¶ Connects the client to the given interactive molecular dynamics server, allowing it to start publishing interactions.
- Parameters
address – The address and port of the IMD server.
port – The port of the IMD server.
-
connect_multiplayer(address: Tuple[str, int])¶ Connects the client to the given multiplayer server.
- Parameters
address – The address and port of the multiplayer server.
-
classmethod
connect_to_server(*, address: Optional[str] = None, port: Optional[int] = None) → Generator[narupatools.app.client.Client, None, None]¶ Connect to a server.
- Parameters
address – Address to connect to, or None to use the default.
port – Port to connect to, or None to use the default.
- Yield
Client connected to all available services on the server at the given destination.
-
classmethod
connect_to_session(session: narupatools.app.session.Session, /) → Generator[narupatools.app.client.Client, None, None]¶ Connect to a session.
- Parameters
session – Session that is running.
- Yields
Client connected to the given session.
-
classmethod
connect_to_single_server(address: Optional[str] = None, port: Optional[int] = None)¶ Connect to a single Narupa server running all services on the same port.
- Parameters
address – Address of the server.
port – Server port
- Returns
Instantiation of a client connected to all available services on the server at the given destination.
-
classmethod
connect_to_single_server_multiple_ports(address: str, trajectory_port: int, imd_port: int, multiplayer_port: int)¶ Connect to a collection of Narupa servers running at the same address but potentially different ports.
- Parameters
address – Address of the server.
multiplayer_port – The port at which multiplayer is running.
trajectory_port – The port at which the trajectory service is running.
imd_port – The port at which the iMD service is running.
- Returns
Instantiation of a client connected to all available services on the server at the given destination.
-
connect_trajectory(address: Tuple[str, int])¶ Connects the client to the given trajectory server, and begin receiving frames.
- Parameters
address – The address and port of the trajectory server.
-
create_selection(name: str, particle_ids: Optional[Iterable[int]] = None) → narupa.app.selection.RenderingSelection¶ Create a particle selection with the given name.
- Parameters
name – The user-friendly name of the selection.
particle_ids – The indices of the particles to include in the selection.
- Returns
The selection that was created.
-
get_selection(selection_id: str) → narupa.app.selection.RenderingSelection¶ Get the selection with the given selection id, throwing a KeyError if it is not present. For the root selection, use the root_selection property.
- Parameters
selection_id – The id of the selection
- Returns
The selection if it is present
Attempts to retrieve the value for the given key in the multiplayer shared value store.
- Parameters
key – The key that identifies the value
- Returns
The value stored in the dictionary
- Raises
grpc._channel._Rendezvous – When not connected to a multiplayer service
-
remove_selection(selection: narupa.app.selection.RenderingSelection)¶ Delete the given selection
Attempts to remove the given key on the multiplayer shared value store.
- Raises
grpc._channel._Rendezvous – When not connected to a multiplayer service
-
run_command(name, **args)¶ Runs a command on the trajectory service, multiplayer service or imd service as appropriate.
- Parameters
name – Name of the command to run
args – Dictionary of arguments to run with the command.
- Returns
Results of the command, if any.
-
run_imd_command(name: str, **args) → Dict[str, object]¶ Runs a command on the iMD service.
- Parameters
name – Name of the command to run
args – Dictionary of arguments to run with the command.
- Returns
Results of the command, if any.
-
run_multiplayer_command(name: str, **args)¶ Runs a command on the multiplayer service.
- Parameters
name – Name of the command to run.
args – Dictionary of arguments to run with the command.
- Returns
Results of the command, if any.
-
run_pause()¶ Sends a request to pause the simulation to the trajectory service.
-
run_play()¶ Sends a request to start playing the trajectory to the trajectory service.
-
run_reset()¶ Sends a request to reset the simulation to the trajectory service.
-
run_step()¶ Sends a request to take one step to the trajectory service.
-
run_trajectory_command(name: str, **args) → Dict[str, object]¶ Runs a command on the trajectory service.
- Parameters
name – Name of the command to run
args – Dictionary of arguments to run with the command.
- Returns
Results of the command, if any.
Attempts to set the given key/value pair on the multiplayer shared value store.
- Parameters
key – The key that identifies the value to be stored.
value – The new value to store.
- Returns
True if successful, False otherwise.
- Raises
grpc._channel._Rendezvous – When not connected to a multiplayer service
-
start_interaction(interaction: Optional[narupa.imd.particle_interaction.ParticleInteraction] = None) → str¶ Start an interaction with the IMD server.
- Parameters
interaction – An optional :class: ParticleInteraction with which to begin.
- Returns
The unique interaction ID of this interaction, which can be used to update the interaction with
update_interaction().- Raises
ValueError, if the there is no IMD connection available.
-
stop_interaction(interaction_id) → bool¶ Stops the interaction identified with the given interaction_id on the server. This method blocks until the server returns a reply indicating that the interaction has stopped.
- Parameters
interaction_id – The unique interaction ID, created with
start_interaction(), that identifies the interaction to stop.- Returns
An
InteractionEndReply, which is an empty message indicating
successful termination of the interaction.
- Raises
ValueError – if the there is no IMD connection available, or if invalid parameters are passed to the server.
KeyError – if the given interaction ID does not exist.
-
subscribe_multiplayer(interval=0.03333333333333333)¶ Subscribe to all multiplayer state updates.
- Parameters
interval – Subscription interval for state updates.
- Raises
grpc._channel._Rendezvous – When not connected to a multiplayer service
-
subscribe_to_all_frames()¶ Request all the frames from the server.
This makes the frames available with the
frames,current_frame,latest_frames, andfirst_frameattributes.All the frames produced by the frame server are sent from the time of the subscription get received by the client. Note that, depending on the server set up, this may not be all the frames generated by the MD engine. Subscribing to all frames is usefull for some applications such as saving a trajectory or analyses, however it may get behind the server if the frames are not received and treated as fast as they are sent.
Warning
A client can subscribe to frames only ones and cannot change how it subscribes.
-
subscribe_to_frames(interval: float = 0.03333333333333333)¶ Request the latest frames at a given time interval.
This makes the frames available with the
frames,current_frame,latest_frames, andfirst_frameattributes.This requests for the server to send the latest available frame at a given framerate expressed with the interval of time between two consecutive frames. This requested interval is the a minimum interval: if the frames are produced slower, then they will be sent as fast as possible.
This is usefull for applications that do not need all the frames but only the latest available ones.
Warning
A client can subscribe to frames only ones and cannot change how it subscribes.
- Parameters
interval – Minimum time, in seconds, between two consecutive frames.
-
update_available_commands() → MutableMapping[str, narupa.command.command_info.CommandInfo]¶ Fetches an updated set of available commands from the services this client is connected to.
- Returns
A collection of
CommandInfo, detailing the commands available.
If the same command name is available on multiple services, the nested nature of the returned
ChainMapwill enable the user to determine the correct one to call.
-
update_interaction(interaction_id, interaction: narupa.imd.particle_interaction.ParticleInteraction)¶ Updates the interaction identified with the given interaction_id on the server with parameters from the given interaction.
- Parameters
interaction_id – The unique id of the interaction to be updated.
interaction – The :class: ParticleInteraction providing new parameters for the interaction.
- Raises
ValueError, if the there is no IMD connection available, or if invalid parameters are passed to the server.
- Raises
KeyError – if the given interaction ID does not exist.
-
update_selection(selection: narupa.app.selection.RenderingSelection)¶ Applies changes to the given selection to the shared key store.
- Parameters
selection – The selection to update.
-
wait_until_first_frame(check_interval=0.01, timeout=1)¶ Wait until the first frame is received from the server.
- Parameters
check_interval – Interval at which to check if a frame has been received.
timeout – Timeout after which to stop waiting for a frame.
- Returns
The first
FrameDatareceived.- Raises
Exception – if no frame is received.
Attributes
Returns True if the client is subscribed to frames with either
subscribe_to_all_frames()orsubscribe_to_frames().A copy of the current state of the trajectory, formed by collating all received frames.
The first received trajectory frame, if any.
The most recently received frames up to the storage limit specified by max_frames.
The dictionary of current interactions received by this client.
The trajectory frame most recently received, if any.
The latest state of the multiplayer shared key/value store.
Event triggered when a frame is received by the client.
Get the root selection, creating it if it does not exist yet.
Get all selections which are stored in the shared key store.
Shared state of the client.
-
all_frames¶
-
are_frames_subscribed¶ Returns True if the client is subscribed to frames with either
subscribe_to_all_frames()orsubscribe_to_frames().
-
current_frame¶ A copy of the current state of the trajectory, formed by collating all received frames.
- Returns
FrameData, which is empty if none have been received.
-
first_frame¶ The first received trajectory frame, if any.
- Returns
The first frame received by this trajectory, or None.
-
frames¶ The most recently received frames up to the storage limit specified by max_frames.
- Returns
Sequence of frames.
-
interactions¶ The dictionary of current interactions received by this client. :return: Dictionary of active interactions, keyed by interaction ID identifying who is performing the interactions.
-
latest_frame¶ The trajectory frame most recently received, if any.
- Returns
FrameData, or None if none has been received.
-
latest_multiplayer_values¶ The latest state of the multiplayer shared key/value store.
- Returns
Dictionary of the current state of multiplayer shared key/value store.
-
on_frame_received¶ Event triggered when a frame is received by the client.
-
root_selection¶ Get the root selection, creating it if it does not exist yet.
- Returns
The selection representing the root selection of the system
-
selections¶ Get all selections which are stored in the shared key store.
- Returns
An iterable of all the selections stored in the shared key store.
Shared state of the client.