piac namespace

Piac declarations and definitions.

Contents

Classes

class Document
Document class to hold a database document and help with JSON serialization.
class Documents
Multiple document hold a list of database documents.
class JSONBase
Base class to interact with JSON serialization library.
class WalletListener
Monero wallet synchronization listener class.

Functions

static auto color_string(const std::string& s, COLOR color = GRAY) -> std::string
static void load_key(const std::string& filename, std::string& key)
static auto usage(const std::string& logfile) -> std::string
static void echo_connection(const std::string& info, const std::string& server)
void matrix_message(const std::string& src_user, const std::string& target_user, const std::string& msg)
Send a message to a user.
void matrix_thread(const std::string& server, const std::string& username, const std::string& password, const std::string& db_key)
Entry point to thread to communicate with a matrix server.
void message_thread()
Entry point to thread to receive matrix messages.
auto sha256(const std::string& msg) -> std::string
Compute sha256 hash of a string.
auto hex(const std::string& digest) -> std::string
Compute hex encoding of a string.
static void save_public_key(const std::string& filename, const std::string& public_key)
static void load_server_key(const std::string& filename, std::string& key)
static void load_client_keys(const std::string& filename, std::vector<std::string>& keys)
static auto usage(const std::string& db_name, const std::string& logfile, const std::string& rpc_server_save_public_key_file, int rpc_port, int p2p_port) -> std::string
void db_update_hashes(const std::string& db_name, std::unordered_set<std::string>& my_hashes)
Update advertisement database hashes.
void db_client_op(zmqpp::socket& client, zmqpp::socket& db_p2p, const std::string& db_name, const std::unordered_map<std::string, zmqpp::socket>& my_peers, std::unordered_set<std::string>& my_hashes, zmqpp::message& msg)
Perform a database operation for a client.
void db_peer_op(const std::string& db_name, zmqpp::message& msg, zmqpp::socket& db_p2p, std::unordered_set<std::string>& my_hashes)
Perform an operation for a peer.
void db_thread(zmqpp::context& ctx_db, const std::string& db_name, int rpc_port, bool use_strict_ports, const std::unordered_map<std::string, zmqpp::socket>& my_peers, std::unordered_set<std::string>& my_hashes, int rpc_secure, const zmqpp::curve::keypair& rpc_server_keys, const std::vector<std::string>& rpc_authorized_clients)
Entry point to thread to perform database operations.
auto p2p_connect_peer(zmqpp::context& ctx, const std::string& addr) -> zmqpp::socket
Create ZeroMQ socket and onnect to peer piac daemon.
void p2p_bcast_peers(int p2p_port, std::unordered_map<std::string, zmqpp::socket>& my_peers, bool& to_bcast_peers)
Broadcast to peers.
void p2p_bcast_hashes(int p2p_port, std::unordered_map<std::string, zmqpp::socket>& my_peers, const std::unordered_set<std::string>& my_hashes, bool& to_bcast_hashes)
Broadcast advertisement database hashes to peers.
void p2p_send_db_requests(int p2p_port, std::unordered_map<std::string, zmqpp::socket>& my_peers, std::unordered_map<std::string, std::unordered_set<std::string>>& db_requests, bool& to_send_db_requests)
Send requests for advertisement database entries to peers.
void p2p_answer_p2p(zmqpp::context& ctx_p2p, zmqpp::socket& db_p2p, zmqpp::message& msg, std::unordered_map<std::string, zmqpp::socket>& my_peers, const std::unordered_set<std::string>& my_hashes, std::unordered_map<std::string, std::unordered_set<std::string>>& db_requests, int p2p_port, bool& to_bcast_peers, bool& to_bcast_hashes, bool& to_send_db_requests)
Answer peer's request.
void p2p_answer_db(zmqpp::message& msg, std::unordered_map<std::string, zmqpp::socket>& my_peers, bool& to_bcast_hashes)
Answer request from db thread.
void p2p_thread(zmqpp::context& ctx_p2p, zmqpp::context& ctx_db, std::unordered_map<std::string, zmqpp::socket>& my_peers, const std::unordered_set<std::string>& my_hashes, int default_p2p_port, int p2p_port, bool use_strict_ports)
Entry point to thread to communicate with peers.
auto get_doccount(const std::string db_name) -> Xapian::doccount
Get number of documents in Xapian database.
auto add_document(const std::string& author, Xapian::TermGenerator& indexer, Xapian::WritableDatabase& db, Document& ndoc) -> std::string
Add document to Xapian database.
auto index_db(const std::string& author, const std::string& db_name, const std::string& input_filename, const std::unordered_set<std::string>& my_hashes = {}) -> std::string
Index Xapian database.
auto db_query(const std::string& db_name, std::string&& cmd) -> std::string
Query Xapian database.
auto db_get_docs(const std::string& db_name, const std::vector<std::string>& hashes) -> std::vector<std::string>
Get documents from Xapian database.
auto db_put_docs(const std::string& db_name, const std::vector<std::string>& docs) -> std::size_t
Put documents to Xapian database.
auto db_rm_docs(const std::string& author, const std::string& db_name, const std::unordered_set<std::string>& hashes_to_delete, const std::unordered_set<std::string>& my_hashes = {}) -> std::string
Remove documents from Xapian database.
auto db_list_hash(const std::string& db_name, bool inhex) -> std::vector<std::string>
List hashes from Xapian database.
auto db_list_doc(const std::string& db_name) -> std::vector<std::string>
List documents from Xapian database.
auto db_list_numuser(const std::string& db_name) -> std::size_t
List number of unique users in Xapian database.
auto db_add(const std::string& author, const std::string& db_name, std::string&& cmd, const std::unordered_set<std::string>& my_hashes = {}) -> std::string
Add documents to Xapian database.
auto db_rm(const std::string& author, const std::string& db_name, std::string&& cmd, const std::unordered_set<std::string>& my_hashes) -> std::string
Remove documents from Xapian database.
auto db_list(const std::string& db_name, std::string&& cmd) -> std::string
List Xapian database.
void setup_logging(const std::string& logfile, const std::string& log_level, bool console_logging, const std::size_t max_log_file_size, const std::size_t max_log_files)
Hook up to monero's logging infrastructure.
void send_cmd(std::string cmd, zmqpp::context& ctx, const std::string& host, const std::string& rpc_server_public_key, const zmqpp::curve::keypair& client_keys, const std::unique_ptr<monero_wallet_full>& wallet)
Send a command to piac daemon.
void start_syncing(const std::string& msg, monero_wallet_full* wallet, WalletListener& listener)
Start wallet sync in the background.
auto create_wallet(const std::string& monerod_host, WalletListener& listener) -> std::unique_ptr<monero_wallet_full>
Create a new monero wallet.
void show_wallet_keys(const std::unique_ptr<monero_wallet_full>& wallet)
Query and print currently active wallet keys.
void show_wallet_balance(const std::unique_ptr<monero_wallet_full>& wallet, const WalletListener& listener)
Query and print currently active wallet balance.
auto switch_user(const std::string& mnemonic, const std::string& monerod_host, WalletListener& listener) -> std::unique_ptr<monero_wallet_full>
Change currently active user id / monero wallet.
void show_user(const std::unique_ptr<monero_wallet_full>& wallet)
Show user id / monero wallet mnemonic.
void trim(std::string& s)
Trim a string to remove white space from front and back.
auto tokenize(const std::string& s) -> std::vector<std::string>
Tokenize, i.e., break up, a string along white space into words.
auto wordcount(const std::string& s) -> int
Count the number of words in a string.
auto split(std::string s, const std::string& delim) -> std::pair<std::string, std::string>
Split string into two substrings at delimiter.
auto daemon_socket(zmqpp::context& ctx, const std::string& host, const std::string& rpc_server_public_key, const zmqpp::curve::keypair& client_keys) -> zmqpp::socket
Create ZMQ socket.
auto pirate_send(const std::string& cmd, zmqpp::context& ctx, const std::string& host, const std::string& rpc_server_public_key, const zmqpp::curve::keypair& client_keys) -> std::string
Send message via a ZMQ socket with retries and time-out.
void try_bind(zmqpp::socket& sock, int& port, int range, bool use_strict_ports)
Try to bind ZMQ socket, attempting unused ports.

