Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / IPC_SAP / ATM_SAP / CPP-server.cpp
blob57acb4a79305d05431e2fa597d4bd04df9640d76
1 #include "ace/OS_main.h"
2 #include "ace/ATM_Acceptor.h"
3 #include "ace/ATM_Addr.h"
4 #include "ace/High_Res_Timer.h"
5 #include "ace/Log_Msg.h"
8 #if defined (ACE_HAS_ATM)
9 // ACE_ATM Server
11 int
12 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
14 ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);
16 //unsigned char selector = ACE_ATM_Addr::DEFAULT_SELECTOR;
17 //int selector_specified = 0;
19 if (argc > 2)
20 ACE_ERROR_RETURN ((LM_ERROR,
21 "Usage: %s [selector]\n",
22 argv[0]),
23 1);
25 // Create a server address.
26 ACE_ATM_Addr addr;
27 //if (selector_specified)
28 unsigned char selector = ( argc == 2 ) ? ACE_OS::atoi( argv[ 1 ]) : ACE_ATM_Addr::DEFAULT_SELECTOR;
29 addr.set_selector( selector );
30 ACE_OS::printf( "ATM_Server: selector changed to %d\n", addr.get_selector());
33 // Create a server, reuse the addr.
34 ACE_ATM_Acceptor peer_acceptor;
35 ACE_ATM_Params params;
37 // Not sure why but reuse_addr set to true/1 causes problems for
38 // FORE/XTI/ATM - this is now handled in ACE_ATM_Acceptor::open()
40 ACE_HANDLE ret = peer_acceptor.open (addr, 5, params);
41 if ( ret == ACE_INVALID_HANDLE )
42 ACE_ERROR_RETURN ((LM_ERROR,
43 "%p\n",
44 "open"),
45 -1);
47 ACE_ATM_Stream new_stream;
48 ACE_ATM_Addr local_addr;
50 local_addr.set_selector( selector );
51 peer_acceptor.get_local_addr( local_addr );
53 ACE_DEBUG ((LM_DEBUG,
54 "starting server at address %s\n",
55 local_addr.addr_to_string ()));
57 // Performs the iterative server activities
58 char buf[BUFSIZ];
59 ACE_High_Res_Timer timer;
60 int total;
61 ACE_Time_Value tv;
62 double real_time;
63 double actual_rate;
65 for (;;)
67 // Create a new ACE_ATM_Stream endpoint (note automatic restart
68 // if errno == EINTR).
69 ACE_OS::printf( "ATM_Server: expecting clients\n" );
71 if (peer_acceptor.accept (new_stream,
72 &addr,
73 &timeout) == -1)
75 ACE_ERROR ((LM_ERROR,
76 "%p\n",
77 "accept"));
78 continue;
81 ACE_OS::printf( "ATM_Server: got a connection\n" );
83 ACE_UINT16 vpi, vci;
84 vpi = vci = 0;
85 // This has problem on PMP connections on NT
86 //new_stream.get_vpi_vci(vpi, vci);
87 ACE_DEBUG ((LM_DEBUG,
88 "connected to VPI %d VCI %d\n",
89 vpi, vci));
91 ACE_OS::printf( "ATM_Server: connection accepted\n" );
93 ACE_DEBUG ((LM_DEBUG,
94 "client %s connected\n",
95 addr.addr_to_string ()));
96 ACE_DEBUG ((LM_DEBUG,
97 "client %s connected to host\n",
98 new_stream.get_peer_name ()));
100 // Read data from client (terminate on error).
102 int recvd = 0;
104 for ( ;; )
106 total = 0;
107 timer.start_incr();
109 for (int r_bytes;
110 (r_bytes = new_stream.recv (buf, sizeof buf, 0)) > 0; )
112 // ACE_OS::printf( "ATM_Server: received %dB\n", r_bytes );
114 // if (ACE_OS::write (ACE_STDOUT,
115 // buf,
116 // r_bytes) != r_bytes)
117 // ACE_ERROR ((LM_ERROR,
118 // "%p\n",
119 // "ACE::send_n"));
120 total += r_bytes;
122 if ( total > 10000000 )
123 break;
126 timer.stop_incr();
127 timer.elapsed_time_incr( tv );
128 real_time = tv.sec() * ACE_ONE_SECOND_IN_USECS + tv.usec();
129 recvd += total;
130 actual_rate = ( double )recvd * ( double )8 / real_time;
132 ACE_DEBUG ((LM_DEBUG,
133 ACE_TEXT ("(%t) bytes = %d, usec = %f, rate = %0.00f Mbps\n"),
134 recvd,
135 real_time,
136 actual_rate < 0 ? 0 : actual_rate ));
139 // Close new endpoint (listening endpoint stays open).
140 if (new_stream.close () == -1)
141 ACE_ERROR ((LM_ERROR,
142 "%p\n",
143 "close"));
146 /* NOTREACHED */
147 return 0;
149 #else
150 int ACE_TMAIN (int, ACE_TCHAR *[])
152 ACE_ERROR_RETURN ((LM_ERROR,
153 "your platform isn't configured to support ATM\n"),
156 #endif /* ACE_HAS_ATM */