Skip to content

Mail Server and last_sync_time

dnotestein edited this page Mar 6, 2014 · 16 revisions

Mail Server

The mail server contains a single database of all encyrpted messages received, using a receipt-timestamp keyed map (_message_db). It also maintains a map of connections to Keyhotee clients, with each connection reading/writing to the central message database. The server only deals in encrypted messages, as it has no keys of it's own to decode messages.

In main.cpp, the Mail server creates an object mail::server and configures it. configure asyncs accept_loop that waits for tcp connections from KH clients. The server is limited to accepting one connection every 10ms. A connection object is created for each accepted connection to process messages from the connection.

Receiving messages from Keyhotee (KH) clients

The connection asyncs a read_loop when it is constructed. The read_loop continuously loops: blocking on the socket until it receives a message from a client, then doing a callback to the server via on_connection_message.

The mail server receives two possible message from a connection via callback to on_connection_message:

  1. an encrypted_message which is stored into the _message_db of the mail server (if the msg is less than 2MB in size)
  2. a client_info_message. The connection's _sync_time is set from this msg, then the connection enters exec_sync_loop (only enters loop if sync_time of 0 wasn't sent, why, what is the special meaning of 0?).

Sending messages to KH clients

There is a _sync_time stored in each mail connection. This is used by exec_sync_loop to send all the encrypted messages after _sync_time (updating the connection's _sync_time to the last message sent as part of this syncing process). Every 15 seconds, the mail connection tries to send any new messages after _sync_time to it's associated KH client, while in exec_sync_loop. This loop is only canceled when the connection is destructed.

Keyhotee Clients

Each profile has a persistent last_sync_time. A KH Client has its own mail::connection which it re-uses to connect to each potential mail server node in mail_connect_loop.

  • application::connect_to_network
    • asyncs mail_connect_loop to connect to mail server(s)
      • for each default_mail_nodes
        • connect to mail server node
        • set connection's _sync_time to profile's last_sync_time
        • send client_info_message containing profile's last_sync_time
        • sets _mail_connected to true on successful connection
      • sleep for 3s then repeat the loop to send last_sync_time
    • asyncs connect_loop to connect to peers

Profile's last_sync_time is set to received message's sent timestamp. The message's timestamp is set in send_email of originating client to be now() + server_time_offset. server_time_offset is set as now() - server_time when receiving server_info_message from mail server.