ErlangConnection

Struct ErlangConnection 

Source
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§cookie: String§status: Arc<Mutex<ConnectionStatus>>

Implementations§

Source§

impl ErlangConnection

Source

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()
);
Source

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!");
Source

pub fn get_remote_node_name(&self) -> &str

Get the remote node name.

§Returns

The full node name string (e.g., “admin@localhost”).

Source

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.

Source

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 name
  • function - Function name to call
  • args - 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);
Source

pub async fn subscribe_to_event_bus(&self) -> Result<()>

Subscribe to the cryptic event bus for receiving events.

§Errors

Currently always returns Ok(()).

Source

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.

Source

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.

Source

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.

Source

pub async fn request_engine_status(&self) -> Result<Term>

Request engine status from the Cryptic engine.

Calls cryptic_rpc:engine_status/0 which returns detailed status about the encryption engine including active sessions, ratchet states, etc.

§Returns

Raw EETF Term containing the engine status map

§Errors

Returns an error if the RPC call fails.

Source

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.

Source

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 peer
  • limit - 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.

Source

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 peer
  • before_timestamp - Unix timestamp - only load messages before this time
  • limit - 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.

Source

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 peer
  • start_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.

Source

pub async fn admin_register_user( &self, fingerprint: &str, key_path: &str, metadata: &str, ) -> Result<()>

Register a new user via admin RPC.

Source

pub async fn admin_suspend_user(&self, fingerprint: &str) -> Result<()>

Suspend a user via admin RPC.

Source

pub async fn admin_reactivate_user(&self, fingerprint: &str) -> Result<()>

Reactivate a suspended user via admin RPC.

Source

pub async fn admin_revoke_user(&self, fingerprint: &str) -> Result<()>

Revoke a user via admin RPC.

Source

pub async fn renew_certificate(&self) -> Result<()>

Trigger certificate renewal via RPC.

Source

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

Source

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 for
  • limit - 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.

Source

pub async fn get_roster(&self) -> Result<Vec<String>>

Get the user roster (contact list).

§Returns

Vector of usernames (currently empty, to be implemented).

§Errors

Returns an error if the RPC call fails.

§TODO

Parse the result properly and return actual usernames.

Source

pub async fn send_message(&self, peer: &str, message: &str) -> Result<()>

Send a message to a peer.

§Arguments
  • peer - Username of the recipient
  • message - Message content to send
§Errors

Returns an error if the RPC call fails.

§TODO

This should trigger encryption and delivery through the Cryptic engine.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> TryAsRef<T> for T

§

fn try_as_ref(&self) -> Option<&T>

§

impl<T> TryAsRef<T> for T

§

fn try_as_ref(&self) -> Option<&T>

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more