Reload from file example of using 51Degrees device detection.
Reload from file example of using 51Degrees device detection.This example shows how to:
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.
#include "../../C/Hash/ExampleBase.h"
#include "ExampleBase.hpp"
public:
string dataFilePath,
string userAgentFilePath,
this->userAgentFilePath = userAgentFilePath;
};
int numberOfReloads = 0;
int numberOfReloadFails = 0;
printf("** Multi Threaded Reload Example **\r\n");
try {
numberOfReloads++;
}
catch (StatusCodeException&) {
numberOfReloadFails++;
}
#ifdef _MSC_VER
Sleep(2000);
#else
usleep(20000000);
#endif
}
}
else {
printf("** Single Threaded Reload Example **\r\n");
try {
numberOfReloads++;
}
catch (StatusCodeException&) {
numberOfReloadFails++;
}
}
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) {
try {
params->dataFilePath,
params->evidenceFilePath,
cppConfig);
reloadFromFile->run();
delete reloadFromFile;
}
catch (const StatusCodeException &e) {
exit(1);
}
}
#ifndef TEST
int main(int argc, char* argv[]) {
if (argc > 1) {
strcpy(dataFilePath, argv[1]);
}
else {
dataDir,
dataFileName,
dataFilePath,
sizeof(dataFilePath));
}
fgetc(stdin);
return 1;
}
if (argc > 2) {
strcpy(userAgentFilePath, argv[2]);
}
else {
dataDir,
userAgentFileName,
userAgentFilePath,
sizeof(userAgentFilePath));
}
fgetc(stdin);
return 1;
}
ExampleParameters params;
params.dataFilePath = dataFilePath;
params.evidenceFilePath = userAgentFilePath;
fiftyoneDegreesExampleMemCheck(
¶ms,
fiftyoneDegreesExampleCPPReloadFromFileRun);
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