A device detection library that is used natively or by 51Degrees products

Hash/ReloadFromFile.cpp

Reload from file example of using 51Degrees device detection.

Reload from file example of using 51Degrees device detection.This example shows how to:

  1. Only maintain a reference to a single EngineHash instance and use the reference to process data.
  2. Use the EngineHash->refreshData() function to reload the dataset from the same location and with the same set of properties.
  3. Retrieve a results instance from the engine and release it when done with detecting current User-Agent.
  4. Use the reload functionality in a single threaded environment.
  5. Use the reload functionality in a multi threaded environment.

This example illustrates how to use a single reference to the engine to use device detection and invoke the reload functionality instead of maintaining a reference to the dataset directly.

The EngineHash->refreshData() function requires an existing engine with the initialized dataset. Function reloads the dataset from the same location and with the same parameters as the original dataset.

Please keep in mind that even if the current dataset was constructed with all available properties this does not guarantee that the new dataset will be initialized with the same set of properties. If the new data file contains properties that were not part of the original data file, the new extra property(ies) will not be initialized. If the new data file does not contain one or more property that were previously available, then these property(ies) will not be initialized.

Each successful data file reload should be accompanied by the integrity check to verify that the properties you want have indeed been loaded. This can be achieved by simply comparing the number of properties before and after the reload as the number can not go up but it can go down.

The reload functionality works both with the single threaded as well as the multi threaded modes. To try the reload functionality in single threaded mode build with FIFTYONE_DEGREES_NO_THREADING defined. Or build without FIFTYONE_DEGREES_NO_THREADING for multi threaded example.

In a single threaded environment the reload function is executed as part of the normal flow of the program execution and will prevent any other actions until the reload is complete. The reload itself takes less than half a second even for Enterprise dataset. For more information see: /Support/Documentation/APIs/C-V32/Benchmarks?utm_source=code&utm_medium=example&utm_campaign=51degrees-cxx&utm_content=examples-cpp-hash-reloadfromfile.cpp&utm_term=header

