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