2 //=============================================================================
6 * A client program for the File IDL module
8 * @author Irfan Pyarali
10 //=============================================================================
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
;
31 parse_args (int argc
, ACE_TCHAR
**argv
)
33 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("t:dk:f:i:"));
36 while ((c
= get_opts ()) != -1)
43 iorfile
= get_opts
.opt_arg ();
46 filename
= get_opts
.opt_arg ();
49 NUM_THREADS
= ACE_OS::atoi (get_opts
.opt_arg ());
52 iterations
= ACE_OS::atoi (get_opts
.opt_arg ());
56 ACE_ERROR_RETURN ((LM_ERROR
,
69 ACE_ERROR_RETURN ((LM_ERROR
,
70 "Please specify the IOR for the servant"), -1);
72 // Indicates successful parsing of command line.
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
82 for (int i
= 0; i
< 100; ++i
)
86 object
->_non_existent ();
88 catch (const CORBA::TRANSIENT
& )
92 catch (const CORBA::Exception
&)
94 // Rethrow any other exceptions.
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
),
117 for( int i
= 0; i
< iterations
; ++i
)
119 //seek to the beginning of the file
121 ACE_DEBUG((LM_DEBUG
,"Making request number %d\n",i
));
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);
134 catch (const CORBA::Exception
& ex
)
136 ex
._tao_print_exception ("Exception caught in main");
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...
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",
165 ACE_Read_Buffer
ior_buffer (input_file
);
166 char *data
= ior_buffer
.read ();
168 ACE_ERROR_RETURN ((LM_ERROR
,
169 "Unable to read ior\n"),
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
),
179 THR_NEW_LWP
| THR_DETACHED
) == -1)
181 ACE_ERROR ((LM_ERROR
,
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");