1 | /* |
---|
2 | recv-tree.c - simple server process using AF_UNIX sockets |
---|
3 | taken from Paul Wang's book on Unix - p. 341 |
---|
4 | */ |
---|
5 | |
---|
6 | #include <stdio.h> |
---|
7 | #include <fcntl.h> |
---|
8 | #include "MP.h" |
---|
9 | #include "gmp.h" |
---|
10 | #include "node.h" |
---|
11 | |
---|
12 | FILE *fd; |
---|
13 | |
---|
14 | int |
---|
15 | process_request(root) |
---|
16 | node_t *root; |
---|
17 | { |
---|
18 | int more; |
---|
19 | |
---|
20 | more = print_node(root, fd); |
---|
21 | fprintf(fd, "\n"); fflush(fd); |
---|
22 | return more; |
---|
23 | } |
---|
24 | |
---|
25 | int get_infilename(fname) |
---|
26 | char *fname; |
---|
27 | { |
---|
28 | int fd; |
---|
29 | printf("get_infilename: fname = %s\n", fname); |
---|
30 | if ((fd = open(fname, O_RDONLY)) < 0) |
---|
31 | fprintf(stderr, "can't open %s for reading\n", fname); |
---|
32 | printf("get_infilename: fd = %d\n", fd); |
---|
33 | return fd; |
---|
34 | } |
---|
35 | |
---|
36 | |
---|
37 | main(argc, argv) |
---|
38 | int argc; |
---|
39 | char *argv[]; |
---|
40 | { |
---|
41 | char *ofname = NULL; |
---|
42 | int more = 0, n = -1; |
---|
43 | node_t *root = NULL; |
---|
44 | MP_Link_pt link = NULL; |
---|
45 | MP_Env_pt env = NULL; |
---|
46 | |
---|
47 | if ((ofname = IMP_GetCmdlineArg(argc, argv, "-outfile")) == NULL){ |
---|
48 | fprintf(stderr,"%s: missing -outfile argument\n", argv[0]); |
---|
49 | return 1; |
---|
50 | } |
---|
51 | |
---|
52 | env = MP_AllocateEnv(); |
---|
53 | |
---|
54 | MP_SetEnvOption(env, MP_BUFFER_SIZE_OPT, 512); |
---|
55 | MP_SetEnvOption(env, MP_MAX_FREE_BUFFERS_OPT, 46); |
---|
56 | MP_SetEnvOption(env, MP_INIT_FREE_BUFFERS_OPT, 24); |
---|
57 | |
---|
58 | env = MP_InitializeEnv(env); |
---|
59 | if (env == NULL) { |
---|
60 | fprintf(stderr, "%s: MP_EnvInitialize() failed!\n", argv[0]); |
---|
61 | exit(1); |
---|
62 | } |
---|
63 | fprintf(stderr,"%s: MP_EnvInitialize() succeeded\n", argv[0]); |
---|
64 | |
---|
65 | if ((link = MP_OpenLink(env, argc, argv)) == NULL) { |
---|
66 | fprintf(stderr, "%s: MP_OpenLink() failed!\n", argv[0]); |
---|
67 | exit(1); |
---|
68 | } |
---|
69 | fprintf(stderr,"%s: MP_OpenLink() succeeded\n", argv[0]); |
---|
70 | MP_SetLinkOption(link, MP_LINK_LOG_MASK_OPT, MP_LOG_READ_EVENTS); |
---|
71 | fprintf(stderr, "done setting first link options\n"); |
---|
72 | MP_SetLinkOption(link, MP_LINK_RECV_MODE_OPT, MP_RECV_WHOLE_MSG); |
---|
73 | fprintf(stderr, "done setting second link options\n");fflush(stderr); |
---|
74 | |
---|
75 | printf("ofname = %s\n", ofname); |
---|
76 | |
---|
77 | fd = fopen(ofname, "w"); |
---|
78 | mpf_set_default_prec(256); /* for the GMP arb prec reals */ |
---|
79 | fprintf(stderr, "done setting mpf value\n");fflush(stderr); |
---|
80 | |
---|
81 | printf("server's annotated tree(s):\n"); |
---|
82 | do { |
---|
83 | if (MP_TestEofMsg(link)) MP_SkipMsg(link); |
---|
84 | printf("\n\n%-20s %-5s %-30s %-7s %-9s\n", "Type", "Dict", "Value", |
---|
85 | "#Annots", "#Children"); |
---|
86 | /* STEP 1: get the request */ |
---|
87 | root = m_get_node(link); |
---|
88 | |
---|
89 | /* STEP 2: process the request */ |
---|
90 | more = process_request(root); |
---|
91 | |
---|
92 | /* STEP 3: cleanup */ |
---|
93 | free_tree(root); /* free the annotated tree */ |
---|
94 | root = NULL; |
---|
95 | free_label_table(); /* free the label table */ |
---|
96 | |
---|
97 | } while (more); |
---|
98 | |
---|
99 | fflush(fd); fclose(fd); |
---|
100 | |
---|
101 | MP_CloseLink(link); |
---|
102 | MP_ReleaseEnv(env); |
---|
103 | |
---|
104 | fprintf(stderr, "%s: All done!\n", argv[0]); |
---|
105 | } |
---|