afsconfig-and-rcsid-all-around-20010705
[openafs.git] / src / rx / bulk.example / 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 <afs/param.h>
11 #include <afsconfig.h>
12
13 RCSID("$Header$");
14
15 #include <sys/types.h>
16 #include <netdb.h>
17 #include <netinet/in.h>
18 #include <sys/stat.h>
19 #include <sys/file.h>
20 #include <stdio.h>
21 #include <rx/xdr.h>
22 #include "bulk.h"
23
24 #define N_SECURITY_OBJECTS 1
25
26 extern BULK__ExecuteRequest();
27
28 main() {
29     struct rx_securityClass *(securityObjects[N_SECURITY_OBJECTS]);
30     struct rx_service *service;
31
32     /* Initialize Rx, telling it port number this server will use for its single service */
33     if (rx_Init(BULK_SERVER_PORT) < 0) Quit("rx_init");
34
35     /* 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 */
36     securityObjects[BULK_NULL] = rxnull_NewServerSecurityObject();
37     if (securityObjects[BULK_NULL] == (struct rx_securityClass *) 0) Quit("rxnull_NewServerSecurityObject");
38
39     /* Instantiate a single BULK service.  The rxgen-generated procedure which is called to decode requests is passed in here (BULK_ExecuteRequest). */
40     service = rx_NewService(0, BULK_SERVICE_ID, "BULK", securityObjects, N_SECURITY_OBJECTS, BULK__ExecuteRequest);
41     if (service == (struct rx_service *) 0) Quit("rx_NewService");
42     rx_SetMaxProcs(service, 2);
43
44     rx_StartServer(1); /* Donate this process to the server process pool */
45     Quit("StartServer returned?");
46 }
47
48 int BULK_FetchFile(call, verbose, name)
49     struct rx_call *call;
50     char *name;
51 {
52     int fd = -1;
53     int error = 0;
54     struct stat status;
55     if (verbose) printf("Fetch file %s\n", name);
56     fd = open(name, O_RDONLY, 0);
57     if (fd < 0 || fstat(fd, &status) < 0) {
58         if (verbose) printf("Failed to open %s\n", name);
59         error = BULK_ERROR;
60     }
61     if (!error) error = bulk_SendFile(fd, call, &status);
62     if (fd >= 0) close(fd);
63     return error;
64 }
65
66 int BULK_StoreFile(call, verbose, name)
67     struct rx_call *call;
68     int verbose;
69     char *name;
70 {
71     int fd = -1;
72     struct stat status;
73     int error = 0;
74     if (verbose) printf("Store file %s\n", name);
75     fd = open(name, O_CREAT|O_TRUNC|O_WRONLY, 0666);
76     if (fd < 0 || fstat(fd, &status) < 0) {
77         fprintf("Could not create %s\n", name);
78         error = BULK_ERROR;
79     }
80     if (!error) error = bulk_ReceiveFile(fd, call, &status);
81     if (fd >= 0) close(fd);
82     return error;
83 }
84
85 Quit(msg, a, b)
86     char *msg;
87 {
88     fprintf(stderr, msg, a, b);
89     exit(1);
90 }
91