2 //=============================================================================
6 * A client program for the File IDL module
8 * @author Irfan Pyarali
10 //=============================================================================
14 #include "tao/debug.h"
15 #include "ace/streams.h"
16 #include "ace/Get_Opt.h"
17 #include "ace/Read_Buffer.h"
18 #include "ace/SString.h"
19 #include "ace/OS_NS_fcntl.h"
20 #include "ace/OS_NS_unistd.h"
22 static const ACE_TCHAR
*iorfile
= 0;
23 static const ACE_TCHAR
*filename
= ACE_TEXT ("test");
24 static const ACE_TCHAR
*message
= ACE_TEXT ("POA rules!!");
27 parse_args (int argc
, ACE_TCHAR
*argv
[])
29 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("dk:f:m:"));
32 while ((c
= get_opts ()) != -1)
39 iorfile
= get_opts
.opt_arg ();
42 filename
= get_opts
.opt_arg ();
45 message
= get_opts
.opt_arg ();
49 ACE_ERROR_RETURN ((LM_ERROR
,
60 ACE_ERROR_RETURN ((LM_ERROR
,
61 "Please specify the IOR for the servant"), -1);
63 // Indicates successful parsing of command line.
68 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
73 CORBA::ORB_var orb
= CORBA::ORB_init (argc
, argv
);
75 // Parse the command-line arguments to get the IOR
76 parse_args (argc
, argv
);
78 // parse args should catch this, but just in case...
82 // Read the file, and get the IOR
83 ACE_HANDLE input_file
= ACE_OS::open (iorfile
, 0);
84 if (input_file
== ACE_INVALID_HANDLE
)
85 ACE_ERROR_RETURN ((LM_ERROR
,
86 "Cannot open input file %s for reading IOR\n",
89 ACE_Read_Buffer
ior_buffer (input_file
);
90 char *data
= ior_buffer
.read ();
92 ACE_ERROR_RETURN ((LM_ERROR
,
93 "Unable to read ior\n"),
96 ACE_CString ior
= data
;
97 ior_buffer
.alloc ()->free (data
);
98 ACE_OS::close (input_file
);
100 CORBA::Object_var object
= orb
->string_to_object (ior
.c_str ());
102 // Narrow the object reference to a File::System
103 File::System_var file_system
= File::System::_narrow (object
.in ());
105 // Creat the file filename i.e "test"
106 File::Descriptor_var fd
= file_system
->open (ACE_TEXT_ALWAYS_CHAR (filename
),
109 int message_length
= ACE_OS::strlen (message
) + 1;
110 CORBA::Octet
*buffer
= File::Descriptor::DataBuffer::allocbuf (message_length
);
111 ACE_OS::strcpy ((char *) buffer
, ACE_TEXT_ALWAYS_CHAR (message
));
112 File::Descriptor::DataBuffer
data_sent (message_length
, message_length
, buffer
, 1);
114 // write the message to the file
115 fd
->write (data_sent
);
117 // seek to the beginning of the file
118 fd
->lseek (0, SEEK_SET
);
120 // Read back the written message
121 File::Descriptor::DataBuffer_var data_received
= fd
->read (message_length
);
123 char *result
= (char *) &data_received
[0];
125 // print the read message
126 ACE_DEBUG((LM_DEBUG
, "%C\n",
132 file_system
->shutdown ();
136 catch (const CORBA::Exception
& ex
)
138 ex
._tao_print_exception ("Exception caught in main");