dca_interface  6.3.4
ipr_samples/iprenumsample/main.cpp
1 /* IBM Source Code */
2 /* (C) Copyright IBM Corp. 2009, 2012 */
3 /* Licensed Materials - Property of IBM */
4 /* US Government Users Restricted Rights - Use duplication or disclosure restricted by GSA Schedule Contract with IBM Corp. */
5 
34 #include <string>
35 #include <vector>
36 #include <iostream>
37 #include <fstream>
38 #include <ctime>
39 
40 #include "dca/dca_base.h"
42 #include "dca/dca_callbacks.h"
43 
44 using namespace dca;
45 using namespace dca_ipr;
46 
47 const std::string S_ToolName = "iprenumsample";
48 const std::string S_ToolVersion = "1.2";
49 
54 const std::string S_UsageString =
55  "<redist-folder> <ticket> <product> [<locale>]\n"
56  " redist-folder - the folder where the DCA is installed\n"
57  " ticket - a valid ticket\n"
58  " product - the product associated with your ticket\n"
59  " locale - optional locale for the IPR category info names and descriptions, default = en_US\n\n"
60  ;
61 
66 #ifdef WIN32
67 # define DCA_BINDIR "bin/Win32"
68 #else
69 # define DCA_BINDIR "bin/linux"
70 #endif
71 
76 #define DCA_INITDIR "init"
77 
82 #define DCA_LOGDIR "./logs"
83 
94 static void SetupInitData( const std::string& strRedistFolder,
95  InitData& aInitData )
96 {
97  aInitData.binDir = strRedistFolder + DCA_BINDIR;
98  aInitData.initDir = strRedistFolder + DCA_INITDIR;
99  aInitData.logDir = DCA_LOGDIR;
100 }
101 
110 static void SetupLicense( const std::string& strTicket,
111  const std::string& strProduct,
112  LicenseData& aLicenseData )
113 {
114  aLicenseData.ticket = strTicket;
115  aLicenseData.product = strProduct;
116 }
117 
124 static void PrintLicenseInfo( const License& aLicense )
125 {
126  const time_t expirationDate = aLicense.getExpirationDate();
127  struct tm *expirationTime = localtime( &expirationDate );
128 
129  std::cout << "License Info:" << std::endl;
130  std::cout << " DCA is " << ( aLicense.isLicensed() ? "licensed." :
131  "not licensed." ) << std::endl;
132  std::cout << " MaxUsers:" << aLicense.getMaxUsers() <<
133  std::endl;
134  std::cout << " MaxSessions:" << aLicense.getMaxSessions() <<
135  std::endl;
136  std::cout << " Ticket:" << aLicense.getTicket() <<
137  std::endl;
138  std::cout << " Session:" << aLicense.getSession() <<
139  std::endl;
140  std::cout << " Last Message:" << aLicense.getLastMessage() <<
141  std::endl;
142  std::cout << " Expiration Date:" << asctime( expirationTime ) <<
143  std::endl;
144 }
145 
150 static void PrintToolHeader()
151 {
152  std::cout << "IBM DCA Sample: " << S_ToolName << " (" << S_ToolVersion << ")" << std::endl;
153 }
154 
161 static void PrintUsage( const char *pSampleName )
162 {
163  std::cout << pSampleName << " usage:" << std::endl;
164  std::cout << S_UsageString << std::endl;
165 }
166 
174 void PrintLocale( const IprLocale& aIprLocale,
175  const std::string& strIndent = "" )
176 {
177  std::cout << strIndent << "\tIprLocale '" << aIprLocale.languageId() <<
178  "' (displayName=" << aIprLocale.displayName() << ")" << std::endl;
179 }
180 
187 void EnumLocales( const IprLocales& aIprLocales )
188 {
189  const DCA_SIZE_TYPE numOfLocales = aIprLocales.size();
190  if( numOfLocales > 0 ) {
191  std::cout << std::endl << "IprLocales (size=" << numOfLocales << ")" << std::endl;
192  for( DCA_INDEX_TYPE i = 0; i < numOfLocales; ++i ) {
193  const IprLocale myIprLocale = aIprLocales[ i ];
194  PrintLocale( myIprLocale, "\t" );
195  }
196  }
197  else std::cout << std::endl;
198 }
199 
208 void PrintEnum( const IprEnum& aIprEnum,
209  const std::string& strLocale,
210  const std::string& strIndent = "" )
211 {
212  std::cout << strIndent << "IprEnum: '" << aIprEnum.name( strLocale )
213  << "' (id=" << aIprEnum.id() << ")" << std::endl;
214 }
215 
224 void EnumCategoryEnums( const IprCategoriesInfo& aIprCategoriesInfo,
225  const IprCategory& aIprCategory,
226  const std::string& strLocale )
227 {
228  const DCA_INDEX_TYPE maxEnumsToDisplay = 5;
229 
230  DCA_SIZE_TYPE numOfEnums = aIprCategory.enumsSize();
231  if( numOfEnums > 0 ) {
232  const IprEnums myIprEnums = aIprCategoriesInfo.getEnums();
233  std::cout << " (enumsSize=" << numOfEnums << ")" << std::endl;
234 
235  for( DCA_INDEX_TYPE i = 0; i < numOfEnums; i++ ) {
236  // if too many items to display shorten output and display only the last one
237  if( i > maxEnumsToDisplay && numOfEnums > maxEnumsToDisplay ) {
238  // Shorten output... and stop enumeration
239  std::cout << "\t\t ...(" << (numOfEnums - maxEnumsToDisplay - 2) <<
240  " more)..." << std::endl;
241  // display the last enum item
242  const DCA_ENUM_ID_TYPE myLastEnumId = aIprCategory.enumId( numOfEnums - 1 );
243  const IprEnum myLastIprEnum = myIprEnums.byId( myLastEnumId );
244  PrintEnum( myLastIprEnum, strLocale, "\t\t" );
245  return;
246  }
247  const DCA_ENUM_ID_TYPE enumId = aIprCategory.enumId( i );
248  const IprEnum myIprEnum = myIprEnums.byId( enumId );
249  PrintEnum( myIprEnum, strLocale, "\t\t" );
250  }
251  }
252  else std::cout << std::endl;
253 }
254 
263 void PrintCategory( const IprCategory& aIprCategory,
264  const std::string& strLocale,
265  const std::string& strIndent = "" )
266 {
267  std::cout << strIndent << "IprCategory: '" << aIprCategory.name( strLocale ) <<
268  "' (id=" << aIprCategory.id() << ")";
269 }
270 
279 void EnumCategories( const IprCategoriesInfo& aIprCategoriesInfo,
280  const IprCategories& aIprCategories,
281  const std::string& strLocale )
282 {
283  const DCA_SIZE_TYPE numOfCategories = aIprCategories.size();
284 
285  if( numOfCategories > 0 ) {
286  std::cout << std::endl << "IprCategories (size=" << numOfCategories <<
287  ")" << std::endl;
288 
289  for( DCA_INDEX_TYPE i = 0; i < numOfCategories; ++i ) {
290  const IprCategory myIprCategory = aIprCategories[ i ];
291  PrintCategory( myIprCategory, strLocale, "\t" );
292  EnumCategoryEnums( aIprCategoriesInfo, myIprCategory, strLocale );
293  }
294  }
295  else std::cout << std::endl;
296 }
297 
305 void EnumEnums( const IprEnums& aIprEnums,
306  const std::string& strLocale )
307 {
308  const DCA_INDEX_TYPE maxEnumsToDisplay = 8;
309  const DCA_SIZE_TYPE numOfEnums = aIprEnums.size();
310 
311  if( numOfEnums > 0 ) {
312  std::cout << std::endl << "IprEnums (size=" << numOfEnums <<
313  ")" << std::endl;
314 
315  for( DCA_INDEX_TYPE i = 0; i < numOfEnums; ++i ) {
316  // if too many items to display shorten output and display only the last one
317  if( i > maxEnumsToDisplay && numOfEnums > maxEnumsToDisplay ) {
318  // Shorten output... and stop enumeration
319  std::cout << "\t ...(" << (numOfEnums - maxEnumsToDisplay - 2) <<
320  " more)..." << std::endl;
321 
322  // display the last one
323  const IprEnum myLastIprEnum = aIprEnums[ numOfEnums - 1 ];
324  PrintEnum( myLastIprEnum, strLocale, "\t" );
325  return;
326  }
327  const IprEnum myIprEnum = aIprEnums[i];
328  PrintEnum( myIprEnum, strLocale, "\t" );
329  }
330  }
331  else std::cout << std::endl;
332 }
333 
341 void EnumIprCategoriesInfo( const IprCategoriesInfo& aIprCategoriesInfo,
342  const std::string& strLocale )
343 {
344  std::cout << "Enumeration of all available IprLocale, IprCategory and IprEnum objects:"
345  << std::endl;
346 
347  const IprLocales myIprLocales = aIprCategoriesInfo.getLocales();
348  EnumLocales( myIprLocales );
349 
350  const IprCategories myIprCategories = aIprCategoriesInfo.getCategories();
351  EnumCategories( aIprCategoriesInfo, myIprCategories, strLocale );
352 
353  const IprEnums myIprEnums = aIprCategoriesInfo.getEnums();
354  EnumEnums( myIprEnums, strLocale );
355 }
356 
364 int main( int argc, char *argv[] )
365 {
366  PrintToolHeader();
367 
368  int rc = 5;
369 
370  try {
371  if( argc < 4 ) {
372  PrintUsage( argv[0] );
373  return 5;
374  }
375 
376  std::string strRedistFolder = argv[ 1 ];
377  const std::string strTicket = argv[ 2 ];
378  const std::string strProduct = argv[ 3 ];
379  std::string strLocale = "en_US"; // Default locale
380 
381  if (argc > 4) {
382  strLocale = argv[4];
383  }
384 
385  if( strRedistFolder.empty() ||
386  strTicket.empty() ||
387  strProduct.empty() )
388  {
389  PrintUsage( argv[0] );
390  return 5;
391  }
392 
393  // check for trailing fileslash - and add if necessary
394  const char c = strRedistFolder[ strRedistFolder.length() - 1 ];
395  if( c != '/' && c != '\\' )
396  strRedistFolder += "/";
397 
398  std::cout << std::endl;
399 
400  // init the 3rd party libraries
401  InitCUrl();
403 
404  // setup DCA directories
405  InitData myInitData;
406  SetupInitData( strRedistFolder, myInitData );
407 
408  // instantiate DCA API
409  DcaInstance myDca;
410  myDca = DcaInstance::create( myInitData );
411 
412  // setup license data
413  LicenseData myLicenseData;
414  SetupLicense( strTicket, strProduct, myLicenseData );
415  const License myLicense = myDca.createLicense( myLicenseData );
416 
417  PrintLicenseInfo( myLicense );
418 
419  if( myLicense.isLicensed( IprClassification::ID ) ) {
420  // initialize the IPR Classification module and create a IprCategoriesInfo instance
421  const IprClassification myIprClassification = IprClassification::create( myDca, myLicense );
422 
423  // create a IPR categories info for printing out the available locales, categories and enums
424  const IprCategoriesInfo myCategoriesInfo = myIprClassification.getCategoriesInfo();
425 
426  // call enumeration routine
427  EnumIprCategoriesInfo( myCategoriesInfo, strLocale );
428 
429  rc = 0;
430  }
431  }
432  catch( const ExDca& ex ) {
433  std::cerr << "DCA Exception occured. Details: " << ex.getDescription() <<
434  " (" << ex.getReturnCode() << ")." << std::endl;
435  rc = 10;
436  }
437  catch( const std::exception& s ) {
438  std::cerr << "std::exception occured. Details: " << s.what() << "." << std::endl;
439  rc = 10;
440  }
441  catch(...) {
442  std::cerr << "Unknown exception caught." << std::endl;
443  rc = 10;
444  }
445 
446  // deinit the 3rd party libraries
448  DeinitCUrl();
449 
450  return rc;
451 }
Encapsulation of a locale class, which allows access to the language id and the display name of local...
Definition: ipr_locale.h:27
DCA_SIZE_TYPE enumsSize() const
Returns the count of IprEnum items associated with current IprCategory.
Is used to create a License object. A license first must be created with DcaInstance::createLicense t...
Definition: base_classes.h:547
DCA_SIZE_TYPE size() const
Returns the number of locales in the container.
Exception class used in the DCA.
Definition: base_classes.h:237
DCA_SIZE_TYPE size() const
Returns the number of enums in the container.
void InitCUrl()
Initializes libcurl. Do not use any DCA function before initializing libcurl.
std::string initDir
the directory in which the DCA init files are stored
Definition: base_classes.h:266
int getMaxSessions() const
Returns the maximum allowed sessions associated with your ticket/license.
time_t getExpirationDate() const
Returns the expiration date of the license in UTC.
std::string getDescription() const
Returns a description of the error.
IprEnums getEnums() const
Returns the contained IprEnums.
static void SetupInitData(const std::string &redist_folder, InitData &initData)
Sets up the given initData by substituting the given redist_folder with DCA subdirectories.
Container class for all IPR enumeration objects.
std::string binDir
the directory in which the DCA binary (*.dca) files are stored
Definition: base_classes.h:265
This header includes initialization/deinitialization support functions for the 3rd party libraries us...
int DCA_ENUM_ID_TYPE
Id type for IprEnum class.
Definition: ipr_base.h:21
void SetOpenSslCallbacks()
Initializes the required callbacks for OpenSSL when using HTTPS or SSL connections in a multi-threade...
DCA_ENUM_ID_TYPE id() const
Returns the internal id of the enumerable item.
void UnsetOpenSslCallbacks()
Unsets the openssl callbacks. Do not call any DCA function after you have called this function.
IprCategories getCategories() const
Returns the contained IprCategories.
IprEnum byId(DCA_ENUM_ID_TYPE id) const
Returns the IprEnum with the given id.
int getMaxUsers() const
Returns the maximum allowed users associated with your ticket/license.
DCA_SIZE_TYPE size() const
Returns the number of categories in the container.
void DeinitCUrl()
Deinitializes libcurl. Do not call any DCA function after you have called this function.
Package header file for the IPR (IP Reputation) Classification Package.
#define DCA_LOGDIR
Relative directory for logfile(s).
#define DCA_INITDIR
DCA subdirectory of the DCA initialization data.
DCA_RESULT_TYPE getReturnCode() const
Gets the code of the error.
static void PrintToolHeader()
Prints out the name and the version of this sample.
Use a License to initialize a classification package or a toolbox package.
Definition: base_classes.h:560
bool isLicensed(DCA_MODULE_ID_TYPE id=0, bool force=false) const
Checks whether the given License is valid for the given module id.
static void PrintLicenseInfo(const License &aLicense)
Prints out the information about the provided License.
std::string ticket
The ticket as provided in the license.
Definition: base_classes.h:548
This header includes all header files of the DCA Base Package.
std::string name(const std::string &localeString=std::string()) const
Returns the localized (display) name of the category.
std::string displayName() const
Returns the display name of the locale.
Encapsulates the init and deinit of the DCA API.
Definition: base_classes.h:315
The IprEnum class describes a single enumerable item, for example the Geo Location "Greenland".
Definition: ipr_enum.h:26
std::string product
The product code used with the license.
Definition: base_classes.h:549
std::string name(const std::string &localeString=std::string()) const
Returns the localized (display) name of the enumerable item.
Definition of a container class for IprEnum objects.
Definition: ipr_enums.h:30
DCA_CATEGORY_ID_TYPE id() const
Returns the internal id of a IprCategory class.
std::string logDir
the directory in which the DCA log file should be created
Definition: base_classes.h:267
static void PrintUsage(const char *name)
Prints out the syntax of the sample.
std::string getLastMessage() const
Returns the last message received from our license server or if none available the last available mes...
size_t DCA_INDEX_TYPE
Type for index access (used for arrays and collections).
Definition: base_types.h:66
size_t DCA_SIZE_TYPE
Type for size (used for size of array and collections).
Definition: base_types.h:72
Definition of a container class for IprCategory objects.
std::string getTicket() const
Returns the ticket of the license as string.
Definition of the IprClassification class.
#define DCA_BINDIR
DCA subdirectory of the DCA binaries.
IprCategoriesInfo getCategoriesInfo() const
Retrieve the IprCategoriesInfo class.
std::string getSession() const
Returns the session of the license as string.
Definition of a container class for IprLocale objects.
Definition: ipr_locales.h:33
The IprCategory class contains information for a single category. An IP address can be assigned to on...
Definition: ipr_category.h:27
DCA_ENUM_ID_TYPE enumId(DCA_INDEX_TYPE index) const
Returns the id of an enum item associated with the current IprCategory.
const std::string S_UsageString
Usage string, displayed if a parameter is missing.
This structure is used to initialize the DcaInstance.
Definition: base_classes.h:264
IprLocales getLocales() const
Returns the contained IprLocales.
static DcaInstance create(const InitData &initData)
Creates a DcaInstance, starts up the DCA API and initializes the required main module.
static void SetupLicense(const std::string &ticket, const std::string &product, LicenseData &licenseData)
Sets up the given licenseData by copying the given ticket and product strings.
std::string languageId() const
Returns the language id of the locale.
License createLicense(const LicenseData &licData, const ProxySettings &proxySettings=ProxySettings(), LogLevel aLogLevel=LOG_Initial) const
Creates a License object using the given LicenseData.
int main(int argc, char *argv[])
The main routine.