dca_interface  6.3.4
url_samples/urldbdownloadsample/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 
37 #include <cstdlib>
38 #include <string>
39 #include <iostream>
40 #include <fstream>
41 
42 #include "dca/dca_base.h"
44 #include "dca/dca_callbacks.h"
45 
47 
48 using namespace dca;
49 
50 const std::string S_ToolName = "urldbdownloadsample";
51 const std::string S_ToolVersion = "1.2";
52 
57 const std::string S_UsageString =
58  " <redist-folder> <ticket> <product>\n"
59  " redist-folder - the folder where the DCA is installed to\n"
60  " ticket - a valid ticket\n"
61  " product - the product associated with your ticket\n\n"
62  ;
63 
68 #ifdef WIN32
69 # define DCA_BINDIR "bin/Win32"
70 #else
71 # define DCA_BINDIR "bin/linux"
72 #endif
73 
78 #define DCA_INITDIR "init"
79 
83 #define DCA_LOGDIR "./logs"
84 
93 static void SetupInitData( const std::string& redist_folder, InitData& initData )
94 {
95  initData.binDir = redist_folder + DCA_BINDIR;
96  initData.initDir = redist_folder + DCA_INITDIR;
97  initData.logDir = DCA_LOGDIR;
98 }
99 
107 static void SetupLicense( const std::string& ticket, const std::string& product,
108  LicenseData& licenseData )
109 {
110  licenseData.ticket = ticket;
111  licenseData.product = product;
112 }
113 
119 static void SetupConnectionData( DbConnectionData& cData )
120 {
121  cData.useLocalDatabase = true;
122  cData.dbType = DBT_Url;
123 }
124 
129 static void PrintToolHeader()
130 {
131  std::cout << "IBM DCA Sample: " << S_ToolName << " (" << S_ToolVersion << ")" << std::endl;
132 }
133 
139 static void PrintUsage( const char *name )
140 {
141  std::cout << "usage: " << name << S_UsageString << std::endl;
142 }
143 
149 static void PrintUpdateResults( const UpdateResults& results )
150 {
151  std::cout << "UpdateResults: size=" << results.size() << ", restartRequired="
152  << ( results.restartRequired() ? "true" : "false" )
153  << std::endl;
154 
155  if( !results.size() ) {
156  std::cout << "Currently there are no updates available" << std::endl;
157  return;
158  }
159 
160  // iterate through all received updates and print out the details
161  const DCA_SIZE_TYPE size = results.size();
162 
163  for( DCA_INDEX_TYPE i = 0; i < size; ++i ) {
164  const UpdateResult result = results[i];
165 
166  std::cout << "Result #" << i << " id=" << result.id() << ", contentUpdated="
167  << ( result.contentUpdated() ? "true" : "false" ) << ", engineUpdated="
168  << ( result.engineUpdated() ? "true" : "false" ) << std::endl;
169 
170  const DCA_SIZE_TYPE numDetals = result.size();
171 
172  for( DCA_INDEX_TYPE j = 0; j < numDetals; ++j ) {
173  const UpdateResultDetail detail = result[j];
174 
175  std::string returnMsg = "internal error";
176  if( detail.returnCode() == DCA_UPDATE_DOWNLOAD_SCHEDULED ) {
177  returnMsg = "download scheduled";
178  }
179  else if( detail.returnCode() == 0 ) {
180  returnMsg = "success";
181  }
182 
183  std::cout << " Detail #" << j << ":"
184  << "\n component=" << detail.component()
185  << "\n old version=" << detail.oldVersion()
186  << "\n new version=" << detail.newVersion()
187  << "\n available=" << ( detail.available() ? "true" : "false" )
188  << "\n downloaded=" << ( detail.downloaded() ? "true" : "false" )
189  << "\n installed=" << ( detail.installed() ? "true" : "false" )
190  << "\n return=" << returnMsg << std::endl;
191  }
192  }
193 }
194 
212 static int PerformUpdateAndSchedule( const DcaInstance& myDca, const DbConnection& myDbConnection, const UpdateModule& myUpdateModule )
213 {
214  const std::string current_db_version = myDbConnection.getDatabaseVersion();
215 
216  // call UpdateModule::performUpdate
217  std::cout << "Start update process, paramater force is set to 'false'..." << std::endl;
218  UpdateResults results;
219  FunctionResult myFR = myUpdateModule.performUpdate( false, results );
220  if( !myFR ) {
221  // we encounter an error... print detailed error code and return
222  std::cerr << "Got error from update. Details: '" << myFR.getDescription()
223  << "' (" << myFR.getReturnCode() << "). Aborting." << std::endl;
224  return 1;
225  }
226 
227  PrintUpdateResults( results );
228 
229  // set up an instance of MyUrlScheduleEventSubscriber to capture and log the schedule event information
230  std::cout << std::endl << "Starting schedule process..." << std::endl;
231  MyUrlScheduleEventSubscriber mySubscriber;
232  myFR = myDca.schedule( &mySubscriber );
233  if( !myFR ) {
234  // we encounter an error... print detailed error code and return
235  std::cerr << "Received error from schedule. Details: '" << myFR.getDescription()
236  << "' (" << myFR.getReturnCode() << ")." << std::endl;
237  return 2;
238  }
239 
240  const std::string new_db_version = myDbConnection.getDatabaseVersion();
241 
242  std::cout << "Database has been updated from version " << current_db_version << " to " << new_db_version << std::endl;
243  return 0;
244 }
245 
253 int main( int argc, char *argv[] )
254 {
255  PrintToolHeader();
256 
257  int rc = 0;
258 
259  try {
260 
261  if( argc < 4 ) {
262  PrintUsage( argv[0] );
263  return 5;
264  }
265 
266  std::string myRedistFolder = argv[ 1 ];
267  const std::string myTicket = argv[ 2 ];
268  const std::string myProduct = argv[ 3 ];
269 
270  if( myRedistFolder.empty() || myTicket.empty() ||
271  myProduct.empty() ) {
272  PrintUsage( argv[0] );
273  return 5;
274  }
275 
276  // check for traling fileslash - and add if necessary
277  const char c = myRedistFolder[ myRedistFolder.length() - 1 ];
278  if( c != '/' && c != '\\' )
279  myRedistFolder += "/";
280 
281  // init the 3rd party libraries
282  InitCUrl();
284 
285  {
286  // setup DCA directories
287  InitData myInitData;
288  SetupInitData( myRedistFolder, myInitData );
289 
290  // instantiate DCA API
291  DcaInstance myDca;
292  myDca = DcaInstance::create( myInitData );
293 
294  // setup license data
295  LicenseData myLicenseData;
296  SetupLicense( myTicket, myProduct, myLicenseData );
297  const License myLicense = myDca.createLicense( myLicenseData );
298 
299  if( !myLicense.isLicensed( UpdateModule::ID ) ) {
300  std::cout << "DCA is not licensed!" << std::endl;
301  rc = 5;
302  }
303  else {
304  // setup a signature database connection
305  DbConnectionData myDbConnectionData;
306  SetupConnectionData( myDbConnectionData );
307  const DbConnection myDbConnection = myDca.createDbConnection( myLicense, myDbConnectionData );
308 
309  // initialize the URL classification, classifier and categories info modules
310  const UrlClassification myUrlClassification = UrlClassification::create( myDca, myLicense );
311  const CategoriesInfo myCategoriesInfo = myDca.getCategoriesInfo( DCA_CAT_INFO_TYPE_URL );
312  const UrlDbClassifier myUrlDbClassifier = myUrlClassification.createDbClassifier( myDbConnection );
313 
314  // create the update module
315  const UpdateModule myUpdateModule = UpdateModule::create( myDca, myLicense );
316 
317  // call performUpdate and schedule functions to update active components
318  rc = PerformUpdateAndSchedule( myDca, myDbConnection, myUpdateModule );
319  }
320  }
321  }
322  catch( const ExDca& ex ) {
323  std::cerr << "DCA Exception occured. Details: " << ex.getDescription() <<
324  " (" << ex.getReturnCode() << ")." << std::endl;
325  rc = 10;
326  }
327  catch( const std::exception& s ) {
328  std::cerr << "std::exception occured. Details: " << s.what() << "." << std::endl;
329  rc = 10;
330  }
331  catch(...) {
332  std::cerr << "Unknown exception caught." << std::endl;
333  rc = 10;
334  }
335 
336  // deinit the 3rd party libraries
338  DeinitCUrl();
339 
340  return rc;
341 }
342 
343 
344 
Is used to create a License object. A license first must be created with DcaInstance::createLicense t...
Definition: base_classes.h:547
Exception class used in the DCA.
Definition: base_classes.h:237
const DCA_CATEGORIES_INFO_TYPE DCA_CAT_INFO_TYPE_URL
Refers to the internal categories info for URL classification.
std::string oldVersion() const
Returns the version active (installed) before the update was invoked.
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
FunctionResult schedule(ScheduleEventSubscriberIntf *pScheduleEventSubscriber=NULL) const
Invokes scheduled tasks, e.g. Database updates and processes that can take a long time.
static UpdateModule create(const DcaInstance &aDcaInstance, const License &aLicense, const ProxySettings &proxySettings=ProxySettings())
std::string getDescription() const
Returns a description of the error.
bool downloaded() const
true if an update sucessfully downloaded for this component, false otherwise
int returnCode() const
The internal code of the update, this can be any internal error code, 0 in case of success or DCA_UPD...
CategoriesInfo getCategoriesInfo(DCA_CATEGORIES_INFO_TYPE categoryType) const
Returns the DCA internal categories, groups and locales.
Encapsulates the details of a result of an update process.
DCA_RESULT_TYPE getReturnCode() const
Returns the last error code (if any).
bool restartRequired() const
true if an update has been installed that requires a restart of the DCA, false otherwise.
static void SetupInitData(const std::string &redist_folder, InitData &initData)
Sets up the given initData by substituting the given redist_folder with DCA subdirectories.
The update module is used to download and install DCA content and engine updates.
Definition: base_classes.h:917
bool installed() const
true if an update sucessfully installed for this component, false otherwise
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...
Encapsulates the results of an update process.
Definition: base_classes.h:998
A container class that allows access to the contained Categories, Groups and Locales.
Header file for the schedule event subscriber.
void SetOpenSslCallbacks()
Initializes the required callbacks for OpenSSL when using HTTPS or SSL connections in a multi-threade...
Stores the connection data for a database.
Definition: base_classes.h:815
void UnsetOpenSslCallbacks()
Unsets the openssl callbacks. Do not call any DCA function after you have called this function.
const int DCA_UPDATE_DOWNLOAD_SCHEDULED
Internal DCA status code to indicate that a potentially time consuming download has been scheduled....
void DeinitCUrl()
Deinitializes libcurl. Do not call any DCA function after you have called this function.
Database connection class for a local or remote database.
Definition: base_classes.h:859
bool contentUpdated() const
If true the update(s) include a content update.
This header includes all header files of the URL Classification Package.
#define DCA_LOGDIR
Relative directory for logfile(s).
Main class for the URL classification.
#define DCA_INITDIR
DCA subdirectory of the DCA initialization data.
DCA_RESULT_TYPE getReturnCode() const
Gets the code of the error.
std::string newVersion() const
Returns the version currently active (installed).
bool available() const
true if an update was available for this component, false otherwise
Encapsulates one of the results of an update process.
URL database classifier class.
static void PrintToolHeader()
Prints out the name and the version of this sample.
Example implementation of a schedule event subscriber.
Use a License to initialize a classification package or a toolbox package.
Definition: base_classes.h:560
FunctionResult performUpdate(bool force, UpdateResults &results) const
Performs an update for all licensed and initialized DCA modules and classifiers.
bool isLicensed(DCA_MODULE_ID_TYPE id=0, bool force=false) const
Checks whether the given License is valid for the given module id.
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.
bool useLocalDatabase
Set to true to connect to a local or custom database, set to false to use a remote database.
Definition: base_classes.h:821
Encapsulates the init and deinit of the DCA API.
Definition: base_classes.h:315
std::string product
The product code used with the license.
Definition: base_classes.h:549
static DCA_MODULE_ID_TYPE ID
This is the module id of the update module.
Definition: base_classes.h:920
const DbType DBT_Url
Used for DbConnection classes for URL classification.
static UrlClassification create(const DcaInstance &aDcaInstance, const License &aLicense)
Creates the URL classification module by using the given DcaInstance and License.
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.
size_t DCA_INDEX_TYPE
Type for index access (used for arrays and collections).
Definition: base_types.h:66
DbType dbType
The type of the database.
Definition: base_classes.h:820
DCA_SIZE_TYPE size() const
Returns the number of available details (if any).
std::string getDescription() const
Returns the description for the error or warning.
size_t DCA_SIZE_TYPE
Type for size (used for size of array and collections).
Definition: base_types.h:72
bool engineUpdated() const
If true the update(s) include an engine update.
#define DCA_BINDIR
DCA subdirectory of the DCA binaries.
Standard function result.
Definition: base_classes.h:148
UrlDbClassifier createDbClassifier(const DbConnection &aDbConnection, const UrlDbClassifierOptions &options=UrlDbClassifierOptions()) const
Create a URL database classifier. The classifier is created by using the provided database connection...
const std::string S_UsageString
Usage string, displayed if a parameter is missing.
DbConnection createDbConnection(const License &aLicense, const DbConnectionData &dbcData, const ProxySettings &proxySettings=ProxySettings(), LogLevel aLogLevel=LOG_Initial) const
Creates a DbConnection object using the given DbConnectionData.
This structure is used to initialize the DcaInstance.
Definition: base_classes.h:264
std::string getDatabaseVersion() const
Returns the currently used database version.
static DcaInstance create(const InitData &initData)
Creates a DcaInstance, starts up the DCA API and initializes the required main module.
DCA_SIZE_TYPE size() const
Returns the number of results available .
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.
License createLicense(const LicenseData &licData, const ProxySettings &proxySettings=ProxySettings(), LogLevel aLogLevel=LOG_Initial) const
Creates a License object using the given LicenseData.
DCA_MODULE_ID_TYPE id() const
Returns the module ID for which the updates have been installed.
std::string component() const
Returns the name of the component the detail applies to.
int main(int argc, char *argv[])
The main routine.