Customization of Diarkis Module

This page introduces parts of the code from the directmessage_simple sample and explains how to customize and use the Diarkis Module. The actual sample source code can be found in samples\directmessage_simple\directmessage_simple.cpp.

Customizing Each Feature of the Diarkis Module

You can integrate application-specific processing into the module you wish to use. For example, the application can recognize when someone joins a Room or leaves it, and update the UI accordingly. In directmessage_simple, the DM feature of the Diarkis Module is customized to determine whether a message has been received from another user at least once. To customize the features of the Diarkis Module, you inherit from the base class of each feature (DiarkisModuleNameBase). The DM feature is implemented in DiarkisDirectMessageBase, and in the sample, this class is inherited to implement the necessary processing. The following code is a partial implementation.

/*
 * Implementation for customizing the DirectMessage-related processing on the application side.
 * On... related callbacks will be triggered upon receiving DirectMessage-related data, allowing the application-specific processing to be implemented.
 * In this sample, the received messages are displayed on the console.
 */
class DirectMessageSimple : public DiarkisDirectMessageBase
{
public:
    DirectMessageSimple() : DiarkisDirectMessageBase() {}
    virtual ~DirectMessageSimple() {}

    bool IsAnyMessageReceived() const
    {
        return anyMessageReceived_;
    }
private:
    /**
     * @~english
     * @brief Get the callback event that is called when disconnecting DirectMessage notifications.
     * @details Triggered when a DirectMessage Disconnect notification (Push from the server) is sent.
     * @~
     */
    virtual void OnDisconnect(const DiarkisDirectMessageEventArgs& e) override
    {
        ...
    }

    /**
     * @~english
     * @brief Get the callback event that is called when a DirectMessage is sent from another remote user.
     * @details Triggered when a DirectMessage Message notification (Push from the server) is sent.
     * @~
     */
    virtual void OnMessage(const DiarkisDirectMessageEventArgs& e) override
    {
        ...
        anyMessageReceived_ = true;
    }
private:
    bool anyMessageReceived_ = false;
};

DiarkisDirectMessageBase primarily defines interfaces for using the DM feature and interfaces for receiving events. The interface for using the DM feature is used for purposes such as executing specific processing before invoking DM functions. On the other hand, the interface for receiving events allows implementing processing for notifications when events like receiving a message occur. In the sample implementation, DiarkisDirectMessageBase::OnMessage is overridden to save whether a message has been received via DM.

Customizing DiarkisInterfaceBase

When using the Diarkis Module, all instances of classes implementing each feature are managed by the DiarkisInterfaceBase class, and the creation of instances also takes place within DiarkisInterfaceBase. If you customize the base class, it is necessary to change the type of the instances generated within DiarkisInterfaceBase, so you implement a customized DiarkisInterfaceBase class by inheriting it. Below is a partial implementation of the sample.

/*
 * To replace modules for each feature managed internally by DirectInterface with application-specific ones, implement a class inheriting from DiarkisInterfaceBase.
 */
class DiarkisInterfaceDirectMessageSimple : public DiarkisInterfaceBase
{
public:
    ...

    void SetupDirectMessage(void)
    {
        if (tcpBase_ != nullptr && tcpBase_->Get() != nullptr)
        {
            // Create the customized class on the application side.
            if (dmBase_ == nullptr)
                dmBase_ = Diarkis::DiarkisAllocShared<DirectMessageSimple>();

            // Initialize the DirectMessage class and register callback events
            dmBase_->SetupTcp(tcpBase_->Get(), this->GetLoggerFactory());
        }

        if (udpBase_ != nullptr && udpBase_->Get() != nullptr)
        {
            // Create the customized class on the application side.
            if (dmBase_ == nullptr)
                dmBase_ = Diarkis::DiarkisAllocShared<DirectMessageSimple>();
            // Initialize the DirectMessage class and register callback events
            dmBase_->SetupUdp(udpBase_->Get(), this->GetLoggerFactory());
        }
    }

    std::shared_ptr<DirectMessageSimple> GetDirectMessage()
    {
        return std::static_pointer_cast<DirectMessageSimple>(GetDirectMessageBase());
    }
};

Instance generation for each feature is performed in DiarkisInterfaceBase::Setup.... Since instance generation for the DM feature is done in DarkisInterfaceBase::SetupDirectMessage(), this method is overridden to generate the user-customized DM feature class. Additionally, a method for obtaining instances of the DM feature with the customized type is added.

By using these customized classes, you can incorporate application-specific processing into the Diarkis Module and customize it.

std::shared_ptr<DiarkisInterfaceDirectMessageSimple> diarkis;

...

// Create a class inheriting from DiarkisInterface to access all features of Diarkis.
diarkis = Diarkis::DiarkisAllocShared<DiarkisInterfaceDirectMessageSimple>(uid);

...

std::shared_ptr<DirectMessageSimple> dm = diarkis->GetDirectMessage();

...

// A simple implementation for synchronizing timing.
// Start sending data from "1111" when the first message arrives from "2222."
while (!dm->IsAnyMessageReceived())
{
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}

Important Notes

Given the nature of Diarkis Module being provided as source code, it's also possible to make the same customization by directly modifying it without inheriting the base class. However, there's a possibility that the source code of Diarkis Module might undergo significant changes during version upgrades, making merges difficult. To avoid such situations, it is recommended to customize as described on this page.

Last updated

Was this helpful?