Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / POA / Default_Servant2 / client.cpp
blob0b529812c8ff7dc08360e0975ba52f377fbe9a17
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/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!!");
26 static int
27 parse_args (int argc, ACE_TCHAR *argv[])
29 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("dk:f:m:"));
30 int c;
32 while ((c = get_opts ()) != -1)
33 switch (c)
35 case 'd':
36 ++TAO_debug_level;
37 break;
38 case 'k':
39 iorfile = get_opts.opt_arg ();
40 break;
41 case 'f':
42 filename = get_opts.opt_arg ();
43 break;
44 case 'm':
45 message = get_opts.opt_arg ();
46 break;
47 case '?':
48 default:
49 ACE_ERROR_RETURN ((LM_ERROR,
50 "usage: %s"
51 "[-k <iorfile>]"
52 "[-f <filename>]"
53 "[-m <message>]"
54 "\n",
55 argv [0]),
56 -1);
59 if (iorfile == 0)
60 ACE_ERROR_RETURN ((LM_ERROR,
61 "Please specify the IOR for the servant"), -1);
63 // Indicates successful parsing of command line.
64 return 0;
67 int
68 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
70 try
72 // Initialize the ORB
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...
79 if (iorfile == 0)
80 return 0;
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",
87 iorfile),
88 -1);
89 ACE_Read_Buffer ior_buffer (input_file);
90 char *data = ior_buffer.read ();
91 if (data == 0)
92 ACE_ERROR_RETURN ((LM_ERROR,
93 "Unable to read ior\n"),
94 -1);
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),
107 O_CREAT | O_RDWR);
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",
127 result));
129 // close the file
130 fd->destroy ();
132 file_system->shutdown ();
134 orb->destroy ();
136 catch (const CORBA::Exception& ex)
138 ex._tao_print_exception ("Exception caught in main");
139 return -1;
142 return 0;