2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
17 #include <sys/types.h>
24 #include <netinet/in.h>
36 /*! \name useful globals */
37 struct ubik_dbase *dbase;
42 struct rx_connection *rxconn;
45 struct ubik_trans *tt;
48 code = ubik_BeginTrans(dbase, UBIK_WRITETRANS, &tt);
51 printf("about to set lock\n");
52 /* now set database locks. Must do this or people may read uncommitted
53 * data. Note that we're just setting a lock at position 1, which is
54 * this program's convention for locking the whole database */
55 code = ubik_SetLock(tt, 1, 1, LOCKWRITE);
56 printf("now have lock\n");
61 /* sleep for a little while to make it possible for us to test for some
64 tv.tv_sec = sleepTime;
66 #ifdef AFS_PTHREAD_ENV
67 select(0, 0, 0, 0, &tv);
69 IOMGR_Select(0, 0, 0, 0, &tv);
72 /* read the original value */
73 code = ubik_Read(tt, &temp, sizeof(afs_int32));
81 temp++; /* bump the value here */
82 /* reset the file pointer back to where it was before the read */
83 code = ubik_Seek(tt, 0, 0);
88 /* write the data back */
89 code = ubik_Write(tt, &temp, sizeof(afs_int32));
94 /* finally, we commit the transaction */
95 code = ubik_EndTrans(tt);
101 SAMPLE_Get(rxconn, gnumber)
102 struct rx_connection *rxconn;
105 afs_int32 code, temp;
106 struct ubik_trans *tt;
109 /* start with a read transaction, since we're only going to do read
110 * operations in this transaction. */
111 code = ubik_BeginTrans(dbase, UBIK_READTRANS, &tt);
114 printf("about to set lock\n");
115 /* obtain a read lock, so we don't read data the other guy is writing */
116 code = ubik_SetLock(tt, 1, 1, LOCKREAD);
117 printf("now have lock\n");
122 /* sleep to allow races */
124 tv.tv_sec = sleepTime;
126 #ifdef AFS_PTHREAD_ENV
127 select(0, 0, 0, 0, &tv);
129 IOMGR_Select(0, 0, 0, 0, &tv);
133 code = ubik_Read(tt, &temp, sizeof(afs_int32));
135 /* premature eof, use 0 */
142 /* end the transaction, automatically releasing locks */
143 code = ubik_EndTrans(tt);
148 SAMPLE_QGet(rxconn, gnumber)
149 struct rx_connection *rxconn;
152 afs_int32 code, temp;
153 struct ubik_trans *tt;
156 /* start with a read transaction, since we're only going to do read
157 * operations in this transaction. */
158 code = ubik_BeginTransReadAny(dbase, UBIK_READTRANS, &tt);
161 printf("about to set lock\n");
162 /* obtain a read lock, so we don't read data the other guy is writing */
163 code = ubik_SetLock(tt, 1, 1, LOCKREAD);
164 printf("now have lock\n");
169 /* sleep to allow races */
171 tv.tv_sec = sleepTime;
173 #ifdef AFS_PTHREAD_ENV
174 select(0, 0, 0, 0, &tv);
176 IOMGR_Select(0, 0, 0, 0, &tv);
180 code = ubik_Read(tt, &temp, sizeof(afs_int32));
182 /* premature eof, use 0 */
189 /* end the transaction, automatically releasing locks */
190 code = ubik_EndTrans(tt);
196 struct rx_connection *rxconn;
199 struct ubik_trans *tt;
202 /* truncation operation requires a write transaction, too */
203 code = ubik_BeginTrans(dbase, UBIK_WRITETRANS, &tt);
206 printf("about to set lock\n");
207 /* lock the database */
208 code = ubik_SetLock(tt, 1, 1, LOCKWRITE);
209 printf("now have lock\n");
215 tv.tv_sec = sleepTime;
217 #ifdef AFS_PTHREAD_ENV
218 select(0, 0, 0, 0, &tv);
220 IOMGR_Select(0, 0, 0, 0, &tv);
223 /* shrink the file */
224 code = ubik_Truncate(tt, 0);
230 code = ubik_EndTrans(tt);
236 struct rx_connection *rxconn;
238 afs_int32 code, temp;
239 struct ubik_trans *tt;
242 /* first start a new transaction. Must be a write transaction since
243 * we're going to change some data (with ubik_Write) */
244 code = ubik_BeginTrans(dbase, UBIK_WRITETRANS, &tt);
247 printf("about to set lock\n");
248 /* now set database locks. Must do this or people may read uncommitted
249 * data. Note that we're just setting a lock at position 1, which is
250 * this program's convention for locking the whole database */
251 code = ubik_SetLock(tt, 1, 1, LOCKWRITE);
252 printf("now have lock\n");
257 /* sleep for a little while to make it possible for us to test for some
260 tv.tv_sec = sleepTime;
262 #ifdef AFS_PTHREAD_ENV
263 select(0, 0, 0, 0, &tv);
265 IOMGR_Select(0, 0, 0, 0, &tv);
268 /* read the original value */
269 code = ubik_Read(tt, &temp, sizeof(afs_int32));
271 printf("short read, using 0\n");
277 ubik_AbortTrans(tt); /* surprise! pretend something went wrong */
282 #include "AFS_component_version_number.c"
288 register afs_int32 code, i;
289 afs_int32 serverList[MAXSERVERS];
291 struct rx_service *tservice;
292 struct rx_securityClass *sc[2];
293 extern int SAMPLE_ExecuteRequest();
294 char dbfileName[128];
297 printf("usage: userver -servers <serverlist> {-sleep <sleeptime>}\n");
301 /* initialize winsock */
302 if (afs_winsockInit() < 0)
305 /* parse our own local arguments */
307 for (i = 1; i < argc; i++) {
308 if (strcmp(argv[i], "-sleep") == 0) {
310 printf("missing time in -sleep argument\n");
313 sleepTime = atoi(argv[i + 1]);
317 /* call routine to parse command line -servers switch, filling in
318 * myHost and serverList arrays appropriately */
319 code = ubik_ParseServerList(argc, argv, &myHost, serverList);
321 printf("could not parse server list, code %d\n", code);
324 /* call ServerInit with the values from ParseServerList. Also specify the
325 * name to use for the database files (/tmp/testdb), and the port (3000)
326 * for RPC requests. ServerInit returns a pointer to the database (in
327 * dbase), which is required for creating new transactions */
329 sprintf(dbfileName, "%s/testdb", gettmpdir());
332 ubik_ServerInit(myHost, htons(3000), serverList, dbfileName, &dbase);
335 printf("ubik init failed with code %d\n", code);
339 sc[0] = rxnull_NewServerSecurityObject();
341 sc[1] = rxvab_NewServerSecurityObject("applexx", 0);
343 tservice = rx_NewService(0, USER_SERVICE_ID, "Sample", sc, 1 /*2 */ ,
344 SAMPLE_ExecuteRequest);
345 if (tservice == (struct rx_service *)0) {
346 printf("Could not create SAMPLE rx service\n");
349 rx_SetMinProcs(tservice, 2);
350 rx_SetMaxProcs(tservice, 3);
352 rx_StartServer(1); /* Why waste this idle process?? */