Class Monitor

Namespace: Keychain

#include "keychain/monitor/monitor.hpp"

Inheritance: monitor

Definition

Monitor subscribes to block updates from the blockchain and updates the database cache state of Persona and Contacts accordingly. Monitor updates attributes such as number of confirmations, public keys and metadata, certificate transaction data. A thread for monitoring the blockchain state is created in the on_start() method.

Monitor method names should be read as "what the Monitor should do in response to the underlying execution environment entering the specified state". For instance, you should call on_start() once your activity/application has been started.

Specifically, note that the Monitor thread is not started during on_start(); it is merely created during that method. This is because some execution environments (namely mobile device environments) prefer to decouple the creation and start states of applications and intend the start process to be as lightweight as possible.

Without at least one Monitor the gateway will run out of sync with the blockchain and will cause the gateway to be able to perform cryptographic operations properly on subsequent data. Thus, you should ensure that a monitor is instantiated within the current Activity.


Constructors

Monitor(std::string db_path,
std::string config_file,
std::string drop_sql_file,
std::string create_sql_file); (1)

Parameters

Parameter Description

std::string db_path

File path of the Keychain database

std::string config_path

File path of the Keychain configuration file

std::string drop_sql_path

File path of the Keychain database schema migration cleanup script

std::string create_sql_path

File path of the Keychain database schema migration script


Method Summary

Return type Method and Description

bool

on_pause()

Pause the monitor thread.

bool

on_resume()

Run/resume the monitor thread.

bool

on_start()

Create the monitor thread.

bool

on_stop()

Stop and destroy the monitor thread.


Methods

on_pause() Method

kc::code on_pause();

Block the monitor thread if it was unblocked and running. The thread will remain blocked until on_resume() is called.

Parameters

None

Returns

bool

true if the thread execution was blocked; otherwise, false.




on_resume() Method

kc::code on_resume();

This method starts/unblocks the monitor thread if it was blocked or not running. If the thread does not exist upon calling on_resume(), on_resume() will call on_start() to create the thread. The thread will remain running until on_pause() is called.

Parameters

None

Returns

bool

true if the thread execution was started/unblocked; otherwise, false.




on_start() Method

kc::code on_start();

Create the monitor thread if it did not exist.

Parameters

None

Returns

bool

true if

  1. the thread did not exist already and was successfully created, or

  2. the thread already existed

otherwise, false.




on_stop() Method

kc::code on_stop();

Destroy the monitor thread if it exists. If the thread is running upon entry in this method, this method will (safely) cause the thread to stop execution before destroying the thread.

As of this version of the library, the worst-case complexity of the stopping process is linear in the number of blockchain certificates and addresses that exist in the database. We expect to reduce this to logarithmic if not constant time in a subsequent release. Until then, some environments (particularly mobile environments) may incur delays in switching activity/application contexts during long-running and stress-test scenarios.

Parameters

None

Returns

bool

true if

  1. the thread existed and was successfully destroyed, or

  2. the thread did not exist

otherwise, false.


Thread Safety

This class is fully thread-safe; any of its methods may be invoked in multiple threads concurrently.


Idempotence

This class’s methods are idempotent.