Function documentation

static std::string piac::color_string(const std::string& s, COLOR color = GRAY)

Parameters
in String to append
color in Color code to insert
Returns String postfixed with ASCII color code

Insert ASCI color string code to a string

static void piac::load_key(const std::string& filename, std::string& key)

Parameters
filename in File to load keys from
key in String to store key in

Load keys from file

static std::string piac::usage(const std::string& logfile)

Parameters
logfile in Logfile name
Returns String containing usage information

Return program usage information

static void piac::echo_connection(const std::string& info, const std::string& server)

Parameters
info in Introductory info message about connection
server in Hostname that will be addressed

Echo connection information to screen

void piac::matrix_thread(const std::string& server, const std::string& username, const std::string& password, const std::string& db_key)

Entry point to thread to communicate with a matrix server.

Parameters
server in Matrix hostname to connect to as <host>[:port]
username in Username to use
password in Password to use
db_key in Database key to use to encrypt session db on disk

std::string piac::sha256(const std::string& msg)

Compute sha256 hash of a string.

Parameters
msg in String whose hash to compute
Returns Hash computed

std::string piac::hex(const std::string& digest)

Compute hex encoding of a string.

Parameters
digest in String to encode in hex
Returns Hex encoding

static void piac::save_public_key(const std::string& filename, const std::string& public_key)

