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>
15 #include <sys/types.h>
22 #include <netinet/in.h>
30 #include <afs/afsutil.h>
35 /*! \name useful globals */
36 struct ubik_dbase *dbase;
41 SAMPLE_Inc(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(struct rx_connection *rxconn, afs_int32 *gnumber)
102 afs_int32 code, temp;
103 struct ubik_trans *tt;
106 /* start with a read transaction, since we're only going to do read
107 * operations in this transaction. */
108 code = ubik_BeginTrans(dbase, UBIK_READTRANS, &tt);
111 printf("about to set lock\n");
112 /* obtain a read lock, so we don't read data the other guy is writing */
113 code = ubik_SetLock(tt, 1, 1, LOCKREAD);
114 printf("now have lock\n");
119 /* sleep to allow races */
121 tv.tv_sec = sleepTime;
123 #ifdef AFS_PTHREAD_ENV
124 select(0, 0, 0, 0, &tv);
126 IOMGR_Select(0, 0, 0, 0, &tv);
130 code = ubik_Read(tt, &temp, sizeof(afs_int32));
132 /* premature eof, use 0 */
139 /* end the transaction, automatically releasing locks */
140 code = ubik_EndTrans(tt);
145 SAMPLE_QGet(struct rx_connection *rxconn, afs_int32 *gnumber)
147 afs_int32 code, temp;
148 struct ubik_trans *tt;
151 /* start with a read transaction, since we're only going to do read
152 * operations in this transaction. */
153 code = ubik_BeginTransReadAny(dbase, UBIK_READTRANS, &tt);
156 printf("about to set lock\n");
157 /* obtain a read lock, so we don't read data the other guy is writing */
158 code = ubik_SetLock(tt, 1, 1, LOCKREAD);
159 printf("now have lock\n");
164 /* sleep to allow races */
166 tv.tv_sec = sleepTime;
168 #ifdef AFS_PTHREAD_ENV
169 select(0, 0, 0, 0, &tv);
171 IOMGR_Select(0, 0, 0, 0, &tv);
175 code = ubik_Read(tt, &temp, sizeof(afs_int32));
177 /* premature eof, use 0 */
184 /* end the transaction, automatically releasing locks */
185 code = ubik_EndTrans(tt);
190 SAMPLE_Trun(struct rx_connection *rxconn)
193 struct ubik_trans *tt;
196 /* truncation operation requires a write transaction, too */
197 code = ubik_BeginTrans(dbase, UBIK_WRITETRANS, &tt);
200 printf("about to set lock\n");
201 /* lock the database */
202 code = ubik_SetLock(tt, 1, 1, LOCKWRITE);
203 printf("now have lock\n");
209 tv.tv_sec = sleepTime;
211 #ifdef AFS_PTHREAD_ENV
212 select(0, 0, 0, 0, &tv);
214 IOMGR_Select(0, 0, 0, 0, &tv);
217 /* shrink the file */
218 code = ubik_Truncate(tt, 0);
224 code = ubik_EndTrans(tt);
229 SAMPLE_Test(struct rx_connection *rxconn)
231 afs_int32 code, temp;
232 struct ubik_trans *tt;
235 /* first start a new transaction. Must be a write transaction since
236 * we're going to change some data (with ubik_Write) */
237 code = ubik_BeginTrans(dbase, UBIK_WRITETRANS, &tt);
240 printf("about to set lock\n");
241 /* now set database locks. Must do this or people may read uncommitted
242 * data. Note that we're just setting a lock at position 1, which is
243 * this program's convention for locking the whole database */
244 code = ubik_SetLock(tt, 1, 1, LOCKWRITE);
245 printf("now have lock\n");
250 /* sleep for a little while to make it possible for us to test for some
253 tv.tv_sec = sleepTime;
255 #ifdef AFS_PTHREAD_ENV
256 select(0, 0, 0, 0, &tv);
258 IOMGR_Select(0, 0, 0, 0, &tv);
261 /* read the original value */
262 code = ubik_Read(tt, &temp, sizeof(afs_int32));
264 printf("short read, using 0\n");
270 ubik_AbortTrans(tt); /* surprise! pretend something went wrong */
275 #include "AFS_component_version_number.c"
278 main(int argc, char **argv)
280 register afs_int32 code, i;
281 afs_int32 serverList[MAXSERVERS];
283 struct rx_service *tservice;
284 struct rx_securityClass *sc[2];
285 char dbfileName[128];
288 printf("usage: userver -servers <serverlist> {-sleep <sleeptime>}\n");
292 /* initialize winsock */
293 if (afs_winsockInit() < 0)
296 /* parse our own local arguments */
298 for (i = 1; i < argc; i++) {
299 if (strcmp(argv[i], "-sleep") == 0) {
301 printf("missing time in -sleep argument\n");
304 sleepTime = atoi(argv[i + 1]);
308 /* call routine to parse command line -servers switch, filling in
309 * myHost and serverList arrays appropriately */
310 code = ubik_ParseServerList(argc, argv, &myHost, serverList);
312 printf("could not parse server list, code %d\n", code);
315 /* call ServerInit with the values from ParseServerList. Also specify the
316 * name to use for the database files (/tmp/testdb), and the port (3000)
317 * for RPC requests. ServerInit returns a pointer to the database (in
318 * dbase), which is required for creating new transactions */
320 sprintf(dbfileName, "%s/testdb", gettmpdir());
323 ubik_ServerInit(myHost, htons(3000), serverList, dbfileName, &dbase);
326 printf("ubik init failed with code %d\n", code);
330 sc[0] = rxnull_NewServerSecurityObject();
332 sc[1] = rxvab_NewServerSecurityObject("applexx", 0);
334 tservice = rx_NewService(0, USER_SERVICE_ID, "Sample", sc, 1 /*2 */ ,
335 SAMPLE_ExecuteRequest);
336 if (tservice == (struct rx_service *)0) {
337 printf("Could not create SAMPLE rx service\n");
340 rx_SetMinProcs(tservice, 2);
341 rx_SetMaxProcs(tservice, 3);
343 rx_StartServer(1); /* Why waste this idle process?? */