Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / rx / bulktest / bulk_server.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
14 #include <sys/types.h>
15 #include <netdb.h>
16 #include <netinet/in.h>
17 #include <sys/stat.h>
18 #include <sys/file.h>
19 #include <signal.h>
20 #include <stdio.h>
21 #include <rx/xdr.h>
22 #include "bulk.h"
23
24 #define N_SECURITY_OBJECTS 1
25
26
27 InterruptSignal()
28 {
29 rx_PrintStats(stdout);
30 exit(0);
31 }
32
33 main()
34 {
35 struct rx_securityClass *(securityObjects[N_SECURITY_OBJECTS]);
36 struct rx_service *service;
37
38 signal(SIGINT, InterruptSignal);
39
40 /* Initialize Rx, telling it port number this server will use for its single service */
41 if (rx_Init(BULK_SERVER_PORT) < 0)
42 Quit("rx_init");
43
44 /* Create a single security object, in this case the null security object, for unauthenticated connections, which will be used to control security on connections made to this server */
45 securityObjects[RX_SECIDX_NULL] = rxnull_NewServerSecurityObject();
46 if (securityObjects[RX_SECIDX_NULL] == (struct rx_securityClass *)0)
47 Quit("rxnull_NewServerSecurityObject");
48
49 /* Instantiate a single BULK service. The rxgen-generated procedure which is called to decode requests is passed in here (BULK_ExecuteRequest). */
50 service =
51 rx_NewService(0, BULK_SERVICE_ID, "BULK", securityObjects,
52 N_SECURITY_OBJECTS, BULK_ExecuteRequest);
53 if (service == (struct rx_service *)0)
54 Quit("rx_NewService");
55 rx_SetMaxProcs(service, 5);
56
57 rx_StartServer(1); /* Donate this process to the server process pool */
58 Quit("StartServer returned?");
59 }
60
61 int bulk_operationNumber = 0;
62
63 int
64 BULK_FetchFile(call, verbose, name)
65 struct rx_call *call;
66 char *name;
67 {
68 int fd = -1;
69 int error = 0;
70 int opnum = ++bulk_operationNumber;
71 struct stat status;
72 if (verbose)
73 printf("%d: fetch %s\n", opnum, name);
74 fd = open(name, O_RDONLY, 0);
75 if (fd < 0 || fstat(fd, &status) < 0) {
76 if (verbose)
77 printf("%d: failed to open %s\n", opnum, name);
78 error = BULK_ERROR;
79 goto fail;
80 }
81 error = bulk_SendFile(fd, call, &status);
82 if (error)
83 printf("%d: fetch of %s failed, error %d\n", opnum, name, error);
84 else if (verbose)
85 printf("%d: fetch of %s complete\n", opnum, name);
86 fail:
87 if (fd >= 0)
88 close(fd);
89 return error;
90 }
91
92 int
93 BULK_StoreFile(call, verbose, name)
94 struct rx_call *call;
95 int verbose;
96 char *name;
97 {
98 int opnum = ++bulk_operationNumber;
99 int fd = -1;
100 struct stat status;
101 int error = 0;
102 if (verbose)
103 printf("%d: store file %s\n", opnum, name);
104 fd = open(name, O_CREAT | O_TRUNC | O_WRONLY, 0666);
105 if (fd < 0 || fstat(fd, &status) < 0) {
106 fprintf(stderr, "%d: could not create %s\n", opnum, name);
107 error = BULK_ERROR;
108 goto fail;
109 }
110 error = bulk_ReceiveFile(fd, call, &status);
111 if (error)
112 printf("%d: store of %s failed, error %d\n", opnum, name, error);
113 else if (verbose)
114 printf("%d: store of %s complete\n", opnum, name);
115
116 fail:
117 if (fd >= 0)
118 close(fd);
119 return error;
120 }
121
122 Quit(msg, a, b)
123 char *msg;
124 {
125 fprintf(stderr, msg, a, b);
126 exit(1);
127 }