]>
Commit | Line | Data |
---|---|---|
0fa91a0a | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
6 | * Copyright (C) 1987 by the Massachusetts Institute of Technology | |
7 | * | |
8 | * | |
9 | * SMS server process. | |
10 | * | |
11 | * Most of this is stolen from ../gdb/tsr.c | |
12 | * | |
13 | * You are in a maze of twisty little finite automata, all different. | |
14 | * Let the reader beware. | |
15 | * | |
16 | * $Log$ | |
17 | * Revision 1.1 1987-05-31 22:06:56 wesommer | |
18 | * Initial revision | |
19 | * | |
20 | */ | |
21 | ||
22 | #ifndef lint | |
23 | static char *rcsid_sms_main_c = "$Header$"; | |
24 | #endif lint | |
25 | ||
26 | #include "sms_private.h" | |
27 | #include "sms_server.h" | |
28 | #include <strings.h> | |
29 | extern char *malloc(); | |
30 | ||
31 | CONNECTION newconn, listencon; | |
32 | int nclients; | |
33 | ||
34 | client **clients; | |
35 | ||
36 | OPERATION listenop; | |
37 | ||
38 | LIST_OF_OPERATIONS op_list; | |
39 | ||
40 | /* | |
41 | * What is this?? | |
42 | */ | |
43 | char otherside[100]; | |
44 | int othersize; | |
45 | TUPLE client_tuple; /* client request goes */ | |
46 | /* here */ | |
47 | ||
48 | ||
49 | main(argc, argv) | |
50 | { | |
51 | int i; | |
52 | ||
53 | gdb_init(); | |
54 | nclients = 0; | |
55 | clients = (client **) malloc(0); | |
56 | ||
57 | do_listen(index(SMS_GDB_SERV, ':') + 1); /* XXX */ | |
58 | ||
59 | op_list = create_list_of_operations(1, listenop); | |
60 | ||
61 | fprintf(stderr, "sms server on the air..\n"); | |
62 | ||
63 | for EVER { | |
64 | op_select_any(op_list, 0, NULL, NULL, NULL, NULL); | |
65 | fprintf(stderr, "tick\n"); | |
66 | if (OP_DONE(listenop)) { | |
67 | new_connection(); | |
68 | } | |
69 | for (i=0; i<nclients; i++) { | |
70 | if (OP_DONE(clients[i]->pending_op)) { | |
71 | do_client(clients[i]); | |
72 | } | |
73 | } | |
74 | ||
75 | } | |
76 | } | |
77 | ||
78 | new_connection() | |
79 | { | |
80 | register client *cp = (client *)malloc(sizeof *cp); | |
81 | ||
82 | fprintf(stderr, "New connection coming in\n"); | |
83 | /* | |
84 | * Make sure there's been no error | |
85 | */ | |
86 | if(OP_STATUS(listenop) != OP_COMPLETE || | |
87 | newconn == NULL) { | |
88 | fprintf(stderr,"Error on listening operation\n"); | |
89 | exit(8); | |
90 | } | |
91 | ||
92 | /* Add a new client to the array.. */ | |
93 | clist_append(cp); | |
94 | ||
95 | /* | |
96 | * Set up the new connection and reply to the client | |
97 | */ | |
98 | ||
99 | cp->state = CL_STARTING; | |
100 | cp->action = CL_ACCEPT; | |
101 | cp->con = newconn; | |
102 | newconn = NULL; | |
103 | ||
104 | cp->pending_op = create_operation(); | |
105 | reset_operation(cp->pending_op); | |
106 | oplist_append(&op_list, cp->pending_op); | |
107 | ||
108 | start_replying_to_client(cp->pending_op, cp->con, GDB_ACCEPTED, | |
109 | "", ""); | |
110 | ||
111 | #ifdef notdef | |
112 | to do this in a production server is moronic; | |
113 | ||
114 | if (nextcl == MAXCLIENTS) { | |
115 | fprintf(stderr,"Too many clients, giving up\n"); | |
116 | exit(8); | |
117 | } | |
118 | #endif notdef | |
119 | ||
120 | /* | |
121 | * Start listening again | |
122 | */ | |
123 | reset_operation(listenop); | |
124 | othersize = sizeof(otherside); | |
125 | ||
126 | start_accepting_client(listencon, listenop, &newconn, | |
127 | (char *)otherside, | |
128 | &othersize, &client_tuple); | |
129 | } | |
130 | ||
131 | int | |
132 | do_listen(service) | |
133 | char *service; | |
134 | { | |
135 | listencon = create_listening_connection(service); | |
136 | ||
137 | if (listencon == NULL) { | |
138 | perror("sms"); | |
139 | fprintf(stderr,"sms: could not create listening connection\n"); | |
140 | exit (4); | |
141 | } | |
142 | ||
143 | listenop = create_operation(); | |
144 | ||
145 | othersize = sizeof(otherside); | |
146 | ||
147 | start_accepting_client(listencon, listenop, &newconn, | |
148 | (char *)otherside, | |
149 | &othersize, &client_tuple); | |
150 | ||
151 | } | |
152 | ||
153 | /* | |
154 | * Welcome to the (finite state) machine (highest level). | |
155 | */ | |
156 | do_client(cp) | |
157 | client *cp; | |
158 | { | |
159 | if (OP_STATUS(cp->pending_op) == OP_CANCELLED) { | |
160 | fprintf(stderr,"dropping connection..\n"); | |
161 | reset_operation(cp->pending_op); | |
162 | cp->state=CL_DEAD; | |
163 | cp->action=0; | |
164 | /* XXX should delete client from array */ | |
165 | return; | |
166 | } | |
167 | switch (cp->action) { | |
168 | case CL_ACCEPT: | |
169 | case CL_SEND: | |
170 | /* Start recieving next request */ | |
171 | gdb_inop(cp->pending_op, sms_start_recv, &cp->args, NULL); | |
172 | gdb_qop(cp->con, CON_INPUT, cp->pending_op); | |
173 | cp->action = CL_RECEIVE; | |
174 | break; | |
175 | case CL_RECEIVE: | |
176 | /* Data is here. Process it & start it heading back */ | |
177 | do_call(cp); /* This may block for a while. */ | |
178 | gdb_inop(cp->pending_op, sms_start_send, &cp->reply, NULL); | |
179 | gdb_qop(cp->con, CON_OUTPUT, cp->pending_op); | |
180 | cp->action = CL_SEND; | |
181 | break; | |
182 | } | |
183 | } | |
184 | ||
185 | do_call(cl) | |
186 | client *cl; | |
187 | { | |
188 | fprintf(stderr, "Handling call\n"); | |
189 | /* for now, just echo the stuff back */ | |
190 | cl->reply=cl->args; | |
191 | } | |
192 | ||
193 | /* | |
194 | * Add a new client to the known clients. | |
195 | */ | |
196 | clist_append(cp) | |
197 | client *cp; | |
198 | { | |
199 | client **clients_n; | |
200 | ||
201 | nclients++; | |
202 | clients_n = (client **)malloc(nclients * sizeof(client *)); | |
203 | bcopy((char *)clients, (char *)clients_n, (nclients-1)*sizeof(cp)); | |
204 | clients_n[nclients-1] = cp; | |
205 | free((char *)clients); | |
206 | clients = clients_n; | |
207 | clients_n = NULL; | |
208 | } | |
209 | ||
210 | /* | |
211 | * Grr. This isn't nice. | |
212 | */ | |
213 | ||
214 | oplist_append(oplp, op) | |
215 | LIST_OF_OPERATIONS *oplp; | |
216 | OPERATION op; | |
217 | { | |
218 | int count = (*oplp)->count+1; | |
219 | LIST_OF_OPERATIONS newlist = (LIST_OF_OPERATIONS) | |
220 | db_alloc(size_of_list_of_operations(count)); | |
221 | bcopy((char *)(*oplp), (char *)newlist, | |
222 | size_of_list_of_operations((*oplp)->count)); | |
223 | if (newlist->count != count-1) abort(); /*XXX*/ | |
224 | newlist->count++; | |
225 | newlist->op[count-1] = op; | |
226 | db_free((*oplp), size_of_list_of_operations(count-1)); | |
227 | (*oplp) = newlist; | |
228 | } | |
229 |