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 resource with initialized dataset. Function reloads the dataset from the provided pointer to the continuous memory space containing the data file. New dataset is created 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:
byte* data,
long length,
string userAgentFilePath,
this->data = data;
this->length = length;
this->userAgentFilePath = userAgentFilePath;
};
int numberOfReloads = 0;
int numberOfReloadFails = 0;
printf("** Multi Threaded Reload Example **\r\n");
try {
engine->refreshData(data, length);
numberOfReloads++;
}
catch (StatusCodeException&) {
numberOfReloadFails++;
}
#ifdef _MSC_VER
Sleep(2000);
#else
usleep(20000000);
#endif
}
}
else {
printf("** Single Threaded Reload Example **\r\n");
try {
engine->refreshData(data, length);
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;
byte *data;
long length;
};
}
}
}
extern "C" void fiftyoneDegreesExampleCPPReloadFromMemoryRun(ExampleParameters *params) {
params->dataFilePath, &reader);
exit(1);
}
try {
params->evidenceFilePath,
cppConfig);
reloadFromMemory->run();
delete reloadFromMemory;
}
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,
fiftyoneDegreesExampleCPPReloadFromMemoryRun);
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 Memory Example.
Definition ReloadFromMemory.cpp:89
ReloadFromMemory(byte *data, long length, string userAgentFilePath, DeviceDetection::Hash::ConfigHash *config)
Definition ReloadFromMemory.cpp:96
void run()
Run the example.
Definition ReloadFromMemory.cpp:110
#define FIFTYONE_DEGREES_FILE_MAX_PATH
Define the max path length on the target system.
Definition file.h:218
EXTERNAL_VAR void(* fiftyoneDegreesFree)(void *__ptr)
Pointer to the method used to free memory.
Definition memory.h:206
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
Used to read data from memory in a similar manner to a file handle.
Definition memory.h:58
byte * startByte
The first byte in memory.
Definition memory.h:59
fiftyoneDegreesFileOffset length
Length of the file in bytes.
Definition memory.h:62