dca_interface  6.3.4
generic_samples/gettingstartedsample/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 
36 #include <string>
37 #include <vector>
38 #include <iostream>
39 #include <fstream>
40 #include <ctime>
41 
42 #include "dca/dca_base.h"
43 #include "dca/dca_callbacks.h"
44 
45 using namespace dca;
46 
47 const std::string S_ToolName = "gettingstartedsample";
48 const std::string S_ToolVersion = "1.0";
49 
54 const std::string S_UsageString =
55  "<dca-redist-folder> <ticket> <product>\n"
56  " dca-redist-folder - the folder where the DCA is installed to\n"
57  " ticket - a valid ticket\n"
58  " product - the product associated with your ticket\n\n"
59  ;
60 
65 #ifdef WIN32
66 # define DCA_BINDIR "bin/Win32"
67 #else
68 # define DCA_BINDIR "bin/linux"
69 #endif
70 
75 #define DCA_INITDIR "init"
76 
80 #define DCA_LOGDIR "./logs"
81 
86 static void PrintToolHeader()
87 {
88  std::cout << "IBM DCA Sample: " << S_ToolName << " (" << S_ToolVersion <<
89  ")" << std::endl;
90 }
91 
97 static void PrintUsage( const char *name )
98 {
99  std::cout << name << " usage:" << std::endl;
100  std::cout << S_UsageString << std::endl;
101 }
102 
110 int main( int argc, char *argv[] )
111 {
112  PrintToolHeader();
113 
114  int rc = 0;
115 
116  try {
117  if( argc < 4 ) {
118  PrintUsage( argv[0] );
119  return 5;
120  }
121 
122  std::string myRedistFolder = argv[ 1 ];
123  const std::string myTicket = argv[ 2 ];
124  const std::string myProduct = argv[ 3 ];
125 
126  if( myRedistFolder.empty() || myTicket.empty() || myProduct.empty() ) {
127  PrintUsage( argv[0] );
128  return 5;
129  }
130 
131  // check for trailing fileslash - and add if necessary
132  const char c = myRedistFolder[ myRedistFolder.length() - 1 ];
133  if( c != '/' && c != '\\' )
134  myRedistFolder += "/";
135 
136  // init the 3rd party libraries
137  std::cout << "Initializing 3rd party libraries "
138  "libcurl and libOpenSSL." << std::endl;
139  InitCUrl();
141 
142  { // Scope defines life of MyDca
143 
144  // setup DCA directories
145  InitData myInitData;
146  myInitData.binDir = myRedistFolder + DCA_BINDIR;
147  myInitData.initDir = myRedistFolder + DCA_INITDIR;
148  myInitData.logDir = DCA_LOGDIR;
149 
150  // instantiate DCA API by initializing a DcaInstance object
151  // if fail, an exception of type ExDca will be thrown
152  const DcaInstance myDca = DcaInstance::create( myInitData );
153 
154  std::cout << "Successfully created DcaInstance by using:" <<
155  std::endl;
156  std::cout << " Binary directory:" << myInitData.binDir <<
157  std::endl;
158  std::cout << " Init directory:" << myInitData.initDir <<
159  std::endl;
160  std::cout << " Logging directory:" << myInitData.logDir <<
161  std::endl;
162 
163  { // scope defines life of MyLicense
164  LicenseData myLicenseData;
165  myLicenseData.ticket = myTicket;
166  myLicenseData.product = myProduct;
167 
168  std::cout << "Create License by using:" <<
169  std::endl;
170  std::cout << " Ticket:" << myLicenseData.ticket <<
171  std::endl;
172  std::cout << " Product:" << myLicenseData.product <<
173  std::endl;
174 
175  // Create the license object
176  // if fail, an exception of type ExDca will be thrown
177  const License myLicense =
178  myDca.createLicense( myLicenseData );
179 
180  // Check whether we're licensed
181  if( !myLicense.isLicensed() ) {
182  // if not licensed print out details
183  std::cout << "DCA is not licensed." << std::endl;
184  std::cout << "Last message:" <<
185  myLicense.getLastMessage() << std::endl;
186  rc = 5;
187  }
188  else {
189  // if licensed then print out details, such as maximum
190  // users, expiration etc.
191  const time_t expirationDate = myLicense.getExpirationDate();
192 
193  std::cout << "License Info:" << std::endl;
194  std::cout << " DCA is licensed." << std::endl;
195  std::cout << " MaxUsers:" << myLicense.getMaxUsers() <<
196  std::endl;
197  std::cout << " MaxSessions:" << myLicense.getMaxSessions() <<
198  std::endl;
199  std::cout << " Ticket:" << myLicense.getTicket() <<
200  std::endl;
201  std::cout << " Session:" << myLicense.getSession() <<
202  std::endl;
203  std::cout << " Last Message:" << myLicense.getLastMessage() <<
204  std::endl;
205  std::cout << " Expiration Date:" << ctime(&expirationDate) <<
206  std::endl;
207  }
208  // MyLicense will be destroyed automatically when leaving this scope
209  }
210  // MyDca will be destroyed automatically when leaving this scope
211  }
212  }
213  catch( const ExDca& ex ) {
214  std::cerr << "DCA Exception occured. Details: " << ex.getDescription() <<
215  " (" << ex.getReturnCode() << ")." << std::endl;
216  rc = 10;
217  }
218  catch( const std::exception& s ) {
219  std::cerr << "std::exception occured. Details: " << s.what() << "." << std::endl;
220  rc = 10;
221  }
222  catch(...) {
223  std::cerr << "Unknown exception caught." << std::endl;
224  rc = 10;
225  }
226 
227  // deinit the 3rd party libraries
229  DeinitCUrl();
230 
231  std::cout << "Deinitializing 3rd party libraries "
232  "libcurl and libOpenSSL." << std::endl;
233  std::cout << "Exiting sample with rc=" << rc << std::endl;
234  return rc;
235 }
236 
237 
238 
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.
time_t getExpirationDate() const
Returns the expiration date of the license in UTC.
std::string getDescription() const
Returns a description of the error.
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.
void DeinitCUrl()
Deinitializes libcurl. Do not call any DCA function after you have called this function.
#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.
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.
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
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...
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.
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.