/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House,
* Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
*
* This Original Work is licensed under the European Union Public Licence
* (EUPL) v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
// Include ExmapleBase.h before others as it includes Windows 'crtdbg.h'
// which requires to be included before 'malloc.h'.
#include "../../C/Hash/ExampleBase.h"
#include "ExampleBase.hpp"
namespace FiftyoneDegrees {
namespace Examples {
namespace Hash {
class ReloadFromFile : public ExampleBase {
public:
string dataFilePath,
string userAgentFilePath,
: ExampleBase(dataFilePath, config) {
this->userAgentFilePath = userAgentFilePath;
};
void run() {
int numberOfReloads = 0;
int numberOfReloadFails = 0;
ExampleBase::SharedState state(engine, userAgentFilePath);
printf("** Multi Threaded Reload Example **\r\n");
state.startThreads();
while (state.threadsFinished < THREAD_COUNT) {
try {
engine->refreshData();
numberOfReloads++;
}
catch (StatusCodeException&) {
numberOfReloadFails++;
}
#ifdef _MSC_VER
Sleep(2000); // milliseconds
#else
usleep(20000000); // microseconds
#endif
}
state.joinThreads();
}
else {
printf("** Single Threaded Reload Example **\r\n");
try {
engine->refreshData();
numberOfReloads++;
}
catch (StatusCodeException&) {
numberOfReloadFails++;
}
}
// Report the number of reloads.
printf("Reloaded '%i' times.\r\n", numberOfReloads);
printf("Failed to reload '%i' times.\r\n", numberOfReloadFails);
printf("Program execution complete. Press Return to exit.");
}
private:
string userAgentFilePath;
};
}
}
}
extern "C" void fiftyoneDegreesExampleCPPReloadFromFileRun(ExampleParameters *params) {
// Call the actual function.
cppConfig->setConcurrency(THREAD_COUNT);
try {
ReloadFromFile *reloadFromFile = new ReloadFromFile(
params->dataFilePath,
params->evidenceFilePath,
cppConfig);
reloadFromFile->run();
delete reloadFromFile;
}
catch (const StatusCodeException &e) {
ExampleBase::reportStatus(e.getCode(), params->dataFilePath);
exit(1);
}
}
#ifndef TEST
int main(int argc, char* argv[]) {
char dataFilePath[FIFTYONE_DEGREES_FILE_MAX_PATH];
char userAgentFilePath[FIFTYONE_DEGREES_FILE_MAX_PATH];
if (argc > 1) {
strcpy(dataFilePath, argv[1]);
}
else {
dataDir,
dataFileName,
dataFilePath,
sizeof(dataFilePath));
}
ExampleBase::reportStatus(status, dataFileName);
fgetc(stdin);
return 1;
}
if (argc > 2) {
strcpy(userAgentFilePath, argv[2]);
}
else {
dataDir,
userAgentFileName,
userAgentFilePath,
sizeof(userAgentFilePath));
}
ExampleBase::reportStatus(status, userAgentFileName);
fgetc(stdin);
return 1;
}
ExampleParameters params;
params.dataFilePath = dataFilePath;
params.evidenceFilePath = userAgentFilePath;
// run the example
fiftyoneDegreesExampleMemCheck(
&params,
fiftyoneDegreesExampleCPPReloadFromFileRun);
// Wait for a character to be pressed.
fgetc(stdin);
return 0;
}
#endif
C++ class wrapper for the fiftyoneDegreesConfigHash configuration structure.
Definition ConfigHash.hpp:72
void setConcurrency(uint16_t concurrency)
Set the expected concurrent requests for all the data set's collections.
State containing the states for all threads running in a multi-threaded example .
Definition ExampleBase.hpp:144
void processUserAgentsSingle()
Processes all the User-Agents in the file named in the shared state using the engine in the state usi...
volatile long threadsFinished
Number of threads that have finished their processing.
Definition ExampleBase.hpp:183
void joinThreads()
Joins the threads and frees the memory occupied by the threads.
void startThreads()
Starts threads that run the processUserAgentsMulti method.
Base class extended by all Hash examples.
Definition ExampleBase.hpp:58
static fiftyoneDegreesStatusCode getFilePath(const char *dataFolderName, const char *fileName, char *destination, size_t size)
Finds a file in the data folder, and downloads it first when it is missing and the data repository li...
EngineHash * engine
Hash Engine used for the example.
Definition ExampleBase.hpp:244
ExampleBase(byte *data, long length, DeviceDetection::Hash::ConfigHash *config)
Construct a new instance of the example to be run using the data pointer provided.
static void reportStatus(fiftyoneDegreesStatusCode status, const char *fileName)
Reports the status of the data file initialization.
DeviceDetection::Hash::ConfigHash * config
Configuration for the Engine.
Definition ExampleBase.hpp:240
Hash Reload From File Example.
Definition ReloadFromFile.cpp:88
ReloadFromFile(string dataFilePath, string userAgentFilePath, DeviceDetection::Hash::ConfigHash *config)
Definition ReloadFromFile.cpp:95
void run()
Run the example.
Definition ReloadFromFile.cpp:106
#define FIFTYONE_DEGREES_FILE_MAX_PATH
Define the max path length on the target system.
Definition file.h:218
EXTERNAL_VAR fiftyoneDegreesConfigHash fiftyoneDegreesHashDefaultConfig
Default detection configuration.
Definition hash.h:436
fiftyoneDegreesStatusCode
Status returned from the initialisation of a resource.
Definition status.h:81
@ FIFTYONE_DEGREES_STATUS_SUCCESS
All okay.
Definition status.h:82
bool fiftyoneDegreesThreadingGetIsThreadSafe()
Determines if the methods that should be thread safe have been compiled so they are thread safe.
C++ Hash engine examples.
Definition ExampleBase.hpp:54
C++ Examples.
Definition dd/FiftyoneDegrees.hpp:39
A C++ wrapper class for transform.h conversion routines.
Definition ComponentMetaDataBuilderHash.hpp:33
Hash specific configuration structure.
Definition hash.h:187