pub struct ErlangConnection {
handle: Arc<Mutex<Option<RpcClientHandle>>>,
remote_node_name: String,
cookie: String,
status: Arc<Mutex<ConnectionStatus>>,
}Expand description
Manages a connection to a remote Erlang node.
This struct handles the distributed Erlang protocol connection and provides methods for making RPC calls to the remote node.
§Thread Safety
This struct uses Arc<Mutex<>> internally and can be safely shared across
threads and async tasks.
§Examples
let conn = ErlangConnection::new(
"admin@localhost".to_string(),
"secretcookie".to_string()
);
conn.connect().await?;Fields§
§handle: Arc<Mutex<Option<RpcClientHandle>>>§remote_node_name: String§status: Arc<Mutex<ConnectionStatus>>Implementations§
Source§impl ErlangConnection
impl ErlangConnection
Sourcepub fn new(remote_node_name: String, cookie: String) -> Self
pub fn new(remote_node_name: String, cookie: String) -> Self
Create a new Erlang connection.
This does not establish the connection immediately. Call connect
to actually connect to the remote node.
§Arguments
remote_node_name- Full node name (e.g., “admin@localhost”)cookie- Erlang cookie for authentication
§Examples
let conn = ErlangConnection::new(
"admin@localhost".to_string(),
"mycookie".to_string()
);Sourcepub async fn connect(&self) -> Result<()>
pub async fn connect(&self) -> Result<()>
Connect to the remote Erlang node.
Establishes a distributed Erlang connection and spawns a background task to run the RPC client loop. The connection uses a unique local node name based on the process ID.
§Errors
Returns an error if:
- The remote node is unreachable
- The cookie is incorrect
- Network issues prevent connection
§Examples
let conn = ErlangConnection::new(
"admin@localhost".to_string(),
"mycookie".to_string()
);
conn.connect().await?;
println!("Connected!");Sourcepub fn get_remote_node_name(&self) -> &str
pub fn get_remote_node_name(&self) -> &str
Sourcepub fn get_username(&self) -> &str
pub fn get_username(&self) -> &str
Extract the username from the node name.
§Returns
The username portion of the node name (e.g., “admin” from “admin@localhost”). Returns the full node name if no ‘@’ is found.
Get the Erlang cookie.
Sourcepub async fn rpc_call(
&self,
module: &str,
function: &str,
args: List,
) -> Result<Term>
pub async fn rpc_call( &self, module: &str, function: &str, args: List, ) -> Result<Term>
Call an RPC function on the remote Erlang node.
Makes a synchronous RPC call and waits for the result. The call uses Erlang’s external term format (EETF) for encoding arguments and results.
§Arguments
module- Erlang module namefunction- Function name to callargs- List of arguments as EETF terms
§Returns
The result as an EETF [Term].
§Errors
Returns an error if:
- Not connected to a node
- The RPC call fails
- The module or function doesn’t exist
- Network issues occur
§Examples
// Call erlang:node()
let result = conn.rpc_call("erlang", "node", List::nil()).await?;
println!("Node: {:?}", result);Sourcepub async fn subscribe_to_event_bus(&self) -> Result<()>
pub async fn subscribe_to_event_bus(&self) -> Result<()>
Sourcepub async fn start_tui_bridge(
&self,
rust_node_name: &str,
passphrase: &str,
) -> Result<Term>
pub async fn start_tui_bridge( &self, rust_node_name: &str, passphrase: &str, ) -> Result<Term>
Start the cryptic_tui_bridge on the Erlang node to forward events to our Rust node.
This should be called after the dist_node is connected so the bridge knows where to send messages.
§Arguments
rust_node_name- The full node name of our Rust node (e.g., “cryptic_tui@127.0.0.1”)passphrase- User’s passphrase for accessing encrypted chat storage
§Returns
The PID of the started bridge process.
§Errors
Returns an error if the RPC call fails or the bridge fails to start.
Sourcepub async fn stop_tui_bridge(&self) -> Result<()>
pub async fn stop_tui_bridge(&self) -> Result<()>
Stop an existing TUI bridge process.
Calls cryptic_tui_bridge:stop/1 with the bridge’s registered PID.
This is useful when restarting the TUI to ensure a clean state.
§Errors
Returns an error if the RPC call fails or if the bridge is not running.
Sourcepub async fn request_online_users(&self) -> Result<()>
pub async fn request_online_users(&self) -> Result<()>
Request the list of online users from the server.
Calls cryptic_rpc:online_users/0 which sends the request to the server. The response will arrive as a websocket_message event via the dist_node.
§Errors
Returns an error if the RPC call fails.
Sourcepub async fn request_engine_status(&self) -> Result<Term>
pub async fn request_engine_status(&self) -> Result<Term>
Sourcepub async fn request_list_users(&self) -> Result<()>
pub async fn request_list_users(&self) -> Result<()>
Request detailed user list from admin API.
Calls cryptic_rpc:list_users/0 which triggers an asynchronous operation.
The response will arrive later via the distributed Erlang node as a
list_users_response message.
§Returns
Ok(()) if the request was sent successfully. The actual response will be received asynchronously through the event bus.
§Errors
Returns an error if the RPC call fails.
Sourcepub async fn load_recent_messages(
&self,
peer: &str,
limit: usize,
) -> Result<Vec<Message>>
pub async fn load_recent_messages( &self, peer: &str, limit: usize, ) -> Result<Vec<Message>>
Load the most recent N messages from a conversation.
Calls cryptic_chat_storage:get_conversation/4 to fetch recent messages
between the current user and the specified peer.
§Arguments
peer- Username of the peerlimit- Maximum number of messages to retrieve
§Returns
Vector of Message structs ordered by timestamp (oldest first)
§Errors
Returns an error if the RPC call fails or parsing fails.
Sourcepub async fn load_messages_before(
&self,
peer: &str,
before_timestamp: u64,
limit: usize,
) -> Result<Vec<Message>>
pub async fn load_messages_before( &self, peer: &str, before_timestamp: u64, limit: usize, ) -> Result<Vec<Message>>
Load messages older than a specific timestamp.
Used for infinite scrolling - loads the next batch of older messages when user scrolls to the top of the conversation.
§Arguments
peer- Username of the peerbefore_timestamp- Unix timestamp - only load messages before this timelimit- Maximum number of messages to retrieve
§Returns
Vector of Message structs ordered by timestamp (oldest first)
§Errors
Returns an error if the RPC call fails or parsing fails.
Sourcepub async fn load_messages_range(
&self,
peer: &str,
start_timestamp: u64,
end_timestamp: u64,
) -> Result<Vec<Message>>
pub async fn load_messages_range( &self, peer: &str, start_timestamp: u64, end_timestamp: u64, ) -> Result<Vec<Message>>
Load messages in a specific time range.
Useful for jumping to a specific date or searching within a time period.
§Arguments
peer- Username of the peerstart_timestamp- Unix timestamp for start of range (inclusive)end_timestamp- Unix timestamp for end of range (inclusive)
§Returns
Vector of Message structs ordered by timestamp (oldest first)
§Errors
Returns an error if the RPC call fails or parsing fails.
Sourcepub async fn admin_register_user(
&self,
fingerprint: &str,
key_path: &str,
metadata: &str,
) -> Result<()>
pub async fn admin_register_user( &self, fingerprint: &str, key_path: &str, metadata: &str, ) -> Result<()>
Register a new user via admin RPC.
Sourcepub async fn admin_suspend_user(&self, fingerprint: &str) -> Result<()>
pub async fn admin_suspend_user(&self, fingerprint: &str) -> Result<()>
Suspend a user via admin RPC.
Sourcepub async fn admin_reactivate_user(&self, fingerprint: &str) -> Result<()>
pub async fn admin_reactivate_user(&self, fingerprint: &str) -> Result<()>
Reactivate a suspended user via admin RPC.
Sourcepub async fn admin_revoke_user(&self, fingerprint: &str) -> Result<()>
pub async fn admin_revoke_user(&self, fingerprint: &str) -> Result<()>
Revoke a user via admin RPC.
Sourcepub async fn renew_certificate(&self) -> Result<()>
pub async fn renew_certificate(&self) -> Result<()>
Trigger certificate renewal via RPC.
Sourcepub async fn admin_list_certificates(&self, fingerprint: &str) -> Result<()>
pub async fn admin_list_certificates(&self, fingerprint: &str) -> Result<()>
List certificates for a user via admin RPC.
Calls cryptic_rpc:admin_list_certificates/1 which triggers an asynchronous operation.
The response will arrive later via the distributed Erlang node as a
list_certificates_response message.
§Arguments
fingerprint- GPG fingerprint of the user
§Returns
Ok(()) if the request was sent successfully. The actual response will be received asynchronously through the event bus.
§Errors
Returns an error if the RPC call fails.
Source§impl ErlangConnection
impl ErlangConnection
Sourcepub async fn load_history(
&self,
peer: &str,
limit: usize,
) -> Result<Vec<String>>
pub async fn load_history( &self, peer: &str, limit: usize, ) -> Result<Vec<String>>
Load message history from Erlang backend.
§Arguments
peer- Username of the peer to fetch history forlimit- Maximum number of messages to retrieve
§Returns
Vector of message strings (currently empty, to be implemented).
§Errors
Returns an error if the RPC call fails.
§TODO
Parse the result properly and return actual messages.
Sourcepub async fn get_roster(&self) -> Result<Vec<String>>
pub async fn get_roster(&self) -> Result<Vec<String>>
Auto Trait Implementations§
impl Freeze for ErlangConnection
impl !RefUnwindSafe for ErlangConnection
impl Send for ErlangConnection
impl Sync for ErlangConnection
impl Unpin for ErlangConnection
impl !UnwindSafe for ErlangConnection
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more