\r\n

51Degrees Device Detection C/C++  4.4

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

ExampleBase.hpp

1 /* *********************************************************************
2  * This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3  * Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
4  * Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
5  *
6  * This Original Work is licensed under the European Union Public Licence
7  * (EUPL) v.1.2 and is subject to its terms as set out below.
8  *
9  * If a copy of the EUPL was not distributed with this file, You can obtain
10  * one at https://opensource.org/licenses/EUPL-1.2.
11  *
12  * The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
13  * amended by the European Commission) shall be deemed incompatible for
14  * the purposes of the Work and the provisions of the compatibility
15  * clause in Article 5 of the EUPL shall not apply.
16  *
17  * If using the Work as, or as part of, a network application, by
18  * including the attribution notice(s) required under Article 5 of the EUPL
19  * in the end user terms of the application under an appropriate heading,
20  * such notice(s) shall fulfill the requirements of that article.
21  * ********************************************************************* */
22 
23 #ifndef FIFTYONE_DEGREES_EXAMPLE_BASE_CPLUSPLUS_INCLUDED
24 #define FIFTYONE_DEGREES_EXAMPLE_BASE_CPLUSPLUS_INCLUDED
25 
26 // Include ExmapleBase.h before others as it includes Windows 'crtdbg.h'
27 // which requires to be included before 'malloc.h'.
28 #include "../../C/Hash/ExampleBase.h"
29 #include "../../../src/hash/EngineHash.hpp"
30 #include <string>
31 #include <iostream>
32 #include <thread>
33 #include "../../../src/common-cxx/textfile.h"
34 
35 #define THREAD_COUNT 4
36 
37 static const char *dataDir = "device-detection-data";
38 
39 static const char *dataFileName = "51Degrees-LiteV4.1.hash";
40 
41 static const char *userAgentFileName = "20000 User Agents.csv";
42 
43 using std::cout;
44 using std::thread;
45 using namespace FiftyoneDegrees::Common;
46 using namespace FiftyoneDegrees::DeviceDetection;
48 
49 namespace FiftyoneDegrees {
50  namespace Examples {
54  namespace Hash {
58  class ExampleBase {
59  public:
60 
69  byte *data,
70  long length,
72 
78  ExampleBase(string dataFilePath);
79 
87  string dataFilePath,
89 
93  virtual ~ExampleBase();
94 
98  virtual void run() = 0;
99 
101  static const char* mobileUserAgent;
102 
104  static const char* desktopUserAgent;
105 
107  static const char* uachPlatform;
108 
110  static const char* uachPlatformVersion;
111 
113  static const char* mediaHubUserAgent;
114 
120  static void reportStatus(
122  const char* fileName);
123 
124  protected:
129  class SharedState {
130  public:
137  SharedState(EngineHash *engine, string userAgentFilePath);
138 
143  void startThreads();
144 
149  void joinThreads();
150 
156  void processUserAgentsSingle();
157 
165  static void processUserAgentsMulti(void *state);
166 
168  volatile long threadsFinished;
173  thread threads[THREAD_COUNT];
175  };
176 
181  class ThreadState {
182  public:
188  ThreadState(EngineHash *engine);
190  int hashCode;
193  };
194 
201  static unsigned long getHashCode(
203  unsigned long hashCode = 0;
204  uint32_t requiredPropertyIndex;
205  Common::Value<string> value;
206 
207  for (requiredPropertyIndex = 0;
208  requiredPropertyIndex < (uint32_t)results->getAvailableProperties();
209  requiredPropertyIndex++) {
210  value = results->getValueAsString(requiredPropertyIndex);
211  if (value.hasValue()) {
212  hashCode ^= generateHash(
213  (unsigned char*)(value.getValue().c_str()));
214  }
215  }
216  return hashCode;
217  }
218 
225  static void processUserAgent(const char *userAgent, void *state);
226 
233 
234  private:
240  static unsigned long generateHash(unsigned char *value);
241  };
242  }
243  }
244 }
245 
246 #endif
Encapsulates a value returned an instance of ResultsBase for a specified property.
Definition: Value.hpp:74
static const char * uachPlatform
Example UACH Platform value.
Definition: ExampleBase.hpp:107
State for a single thread carrying out processing in order to store a hash of the results.
Definition: ExampleBase.hpp:181
fiftyoneDegreesStatusCode
Status returned from the initialisation of a resource.
Definition: status.h:77
RequiredPropertiesConfig * properties
Properties to initialise the Engine with.
Definition: ExampleBase.hpp:230
static const char * uachPlatformVersion
Example UACH Platform Version value.
Definition: ExampleBase.hpp:110
T getValue()
Gets the value contained in the Value instance.
Definition: Value.hpp:129
int getAvailableProperties() const
Get the number of available properties contained in the Results instance.
Encapsulates the results of a Hash device detection engine's processing.
Definition: ResultsHash.hpp:64
Encapsulates the Hash engine class which implements EngineDeviceDetection.
Definition: EngineHash.hpp:94
Hash Device Detection specific classes.
Definition: ComponentMetaDataBuilderHash.hpp:35
static unsigned long getHashCode(DeviceDetection::Hash::ResultsHash *results)
Get the hash code for all the values stored in the results instance.
Definition: ExampleBase.hpp:201
51Degrees base namespace.
Definition: ComponentMetaDataBuilderHash.hpp:33
static const char * desktopUserAgent
Example desktop User-Agent string.
Definition: ExampleBase.hpp:104
C++ class wrapper for the fiftyoneDegreesConfigHash configuration structure.
Definition: ConfigHash.hpp:72
Value< string > getValueAsString(const char *propertyName)
Get a string representation of the value associated with the required property name.
volatile long threadsFinished
Number of threads that have finished their processing.
Definition: ExampleBase.hpp:168
string userAgentFilePath
Path to the User-Agents to process.
Definition: ExampleBase.hpp:171
bool hasValue()
Indicates whether or not a valid value has been returned by the ResultsBase instance.
Definition: Value.hpp:100
C++ class wrapper for the fiftyoneDegreesPropertiesRequired structure.
Definition: RequiredPropertiesConfig.hpp:58
State containing the states for all threads running in a multi-threaded example .
Definition: ExampleBase.hpp:129
DeviceDetection::Hash::ConfigHash * config
Configuration for the Engine.
Definition: ExampleBase.hpp:228
static const char * mobileUserAgent
Example mobile User-Agent string.
Definition: ExampleBase.hpp:101
Base class extended by all Hash examples.
Definition: ExampleBase.hpp:58
static const char * mediaHubUserAgent
Example media hub User-Agent string.
Definition: ExampleBase.hpp:113
Device Detection specific classes.
Definition: ComponentMetaDataBuilderHash.hpp:34
EngineHash * engine
Hash Engine used for the example.
Definition: ExampleBase.hpp:232
EngineHash * engine
Pointer to the engine.
Definition: ExampleBase.hpp:167
Shared classes.
Definition: Collection.hpp:31
int hashCode
Running hash code for the processing being carried out.
Definition: ExampleBase.hpp:190
EngineHash * engine
Pointer to the engine.
Definition: ExampleBase.hpp:189