把chord下dbm_noauth做成静态库,提供接口

我的dbm.C文件如下,在src/chord-0.1/tools/下

View Code
/*
*
* Copyright (C) 2001 Frank Dabek ([email protected]),
* Massachusetts Institute of Technology
*
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/

#include <async.h>
#include <dhash_common.h>
#include <dhash_prot.h>
#include <dhashclient.h>
#include <dhblock.h>
#include <dbfe.h>
#include <crypt.h>
#include <sys/time.h>

#include <string>
#include <iostream>
#include <vector>
using namespace std;

string strResult;
static vector<string> str_vec;

str control_socket;
static FILE *outfile;
unsigned int datasize;

ptr<axprt_stream> xprt;
int fconnected = 0;
int out = 0;
int MAX_OPS_OUT = 1024;

int bps = 0;
int sec = 0;
timecb_t *measurer = NULL;

u_int64_t
getusec ()
{
timeval tv;
gettimeofday (&tv, NULL);
return tv.tv_sec * INT64(1000000) + tv.tv_usec;
}



void
store_cb (u_int64_t start, dhash_stat status, ptr<insert_info> i)
{
out++;

strbuf s;
if (status != DHASH_OK) {
s << "store_cb: " << i->key << " " << status << "\n";
} else {
bps++;
s << i->key << " / " << (getusec () - start)/1000 << " /";
for (size_t j = 0; j < i->path.size (); j++)
s << " " << i->path[j];
s << "\n";
}
str buf (s);
fprintf (outfile, "%s", buf.cstr ());
if (outfile != stdout)
warnx << buf;
}


int
store (dhashclient *dhash, char *key, char *value)
{

dhash->insert (compute_hash (key, strlen(key)),
value, strlen(value) + 1,
wrap (store_cb, getusec ()), NULL, DHASH_NOAUTH);

dhash->insert (compute_hash (key, strlen(key)),
value, strlen(value) + 1,
wrap (store_cb, getusec ()), NULL, DHASH_NOAUTH);

while (out == 0) acheck ();
return 0;
}



void
fetch_cb (dhash_stat stat, ptr<dhash_block> blk, vec<chordID> path)
{
out++;

if (!blk) {
strbuf buf;
fprintf (outfile, str (buf).cstr ());
}

if (blk) {
strbuf buf;


bps++;
buf << " /";
for (u_int i = 0; i < blk->times.size (); i++)
buf << " " << blk->times[i];
buf << " /";

buf << " " << blk->hops << " " << blk->errors
<< " " << blk->retries << " ";
for (u_int i=0; i < path.size (); i++) {
buf << path[i] << " ";
}

buf << " : ";
if (blk->vData.size () > 0) {
warn << blk->vData.size () << "\n";
for (unsigned int i = 0; i < blk->vData.size (); i++) {
buf << "" << i << ": " << blk->vData[i] << " -- \n";
warn << blk->vData[i] << "\n";
str_vec.push_back(blk->vData[i].cstr());
strResult += blk->vData[i].cstr();
strResult += "/";
}
} else {
buf << blk->data << "\n";
str_vec.push_back(blk->data.cstr());
strResult += blk->data.cstr();
strResult += "/";
}
cout << "\nstrResult is:" << strResult << endl;
buf << "\n\n";

fprintf (outfile, str (buf).cstr ());
if (outfile != stdout)
warnx << buf;
}
}


void
fetch (dhashclient &dhash, char *key)
{


dhash.retrieve (compute_hash (key, strlen(key)), DHASH_NOAUTH,
wrap (fetch_cb));

while (out == 0) acheck ();
}

void
usage (char *progname)
{
warn << "control_socket f|s key [value]\n";
exit(0);
}

void
cleanup (void)
{
if (outfile) {
fclose (outfile);
}

exit (1);
}

void
eofhandler ()
{
warn << "Unexpected EOF: block too large?\n";
cleanup ();
}

void
connected (dhashclient *dhash, int argc, char **argv)
{
dhash->seteofcb (wrap (eofhandler));

//dbm sock [f|s] key value

fconnected = 1;
outfile = stdout;

struct timeval start;
gettimeofday (&start, NULL);

if (argv[2][0] == 's')
store (dhash, argv[3], argv[4]);
else
fetch (*dhash, argv[3]);


delete dhash;
}

void
tcp_connect_cb (int argc, char **argv, int fd)
{
if (fd < 0)
fatal << "connect failed\n";
warnx << "... connected!\n";
xprt = axprt_stream::alloc (fd);
dhashclient *dhash = New dhashclient (xprt);
connected (dhash, argc, argv);
}

int
main (int argc, char **argv)
{
str_vec.clear();
setprogname (argv[0]);

if (argc < 4) usage (argv[0]);

sigcb (SIGTERM, wrap (&cleanup));
sigcb (SIGINT, wrap (&cleanup));

control_socket = argv[1];
char *cstr = (char *)control_socket.cstr ();
if (strchr (cstr, ':')) {
char *port = strchr (cstr, ':');
*port = 0; //isolate host
port++; // point at port
char *host = cstr;
short i_port = atoi (port);
warn << "Connecting to " << host << ":" << i_port << " via TCP...";
tcpconnect (host, i_port, wrap (&tcp_connect_cb, argc, argv));
while (!fconnected) acheck ();
} else {
dhashclient *dhash = New dhashclient (control_socket);
connected (dhash, argc, argv);
}
printf("end of func\n");
unsigned int j;
for (j = 0; j < str_vec.size(); ++j) {
cout << str_vec[j] << endl;
}
}



在build/chord/tools的目录下还有dbm_use.h dbm_use.C两个文件

dbm_use.h

#ifndef _DBM_USE_H_
#define _DMB_USE_H_

#include <string>
#include <vector>
using namespace std;
vector<string> Fetch(const string & key);
void Store(const string & key, const string & val);

#endif

dbm_use.C

View Code
  1 /*
2 *
3 * Copyright (C) 2001 Frank Dabek ([email protected]),
4 * Massachusetts Institute of Technology
5 *
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
28 #include <async.h>
29 #include <dhash_common.h>
30 #include <dhash_prot.h>
31 #include <dhashclient.h>
32 #include <dhblock.h>
33 #include <dbfe.h>
34 #include <crypt.h>
35 #include <sys/time.h>
36
37 #include <string>
38 #include <iostream>
39 #include <vector>
40 using namespace std;
41
42 #include "dbm_use.h"
43 string strResult;
44 static vector<string> str_vec;
45
46 str control_socket;
47 static FILE *outfile;
48 unsigned int datasize;
49
50 ptr<axprt_stream> xprt;
51 int fconnected = 0;
52 int out = 0;
53 int MAX_OPS_OUT = 1024;
54
55 int bps = 0;
56 int sec = 0;
57 timecb_t *measurer = NULL;
58
59 u_int64_t
60 getusec ()
61 {
62 timeval tv;
63 gettimeofday (&tv, NULL);
64 return tv.tv_sec * INT64(1000000) + tv.tv_usec;
65 }
66
67
68
69 void
70 store_cb (u_int64_t start, dhash_stat status, ptr<insert_info> i)
71 {
72 out++;
73
74 strbuf s;
75 if (status != DHASH_OK) {
76 s << "store_cb: " << i->key << " " << status << "\n";
77 } else {
78 bps++;
79 s << i->key << " / " << (getusec () - start)/1000 << " /";
80 for (size_t j = 0; j < i->path.size (); j++)
81 s << " " << i->path[j];
82 s << "\n";
83 }
84 str buf (s);
85 fprintf (outfile, "%s", buf.cstr ());
86 if (outfile != stdout)
87 warnx << buf;
88 }
89
90
91 int
92 store (dhashclient *dhash, char *key, char *value)
93 {
94
95 dhash->insert (compute_hash (key, strlen(key)),
96 value, strlen(value) + 1,
97 wrap (store_cb, getusec ()), NULL, DHASH_NOAUTH);
98
99 dhash->insert (compute_hash (key, strlen(key)),
100 value, strlen(value) + 1,
101 wrap (store_cb, getusec ()), NULL, DHASH_NOAUTH);
102
103 while (out == 0) acheck ();
104 return 0;
105 }
106
107
108
109 void
110 fetch_cb (dhash_stat stat, ptr<dhash_block> blk, vec<chordID> path)
111 {
112 out++;
113
114 if (!blk) {
115 strbuf buf;
116 fprintf (outfile, str (buf).cstr ());
117 }
118
119 if (blk) {
120 strbuf buf;
121
122
123 bps++;
124 buf << " /";
125 for (u_int i = 0; i < blk->times.size (); i++)
126 buf << " " << blk->times[i];
127 buf << " /";
128
129 buf << " " << blk->hops << " " << blk->errors
130 << " " << blk->retries << " ";
131 for (u_int i=0; i < path.size (); i++) {
132 buf << path[i] << " ";
133 }
134
135 buf << " : ";
136 if (blk->vData.size () > 0) {
137 warn << blk->vData.size () << "\n";
138 for (unsigned int i = 0; i < blk->vData.size (); i++) {
139 buf << "" << i << ": " << blk->vData[i] << " -- \n";
140 warn << blk->vData[i] << "\n";
141 str_vec.push_back(blk->vData[i].cstr());
142 strResult += blk->vData[i].cstr();
143 strResult += "/";
144 }
145 } else {
146 buf << blk->data << "\n";
147 str_vec.push_back(blk->data.cstr());
148 strResult += blk->data.cstr();
149 strResult += "/";
150 }
151 cout << "\nstrResult is:" << strResult << endl;
152 buf << "\n\n";
153
154 fprintf (outfile, str (buf).cstr ());
155 if (outfile != stdout)
156 warnx << buf;
157 }
158 }
159
160
161 void
162 fetch (dhashclient &dhash, char *key)
163 {
164
165
166 dhash.retrieve (compute_hash (key, strlen(key)), DHASH_NOAUTH,
167 wrap (fetch_cb));
168
169 while (out == 0) acheck ();
170 }
171
172 void
173 usage (char *progname)
174 {
175 warn << "control_socket f|s key [value]\n";
176 exit(0);
177 }
178
179 void
180 cleanup (void)
181 {
182 if (outfile) {
183 fclose (outfile);
184 }
185
186 exit (1);
187 }
188
189 void
190 eofhandler ()
191 {
192 warn << "Unexpected EOF: block too large?\n";
193 cleanup ();
194 }
195
196 void
197 connected (dhashclient *dhash, int argc, char **argv)
198 {
199 dhash->seteofcb (wrap (eofhandler));
200
201 //dbm sock [f|s] key value
202
203 fconnected = 1;
204 outfile = stdout;
205
206 struct timeval start;
207 gettimeofday (&start, NULL);
208
209 if (argv[2][0] == 's')
210 store (dhash, argv[3], argv[4]);
211 else
212 fetch (*dhash, argv[3]);
213
214
215 delete dhash;
216 }
217
218 void
219 tcp_connect_cb (int argc, char **argv, int fd)
220 {
221 if (fd < 0)
222 fatal << "connect failed\n";
223 warnx << "... connected!\n";
224 xprt = axprt_stream::alloc (fd);
225 dhashclient *dhash = New dhashclient (xprt);
226 connected (dhash, argc, argv);
227 }
228
229
230 #define MAX_KEY_LEN 1024
231 #define MAX_VAL_LEN 1024
232
233 vector<string> Fetch(const string & key)
234 {
235 str_vec.clear();
236 char key_star[MAX_KEY_LEN];
237 strcpy(key_star, key.c_str());
238 char * argvo[4] = {"./dbm", "/tmp/chord-sock", "f", key_star};
239 char **argv = argvo;
240 int argc = 4;
241
242 if (argc < 4) usage (argv[0]);
243
244 sigcb (SIGTERM, wrap (&cleanup));
245 sigcb (SIGINT, wrap (&cleanup));
246
247 control_socket = argv[1];
248 char *cstr = (char *)control_socket.cstr ();
249 if (strchr (cstr, ':')) {
250 char *port = strchr (cstr, ':');
251 *port = 0; //isolate host
252 port++; // point at port
253 char *host = cstr;
254 short i_port = atoi (port);
255 warn << "Connecting to " << host << ":" << i_port << " via TCP...";
256 tcpconnect (host, i_port, wrap (&tcp_connect_cb, argc, argv));
257 while (!fconnected) acheck ();
258 } else {
259 dhashclient *dhash = New dhashclient (control_socket);
260 connected (dhash, argc, argv);
261 }
262 printf("end of func\n");
263 unsigned int j;
264 for (j = 0; j < str_vec.size(); ++j) {
265 cout << str_vec[j] << endl;
266 }
267 return str_vec;
268 }
269
270 void Store(const string & key, const string & val)
271 {
272 char key_star[MAX_KEY_LEN];
273 char val_star[MAX_VAL_LEN];
274 strcpy(key_star, key.c_str());
275 strcpy(val_star, val.c_str());
276 char * argvo[5] = {"./dbm", "/tmp/chord-sock", "s", key_star, val_star};
277 char **argv = argvo;
278 int argc = 5;
279
280 if (argc < 4) usage (argv[0]);
281
282 sigcb (SIGTERM, wrap (&cleanup));
283 sigcb (SIGINT, wrap (&cleanup));
284
285 control_socket = argv[1];
286 char *cstr = (char *)control_socket.cstr ();
287 if (strchr (cstr, ':')) {
288 char *port = strchr (cstr, ':');
289 *port = 0; //isolate host
290 port++; // point at port
291 char *host = cstr;
292 short i_port = atoi (port);
293 warn << "Connecting to " << host << ":" << i_port << " via TCP...";
294 tcpconnect (host, i_port, wrap (&tcp_connect_cb, argc, argv));
295 while (!fconnected) acheck ();
296 } else {
297 dhashclient *dhash = New dhashclient (control_socket);
298 connected (dhash, argc, argv);
299 }
300 printf("end of func\n");
301 }
302
303 /*int main()
304 {
305 string key = "mykey";
306 string val = "myvalue4";
307 // Store(key, val);
308 vector<string> result = Fetch(key);
309
310 unsigned int i;
311 for (i = 0; i < result.size(); ++i) {
312 cout << result[i] << endl;
313 }
314 }*/




