File: ex_rq_master.c

package info (click to toggle)
evolution-data-server 1.0.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 39,504 kB
  • ctags: 26,423
  • sloc: ansic: 175,347; tcl: 30,499; sh: 20,699; perl: 11,320; xml: 9,039; java: 7,653; cpp: 6,029; makefile: 4,866; awk: 1,338; yacc: 1,103; sed: 772; cs: 505; lex: 134; asm: 14
file content (165 lines) | stat: -rw-r--r-- 3,484 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*-
 * #include <pthread.h>
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2001-2002
 *	Sleepycat Software.  All rights reserved.
 *
 * $Id: ex_rq_master.c,v 1.1.1.1 2003/11/20 22:13:29 toshok Exp $
 */

#include <sys/types.h>

#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <db.h>

#include "ex_repquote.h"

static void *master_loop __P((void *));

#define	BUFSIZE 1024

int
domaster(dbenv, progname)
	DB_ENV *dbenv;
	const char *progname;
{
	int ret, t_ret;
	pthread_t interface_thr;
	pthread_attr_t attr;

	COMPQUIET(progname, NULL);

	/* Spawn off a thread to handle the basic master interface. */
	if ((ret = pthread_attr_init(&attr)) != 0 &&
	    (ret = pthread_attr_setdetachstate(&attr,
	    PTHREAD_CREATE_DETACHED)) != 0)
		goto err;

	if ((ret = pthread_create(&interface_thr,
	    &attr, master_loop, (void *)dbenv)) != 0)
		goto err;

err:	if ((t_ret = pthread_attr_destroy(&attr)) != 0 && ret == 0)
		ret = t_ret;

	return (ret);
}

static void *
master_loop(dbenvv)
	void *dbenvv;
{
	DB *dbp;
	DB_ENV *dbenv;
	DB_TXN *txn;
	DBT key, data;
	char buf[BUFSIZE], *rbuf;
	int ret;

	dbp = NULL;
	txn = NULL;

	dbenv = (DB_ENV *)dbenvv;
	/*
	 * Check if the database exists and if it verifies cleanly.
	 * If it does, run with it; else recreate it and go.  Note
	 * that we have to verify outside of the environment.
	 */
#ifdef NOTDEF
	if ((ret = db_create(&dbp, NULL, 0)) != 0)
		return (ret);
	if ((ret = dbp->verify(dbp, DATABASE, NULL, NULL, 0)) != 0) {
		if ((ret = dbp->remove(dbp, DATABASE, NULL, 0)) != 0 &&
		    ret != DB_NOTFOUND && ret != ENOENT)
			return (ret);
#endif
		if ((ret = db_create(&dbp, dbenv, 0)) != 0)
			return ((void *)ret);

		if ((ret = dbenv->txn_begin(dbenv, NULL, &txn, 0)) != 0)
			goto err;
		if ((ret = dbp->open(dbp, txn, DATABASE,
		    NULL, DB_BTREE, DB_CREATE /* | DB_THREAD */, 0)) != 0)
			goto err;
		ret = txn->commit(txn, 0);
		txn = NULL;
		if (ret != 0) {
			dbp = NULL;
			goto err;
		}

#ifdef NOTDEF
	} else {
		/* Reopen in the environment. */
		if ((ret = dbp->close(dbp, 0)) != 0)
			return (ret);
		if ((ret = db_create(&dbp, dbenv, 0)) != 0)
			return (ret);
		if ((ret = dbp->open(dbp,
		    DATABASE, NULL, DB_UNKNOWN, DB_THREAD, 0)) != 0)
			goto err;
	}
#endif
	/*
	 * XXX
	 * It would probably be kind of cool to do this in Tcl and
	 * have a nice GUI.  It would also be cool to be independently
	 * wealthy.
	 */
	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));
	for (;;) {
		printf("QUOTESERVER> ");
		fflush(stdout);

		if (fgets(buf, sizeof(buf), stdin) == NULL)
			break;
		(void)strtok(&buf[0], " \t\n");
		rbuf = strtok(NULL, " \t\n");
		if (rbuf == NULL || rbuf[0] == '\0') {
			if (strncmp(buf, "exit", 4) == 0 ||
			    strncmp(buf, "quit", 4) == 0)
				break;
			dbenv->errx(dbenv, "Format: TICKER VALUE");
			continue;
		}

		key.data = buf;
		key.size = strlen(buf);

		data.data = rbuf;
		data.size = strlen(rbuf);

		if ((ret = dbenv->txn_begin(dbenv, NULL, &txn, 0)) != 0)
			goto err;
		switch (ret =
		    dbp->put(dbp, txn, &key, &data, 0)) {
		case 0:
			break;
		default:
			dbp->err(dbp, ret, "DB->put");
			if (ret != DB_KEYEXIST)
				goto err;
			break;
		}
		ret = txn->commit(txn, 0);
		txn = NULL;
		if (ret != 0)
			goto err;
	}

err:	if (txn != NULL)
		(void)txn->abort(txn);

	if (dbp != NULL)
		(void)dbp->close(dbp, DB_NOSYNC);

	return ((void *)ret);
}