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>
37 struct ubik_dbase *dbase;
41 struct rx_connection *rxconn;
44 struct ubik_trans *tt;
47 code = ubik_BeginTrans(dbase, UBIK_WRITETRANS, &tt);
50 printf("about to set lock\n");
51 /* now set database locks. Must do this or people may read uncommitted
52 * data. Note that we're just setting a lock at position 1, which is
53 * this program's convention for locking the whole database */
54 code = ubik_SetLock(tt, 1, 1, LOCKWRITE);
55 printf("now have lock\n");
60 /* sleep for a little while to make it possible for us to test for some
63 tv.tv_sec = sleepTime;
65 #ifdef AFS_PTHREAD_ENV
66 select(0, 0, 0, 0, &tv);
68 IOMGR_Select(0, 0, 0, 0, &tv);
71 /* read the original value */
72 code = ubik_Read(tt, &temp, sizeof(afs_int32));
80 temp++; /* bump the value here */
81 /* reset the file pointer back to where it was before the read */
82 code = ubik_Seek(tt, 0, 0);
87 /* write the data back */
88 code = ubik_Write(tt, &temp, sizeof(afs_int32));
93 /* finally, we commit the transaction */
94 code = ubik_EndTrans(tt);
100 SAMPLE_Get(rxconn, gnumber)
101 struct rx_connection *rxconn;
104 afs_int32 code, temp;
105 struct ubik_trans *tt;
108 /* start with a read transaction, since we're only going to do read
109 * operations in this transaction. */
110 code = ubik_BeginTrans(dbase, UBIK_READTRANS, &tt);
113 printf("about to set lock\n");
114 /* obtain a read lock, so we don't read data the other guy is writing */
115 code = ubik_SetLock(tt, 1, 1, LOCKREAD);
116 printf("now have lock\n");
121 /* sleep to allow races */
123 tv.tv_sec = sleepTime;
125 #ifdef AFS_PTHREAD_ENV
126 select(0, 0, 0, 0, &tv);
128 IOMGR_Select(0, 0, 0, 0, &tv);
132 code = ubik_Read(tt, &temp, sizeof(afs_int32));
134 /* premature eof, use 0 */
141 /* end the transaction, automatically releasing locks */
142 code = ubik_EndTrans(tt);
147 SAMPLE_QGet(rxconn, gnumber)
148 struct rx_connection *rxconn;
151 afs_int32 code, temp;
152 struct ubik_trans *tt;
155 /* start with a read transaction, since we're only going to do read
156 * operations in this transaction. */
157 code = ubik_BeginTransReadAny(dbase, UBIK_READTRANS, &tt);
160 printf("about to set lock\n");
161 /* obtain a read lock, so we don't read data the other guy is writing */
162 code = ubik_SetLock(tt, 1, 1, LOCKREAD);
163 printf("now have lock\n");
168 /* sleep to allow races */
170 tv.tv_sec = sleepTime;
172 #ifdef AFS_PTHREAD_ENV
173 select(0, 0, 0, 0, &tv);
175 IOMGR_Select(0, 0, 0, 0, &tv);
179 code = ubik_Read(tt, &temp, sizeof(afs_int32));
181 /* premature eof, use 0 */
188 /* end the transaction, automatically releasing locks */
189 code = ubik_EndTrans(tt);
195 struct rx_connection *rxconn;
198 struct ubik_trans *tt;
201 /* truncation operation requires a write transaction, too */
202 code = ubik_BeginTrans(dbase, UBIK_WRITETRANS, &tt);
205 printf("about to set lock\n");
206 /* lock the database */
207 code = ubik_SetLock(tt, 1, 1, LOCKWRITE);
208 printf("now have lock\n");
214 tv.tv_sec = sleepTime;
216 #ifdef AFS_PTHREAD_ENV
217 select(0, 0, 0, 0, &tv);
219 IOMGR_Select(0, 0, 0, 0, &tv);
222 /* shrink the file */
223 code = ubik_Truncate(tt, 0);
229 code = ubik_EndTrans(tt);
235 struct rx_connection *rxconn;
237 afs_int32 code, temp;
238 struct ubik_trans *tt;
241 /* first start a new transaction. Must be a write transaction since
242 * we're going to change some data (with ubik_Write) */
243 code = ubik_BeginTrans(dbase, UBIK_WRITETRANS, &tt);
246 printf("about to set lock\n");
247 /* now set database locks. Must do this or people may read uncommitted
248 * data. Note that we're just setting a lock at position 1, which is
249 * this program's convention for locking the whole database */
250 code = ubik_SetLock(tt, 1, 1, LOCKWRITE);
251 printf("now have lock\n");
256 /* sleep for a little while to make it possible for us to test for some
259 tv.tv_sec = sleepTime;
261 #ifdef AFS_PTHREAD_ENV
262 select(0, 0, 0, 0, &tv);
264 IOMGR_Select(0, 0, 0, 0, &tv);
267 /* read the original value */
268 code = ubik_Read(tt, &temp, sizeof(afs_int32));
270 printf("short read, using 0\n");
276 ubik_AbortTrans(tt); /* surprise! pretend something went wrong */
281 #include "AFS_component_version_number.c"
287 register afs_int32 code, i;
288 afs_int32 serverList[MAXSERVERS];
290 struct rx_service *tservice;
291 struct rx_securityClass *sc[2];
292 extern int SAMPLE_ExecuteRequest();
293 char dbfileName[128];
296 printf("usage: userver -servers <serverlist> {-sleep <sleeptime>}\n");
300 /* initialize winsock */
301 if (afs_winsockInit() < 0)
304 /* parse our own local arguments */
306 for (i = 1; i < argc; i++) {
307 if (strcmp(argv[i], "-sleep") == 0) {
309 printf("missing time in -sleep argument\n");
312 sleepTime = atoi(argv[i + 1]);
316 /* call routine to parse command line -servers switch, filling in
317 * myHost and serverList arrays appropriately */
318 code = ubik_ParseServerList(argc, argv, &myHost, serverList);
320 printf("could not parse server list, code %d\n", code);
323 /* call ServerInit with the values from ParseServerList. Also specify the
324 * name to use for the database files (/tmp/testdb), and the port (3000)
325 * for RPC requests. ServerInit returns a pointer to the database (in
326 * dbase), which is required for creating new transactions */
328 sprintf(dbfileName, "%s/testdb", gettmpdir());
331 ubik_ServerInit(myHost, htons(3000), serverList, dbfileName, &dbase);
334 printf("ubik init failed with code %d\n", code);
338 sc[0] = rxnull_NewServerSecurityObject();
340 sc[1] = rxvab_NewServerSecurityObject("applexx", 0);
342 tservice = rx_NewService(0, USER_SERVICE_ID, "Sample", sc, 1 /*2 */ ,
343 SAMPLE_ExecuteRequest);
344 if (tservice == (struct rx_service *)0) {
345 printf("Could not create SAMPLE rx service\n");
348 rx_SetMinProcs(tservice, 2);
349 rx_SetMaxProcs(tservice, 3);
351 rx_StartServer(1); /* Why waste this idle process?? */