Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / File_IO / client.cpp
bloba68ed75cba69459579e48f901f816f23ba3f60e3
2 //=============================================================================
3 /**
4 * @file client.cpp
6 * A client program for the File IDL module
8 * @author Irfan Pyarali
9 */
10 //=============================================================================
13 #include "FileC.h"
14 #include "tao/debug.h"
15 #include "ace/Get_Opt.h"
16 #include "ace/Read_Buffer.h"
17 #include "ace/SString.h"
18 #include "ace/Thread_Manager.h"
19 #include "ace/OS_NS_fcntl.h"
20 #include "ace/OS_NS_unistd.h"
22 static const ACE_TCHAR *iorfile = ACE_TEXT("ior");
23 static const ACE_TCHAR *filename = ACE_TEXT("big.txt");
25 static int NUM_THREADS = 4;
26 static int iterations = 100;
28 static CORBA::ORB_var orb;
30 static int
31 parse_args (int argc, ACE_TCHAR **argv)
33 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("t:dk:f:i:"));
34 int c;
36 while ((c = get_opts ()) != -1)
37 switch (c)
39 case 'd':
40 TAO_debug_level++;
41 break;
42 case 'k':
43 iorfile = get_opts.opt_arg ();
44 break;
45 case 'f':
46 filename = get_opts.opt_arg ();
47 break;
48 case 't':
49 NUM_THREADS = ACE_OS::atoi (get_opts.opt_arg ());
50 break;
51 case 'i':
52 iterations = ACE_OS::atoi (get_opts.opt_arg ());
53 break;
54 case '?':
55 default:
56 ACE_ERROR_RETURN ((LM_ERROR,
57 "usage: %s"
58 "[-k <iorfile>]"
59 "[-f <filename>]"
60 "[-m <message>]"
61 "[-t <threads>]"
62 "[-i <iterations>]"
63 "\n",
64 argv [0]),
65 -1);
68 if (iorfile == 0)
69 ACE_ERROR_RETURN ((LM_ERROR,
70 "Please specify the IOR for the servant"), -1);
72 // Indicates successful parsing of command line.
73 return 0;
76 void
77 validate_connection (CORBA::Object_ptr object)
79 // Try to validate the connection several times, ignoring transient
80 // exceptions. If the connection can still not be setup, return
81 // failure.
82 for (int i = 0; i < 100; ++i)
84 try
86 object->_non_existent ();
88 catch (const CORBA::TRANSIENT& )
90 // Ignore...
92 catch (const CORBA::Exception&)
94 // Rethrow any other exceptions.
95 throw;
100 static void *
101 MTTEST (void *args)
103 ACE_CString &ior = *(ACE_CString*)args;
106 CORBA::Object_var object = orb->string_to_object (ior.c_str ());
108 validate_connection (object.in ());
110 // Narrow the object reference to a File::System
111 File::System_var file_system = File::System::_narrow (object.in ());
113 // Create the file filename i.e "test"
114 File::Descriptor_var fd = file_system->open (ACE_TEXT_ALWAYS_CHAR(filename),
115 O_RDONLY);
117 for( int i = 0; i < iterations; ++i)
119 //seek to the beginning of the file
120 #if 0
121 ACE_DEBUG((LM_DEBUG,"Making request number %d\n",i));
122 #endif /*if 0*/
124 fd->lseek (0, SEEK_SET);
126 // Read back the written message
127 // Twice the size of the socket buffer
128 File::Descriptor::DataBuffer_var data_received = fd->read (128*1024);
131 // close the file
132 fd->destroy ();
134 catch (const CORBA::Exception& ex)
136 ex._tao_print_exception ("Exception caught in main");
137 return 0;
140 return 0;
144 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
148 // Initialize the ORB
149 orb = CORBA::ORB_init (argc, argv);
151 // Parse the command-line arguments to get the IOR
152 parse_args (argc, argv);
154 // parse args should catch this, but just in case...
155 if (iorfile == 0)
156 return 0;
158 // Read the file, and get the IOR
159 ACE_HANDLE input_file = ACE_OS::open (iorfile, 0);
160 if (input_file == ACE_INVALID_HANDLE)
161 ACE_ERROR_RETURN ((LM_ERROR,
162 "Cannot open input file for reading IOR: %s\n",
163 iorfile),
164 -1);
165 ACE_Read_Buffer ior_buffer (input_file);
166 char *data = ior_buffer.read ();
167 if (data == 0)
168 ACE_ERROR_RETURN ((LM_ERROR,
169 "Unable to read ior\n"),
170 -1);
172 ACE_CString ior = data;
173 ior_buffer.alloc ()->free (data);
174 ACE_OS::close (input_file);
176 if (ACE_Thread_Manager::instance ()->spawn_n (NUM_THREADS,
177 ACE_THR_FUNC (MTTEST),
178 &ior,
179 THR_NEW_LWP | THR_DETACHED) == -1)
181 ACE_ERROR ((LM_ERROR,
182 ACE_TEXT ("%p\n%a"),
183 ACE_TEXT ("thread create failed")));
185 ACE_Thread_Manager::instance()->wait();
187 catch (const CORBA::Exception& ex)
189 ex._tao_print_exception ("Exception caught in main");
190 return -1;
193 return 0;