Parameters
filename in File to save keys to
public_key in Public key to save

Save generated public key to file

static void piac::load_server_key(const std::string& filename, std::string& key)

Parameters
filename in File to load key from
key in String to store key in

Load server key from file

static void piac::load_client_keys(const std::string& filename, std::vector<std::string>& keys)

Parameters
filename in File to load keys from
keys in Vector of strings to store key in

Load keys from file

static std::string piac::usage(const std::string& db_name, const std::string& logfile, const std::string& rpc_server_save_public_key_file, int rpc_port, int p2p_port)

Parameters
db_name in Name of database to use to store ads
logfile in Logfile name
rpc_server_save_public_key_file in File to save generated public key
rpc_port in Port to use for client communication
p2p_port in Port to use for peer-to-peer communication
Returns String containing usage information

Return program usage information

void piac::db_update_hashes(const std::string& db_name, std::unordered_set<std::string>& my_hashes)

Update advertisement database hashes.

Parameters
db_name in The name of the database to query for the hashes
my_hashes in Set of advertisement database hashes to update

void piac::db_client_op(zmqpp::socket& client, zmqpp::socket& db_p2p, const std::string& db_name, const std::unordered_map<std::string, zmqpp::socket>& my_peers, std::unordered_set<std::string>& my_hashes, zmqpp::message& msg)

Perform a database operation for a client.

Parameters
client in/out ZMQ socket of the client
db_p2p in/out ZMQ socket of the daemon's p2p thread
db_name in The name of the database to operate on
my_peers in List of this daemon's peers (address and socket)
my_hashes in/out Set of this daemon's advertisement database hashes
msg in/out Incoming message to answer

void piac::db_peer_op(const std::string& db_name, zmqpp::message& msg, zmqpp::socket& db_p2p, std::unordered_set<std::string>& my_hashes)

Perform an operation for a peer.

Parameters
db_name in The name of the database to operate on
msg in/out Incoming message to answer
db_p2p in/out ZMQ socket of the daemon's p2p thread
my_hashes in/out Set of this daemon's advertisement database hashes

void piac::db_thread(zmqpp::context& ctx_db, const std::string& db_name, int rpc_port, bool use_strict_ports, const std::unordered_map<std::string, zmqpp::socket>& my_peers, std::unordered_set<std::string>& my_hashes, int rpc_secure, const zmqpp::curve::keypair& rpc_server_keys, const std::vector<std::string>& rpc_authorized_clients)

Entry point to thread to perform database operations.

