TT CORE SDK

Initializing an Application

Initializing an Application

To create a basic TT Core SDK application, you need to:

  • Create a class that is derived from ttsdk::IEventHandler and defines your SDK event handler. You must provide implementations for:
    • virtual void OnStatus(const Status status) = 0;
  • Create an instance of the TTSDKOptions class. Define the TT environment to which your application will connect and specify your application key for this environment.
  • Initialize the TT Core SDK passing your TTSDKOptions instance variable and your SDK event handler instance variable.
    • Once an application initializes the TT Core SDK shared library, all other applications will use the same instance. Any subsequent calls will be a no-op.

The following code snippet demonstrates an example of this process.

      

#include <iostream>
#include <iomanip>
#include <atomic>
#include <sstream>
#include <string.h>
#include <tt_cplus_sdk.h>
#include <condition_variable>
  
std::mutex mutex;
std::condition_variable sdkReadyCondition;
  
//
//  SDK event handler class
//
  
class SDKEventHandler : public ttsdk::IEventHandler
{
public:
  SDKEventHandler() : IEventHandler() {}
  virtual ~SDKEventHandler() {}
  
  // received on an SDK managed thread that can be used for processing.
  // beware that time consuming tasks can delay the delivery of another status event.
  
  virtual void OnStatus(const ttsdk::IEventHandler::Status status) override
  {
    if (status == ttsdk::IEventHandler::Status::INITIALIZED)
    {
        std::cout << "---SDK is initialized." << std::endl;
        std::lock_guard lock(mutex);
        sdkReadyCondition.notify_one();
    }
    else
    {
        std::cout << "SDKEventHandler::OnStatus (" << (uint32_t)status 
              << ")" << std::endl;
    }
  }
};

  
//
//  Main
//

int main(int argc, char* argv[])
{
    //Set the environment the app needs to run in here
    ttsdk::Environment env = ttsdk::Environment::ProdLive;

    // Add your app secret Key here. It looks like: 
00000000-0000-0000-0000-000000000000:00000000-0000-0000-0000-000000000000
    std::string app_key = “Your App Key”;

    ttsdk::TTSDKOptions options;
    options.environment = env;
    options.app_key_secret = app_key.c_str();

    SDKEventHandler myObserver;
    
    if (!ttsdk::Initialize(options, &myObserver, nullptr))
    {
        std::cout << "Unable to initialize SDK!" << std::endl;
        return -1;
    }  

    std::unique_lock lock(mutex);
    if (sdkReadyCondition.wait_for(lock, std::chrono::seconds(300)) == std::cv_status::timeout)
    {
        std::cout << "Timeout waiting for SDK to initialize!" << std::endl;
        return -1;
    }

    std::cout << std::endl;
    std::cout << "<<<<< TT CORE SDK is initialized. >>>>>" << std::endl;

    std::cout << std::endl << "Press q to exit....." << std::endl;
    std::string command;
    while (std::cin >> command)
    {
        if (command == "q")
        {
            std::cout << "Quitting...\n";
            break;
        }
    }
    std::cout << "Exiting..." << std::endl;
    ttsdk::Shutdown();
}

 

Onload

The servers on which TT Core SDK applications execute are equipped to utilize Onload which supports a kernel bypass accelerated user-level TCP/IP network stack, providing extremely low latency.

To take advantage of this, you will first need to run the onload.sh script included with the TT Core SDK software bundle. This script sets a number of environment variables. Alternatively, you could copy these environment variables to your own startup scripts. Regardless of the method you choose, these must be available to your application when it is initialized.

Note: Onload should only be used if you are running one TT Core SDK application on the server.