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 is located at 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 or leaves a room and update the UI accordingly. In directmessage_simple, the DM functionality of the Diarkis Module is customized to determine if any message from another user has been received at least once. To customize a function in the Diarkis Module, you inherit the base class (DiarkisModuleNameBase) for each feature. The DM feature is implemented in DiarkisDirectMessageBase, and in the sample, this class is inherited to implement the necessary processes. The following code shows a part of the actual implementation.

/*
 * Implementation to customize DirectMessage-related processes on the application side.
 * When data related to DirectMessage is received, On... type callbacks are triggered, allowing the application to process them.
 * In this sample, 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 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 mainly defines interfaces for using DM features and interfaces for receiving events. The interface for using DM features can be employed for executing a specific process before calling a DM function. Meanwhile, the interface for receiving events can be used to implement processing against notifications when events occur, such as receiving messages in the DM feature. In the sample implementation, DiarkisDirectMessageBase::OnMessage, which is triggered when a message is received via DM, is overridden to save the state of whether a message has been received.

Customizing DiarkisInterfaceBase

When using the Diarkis Module, instances of classes implementing each feature are all managed by the DiarkisInterfaceBase class, and instance creation is done internally by DiarkisInterfaceBase. If the base class is customized, it's necessary to change the type of instance generated within DiarkisInterfaceBase, hence a customized DiarkisInterfaceBase class is implemented by inheriting it. Below is a part of the sample implementation.

/*
 * Implement a class that inherits DiarkisInterfaceBase to replace each feature module managed internally by a DirectInterface with an application-specific one.
 */
class DiarkisInterfaceDirectMessageSimple : public DiarkisInterfaceBase
{
public:
    ...

    void SetupDirectMessage(void)
    {
        if (tcpBase_ != nullptr && tcpBase_->Get() != nullptr)
        {
            // Create a 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 a 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 conducted in DiarkisInterfaceBase::Setup.... Since the DM feature instance is handled in DiarkisInterfaceBase::SetupDirectMessage(), this method is overridden to generate the user-customized DM feature class. Also, a method has been added to retrieve the DM feature instance using the customized type.

By using these customized classes, you can integrate application-specific processing into the Diarkis Module for customization.

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 timing synchronization.
// Begin sending data from "1111" when the first message from "2222" arrives.
while (!dm->IsAnyMessageReceived())
{
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}

Points to Note

Since the Diarkis Module is provided with source code, it is possible to directly modify it without inheriting from the base class for similar customization. However, as the source code of the Diarkis Module may undergo significant changes during version upgrades, it is anticipated that merging could become difficult. To avoid such situations, it is recommended to customize the module using the method introduced on this page.

Last updated