| 1 |
/*- |
| 2 |
* See the file LICENSE for redistribution information. |
| 3 |
* |
| 4 |
* Copyright (c) 2006, 2013 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
* $Id$ |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <cstdlib> |
| 10 |
#include <cstring> |
| 11 |
|
| 12 |
#include <iostream> |
| 13 |
#include <errno.h> |
| 14 |
|
| 15 |
#include <db_cxx.h> |
| 16 |
#include "SimpleConfigInfo.h" |
| 17 |
|
| 18 |
using std::cout; |
| 19 |
using std::cin; |
| 20 |
using std::cerr; |
| 21 |
using std::endl; |
| 22 |
using std::flush; |
| 23 |
|
| 24 |
#define CACHESIZE (10 * 1024 * 1024) |
| 25 |
#define DATABASE "quote.db" |
| 26 |
|
| 27 |
const char *progname = "excxx_repquote_gsg_simple"; |
| 28 |
|
| 29 |
#ifdef _WIN32 |
| 30 |
#define WIN32_LEAN_AND_MEAN |
| 31 |
#include <windows.h> |
| 32 |
#include <direct.h> |
| 33 |
|
| 34 |
extern "C" { |
| 35 |
extern int getopt(int, char * const *, const char *); |
| 36 |
extern char *optarg; |
| 37 |
} |
| 38 |
#endif |
| 39 |
|
| 40 |
class RepMgr |
| 41 |
{ |
| 42 |
public: |
| 43 |
// Constructor. |
| 44 |
RepMgr(); |
| 45 |
// Initialization method. Creates and opens our environment handle. |
| 46 |
int init(SimpleConfigInfo* config); |
| 47 |
// The doloop is where all the work is performed. |
| 48 |
int doloop(); |
| 49 |
// terminate() provides our shutdown code. |
| 50 |
int terminate(); |
| 51 |
|
| 52 |
private: |
| 53 |
// Disable copy constructor. |
| 54 |
RepMgr(const RepMgr &); |
| 55 |
void operator = (const RepMgr &); |
| 56 |
|
| 57 |
// Internal data members. |
| 58 |
SimpleConfigInfo *app_config; |
| 59 |
DbEnv dbenv; |
| 60 |
|
| 61 |
// Private methods. |
| 62 |
// print_stocks() is used to display the contents of our database. |
| 63 |
static int print_stocks(Db *dbp); |
| 64 |
}; |
| 65 |
|
| 66 |
static void usage() |
| 67 |
{ |
| 68 |
cerr << "usage: " << progname << " -h home" << endl; |
| 69 |
exit(EXIT_FAILURE); |
| 70 |
} |
| 71 |
|
| 72 |
int main(int argc, char **argv) |
| 73 |
{ |
| 74 |
SimpleConfigInfo config; |
| 75 |
char ch; |
| 76 |
int ret; |
| 77 |
|
| 78 |
// Extract the command line parameters |
| 79 |
while ((ch = getopt(argc, argv, "h:")) != EOF) { |
| 80 |
switch (ch) { |
| 81 |
case 'h': |
| 82 |
config.home = optarg; |
| 83 |
break; |
| 84 |
case '?': |
| 85 |
default: |
| 86 |
usage(); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
// Error check command line. |
| 91 |
if (config.home == NULL) |
| 92 |
usage(); |
| 93 |
|
| 94 |
RepMgr runner; |
| 95 |
try { |
| 96 |
if((ret = runner.init(&config)) != 0) |
| 97 |
goto err; |
| 98 |
if((ret = runner.doloop()) != 0) |
| 99 |
goto err; |
| 100 |
} catch (DbException dbe) { |
| 101 |
cerr << "Caught an exception during initialization or" |
| 102 |
<< " processing: " << dbe.what() << endl; |
| 103 |
} |
| 104 |
err: |
| 105 |
runner.terminate(); |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
RepMgr::RepMgr() : app_config(0), dbenv(0) |
| 110 |
{ |
| 111 |
} |
| 112 |
|
| 113 |
int RepMgr::init(SimpleConfigInfo *config) |
| 114 |
{ |
| 115 |
int ret = 0; |
| 116 |
|
| 117 |
app_config = config; |
| 118 |
|
| 119 |
dbenv.set_errfile(stderr); |
| 120 |
dbenv.set_errpfx(progname); |
| 121 |
|
| 122 |
|
| 123 |
// We can now open our environment. |
| 124 |
dbenv.set_cachesize(0, CACHESIZE, 0); |
| 125 |
dbenv.set_flags(DB_TXN_NOSYNC, 1); |
| 126 |
|
| 127 |
try { |
| 128 |
dbenv.open(app_config->home, DB_CREATE | DB_RECOVER | |
| 129 |
DB_THREAD | DB_INIT_LOCK | DB_INIT_LOG | |
| 130 |
DB_INIT_MPOOL | DB_INIT_TXN, 0); |
| 131 |
} catch(DbException dbe) { |
| 132 |
cerr << "Caught an exception during DB environment open." << endl |
| 133 |
<< "Ensure that the home directory is created prior to starting" |
| 134 |
<< " the application." << endl; |
| 135 |
ret = ENOENT; |
| 136 |
goto err; |
| 137 |
} |
| 138 |
|
| 139 |
err: |
| 140 |
return ret; |
| 141 |
} |
| 142 |
|
| 143 |
int RepMgr::terminate() |
| 144 |
{ |
| 145 |
try { |
| 146 |
dbenv.close(0); |
| 147 |
} catch (DbException dbe) { |
| 148 |
cerr << "error closing environment: " << dbe.what() << endl; |
| 149 |
} |
| 150 |
return 0; |
| 151 |
} |
| 152 |
|
| 153 |
// Provides the main data processing function for our application. |
| 154 |
// This function provides a command line prompt to which the user |
| 155 |
// can provide a ticker string and a stock price. Once a value is |
| 156 |
// entered to the application, the application writes the value to |
| 157 |
// the database and then displays the entire database. |
| 158 |
#define BUFSIZE 1024 |
| 159 |
int RepMgr::doloop() |
| 160 |
{ |
| 161 |
Db *dbp; |
| 162 |
Dbt key, data; |
| 163 |
char buf[BUFSIZE], *rbuf; |
| 164 |
int ret; |
| 165 |
|
| 166 |
dbp = NULL; |
| 167 |
memset(&key, 0, sizeof(key)); |
| 168 |
memset(&data, 0, sizeof(data)); |
| 169 |
ret = 0; |
| 170 |
|
| 171 |
for (;;) { |
| 172 |
if (dbp == NULL) { |
| 173 |
dbp = new Db(&dbenv, 0); |
| 174 |
|
| 175 |
try { |
| 176 |
dbp->open(NULL, DATABASE, NULL, DB_BTREE, |
| 177 |
DB_CREATE | DB_AUTO_COMMIT, 0); |
| 178 |
} catch(DbException dbe) { |
| 179 |
dbenv.err(ret, "DB->open"); |
| 180 |
throw dbe; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
cout << "QUOTESERVER" ; |
| 185 |
cout << "> " << flush; |
| 186 |
|
| 187 |
if (fgets(buf, sizeof(buf), stdin) == NULL) |
| 188 |
break; |
| 189 |
if (strtok(&buf[0], " \t\n") == NULL) { |
| 190 |
switch ((ret = print_stocks(dbp))) { |
| 191 |
case 0: |
| 192 |
continue; |
| 193 |
default: |
| 194 |
dbp->err(ret, "Error traversing data"); |
| 195 |
goto err; |
| 196 |
} |
| 197 |
} |
| 198 |
rbuf = strtok(NULL, " \t\n"); |
| 199 |
if (rbuf == NULL || rbuf[0] == '\0') { |
| 200 |
if (strncmp(buf, "exit", 4) == 0 || |
| 201 |
strncmp(buf, "quit", 4) == 0) |
| 202 |
break; |
| 203 |
dbenv.errx("Format: TICKER VALUE"); |
| 204 |
continue; |
| 205 |
} |
| 206 |
|
| 207 |
key.set_data(buf); |
| 208 |
key.set_size((u_int32_t)strlen(buf)); |
| 209 |
|
| 210 |
data.set_data(rbuf); |
| 211 |
data.set_size((u_int32_t)strlen(rbuf)); |
| 212 |
|
| 213 |
if ((ret = dbp->put(NULL, &key, &data, 0)) != 0) |
| 214 |
{ |
| 215 |
dbp->err(ret, "DB->put"); |
| 216 |
if (ret != DB_KEYEXIST) |
| 217 |
goto err; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
err: |
| 222 |
if (dbp != NULL) |
| 223 |
(void)dbp->close(DB_NOSYNC); |
| 224 |
|
| 225 |
return (ret); |
| 226 |
} |
| 227 |
|
| 228 |
// Display all the stock quote information in the database. |
| 229 |
int RepMgr::print_stocks(Db *dbp) |
| 230 |
{ |
| 231 |
Dbc *dbc; |
| 232 |
Dbt key, data; |
| 233 |
#define MAXKEYSIZE 10 |
| 234 |
#define MAXDATASIZE 20 |
| 235 |
char keybuf[MAXKEYSIZE + 1], databuf[MAXDATASIZE + 1]; |
| 236 |
int ret, t_ret; |
| 237 |
u_int32_t keysize, datasize; |
| 238 |
|
| 239 |
if ((ret = dbp->cursor(NULL, &dbc, 0)) != 0) { |
| 240 |
dbp->err(ret, "can't open cursor"); |
| 241 |
return (ret); |
| 242 |
} |
| 243 |
|
| 244 |
memset(&key, 0, sizeof(key)); |
| 245 |
memset(&data, 0, sizeof(data)); |
| 246 |
|
| 247 |
cout << "\tSymbol\tPrice" << endl |
| 248 |
<< "\t======\t=====" << endl; |
| 249 |
|
| 250 |
for (ret = dbc->get(&key, &data, DB_FIRST); |
| 251 |
ret == 0; |
| 252 |
ret = dbc->get(&key, &data, DB_NEXT)) { |
| 253 |
keysize = key.get_size() > MAXKEYSIZE ? MAXKEYSIZE : key.get_size(); |
| 254 |
memcpy(keybuf, key.get_data(), keysize); |
| 255 |
keybuf[keysize] = '\0'; |
| 256 |
|
| 257 |
datasize = data.get_size() >= |
| 258 |
MAXDATASIZE ? MAXDATASIZE : data.get_size(); |
| 259 |
memcpy(databuf, data.get_data(), datasize); |
| 260 |
databuf[datasize] = '\0'; |
| 261 |
|
| 262 |
cout << "\t" << keybuf << "\t" << databuf << endl; |
| 263 |
} |
| 264 |
cout << endl << flush; |
| 265 |
|
| 266 |
if ((t_ret = dbc->close()) != 0 && ret == 0) { |
| 267 |
cout << "closed cursor" << endl; |
| 268 |
ret = t_ret; |
| 269 |
} |
| 270 |
|
| 271 |
switch (ret) { |
| 272 |
case 0: |
| 273 |
case DB_NOTFOUND: |
| 274 |
return (0); |
| 275 |
default: |
| 276 |
return (ret); |
| 277 |
} |
| 278 |
} |
| 279 |
|