Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / tests / Svc_Handler_Test.cpp
blob0b1973d4f4787fcc729866a0d6375f050e1fc424
2 //=============================================================================
3 /**
4 * @file Svc_Handler_Test.cpp
6 * This tests illustrates the "buffering" strategy of the
7 * <ACE_Buffered_Svc_Handler>. This test also illustrates how the
8 * <ACE_FILE_IO> classes work.
10 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
12 //=============================================================================
15 #include "test_config.h"
16 #include "ace/OS_NS_string.h"
17 #include "ace/FILE_Connector.h"
18 #include "ace/Null_Condition.h"
19 #include "ace/Null_Mutex.h"
20 #include "ace/Svc_Handler.h"
21 #include "ace/Synch_Traits.h"
23 using SVC_HANDLER = ACE_Buffered_Svc_Handler<ACE_FILE_IO, ACE_NULL_SYNCH>;
25 static void
26 run_test (SVC_HANDLER &svc_handler,
27 size_t iterations)
29 // Create a whole slew of message blocks and pass them to the
30 // <svc_handler>.
31 for (size_t i = 0; i < iterations; i++)
33 ACE_Message_Block *mb = 0;
34 ACE_NEW (mb,
35 ACE_Message_Block (sizeof (ACE_TEXT("hello "))));
37 ACE_Message_Block *cb1 = 0;
38 ACE_NEW (cb1,
39 ACE_Message_Block (sizeof (ACE_TEXT("there\n"))));
41 ACE_Message_Block *cb2 = 0;
42 ACE_NEW (cb2,
43 ACE_Message_Block (sizeof (ACE_TEXT("there\n"))));
45 mb->copy ("hello ",
46 ACE_OS::strlen (ACE_TEXT("hello ")));
47 cb1->copy ("there ",
48 ACE_OS::strlen (ACE_TEXT("there ")));
49 mb->cont (cb1);
50 cb2->copy ("doug\n",
51 ACE_OS::strlen (ACE_TEXT("doug\n")));
52 cb1->cont (cb2);
54 // Note that this is a buffered call!
55 if (svc_handler.put (mb) == -1)
56 ACE_ERROR ((LM_ERROR,
57 ACE_TEXT ("%p\n"),
58 ACE_TEXT ("put")));
61 ACE_DEBUG ((LM_DEBUG,
62 ACE_TEXT ("final flush\n")));
64 // Make sure to flush everything out before we exit.
65 if (svc_handler.flush () == -1)
66 ACE_ERROR ((LM_ERROR,
67 ACE_TEXT ("%p\n"),
68 ACE_TEXT ("flush")));
71 int
72 run_main (int argc, ACE_TCHAR *argv[])
74 ACE_START_TEST (ACE_TEXT ("Svc_Handler_Test"));
76 size_t max_buffer_size = BUFSIZ;
77 size_t iterations = 10;
79 if (argc > 1)
80 max_buffer_size = ACE_OS::atoi (argv[1]);
81 if (argc > 2)
82 iterations = ACE_OS::atoi (argv[2]);
84 ACE_FILE_Connector connector;
85 ACE_FILE_IO file_io;
86 // Create a temporary filename.
88 // Open up a temp file.
89 if (connector.connect (file_io, ACE_sap_any_cast (ACE_FILE_Addr &)) == -1)
90 ACE_ERROR_RETURN ((LM_ERROR,
91 ACE_TEXT ("%p\n"),
92 ACE_TEXT ("connect")),
93 1);
94 // Find the name of the temp file
95 ACE_FILE_Addr file;
96 if (file_io.get_local_addr (file) == -1)
97 ACE_ERROR_RETURN ((LM_ERROR,
98 ACE_TEXT ("%p\n"),
99 ACE_TEXT ("get_local_addr")),
102 // Create the service handler and assign it <file_io> as its data
103 // sink.
104 SVC_HANDLER svc_handler (0,
107 max_buffer_size,
109 svc_handler.peer () = file_io;
111 // Run the test.
112 run_test (svc_handler, iterations);
114 file_io.close ();
116 // Open up the temp file.
117 if (connector.connect (file_io, file) == -1)
118 ACE_ERROR_RETURN ((LM_ERROR,
119 ACE_TEXT ("connect failed for %p\n"),
120 file.get_path_name ()),
122 char buf[ACE_MAXLOGMSGLEN + 1];
123 ACE_LOG_MSG->clr_flags (ACE_Log_Msg::VERBOSE_LITE);
125 ACE_FILE_Info info;
126 file_io.get_info (info);
127 ACE_DEBUG ((LM_DEBUG, ACE_TEXT("file size = %d\n"), info.size_));
129 for (ssize_t n_bytes; (n_bytes = file_io.recv (buf, ACE_MAXLOGMSGLEN)) > 0; )
131 buf[n_bytes] = '\0';
132 ACE_DEBUG ((LM_DEBUG, ACE_TEXT("%C"), buf));
135 ACE_DEBUG ((LM_DEBUG, ACE_TEXT("\n")));
137 file_io.close ();
139 if (file_io.unlink () == -1)
140 ACE_ERROR_RETURN ((LM_ERROR,
141 ACE_TEXT ("unlink failed for %p\n"),
142 file.get_path_name ()),
146 ACE_END_TEST;
147 return 0;