在build的tools下还有test.cpp文件,同时有.deps目录,本目录有个libtool,mymakefile如下:

a.out: 
mkdir .deps
g++ -DHAVE_CONFIG_H -I. -I/root/src/chord-0.1/tools -I/root/build/chord -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 -I/u sr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/root/build/chord/svc -I/root/src/chord-0.1/lsd -I/ro ot/src/chord-0.1/svc -I/root/src/chord-0.1/dhash -I/root/src/chord-0.1/chord -I/root/src/chord-0.1/merkle -I/root/src/chord-0.1/utils -I/root/build/chor d/../sfslite -I/root/src/sfslite-0.8.16/async -I/root/src/sfslite-0.8.16/arpc -I/root/src/sfslite-0.8.16/crypt -I/root/src/sfslite-0.8.16/sfsmisc -I/root /src/sfslite-0.8.16/libsfs -I/root/build/chord/../sfslite/svc -I/usr/include/db4 -DSLEEPYCAT -g -O2 -Wall -Werror -MT dbm_use.o -MD -MP -MF ".deps/db m_use.Tpo" -c -o dbm_use.o ./dbm_use.C;
ar -r libdbmuse.a dbm_use.o
g++ -DHAVE_CONFIG_H -I. -I/root/src/chord-0.1/tools -I/root/build/chord -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 -I/u sr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/root/build/chord/svc -I/root/src/chord-0.1/lsd -I/ro ot/src/chord-0.1/svc -I/root/src/chord-0.1/dhash -I/root/src/chord-0.1/chord -I/root/src/chord-0.1/merkle -I/root/src/chord-0.1/utils -I/root/build/chor d/../sfslite -I/root/src/sfslite-0.8.16/async -I/root/src/sfslite-0.8.16/arpc -I/root/src/sfslite-0.8.16/crypt -I/root/src/sfslite-0.8.16/sfsmisc -I/root /src/sfslite-0.8.16/libsfs -I/root/build/chord/../sfslite/svc -I/usr/include/db4 -DSLEEPYCAT -g -O2 -Wall -Werror -MT test.o -MD -MP -MF ".deps/test. Tpo" -c -o test.o ./test.cpp;
mv -f ".deps/test.Tpo" ".deps/test.Po";
/bin/sh ./libtool --tag=CXX --mode=link g++ -g -O2 -Wall -Werror -o a.out test.o ./libdbmuse.a /root/build/chord/dhash/libdhashclient.a /root/bu ild/chord/utils/libutil.a /root/build/chord/svc/libsvc.la /root/build/chord/../sfslite/libtame/libtame.la /root/build/chord/../sfslite/sfsmisc/libsfsmisc .la /root/build/chord/../sfslite/svc/libsvc.la /root/build/chord/../sfslite/crypt/libsfscrypt.la /root/build/chord/../sfslite/arpc/libarpc.la /root/ build/chord/../sfslite/async/libasync.la -lgmp;
g++ -static -g -O2 -Wall -Werror -o a.out test.o ./libdbmuse.a /root/build/chord/dhash/libdhashclient.a /root/build/chord/utils/libutil.a /root/buil d/chord/svc/.libs/libsvc.a /root/build/chord/../sfslite/libtame/.libs/libtame.a /root/build/chord/../sfslite/sfsmisc/.libs/libsfsmisc.a /root/build/chord /../sfslite/svc/.libs/libsvc.a /root/build/chord/../sfslite/crypt/.libs/libsfscrypt.a /root/build/chord/../sfslite/arpc/.libs/libarpc.a /root/build/chord /../sfslite/async/.libs/libasync.a -lnsl -lresolv -lgmp;

clean:
rm -rf *.o a.out libdbmuse.a .deps

 

 

你可能感兴趣的:(OAuth)