dca_interface  6.3.4
generic_samples/getversionsample/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 
34 #include <string>
35 #include <vector>
36 #include <iostream>
37 #include <fstream>
38 #include <ctime>
39 
40 #include "dca/dca_base.h"
41 #include "dca/dca_callbacks.h"
42 
43 using namespace dca;
44 
45 const std::string S_ToolName = "getversionsample";
46 const std::string S_ToolVersion = "1.0";
47 
52 const std::string S_UsageString =
53  "<dca-redist-folder> <ticket> <product>\n"
54  " dca-redist-folder - the folder where the DCA is installed to\n"
55  " ticket - a valid ticket\n"
56  " product - the product associated with your ticket\n\n"
57  ;
58 
63 #ifdef WIN32
64 # define DCA_BINDIR "bin/Win32"
65 #else
66 # define DCA_BINDIR "bin/linux"
67 #endif
68 
73 #define DCA_INITDIR "init"
74 
78 #define DCA_LOGDIR "./logs"
79 
88 static void SetupInitData( const std::string& redist_folder, InitData& initData )
89 {
90  initData.binDir = redist_folder + DCA_BINDIR;
91  initData.initDir = redist_folder + DCA_INITDIR;
92  initData.logDir = DCA_LOGDIR;
93 }
94 
102 static void SetupLicense( const std::string& ticket, const std::string& product,
103  LicenseData& licenseData )
104 {
105  licenseData.ticket = ticket;
106  licenseData.product = product;
107 }
108 
114 static void PrintLicenseInfo( const License& aLicense )
115 {
116  const time_t expirationDate = aLicense.getExpirationDate();
117  struct tm *expirationTime = localtime( &expirationDate );
118 
119  std::cout << "License Info:" << std::endl;
120  std::cout << " DCA is " << ( aLicense.isLicensed() ? "licensed." :
121  "not licensed." ) << std::endl;
122  std::cout << " MaxUsers:" << aLicense.getMaxUsers() <<
123  std::endl;
124  std::cout << " MaxSessions:" << aLicense.getMaxSessions() <<
125  std::endl;
126  std::cout << " Ticket:" << aLicense.getTicket() <<
127  std::endl;
128  std::cout << " Session:" << aLicense.getSession() <<
129  std::endl;
130  std::cout << " Last Message:" << aLicense.getLastMessage() <<
131  std::endl;
132  std::cout << " Expiration Date:" << asctime( expirationTime ) <<
133  std::endl;
134 }
135 
141 static void PrintVersion( const VersionInfoResults& aVersionInfoResults )
142 {
143  const DCA_SIZE_TYPE numOfVersionInfoResults = aVersionInfoResults.size( );
144 
145  if( numOfVersionInfoResults == 0 ) {
146  // should never happen!
147  std::cout << "No version information found." << std::endl;
148  return;
149  }
150 
151  std::cout << "Retrieved " << numOfVersionInfoResults <<
152  " VersionInfoResult objects" << std::endl;
153 
154  for( DCA_INDEX_TYPE i = 0; i < numOfVersionInfoResults; ++i ) {
155  const VersionInfoResult myVersionInfoResult = aVersionInfoResults[ i ];
156  const DCA_SIZE_TYPE numOfVersionInfoDetails = myVersionInfoResult.size( );
157 
158  std::cout << " #" << (i+1) << ") VersionInfoResult [ name=" <<
159  myVersionInfoResult.name() << ", id=" << myVersionInfoResult.id() <<
160  ", size=" << numOfVersionInfoDetails << " ]" << std::endl;
161 
162  for( DCA_INDEX_TYPE u = 0; u < numOfVersionInfoDetails; ++u ) {
163  const VersionInfoResultDetail myVersionInfoResultDetail =
164  myVersionInfoResult[ u ];
165 
166  std::cout << " #" << (u+1) << ") VersionInfoResultDetail [" <<
167  " component='" << myVersionInfoResultDetail.component() <<
168  ", version=" << myVersionInfoResultDetail.version() << "] "
169  << std::endl;
170  }
171  }
172 }
173 
178 static void PrintToolHeader()
179 {
180  std::cout << "IBM DCA Sample: " << S_ToolName << " (" << S_ToolVersion << ")" << std::endl;
181 }
182 
188 static void PrintUsage( const char *name )
189 {
190  std::cout << name << " usage:" << std::endl;
191  std::cout << S_UsageString << std::endl;
192 }
193 
201 int main( int argc, char *argv[] )
202 {
203  PrintToolHeader();
204 
205  int rc = 5;
206 
207  try {
208  if( argc < 4 ) {
209  PrintUsage( argv[0] );
210  return 5;
211  }
212 
213  std::string myRedistFolder = argv[ 1 ];
214  const std::string myTicket = argv[ 2 ];
215  const std::string myProduct = argv[ 3 ];
216 
217  if( myRedistFolder.empty() || myTicket.empty() || myProduct.empty() ) {
218  PrintUsage( argv[0] );
219  return 5;
220  }
221 
222  // check for trailing fileslash - and add if necessary
223  const char c = myRedistFolder[ myRedistFolder.length() - 1 ];
224  if( c != '/' && c != '\\' )
225  myRedistFolder += "/";
226 
227  // init the 3rd party libraries
228  InitCUrl();
230 
231  {
232  // setup DCA directories
233  InitData myInitData;
234  SetupInitData( myRedistFolder, myInitData );
235 
236  // instantiate DCA API
237  DcaInstance myDca;
238  myDca = DcaInstance::create( myInitData );
239 
240  // setup license data
241  LicenseData myLicenseData;
242  SetupLicense( myTicket, myProduct, myLicenseData );
243  const License myLicense = myDca.createLicense( myLicenseData );
244 
245  PrintLicenseInfo( myLicense );
246 
247  if( myLicense.isLicensed() ) {
248  // gather VersionInfoResults and print them out
249  const VersionInfoResults myVersionInfoResults = myDca.getVersionInfo();
250  PrintVersion( myVersionInfoResults );
251  rc = 0;
252  }
253  }
254  }
255  catch( const ExDca& ex ) {
256  std::cerr << "DCA Exception occured. Details: " << ex.getDescription() <<
257  " (" << ex.getReturnCode() << ")." << std::endl;
258  rc = 10;
259  }
260  catch( const std::exception& s ) {
261  std::cerr << "std::exception occured. Details: " << s.what() << "." <<
262  std::endl;
263  rc = 10;
264  }
265  catch(...) {
266  std::cerr << "Unknown exception caught." << std::endl;
267  rc = 10;
268  }
269 
270  // deinit the 3rd party libraries
272  DeinitCUrl();
273 
274  return rc;
275 }
276 
277 
278 
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
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.
VersionInfoResults getVersionInfo() const
Returns enumerable version information about all currently loaded modules.
time_t getExpirationDate() const
Returns the expiration date of the license in UTC.
std::string getDescription() const
Returns a description of the error.
DCA_SIZE_TYPE size() const
Returns the number of VersionInfoResultDetail objects in the container.
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 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...
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.
int getMaxUsers() const
Returns the maximum allowed users associated with your ticket/license.
DCA_MODULE_ID_TYPE id() const
void DeinitCUrl()
Deinitializes libcurl. Do not call any DCA function after you have called this function.
std::string version() const
#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.
Class that contains all version information of all initialized DCA modules.
static void PrintToolHeader()
Prints out the name and the version of this sample.
Class that contains all details of type dca::VersionInfoResult object.
static void PrintVersion(const VersionInfoResults &aVersionInfoResults)
Prints out the version information gathered from current DcaInstance.
Use a License to initialize a classification package or a toolbox package.
Definition: base_classes.h:560
std::string name() const
Returns the name.
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.
Class that contains all details of a dca::VersionInfoResult object.
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
DCA_SIZE_TYPE size() const
Returns the number of VersionInfoResult objects in the container.
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
std::string component() const
size_t DCA_SIZE_TYPE
Type for size (used for size of array and collections).
Definition: base_types.h:72
std::string getTicket() const
Returns the ticket of the license as string.
#define DCA_BINDIR
DCA subdirectory of the DCA binaries.
std::string getSession() const
Returns the session of the license as string.
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
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.
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.