Program Listing for File CoreSDK.h

Return to documentation for file (axr.sdk/CoreSDK.h)

// Copyright (C) AcceleratXR, Inc. All rights reserved.
//
// Author: Jean-Philippe Steinmetz <info@acceleratxr.com>
#pragma once

#include "Configuration.h"
#include "Variant.h"
#include "Object.h"
#include "ServiceFactory.h"
#include "net/IApiClient.h"
#include "net/IWebSocket.h"
#include "models/AuthToken.h"
#include "models/User.h"
#include "utils/EntityWatchdogFactory.h"
#include "utils/Timer.h"

#include <any>
#include <cpprest/asyncrt_utils.h>
#include <memory>

// Forward declare certain dependencies so we don't have to include the header.
// Including the header causes lots of problems as it includes Windows.h and this causes issues for implementing
// apps like Unreal Engine.
#ifdef _WIN32
template<typename _Type>
class pplx::task;
#else
namespace pplx
{
    template<typename _Type>
    class task;
}
#endif

namespace axr {
namespace sdk {

enum class ConnectionStatus
{
    NOT_CONNECTED,
    CONNECTING,
    CONNECTED,
    LOST_CONNECTION,
    REFUSED
};

enum class LoginStatus
{
    NOT_LOGGED_IN,
    LOGGING_IN,
    LOGGED_IN,
    LOGGED_IN_LOCAL,
    REFUSED,
};

class CoreSDK
{
public:
    typedef std::function<void(Object&)> PushNotificationCallback;

    typedef std::function<pplx::task<utility::string_t>()> TotpChallengeCallback;

    CoreSDK(const utility::string_t& InstanceName, std::shared_ptr<Configuration> config);

    CoreSDK(const utility::string_t& InstanceName, std::shared_ptr<Configuration> config, std::shared_ptr<net::IApiClient> apiClient);

    ~CoreSDK();

    std::shared_ptr<net::IApiClient> GetApiClient() const
    {
        return m_apiClient;
    }

    std::shared_ptr<Configuration> GetConfiguration() const
    {
        return m_configuration;
    }

    ConnectionStatus GetConnectionStatus() const
    {
        return m_connStatus;
    }

    LoginStatus GetLoginStatus() const
    {
        return m_loginStatus;
    }

    std::shared_ptr<ServiceFactory> GetServiceFactory() const
    {
        return m_serviceFactory;
    }

    std::shared_ptr<utils::EntityWatchdogFactory> GetEntityWatchdogFactory() const
    {
        return m_watchdogFactory;
    }

    template<typename _ReturnType>
    __declspec(noinline)
    pplx::task<_ReturnType> CreateTask(const pplx::task<_ReturnType>& _Task)
    {
        return pplx::create_task(_Task, m_configuration->GetCancellationToken());
    }

    std::vector<PushNotificationCallback>::const_iterator AddPushListener(PushNotificationCallback callback);

    void RemovePushListener(std::vector<PushNotificationCallback>::const_iterator iter);

    void LoginLocal(std::shared_ptr<models::User> newUser = nullptr);

    void LoginLocal(utility::string_t token);

    pplx::task<void> Logout();

    std::shared_ptr<models::User> GetLoggedInUser();

    pplx::task<void> LoginDevice();

    pplx::task<void> LoginEmail(const utility::string_t& email);

    pplx::task<void> LoginEmail(const utility::string_t& email, const utility::string_t& code);

    pplx::task<void> LoginPassword(const utility::string_t& username, const utility::string_t& password);

    pplx::task<void> LoginPhone(const utility::string_t& email);

    pplx::task<void> LoginPhone(const utility::string_t& email, const utility::string_t& code);

    pplx::task<void> LoginToken(const utility::string_t& token, bool newSession = false);

    pplx::task<std::shared_ptr<models::User>> RegisterUser(std::shared_ptr<models::User> user);

    pplx::task<std::shared_ptr<models::User>> RegisterUserAndPassword(std::shared_ptr<models::User> user, const utility::string_t& password);

    void SetTotpChallengeCallback(TotpChallengeCallback callback);

private:
        std::shared_ptr<net::IApiClient> m_apiClient;
        std::shared_ptr<Configuration> m_configuration;
        ConnectionStatus m_connStatus;
        utility::string_t m_instanceName;
        std::shared_ptr<models::User> m_loggedInUser;
        LoginStatus m_loginStatus;
        std::vector<PushNotificationCallback> m_pushListeners;
        std::shared_ptr<ServiceFactory> m_serviceFactory;
        std::shared_ptr<utils::EntityWatchdogFactory> m_watchdogFactory;
        utils::Timer m_refreshTokenTimer;
        TotpChallengeCallback m_totpChallengeCallback;
        std::shared_ptr<net::IWebSocket> m_websocket;

        pplx::task<void> ConnectWebsocket();

        utility::string_t GetDevicePassword();

        void StartRefreshTimer(std::shared_ptr<models::AuthToken> token);
};

} // namespace axr
} // namespace sdk