Parameters
ctx_db in/out ZMQ context used for communication with the db thread
db_name in The name of the database to operate on
rpc_port in Port to use for client communication
use_strict_ports in True to try only the default port
my_peers in List of this daemon's peers (address and socket)
my_hashes in/out Set of this daemon's advertisement database hashes
rpc_secure in Non-zero to use secure client communication
rpc_server_keys in CurveMQ keypair to use for secure client comm.
rpc_authorized_clients in Only communicate with these clients if secure communication is used to talk to clients

zmqpp::socket piac::p2p_connect_peer(zmqpp::context& ctx, const std::string& addr)

Create ZeroMQ socket and onnect to peer piac daemon.

Parameters
ctx in/out ZeroMQ socket contex
addr in Address (hostname or IP + port) of peer to connect to
Returns ZeroMQ socket created

void piac::p2p_bcast_peers(int p2p_port, std::unordered_map<std::string, zmqpp::socket>& my_peers, bool& to_bcast_peers)

Broadcast to peers.

Parameters
p2p_port in Peer-to-peer port to use
my_peers in List of peers (address and socket) to broadcast to
to_bcast_peers in/out True to broadcast, false to not

void piac::p2p_bcast_hashes(int p2p_port, std::unordered_map<std::string, zmqpp::socket>& my_peers, const std::unordered_set<std::string>& my_hashes, bool& to_bcast_hashes)

Broadcast advertisement database hashes to peers.

Parameters
p2p_port in Peer-to-peer port to use
my_peers in/out List of peers (address and socket) to broadcast to
my_hashes in Set of advertisement database hashes to broadcast
to_bcast_hashes in/out True to broadcast, false to not

void piac::p2p_send_db_requests(int p2p_port, std::unordered_map<std::string, zmqpp::socket>& my_peers, std::unordered_map<std::string, std::unordered_set<std::string>>& db_requests, bool& to_send_db_requests)

Send requests for advertisement database entries to peers.

Parameters
p2p_port in Peer-to-peer port to use
my_peers in/out List of peers (address and socket) to broadcast to
db_requests in/out Multiple ad requests from multiple peers
to_send_db_requests in/out True to send requests, false to not

void piac::p2p_answer_p2p(zmqpp::context& ctx_p2p, zmqpp::socket& db_p2p, zmqpp::message& msg, std::unordered_map<std::string, zmqpp::socket>& my_peers, const std::unordered_set<std::string>& my_hashes, std::unordered_map<std::string, std::unordered_set<std::string>>& db_requests, int p2p_port, bool& to_bcast_peers, bool& to_bcast_hashes, bool& to_send_db_requests)

Answer peer's request.

Parameters
ctx_p2p in/out ZMQ context used for peer-to-peer communication
db_p2p in/out ZMQ context used for communication with the db thread
msg in/out Incoming message to answer
my_peers in/out List of this daemon's peers (address and socket)
my_hashes in This daemon's set of advertisement database hashes
db_requests in/out Store multiple ad requests from multiple peers
p2p_port in Peer-to-peer port to use
to_bcast_peers in/out True to broadcast to peers next, false to not
to_bcast_hashes in/out True to broadcast hashes next, false to not
to_send_db_requests in/out True to send db requests next, false: not

void piac::p2p_answer_db(zmqpp::message& msg, std::unordered_map<std::string, zmqpp::socket>& my_peers, bool& to_bcast_hashes)

Answer request from db thread.

Parameters
msg in/out Incoming message to answer
my_peers in/out List of this daemon's peers (address and socket)
to_bcast_hashes in/out True to broadcast hashes next, false to not

void piac::p2p_thread(zmqpp::context& ctx_p2p, zmqpp::context& ctx_db, std::unordered_map<std::string, zmqpp::socket>& my_peers, const std::unordered_set<std::string>& my_hashes, int default_p2p_port, int p2p_port, bool use_strict_ports)

Entry point to thread to communicate with peers.

Parameters
ctx_p2p in/out ZMQ context used for peer-to-peer communication
ctx_db in/out ZMQ context used for communication with the db thread
my_peers in/out List of this daemon's peers (address and socket)
my_hashes in This daemon's set of advertisement database hashes
default_p2p_port in Port to use by default for peer communication
p2p_port in Port that is used for peer communication
use_strict_ports in True to try only the default port

