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>
19 #include <afs/afsutil.h>
25 /*! \name useful globals */
26 struct ubik_dbase
*dbase
;
31 SSAMPLE_Inc(struct rx_call
*call
)
34 struct ubik_trans
*tt
;
37 code
= ubik_BeginTrans(dbase
, UBIK_WRITETRANS
, &tt
);
40 printf("about to set lock\n");
41 /* now set database locks. Must do this or people may read uncommitted
42 * data. Note that we're just setting a lock at position 1, which is
43 * this program's convention for locking the whole database */
44 code
= ubik_SetLock(tt
, 1, 1, LOCKWRITE
);
45 printf("now have lock\n");
50 /* sleep for a little while to make it possible for us to test for some
53 tv
.tv_sec
= sleepTime
;
55 #ifdef AFS_PTHREAD_ENV
56 select(0, 0, 0, 0, &tv
);
58 IOMGR_Select(0, 0, 0, 0, &tv
);
61 /* read the original value */
62 code
= ubik_Read(tt
, &temp
, sizeof(afs_int32
));
70 temp
++; /* bump the value here */
71 /* reset the file pointer back to where it was before the read */
72 code
= ubik_Seek(tt
, 0, 0);
77 /* write the data back */
78 code
= ubik_Write(tt
, &temp
, sizeof(afs_int32
));
83 /* finally, we commit the transaction */
84 code
= ubik_EndTrans(tt
);
90 SSAMPLE_Get(struct rx_call
*call
, afs_int32
*gnumber
)
93 struct ubik_trans
*tt
;
96 /* start with a read transaction, since we're only going to do read
97 * operations in this transaction. */
98 code
= ubik_BeginTrans(dbase
, UBIK_READTRANS
, &tt
);
101 printf("about to set lock\n");
102 /* obtain a read lock, so we don't read data the other guy is writing */
103 code
= ubik_SetLock(tt
, 1, 1, LOCKREAD
);
104 printf("now have lock\n");
109 /* sleep to allow races */
111 tv
.tv_sec
= sleepTime
;
113 #ifdef AFS_PTHREAD_ENV
114 select(0, 0, 0, 0, &tv
);
116 IOMGR_Select(0, 0, 0, 0, &tv
);
120 code
= ubik_Read(tt
, &temp
, sizeof(afs_int32
));
122 /* premature eof, use 0 */
129 /* end the transaction, automatically releasing locks */
130 code
= ubik_EndTrans(tt
);
135 SSAMPLE_QGet(struct rx_call
*call
, afs_int32
*gnumber
)
137 afs_int32 code
, temp
;
138 struct ubik_trans
*tt
;
141 /* start with a read transaction, since we're only going to do read
142 * operations in this transaction. */
143 code
= ubik_BeginTransReadAny(dbase
, UBIK_READTRANS
, &tt
);
146 printf("about to set lock\n");
147 /* obtain a read lock, so we don't read data the other guy is writing */
148 code
= ubik_SetLock(tt
, 1, 1, LOCKREAD
);
149 printf("now have lock\n");
154 /* sleep to allow races */
156 tv
.tv_sec
= sleepTime
;
158 #ifdef AFS_PTHREAD_ENV
159 select(0, 0, 0, 0, &tv
);
161 IOMGR_Select(0, 0, 0, 0, &tv
);
165 code
= ubik_Read(tt
, &temp
, sizeof(afs_int32
));
167 /* premature eof, use 0 */
174 /* end the transaction, automatically releasing locks */
175 code
= ubik_EndTrans(tt
);
180 SSAMPLE_Trun(struct rx_call
*call
)
183 struct ubik_trans
*tt
;
186 /* truncation operation requires a write transaction, too */
187 code
= ubik_BeginTrans(dbase
, UBIK_WRITETRANS
, &tt
);
190 printf("about to set lock\n");
191 /* lock the database */
192 code
= ubik_SetLock(tt
, 1, 1, LOCKWRITE
);
193 printf("now have lock\n");
199 tv
.tv_sec
= sleepTime
;
201 #ifdef AFS_PTHREAD_ENV
202 select(0, 0, 0, 0, &tv
);
204 IOMGR_Select(0, 0, 0, 0, &tv
);
207 /* shrink the file */
208 code
= ubik_Truncate(tt
, 0);
214 code
= ubik_EndTrans(tt
);
219 SSAMPLE_Test(struct rx_call
*call
)
221 afs_int32 code
, temp
;
222 struct ubik_trans
*tt
;
225 /* first start a new transaction. Must be a write transaction since
226 * we're going to change some data (with ubik_Write) */
227 code
= ubik_BeginTrans(dbase
, UBIK_WRITETRANS
, &tt
);
230 printf("about to set lock\n");
231 /* now set database locks. Must do this or people may read uncommitted
232 * data. Note that we're just setting a lock at position 1, which is
233 * this program's convention for locking the whole database */
234 code
= ubik_SetLock(tt
, 1, 1, LOCKWRITE
);
235 printf("now have lock\n");
240 /* sleep for a little while to make it possible for us to test for some
243 tv
.tv_sec
= sleepTime
;
245 #ifdef AFS_PTHREAD_ENV
246 select(0, 0, 0, 0, &tv
);
248 IOMGR_Select(0, 0, 0, 0, &tv
);
251 /* read the original value */
252 code
= ubik_Read(tt
, &temp
, sizeof(afs_int32
));
254 printf("short read, using 0\n");
260 ubik_AbortTrans(tt
); /* surprise! pretend something went wrong */
265 #include "AFS_component_version_number.c"
268 main(int argc
, char **argv
)
271 afs_uint32 serverList
[MAXSERVERS
];
273 struct rx_service
*tservice
;
274 struct rx_securityClass
*sc
[2];
275 char dbfileName
[128];
278 printf("usage: userver -servers <serverlist> {-sleep <sleeptime>}\n");
282 /* initialize winsock */
283 if (afs_winsockInit() < 0)
286 /* parse our own local arguments */
288 for (i
= 1; i
< argc
; i
++) {
289 if (strcmp(argv
[i
], "-sleep") == 0) {
291 printf("missing time in -sleep argument\n");
294 sleepTime
= atoi(argv
[i
+ 1]);
298 /* call routine to parse command line -servers switch, filling in
299 * myHost and serverList arrays appropriately */
300 code
= ubik_ParseServerList(argc
, argv
, &myHost
, serverList
);
302 printf("could not parse server list, code %d\n", code
);
305 /* call ServerInit with the values from ParseServerList. Also specify the
306 * name to use for the database files (/tmp/testdb), and the port (3000)
307 * for RPC requests. ServerInit returns a pointer to the database (in
308 * dbase), which is required for creating new transactions */
310 sprintf(dbfileName
, "%s/testdb", gettmpdir());
313 ubik_ServerInit(myHost
, htons(3000), serverList
, dbfileName
, &dbase
);
316 printf("ubik init failed with code %d\n", code
);
320 sc
[0] = rxnull_NewServerSecurityObject();
322 sc
[1] = rxvab_NewServerSecurityObject("applexx", 0);
324 tservice
= rx_NewService(0, USER_SERVICE_ID
, "Sample", sc
, 1 /*2 */ ,
325 SAMPLE_ExecuteRequest
);
326 if (tservice
== (struct rx_service
*)0) {
327 printf("Could not create SAMPLE rx service\n");
330 rx_SetMinProcs(tservice
, 2);
331 rx_SetMaxProcs(tservice
, 3);
333 rx_StartServer(1); /* Why waste this idle process?? */