Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

ex_rq_master.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2001-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: ex_rq_master.c,v 12.5 2005/11/02 22:14:24 alanb Exp $
00008  */
00009 
00010 #include <sys/types.h>
00011 #include <errno.h>
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015 
00016 #include <db.h>
00017 
00018 #include "ex_repquote.h"
00019 
00020 static void *master_loop __P((void *));
00021 
00022 #define BUFSIZE 1024
00023 
00024 int
00025 domaster(dbenv, progname)
00026         DB_ENV *dbenv;
00027         const char *progname;
00028 {
00029         int ret;
00030         thread_t interface_thr;
00031 #ifndef _WIN32
00032         int t_ret;
00033         pthread_attr_t attr;
00034 
00035         /* Spawn off a thread to handle the basic master interface. */
00036         if ((ret = pthread_attr_init(&attr)) != 0 ||
00037             (ret = pthread_attr_setdetachstate(&attr,
00038             PTHREAD_CREATE_DETACHED)) != 0) {
00039                 dbenv->err(dbenv, ret,
00040                     "can't set up pthread DETACHED attribute");
00041                 goto err;
00042         }
00043 #endif
00044 
00045         if ((ret = thread_create(&interface_thr,
00046             &attr, master_loop, (void *)dbenv)) != 0) {
00047                 dbenv->err(dbenv, ret, "can't create master thread");
00048                 goto err;
00049         }
00050 
00051 err:
00052 #ifndef _WIN32
00053         if ((t_ret = pthread_attr_destroy(&attr)) != 0) {
00054                 dbenv->err(dbenv, t_ret, "can't destroy thread attribute");
00055                 if (ret == 0)
00056                         ret = t_ret;
00057         }
00058 #endif
00059         COMPQUIET(progname, NULL);
00060 
00061         return (ret);
00062 }
00063 
00064 static void *
00065 master_loop(dbenvv)
00066         void *dbenvv;
00067 {
00068         DB *dbp;
00069         DBT key, data;
00070         DB_ENV *dbenv;
00071         DB_TXN *txn;
00072         int ret;
00073         char buf[BUFSIZE], *rbuf;
00074 
00075         dbp = NULL;
00076         txn = NULL;
00077 
00078         dbenv = (DB_ENV *)dbenvv;
00079         /*
00080          * Check if the database exists and if it verifies cleanly.
00081          * If it does, run with it; else recreate it and go.  Note
00082          * that we have to verify outside of the environment.
00083          */
00084 #ifdef NOTDEF
00085         if ((ret = db_create(&dbp, NULL, 0)) != 0)
00086                 goto err;
00087         if ((ret = dbp->verify(dbp, DATABASE, NULL, NULL, 0)) != 0) {
00088                 if ((ret = dbp->remove(dbp, DATABASE, NULL, 0)) != 0 &&
00089                     ret != DB_NOTFOUND && ret != ENOENT)
00090                         goto err;
00091 #endif
00092                 if ((ret = db_create(&dbp, dbenv, 0)) != 0)
00093                         goto err;
00094                 /* Set page size small so we can easily do page allocation. */
00095                 if ((ret = dbp->set_pagesize(dbp, 512)) != 0)
00096                         goto err;
00097 
00098                 if ((ret = dbenv->txn_begin(dbenv, NULL, &txn, 0)) != 0)
00099                         goto err;
00100                 if ((ret = dbp->open(dbp, txn, DATABASE,
00101                     NULL, DB_BTREE, DB_CREATE /* | DB_THREAD */, 0)) != 0)
00102                         goto err;
00103                 ret = txn->commit(txn, 0);
00104                 txn = NULL;
00105                 if (ret != 0) {
00106                         dbp = NULL;
00107                         goto err;
00108                 }
00109 
00110 #ifdef NOTDEF
00111         } else {
00112                 /* Reopen in the environment. */
00113                 if ((ret = dbp->close(dbp, 0)) != 0)
00114                         goto err;
00115                 if ((ret = db_create(&dbp, dbenv, 0)) != 0)
00116                         goto err;
00117                 if ((ret = dbp->open(dbp,
00118                     DATABASE, NULL, DB_UNKNOWN, DB_THREAD, 0)) != 0)
00119                         goto err;
00120         }
00121 #endif
00122         /*
00123          * XXX
00124          * It would probably be kind of cool to do this in Tcl and
00125          * have a nice GUI.  It would also be cool to be independently
00126          * wealthy.
00127          */
00128         memset(&key, 0, sizeof(key));
00129         memset(&data, 0, sizeof(data));
00130         for (;;) {
00131                 printf("QUOTESERVER> ");
00132                 fflush(stdout);
00133 
00134                 if (fgets(buf, sizeof(buf), stdin) == NULL)
00135                         break;
00136                 (void)strtok(&buf[0], " \t\n");
00137                 rbuf = strtok(NULL, " \t\n");
00138                 if (rbuf == NULL || rbuf[0] == '\0') {
00139                         if (strncmp(buf, "exit", 4) == 0 ||
00140                             strncmp(buf, "quit", 4) == 0)
00141                                 break;
00142                         dbenv->errx(dbenv, "Format: TICKER VALUE");
00143                         continue;
00144                 }
00145 
00146                 key.data = buf;
00147                 key.size = (u_int32_t)strlen(buf);
00148 
00149                 data.data = rbuf;
00150                 data.size = (u_int32_t)strlen(rbuf);
00151 
00152                 if ((ret = dbenv->txn_begin(dbenv, NULL, &txn, 0)) != 0)
00153                         goto err;
00154                 switch (ret =
00155                     dbp->put(dbp, txn, &key, &data, 0)) {
00156                 case 0:
00157                         break;
00158                 default:
00159                         dbp->err(dbp, ret, "DB->put");
00160                         if (ret != DB_KEYEXIST)
00161                                 goto err;
00162                         break;
00163                 }
00164                 ret = txn->commit(txn, 0);
00165                 txn = NULL;
00166                 if (ret != 0)
00167                         goto err;
00168         }
00169 
00170 err:    if (txn != NULL)
00171                 (void)txn->abort(txn);
00172 
00173         if (dbp != NULL)
00174                 (void)dbp->close(dbp, DB_NOSYNC);
00175 
00176         return ((void *)(uintptr_t)ret);
00177 }

Generated on Sun Dec 25 12:14:25 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2