Xapian::doccount piac::get_doccount(const std::string db_name)

Get number of documents in Xapian database.

Parameters
db_name in Name of Xapian db to operate on
Returns Number of documents in database

std::string piac::add_document(const std::string& author, Xapian::TermGenerator& indexer, Xapian::WritableDatabase& db, Document& ndoc)

Add document to Xapian database.

Parameters
author in Author of the database document
indexer in/out Xapian indexer to use for database indexing
db in/out Xapian database object to add document to
ndoc in/out Json document to add
Returns Hash of the document added

std::string piac::index_db(const std::string& author, const std::string& db_name, const std::string& input_filename, const std::unordered_set<std::string>& my_hashes = {})

Index Xapian database.

Parameters
author in Author of the database document
db_name in Name of the Xapian database object
input_filename in File to read JSON data from
my_hashes in Hashes to check for duplicates when adding documents
Returns Info string showing how many documents have been added

std::string piac::db_query(const std::string& db_name, std::string&& cmd)

Query Xapian database.

Parameters
db_name in Name of the Xapian database object
cmd in/out Query command
Returns Result of the database query

std::vector<std::string> piac::db_get_docs(const std::string& db_name, const std::vector<std::string>& hashes)

Get documents from Xapian database.

Parameters
db_name in Name of the Xapian database object
hashes in Hashes of database documents to get retrieve
Returns Result of the database query

std::size_t piac::db_put_docs(const std::string& db_name, const std::vector<std::string>& docs)

Put documents to Xapian database.

Parameters
db_name in Name of the Xapian database object
docs in Documents to insert to Xapian database
Returns Number of documents inserted

std::string piac::db_rm_docs(const std::string& author, const std::string& db_name, const std::unordered_set<std::string>& hashes_to_delete, const std::unordered_set<std::string>& my_hashes = {})

Remove documents from Xapian database.

Parameters
author in Author of the database document
db_name in Name of the Xapian database object
hashes_to_delete in Hashes of documents to delete
my_hashes in Hashes to check for duplicates when removing documents
Returns Info on number of documents removed

std::vector<std::string> piac::db_list_hash(const std::string& db_name, bool inhex)

List hashes from Xapian database.

Parameters
db_name in Name of the Xapian database object
inhex in True to list hashes hex-encoded
Returns List of hashes

std::vector<std::string> piac::db_list_doc(const std::string& db_name)

List documents from Xapian database.

Parameters
db_name in Name of the Xapian database object
Returns List of documents

std::size_t piac::db_list_numuser(const std::string& db_name)

List number of unique users in Xapian database.

Parameters
db_name in Name of the Xapian database object
Returns Number of unique users created documents in database

std::string piac::db_add(const std::string& author, const std::string& db_name, std::string&& cmd, const std::unordered_set<std::string>& my_hashes = {})

Add documents to Xapian database.

Parameters
author in Author of the database document
db_name in Name of the Xapian database object
cmd in/out Add command
my_hashes in Hashes to check for duplicates when adding documents
Returns Info string after add database operation

std::string piac::db_rm(const std::string& author, const std::string& db_name, std::string&& cmd, const std::unordered_set<std::string>& my_hashes)

Remove documents from Xapian database.

Parameters
author in Author of the database document
db_name in Name of the Xapian database object
cmd in/out Remove command
my_hashes in Hashes to check for duplicates when removing documents
Returns Info string after remove database operation

std::string piac::db_list(const std::string& db_name, std::string&& cmd)

List Xapian database.

Parameters
db_name in Name of the Xapian database object
cmd in/out List command
Returns List of items queried from database

void piac::setup_logging(const std::string& logfile, const std::string& log_level, bool console_logging, const std::size_t max_log_file_size, const std::size_t max_log_files)

Hook up to monero's logging infrastructure.

Parameters
logfile in Log file name
log_level in Log level to set
console_logging in True: enable logging to console, false: file only
max_log_file_size in Set maximum log file size in bytes
max_log_files in Set limit on number of log files

