| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | |
| 4 | #include "t3std.h" |
| 5 | #include "os.h" |
| 6 | #include "osifc.h" |
| 7 | #include "osifcnet.h" |
| 8 | #include "vmnet.h" |
| 9 | #include "vmrun.h" |
| 10 | #include "vmglob.h" |
| 11 | |
| 12 | /* our set of events */ |
| 13 | static OS_Event *events[5]; |
| 14 | static OS_Event *quit_event; |
| 15 | |
| 16 | /* test thread */ |
| 17 | class TestThread: public OS_Thread |
| 18 | { |
| 19 | public: |
| 20 | TestThread(int n, unsigned long timeout) |
| 21 | { |
| 22 | /* remember our statistics */ |
| 23 | this->n = n; |
| 24 | this->timeout = timeout; |
| 25 | |
| 26 | /* add a reference to each event we use */ |
| 27 | quit_event->add_ref(); |
| 28 | for (int i = 0 ; i < 3 ; ++i) |
| 29 | events[i+n]->add_ref(); |
| 30 | } |
| 31 | |
| 32 | ~TestThread() |
| 33 | { |
| 34 | /* release our event references */ |
| 35 | quit_event->release_ref(); |
| 36 | for (int i = 0 ; i < 3 ; ++i) |
| 37 | events[i+n]->release_ref(); |
| 38 | } |
| 39 | |
| 40 | int n; |
| 41 | unsigned long timeout; |
| 42 | |
| 43 | void thread_main() |
| 44 | { |
| 45 | OS_Waitable *w[4]; |
| 46 | |
| 47 | /* |
| 48 | * set up our list of events - we wait for ABC + our thread index |
| 49 | * (so thread 0 waits for ABC, 1 for BCD, 2 for CDE) |
| 50 | */ |
| 51 | for (int i = 0 ; i < 3 ; ++i) |
| 52 | w[i] = events[i+n]; |
| 53 | |
| 54 | /* we also wait for the 'quit' event */ |
| 55 | w[3] = quit_event; |
| 56 | |
| 57 | printf("t%d: entering\n", n); |
| 58 | for (;;) |
| 59 | { |
| 60 | printf("t%d: waiting for %c, %c, %c\n", n, |
| 61 | 'A' + n, 'B' + n, 'C' + n); |
| 62 | int ret = OS_Waitable::multi_wait(4, w, timeout); |
| 63 | if (ret == OSWAIT_TIMEOUT) |
| 64 | printf("t%d: timeout\n", n); |
| 65 | else if (ret == OSWAIT_ERROR) |
| 66 | printf("t%d: error\n", n); |
| 67 | else if (ret >= OSWAIT_EVENT && ret <= OSWAIT_EVENT + 2) |
| 68 | printf("t%d: got event %c\n", n, 'A' + n + ret - OSWAIT_EVENT); |
| 69 | else if (ret == OSWAIT_EVENT + 3) |
| 70 | { |
| 71 | printf("t%d: got quit event\n", n); |
| 72 | break; |
| 73 | } |
| 74 | else |
| 75 | printf("t%d: unknown wait status %d\n", n, ret); |
| 76 | } |
| 77 | |
| 78 | /* done */ |
| 79 | printf("t2: exiting\n"); |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | /* socket handler thread */ |
| 84 | class SocketThread: public OS_Thread |
| 85 | { |
| 86 | public: |
| 87 | SocketThread(OS_Socket *s) |
| 88 | { |
| 89 | /* keep a reference on the socket */ |
| 90 | this->s = s; |
| 91 | s->add_ref(); |
| 92 | } |
| 93 | |
| 94 | ~SocketThread() |
| 95 | { |
| 96 | /* release our socket reference */ |
| 97 | s->close(); |
| 98 | s->release_ref(); |
| 99 | } |
| 100 | |
| 101 | OS_Socket *s; |
| 102 | |
| 103 | void thread_main() |
| 104 | { |
| 105 | printf("socket handler %lx: entering\n", (long)s); |
| 106 | |
| 107 | /* read the socket in non-blocking mode */ |
| 108 | s->set_non_blocking(); |
| 109 | |
| 110 | /* wait for the socket and the 'quit' event */ |
| 111 | OS_Waitable *w[2]; |
| 112 | w[0] = quit_event; |
| 113 | w[1] = s; |
| 114 | |
| 115 | /* read from the socket until it closes */ |
| 116 | for (int done = FALSE ; !done ; ) |
| 117 | { |
| 118 | /* wait for the next event */ |
| 119 | int ret = OS_Waitable::multi_wait(2, w); |
| 120 | switch (ret) |
| 121 | { |
| 122 | case OSWAIT_EVENT: |
| 123 | /* quit event */ |
| 124 | printf("socket handler %lx: got quit event\n", (long)s); |
| 125 | done = TRUE; |
| 126 | break; |
| 127 | |
| 128 | case OSWAIT_EVENT + 1: |
| 129 | /* socket event - reset the event */ |
| 130 | s->reset_event(); |
| 131 | |
| 132 | /* read the socket until we run out incoming data */ |
| 133 | for (;;) |
| 134 | { |
| 135 | char buf[128]; |
| 136 | int len = s->recv(buf, sizeof(buf)); |
| 137 | if (len == OS_SOCKET_ERROR) |
| 138 | { |
| 139 | if (s->last_error() == OS_EWOULDBLOCK) |
| 140 | { |
| 141 | printf("socket handler %lx: WOULDBLOCK\n", |
| 142 | (long)s); |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | printf("socket handler %lx: recv error %d\n", |
| 147 | (long)s, s->last_error()); |
| 148 | done = TRUE; |
| 149 | } |
| 150 | break; |
| 151 | } |
| 152 | else if (len == 0) |
| 153 | { |
| 154 | printf("socket handler %lx: connection closed\n", |
| 155 | (long)s); |
| 156 | done = TRUE; |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | printf("socket handler %lx: recv bytes [%.*s]\n", |
| 161 | (long)s, len, buf); |
| 162 | } |
| 163 | break; |
| 164 | |
| 165 | default: |
| 166 | printf("socket handler %lx: error in wait: %d\n", |
| 167 | (long)s, ret); |
| 168 | done = TRUE; |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | printf("socket handler %lx: exiting\n", (long)s); |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | /* listener thread */ |
| 178 | class ListenerThread: public OS_Thread |
| 179 | { |
| 180 | public: |
| 181 | ListenerThread(const char *hostname) |
| 182 | { |
| 183 | this->hostname = hostname; |
| 184 | quit_event->add_ref(); |
| 185 | } |
| 186 | |
| 187 | ~ListenerThread() |
| 188 | { |
| 189 | quit_event->release_ref(); |
| 190 | } |
| 191 | |
| 192 | const char *hostname; |
| 193 | |
| 194 | void thread_main() |
| 195 | { |
| 196 | printf("listener: entering\n"); |
| 197 | |
| 198 | /* set up our listener */ |
| 199 | OS_Listener *l = new OS_Listener(); |
| 200 | if (l->open(hostname)) |
| 201 | { |
| 202 | /* show our port information */ |
| 203 | char *ip; |
| 204 | int port; |
| 205 | if (l->get_local_addr(ip, port)) |
| 206 | { |
| 207 | printf("listener: listening on %s:%d\n", ip, port); |
| 208 | lib_free_str(ip); |
| 209 | } |
| 210 | else |
| 211 | printf("listener: unable to get local address information\n"); |
| 212 | |
| 213 | /* set non-blocking mode */ |
| 214 | l->set_non_blocking(); |
| 215 | |
| 216 | /* wait for connections; stop on the 'quit' signal */ |
| 217 | OS_Waitable *w[2]; |
| 218 | w[0] = quit_event; |
| 219 | w[1] = l; |
| 220 | |
| 221 | /* keep going until we get the 'quit' signal */ |
| 222 | for (;;) |
| 223 | { |
| 224 | /* wait for an incoming connection */ |
| 225 | int ret = OS_Waitable::multi_wait(2, w); |
| 226 | if (ret == OSWAIT_EVENT) |
| 227 | { |
| 228 | /* quit event */ |
| 229 | printf("listener: got quit event\n"); |
| 230 | break; |
| 231 | } |
| 232 | else if (ret == OSWAIT_EVENT + 1) |
| 233 | { |
| 234 | /* a request is ready */ |
| 235 | printf("listener: port is ready\n"); |
| 236 | |
| 237 | /* reset the listener event */ |
| 238 | l->reset_event(); |
| 239 | |
| 240 | /* check for a request */ |
| 241 | OS_Socket *s = l->accept(); |
| 242 | if (s == 0) |
| 243 | { |
| 244 | /* no request - check for a WOULDBLOCK condition */ |
| 245 | if (l->last_error() == OS_EWOULDBLOCK) |
| 246 | { |
| 247 | /* no request is ready - keep waiting */ |
| 248 | printf("listener: EWOULDBLOCK\n"); |
| 249 | continue; |
| 250 | } |
| 251 | |
| 252 | /* for any other error, abort */ |
| 253 | printf("listener: error %d in accept()\n", |
| 254 | l->last_error()); |
| 255 | break; |
| 256 | } |
| 257 | |
| 258 | /* got a request */ |
| 259 | printf("listener: got a connection request\n"); |
| 260 | |
| 261 | /* launch a thread to read the socket */ |
| 262 | OS_Thread *t = new SocketThread(s); |
| 263 | if (!t->launch()) |
| 264 | printf("listener: failed to launch socket thread\n"); |
| 265 | |
| 266 | /* done with the thread and socket */ |
| 267 | t->release_ref(); |
| 268 | s->release_ref(); |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | printf("listener: error in multi_wait: %d\n", ret); |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* done with the listener port */ |
| 278 | l->close(); |
| 279 | l->release_ref(); |
| 280 | } |
| 281 | else |
| 282 | printf("listener: listen() failed\n"); |
| 283 | |
| 284 | /* done */ |
| 285 | printf("listener: exiting\n"); |
| 286 | } |
| 287 | }; |
| 288 | |
| 289 | /* dummy entrypoint for HTTPRequest intrinsic class construction */ |
| 290 | vm_obj_id_t TadsHttpRequest::prep_event_obj(VMG_ int *, int *) |
| 291 | { |
| 292 | return VM_INVALID_OBJ; |
| 293 | } |
| 294 | |
| 295 | /* dummy entrypoints for exception throwing from vmnet.cpp */ |
| 296 | void CVmRun::push_string(VMG_ const char *, size_t) { } |
| 297 | void CVmRun::push_stringf(VMG_ const char *, ...) { } |
| 298 | void CVmRun::throw_new_rtesub(VMG_ vm_obj_id_t, uint, int) { } |
| 299 | |
| 300 | /* main program entrypoint */ |
| 301 | int main(int argc, char **argv) |
| 302 | { |
| 303 | OS_Thread *t[4]; |
| 304 | int i; |
| 305 | static unsigned long timeouts[] = { 10000, 27000, 71000 }; |
| 306 | const char *hostname = "localhost"; |
| 307 | |
| 308 | /* show usage */ |
| 309 | printf("usage: osnettest1 <hostname>\n" |
| 310 | "hostname defaults to 'localhost' if '-' or missing\n"); |
| 311 | |
| 312 | /* get the listener host name, if present */ |
| 313 | if (argc >= 2) |
| 314 | { |
| 315 | hostname = argv[1]; |
| 316 | ++argv; |
| 317 | --argc; |
| 318 | |
| 319 | if (strcmp(hostname, "-") == 0) |
| 320 | hostname = "localhost"; |
| 321 | } |
| 322 | |
| 323 | /* initialize the network/thread package */ |
| 324 | os_net_init(0); |
| 325 | |
| 326 | /* create the events */ |
| 327 | printf("main: creating events\n"); |
| 328 | for (i = 0 ; i < 5 ; ++i) |
| 329 | events[i] = new OS_Event(FALSE); |
| 330 | |
| 331 | /* |
| 332 | * create the 'quit' event - this is manual-reset, because once it's |
| 333 | * signaled, we want all threads to see the signal and quit |
| 334 | */ |
| 335 | quit_event = new OS_Event(TRUE); |
| 336 | |
| 337 | /* launch the threads */ |
| 338 | printf("main: launching threads\n"); |
| 339 | for (i = 0 ; i < 3 ; ++i) |
| 340 | { |
| 341 | t[i] = new TestThread(i, timeouts[i]); |
| 342 | if (!t[i]->launch()) |
| 343 | printf("thread %i failed to launch\n" ,i); |
| 344 | } |
| 345 | |
| 346 | /* launch the listener thread */ |
| 347 | t[i] = new ListenerThread(hostname); |
| 348 | if (!t[i]->launch()) |
| 349 | printf("listener thread failed to launch\n"); |
| 350 | ++i; |
| 351 | |
| 352 | /* show instructions */ |
| 353 | printf("Enter one or more events to fire, A-E. For example, to fire\n" |
| 354 | "A and B, type AB.\n" |
| 355 | "In addition, Q is the 'Quit' event.\n\n"); |
| 356 | |
| 357 | /* process input */ |
| 358 | while (!quit_event->test()) |
| 359 | { |
| 360 | char buf[128]; |
| 361 | if (fgets(buf, sizeof(buf), stdin) == 0) |
| 362 | break; |
| 363 | |
| 364 | for (char *p = buf ; *p != '\0' ; ++p) |
| 365 | { |
| 366 | if (*p >= 'A' && *p <= 'E') |
| 367 | events[*p - 'A']->signal(); |
| 368 | if (*p >= 'a' && *p <= 'e') |
| 369 | events[*p - 'a']->signal(); |
| 370 | |
| 371 | if (*p == 'Q' || *p == 'q') |
| 372 | quit_event->signal(); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /* release our events */ |
| 377 | quit_event->release_ref(); |
| 378 | for (i = 0 ; i < 5 ; ++i) |
| 379 | events[i]->release_ref(); |
| 380 | |
| 381 | /* wait for threads to exit */ |
| 382 | for (i = 0 ; i < 4 ; ++i) |
| 383 | { |
| 384 | printf("main: waiting for thread %d to exit...\n", i); |
| 385 | t[i]->wait(); |
| 386 | t[i]->release_ref(); |
| 387 | printf("main: thread %d exited!\n", i); |
| 388 | } |
| 389 | |
| 390 | /* clean up the network/thread package */ |
| 391 | os_net_cleanup(); |
| 392 | } |
| 393 |