printf-sanity-20090317
[openafs.git] / src / ubik / utst_client.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12 #include <afs/stds.h>
13
14 RCSID
15     ("$Header$");
16
17 #include <sys/types.h>
18 #ifdef AFS_NT40_ENV
19 #include <winsock2.h>
20 #include <afsutil.h>
21 #else
22 #include <sys/file.h>
23 #include <netdb.h>
24 #include <netinet/in.h>
25 #endif
26 #include <time.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <rx/xdr.h>
30 #include <rx/rx.h>
31 #include <lock.h>
32 #include "ubik.h"
33 #include "utst_int.h"
34
35 /* main program */
36
37 #include "AFS_component_version_number.c"
38
39 int
40 main(int argc, char **argv)
41 {
42     register afs_int32 code;
43     struct ubik_client *cstruct = 0;
44     afs_int32 serverList[MAXSERVERS];
45     struct rx_connection *serverconns[MAXSERVERS];
46     struct rx_securityClass *sc;
47     register afs_int32 i;
48     afs_int32 temp;
49
50     if (argc == 1) {
51         printf
52             ("uclient: usage is 'uclient -servers ... [-try] [-get] [-inc] [-minc] [-trunc]\n");
53         exit(0);
54     }
55 #ifdef AFS_NT40_ENV
56     /* initialize winsock */
57     if (afs_winsockInit() < 0)
58         return -1;
59 #endif
60     /* first parse '-servers <server-1> <server-2> ... <server-n>' from command line */
61     code = ubik_ParseClientList(argc, argv, serverList);
62     if (code) {
63         printf("could not parse server list, code %d\n", code);
64         exit(1);
65     }
66     rx_Init(0);
67     sc = rxnull_NewClientSecurityObject();
68     for (i = 0; i < MAXSERVERS; i++) {
69         if (serverList[i]) {
70             serverconns[i] =
71                 rx_NewConnection(serverList[i], htons(3000), USER_SERVICE_ID,
72                                  sc, 0);
73         } else {
74             serverconns[i] = (struct rx_connection *)0;
75             break;
76         }
77     }
78
79     /* next, pass list of server rx_connections (in serverconns), and
80      * a place to put the returned client structure that we'll use in
81      * all of our rpc calls (via ubik_Calll) */
82     code = ubik_ClientInit(serverconns, &cstruct);
83
84     /* check code from init */
85     if (code) {
86         printf("ubik client init failed with code %d\n", code);
87         exit(1);
88     }
89
90     /* parse command line for our own operations */
91     for (i = 1; i < argc; i++) {
92         if (!strcmp(argv[i], "-inc")) {
93             /* use ubik_Call to do the work, finding an up server and handling
94              * the job of finding a sync site, if need be */
95             code = ubik_SAMPLE_Inc(cstruct, 0);
96             printf("return code is %d\n", code);
97         } else if (!strcmp(argv[i], "-try")) {
98             code = ubik_SAMPLE_Test(cstruct, 0);
99             printf("return code is %d\n", code);
100         } else if (!strcmp(argv[i], "-qget")) {
101             code = ubik_SAMPLE_QGet(cstruct, 0, &temp);
102             printf("got quick value %d (code %d)\n", temp, code);
103         } else if (!strcmp(argv[i], "-get")) {
104             code = ubik_SAMPLE_Get(cstruct, 0, &temp);
105             printf("got value %d (code %d)\n", temp, code);
106         } else if (!strcmp(argv[i], "-trunc")) {
107             code = ubik_SAMPLE_Trun(cstruct, 0);
108             printf("return code is %d\n", code);
109         } else if (!strcmp(argv[i], "-minc")) {
110             afs_int32 temp;
111             struct timeval tv;
112             tv.tv_sec = 1;
113             tv.tv_usec = 0;
114             printf("ubik_client: Running minc...\n");
115
116             while (1) {
117                 temp = 0;
118                 code = ubik_SAMPLE_Get(cstruct, 0, &temp);
119                 if (code != 0) {
120                     printf("SAMPLE_Get #1 failed with code %ld\n", code);
121                 } else {
122                     printf("SAMPLE_Get #1 succeeded, got value %d\n", temp);
123                 }
124
125                 temp = 0;
126                 code = ubik_SAMPLE_Inc(cstruct, 0);
127                 if (code != 0) {
128                     printf("SAMPLE_Inc #1 failed with code %ld\n", 
129                            afs_cast_int32(code));
130                 } else {
131                     printf("SAMPLE_Inc #1 succeeded, incremented integer\n");
132                 }
133                 temp = 0;
134                 code = ubik_SAMPLE_Get(cstruct, 0, &temp);
135                 if (code != 0) {
136                     printf("SAMPLE_Get #2 failed with code %ld\n", code);
137                 } else {
138                     printf("SAMPLE_Get #2 succeeded, got value %d\n", temp);
139                 }
140
141                 temp = 0;
142                 code = ubik_SAMPLE_Inc(cstruct, 0);
143                 if (code != 0)
144                     printf("SAMPLE_Inc #2 failed with code %ld\n", 
145                            afs_cast_int32(code));
146                 else
147                     printf("SAMPLE_Inc #2 succeeded, incremented integer\n");
148
149                 tv.tv_sec = 1;
150                 tv.tv_usec = 0;
151 #ifdef AFS_PTHREAD_ENV
152                 select(0, 0, 0, 0, &tv);
153 #else
154                 IOMGR_Select(0, 0, 0, 0, &tv);
155 #endif
156                 printf("Repeating the SAMPLE operations again...\n");
157             }
158         } else if (!strcmp(argv[i], "-mget")) {
159             afs_int32 temp;
160             struct timeval tv;
161             tv.tv_sec = 1;
162             tv.tv_usec = 0;
163             while (1) {
164                 code = ubik_SAMPLE_Get(cstruct, 0, &temp);
165                 printf("got value %d (code %d)\n", temp, code);
166
167                 code = ubik_SAMPLE_Inc(cstruct, 0);
168                 printf("update return code is %d\n", code);
169
170                 code = ubik_SAMPLE_Get(cstruct, 0, &temp);
171                 printf("got value %d (code %d)\n", temp, code);
172
173                 code = ubik_SAMPLE_Get(cstruct, 0, &temp);
174                 printf("got value %d (code %d)\n", temp, code);
175
176                 tv.tv_sec = 1;
177                 tv.tv_usec = 0;
178 #ifdef AFS_PTHREAD_ENV
179                 select(0, 0, 0, 0, &tv);
180 #else
181                 IOMGR_Select(0, 0, 0, 0, &tv);
182 #endif
183             }
184         }
185     }
186     return 0;
187 }