void piac::send_cmd(std::string cmd, zmqpp::context& ctx, const std::string& host, const std::string& rpc_server_public_key, const zmqpp::curve::keypair& client_keys, const std::unique_ptr<monero_wallet_full>& wallet)

Send a command to piac daemon.

Parameters
cmd in Command to send to piac daemon
ctx in/out ZeroMQ socket context to use
host in Hostname or IP + port of piac daemon to send cmd to
rpc_server_public_key in CurveZMQ server public key to use
client_keys in CurveMQ client keypair to use
wallet in Monero wallet to use as author / user id

void piac::start_syncing(const std::string& msg, monero_wallet_full* wallet, WalletListener& listener)

Start wallet sync in the background.

Parameters
msg in Info message to print before starting to sync
wallet in/out Pointer to monero wallet object from monero-cpp
listener in/out Wallet listener object up to initialize used to interact with the background sync

std::unique_ptr<monero_wallet_full> piac::create_wallet(const std::string& monerod_host, WalletListener& listener)

Create a new monero wallet.

Parameters
monerod_host in Hostname + port of a monero daemon to connect to
listener in/out Wallet listener object up to initialize used to interact with the background sync
Returns Smart pointer to monero wallet object

void piac::show_wallet_keys(const std::unique_ptr<monero_wallet_full>& wallet)

Query and print currently active wallet keys.

Parameters
wallet in Smart pointer to monero wallet object

void piac::show_wallet_balance(const std::unique_ptr<monero_wallet_full>& wallet, const WalletListener& listener)

Query and print currently active wallet balance.

Parameters
wallet in Smart pointer to monero wallet object
listener in Wallet listener object interacting with background sync

std::unique_ptr<monero_wallet_full> piac::switch_user(const std::string& mnemonic, const std::string& monerod_host, WalletListener& listener)

Change currently active user id / monero wallet.

Parameters
mnemonic in Monero mnemonic to use
monerod_host in Hostname + port of a monero daemon to connect to
listener in/out Wallet listener object up to initialize used to interact with the background sync

void piac::show_user(const std::unique_ptr<monero_wallet_full>& wallet)

Show user id / monero wallet mnemonic.

Parameters
wallet in Smart pointer to monero wallet object

void piac::trim(std::string& s)

Trim a string to remove white space from front and back.

Parameters
in/out String to remove white spaces from

std::vector<std::string> piac::tokenize(const std::string& s)

Tokenize, i.e., break up, a string along white space into words.

Parameters
in String to tokenize
Returns Tokenized string

int piac::wordcount(const std::string& s)

Count the number of words in a string.

Parameters
in String to count words in
Returns Number of words found in string

std::pair<std::string, std::string> piac::split(std::string s, const std::string& delim)

Split string into two substrings at delimiter.

Parameters
in String to split
delim in Delimiter at which to split

zmqpp::socket piac::daemon_socket(zmqpp::context& ctx, const std::string& host, const std::string& rpc_server_public_key, const zmqpp::curve::keypair& client_keys)

Create ZMQ socket.

Parameters
ctx in/out ZeroMQ socket context
host in Hostname or IP + port to send cmd to
rpc_server_public_key in CurveZMQ server public key to use
client_keys in CurveMQ client keypair to use
Returns ZMQ socket created

std::string piac::pirate_send(const std::string& cmd, zmqpp::context& ctx, const std::string& host, const std::string& rpc_server_public_key, const zmqpp::curve::keypair& client_keys)

Send message via a ZMQ socket with retries and time-out.

Parameters
cmd in Command to send
ctx in/out ZeroMQ socket context
host in Hostname or IP + port to send cmd to
rpc_server_public_key in CurveZMQ server public key to use
client_keys in CurveMQ client keypair to use
Returns Response from remote host

void piac::try_bind(zmqpp::socket& sock, int& port, int range, bool use_strict_ports)

Try to bind ZMQ socket, attempting unused ports.

Parameters
sock in/out Socket to use
port in/out Port to try first
range in Number of ports to attempt in increasing order: port+range
use_strict_ports in True to try only the default port