dca_interface  6.3.4
generic_samples/updatesample/main.cpp
Go to the documentation of this file.
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 
39 #include <string>
40 #include <cstdio>
41 #include <iostream>
42 #include <ctime>
43 
44 #include "dca/dca_base.h"
45 #include "dca/dca_callbacks.h"
46 
47 using namespace dca;
48 
49 const std::string S_ToolName = "updatesample";
50 const std::string S_ToolVersion = "1.2";
51 
56 const std::string S_UsageString =
57  "<dca-redist-folder> <ticket> <product>\n"
58  " dca-redist-folder - the folder where the DCA is installed to\n"
59  " ticket - a valid ticket\n"
60  " product - the product associated with your ticket\n\n"
61  ;
62 
67 #ifdef WIN32
68 # define DCA_BINDIR "bin/Win32"
69 #else
70 # define DCA_BINDIR "bin/linux"
71 #endif
72 
77 #define DCA_INITDIR "init"
78 
83 #define DCA_LOGDIR "./logs"
84 
95 static void SetupInitData( const std::string& redist_folder, InitData& initData )
96 {
97  initData.binDir = redist_folder + DCA_BINDIR;
98  initData.initDir = redist_folder + DCA_INITDIR;
99  initData.logDir = DCA_LOGDIR;
100 }
101 
110 static void SetupLicense( const std::string& ticket, const std::string& product,
111  LicenseData& licenseData )
112 {
113  licenseData.ticket = ticket;
114  licenseData.product = product;
115 }
116 
122 static void PrintLicenseInfo( const License& aLicense )
123 {
124  const time_t expirationDate = aLicense.getExpirationDate();
125  struct tm *expirationTime = localtime( &expirationDate );
126 
127  std::cout << "License Info:" << std::endl;
128  std::cout << " DCA is " << ( aLicense.isLicensed() ? "licensed." :
129  "not licensed." ) << std::endl;
130  std::cout << " MaxUsers:" << aLicense.getMaxUsers() <<
131  std::endl;
132  std::cout << " MaxSessions:" << aLicense.getMaxSessions() <<
133  std::endl;
134  std::cout << " Ticket:" << aLicense.getTicket() <<
135  std::endl;
136  std::cout << " Session:" << aLicense.getSession() <<
137  std::endl;
138  std::cout << " Last Message:" << aLicense.getLastMessage() <<
139  std::endl;
140  std::cout << " Expiration Date:" << asctime( expirationTime ) <<
141  std::endl;
142 }
143 
157 int TestUpdate( const DcaInstance& myDca, const License& myLicense )
158 {
159  std::cout << "Starting update routine..." << std::endl;
160  // create the update module
161  const UpdateModule myUpdateModule = UpdateModule::create( myDca, myLicense );
162 
163  std::cout << "Start update process, paramater force is set to 'false'..." << std::endl;
164  UpdateResults results;
165  FunctionResult myFR = myUpdateModule.performUpdate( false, results );
166  if( !myFR ) {
167  // we encounter an error... print detailed error code and return
168  std::cerr << "Got error from update. Details: '" << myFR.getDescription()
169  << "' (" << myFR.getReturnCode() << "). Aborting." << std::endl;
170  return 1;
171  }
172 
173  std::cout << "UpdateResults: size=" << results.size() << ", restartRequired="
174  << ( results.restartRequired() ? "true" : "false" )
175  << std::endl;
176 
177  if( !results.size() ) {
178  std::cout << "Currently there are no updates available" << std::endl;
179  return 0;
180  }
181 
182  // iterate through all received updates and print out the details
183  const DCA_SIZE_TYPE size = results.size();
184 
185  for( DCA_INDEX_TYPE i = 0; i < size; ++i ) {
186  const UpdateResult result = results[i];
187 
188  std::cout << "Result #" << i << " id=" << result.id() << ", contentUpdated="
189  << ( result.contentUpdated() ? "true" : "false" ) << ", engineUpdated="
190  << ( result.engineUpdated() ? "true" : "false" ) << std::endl;
191 
192  const DCA_SIZE_TYPE numDetails = result.size();
193 
194  for( DCA_INDEX_TYPE j = 0; j < numDetails; ++j ) {
195  const UpdateResultDetail detail = result[j];
196 
197  std::string returnMsg = "internal error";
198  if( detail.returnCode() == DCA_UPDATE_DOWNLOAD_SCHEDULED ) {
199  returnMsg = "download scheduled";
200  }
201  else if( detail.returnCode() == 0 ) {
202  returnMsg = "success";
203  }
204 
205  std::cout << " Detail #" << j << ":"
206  << "\n component=" << detail.component()
207  << "\n old version=" << detail.oldVersion()
208  << "\n new version=" << detail.newVersion()
209  << "\n available=" << ( detail.available() ? "true" : "false" )
210  << "\n downloaded=" << ( detail.downloaded() ? "true" : "false" )
211  << "\n installed=" << ( detail.installed() ? "true" : "false" )
212  << "\n return=" << returnMsg << std::endl;
213  }
214  }
215 
216  std::cout << "Leaving update routine. All done" << std::endl;
217  return 0;
218 }
219 
224 static void PrintToolHeader()
225 {
226  std::cout << "IBM DCA Sample: " << S_ToolName << " (" <<
227  S_ToolVersion << ")" << std::endl;
228 }
229 
235 static void PrintUsage( const char *name )
236 {
237  std::cout << name << std::endl << "Usage:" << std::endl;
238  std::cout << S_UsageString << std::endl;
239 }
240 
252 int main( int argc, char *argv[] )
253 {
254  PrintToolHeader();
255 
256  int rc = 5;
257 
258  try {
259 
260  if( argc < 4 ) {
261  PrintUsage( argv[0] );
262  return 4;
263  }
264 
265  std::string redist_folder = argv[1];
266  const std::string ticket = argv[2];
267  const std::string product = argv[3];
268 
269  if( redist_folder.empty() || ticket.empty() ||
270  product.empty() ) {
271  PrintUsage( argv[0] );
272  return 5;
273  }
274 
275  // check for trailing fileslash - and add if necessary
276  const char c = redist_folder[ redist_folder.length() - 1 ];
277  if( c != '/' && c != '\\' )
278  redist_folder += "/";
279 
280  // init the 3rd party libraries
281  InitCUrl();
283 
284  {
285  // setup DCA directories
286  InitData myInitData;
287  SetupInitData( redist_folder, myInitData );
288 
289  // instantiate the DCA API
290  DcaInstance myDca;
291  myDca = DcaInstance::create( myInitData );
292 
293  // setup license data
294  LicenseData myLicenseData;
295  SetupLicense( ticket, product, myLicenseData );
296  const License myLicense = myDca.createLicense( myLicenseData );
297 
298  PrintLicenseInfo( myLicense );
299 
300  if( myLicense.isLicensed( UpdateModule::ID ) ) {
301  // NOTE:
302  // The update process includes only already loaded modules and classifiers
303  // for content updates.
304  // To get this sample doing a real update initialize some of your licensed modules
305  // here e.g. TextClassification or UrlClassification - refer to the related sample
306  // of those modules to see how they can be initialized.
307 
308  // call update routine - usually this should be called from a dedicated thread
309  rc = TestUpdate( myDca, myLicense );
310  }
311  }
312  }
313  catch( const ExDca& ex ) {
314  std::cerr << "DCA Exception occured. Details: " << ex.getDescription() <<
315  " (" << ex.getReturnCode() << ")." << std::endl;
316  rc = 10;
317  }
318  catch( const std::exception& s ) {
319  std::cerr << "std::exception occured. Details: " << s.what() << "." << std::endl;
320  rc = 10;
321  }
322  catch(...) {
323  std::cerr << "Unknown exception caught." << std::endl;
324  rc = 10;
325  }
326 
327  // deinit the 3rd party libraries
329  DeinitCUrl();
330 
331  return rc;
332 }
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
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
static UpdateModule create(const DcaInstance &aDcaInstance, const License &aLicense, const ProxySettings &proxySettings=ProxySettings())
int getMaxSessions() const
Returns the maximum allowed sessions associated with your ticket/license.
const std::string S_UsageString
Usage string - that is printed out if a parameter was missing.
time_t getExpirationDate() const
Returns the expiration date of the license in UTC.
std::string getDescription() const
Returns a description of the error.
#define DCA_LOGDIR
Relative directory for logfile(s).
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...
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.
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
static void PrintLicenseInfo(const License &aLicense)
Prints out the information about the provided License.
void SetOpenSslCallbacks()
Initializes the required callbacks for OpenSSL when using HTTPS or SSL connections in a multi-threade...
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....
int getMaxUsers() const
Returns the maximum allowed users associated with your ticket/license.
void DeinitCUrl()
Deinitializes libcurl. Do not call any DCA function after you have called this function.
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.
bool contentUpdated() const
If true the update(s) include a content update.
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.
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.
static void PrintUsage(const char *name)
Prints out the syntax of the sample.
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
static void SetupInitData(const std::string &redist_folder, InitData &initData)
Sets up the given initData by substituting the given redist_folder with DCA subdirectories.
std::string logDir
the directory in which the DCA log file should be created
Definition: base_classes.h:267
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
#define DCA_BINDIR
DCA subdirectory of the DCA binaries.
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.
static void PrintToolHeader()
Prints out the name and the version of this sample.
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.
std::string getTicket() const
Returns the ticket of the license as string.
Standard function result.
Definition: base_classes.h:148
std::string getSession() const
Returns the session of the license as string.
#define DCA_INITDIR
DCA subdirectory of the DCA initialization data.
int TestUpdate(const DcaInstance &myDca, const License &myLicense)
Sets up required classes and performs an update by calling UpdateModule::performUpdate().
This structure is used to initialize the DcaInstance.
Definition: base_classes.h:264
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 .
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.
int main(int argc, char *argv[])
The main routine.
std::string component() const
Returns the name of the component the detail applies to.