Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / examples / IPC_SAP / TLI_SAP / db-server.cpp
blobde27c79613a48fde4f3c047c7327d325e5e69f3c
1 // Simple multi-threaded database server example.
3 #include "ace/OS_NS_stdio.h"
4 #include "ace/TLI_Acceptor.h"
5 #include "ace/Thread_Manager.h"
8 #if defined (ACE_HAS_THREADS) && defined (ACE_HAS_TLI)
10 // Global thread manager.
11 ACE_Thread_Manager thr_mgr;
13 void *
14 lookup_name (ACE_HANDLE handle)
16 enum
18 MAXLINE = 255,
19 EMPNAMELEN = 512
22 static struct
24 int emp_id;
25 const char *emp_name;
26 } employee_db[] =
28 {123, "John Wayne Bobbit"},
29 {124, "Woody Allen"},
30 {125, "O. J. Simpson"},
31 {126, "Bill Clinton"},
32 {127, "Rush Limbaugh"},
33 {128, "Michael Jackson"},
34 {129, "Kenneth Starr"},
35 {130, "Paula Jones"},
36 {131, "Monica Lewinsky"},
37 {132, "Marv Albert"},
38 {0, ""}
41 int flags;
42 int employee_id;
43 int index;
44 int found;
45 ACE_TLI_Stream stream;
46 char recvline[MAXLINE];
47 char sendline[MAXLINE];
49 ACE_DEBUG ((LM_DEBUG,
50 "stream handle = %d, thread id = %t\n",
51 handle));
52 stream.set_handle (handle);
54 ssize_t n = stream.recv (recvline, MAXLINE, &flags);
56 if (n == -1)
57 ACE_OS::t_error ("stream.recv error");
59 employee_id = ACE_OS::atoi (recvline);
60 found = 0;
62 for (index = 0; found == 0 && employee_db[index].emp_id; index++)
63 if (employee_id == employee_db[index].emp_id)
65 found = 1;
66 n = ACE_OS::sprintf (sendline,
67 "%s", employee_db[index].emp_name);
70 if (found == 0)
71 n = ACE_OS::sprintf (sendline, "%s", "ERROR");
73 if (stream.send (sendline, n + 1, 0) == -1)
74 ACE_OS::t_error ("stream.send error");
76 if (stream.sndrel () == -1)
77 ACE_OS::t_error ("stream.send error");
79 if (stream.close () == -1)
80 ACE_OS::t_error ("stream.close error");
82 return 0;
85 int
86 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
88 u_short port = argc > 1 ? ACE_OS::atoi (argv[1]) : ACE_DEFAULT_SERVER_PORT;
89 ACE_INET_Addr l_addr (port);
90 ACE_TLI_Acceptor server (l_addr, 1); // Create server, reuse addr if in use.
91 ACE_TLI_Stream new_stream;
93 // Wait for a connection from a client. This is an example of a
94 // concurrent server.
96 for (;;)
98 if (server.accept (new_stream) == -1)
99 ACE_OS::t_error ("server.accept error");
101 if (thr_mgr.spawn (ACE_THR_FUNC (lookup_name),
102 (void *) new_stream.get_handle (),
103 THR_DETACHED) == -1)
104 ACE_DEBUG ((LM_ERROR,
105 "server: can't create worker thread %d\n"));
108 ACE_NOTREACHED (return 0);
110 #else
111 #include <stdio.h>
112 int ACE_TMAIN (int, ACE_TCHAR *[])
114 ACE_ERROR_RETURN ((LM_ERROR,
115 "platform isn't configured to support TLI\n"),
118 #endif /* ACE_HAS_THREADS */