Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / POA / DSI / client.cpp
blob589b90aa9582f152bcf3b5b54f783bf3cfae428b
2 //=============================================================================
3 /**
4 * @file client.cpp
6 * A client program for the Database IDL module
8 * @author Irfan Pyarali
9 */
10 //=============================================================================
13 #include "ace/Get_Opt.h"
14 #include "ace/Read_Buffer.h"
15 #include "DatabaseC.h"
16 #include "ace/OS_NS_fcntl.h"
17 #include "ace/OS_NS_unistd.h"
18 #include "ace/OS_NS_string.h"
20 static char *IOR = 0;
21 static const ACE_TCHAR *IOR_file = 0;
22 static int shutdown_server = 0;
24 static int
25 parse_args (int argc, ACE_TCHAR **argv)
27 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("xk:f:"));
28 int c;
30 while ((c = get_opts ()) != -1)
31 switch (c)
33 case 'k':
34 IOR = ACE_OS::strdup (ACE_TEXT_ALWAYS_CHAR (get_opts.opt_arg ()));
35 break;
37 case 'f':
38 IOR_file = get_opts.opt_arg ();
39 break;
41 case 'x':
42 shutdown_server = 1;
43 break;
45 case '?':
46 default:
47 ACE_ERROR_RETURN ((LM_ERROR,
48 "usage: %s"
49 "-k IOR "
50 "-f IOR file "
51 "-x [for shutting down the server] "
52 "\n",
53 argv [0]),
54 -1);
57 if (IOR == 0 && IOR_file == 0)
58 ACE_ERROR_RETURN ((LM_ERROR,
59 "Please specify the IOR or IOR_file for the servant\n"),
60 -1);
62 // Indicates successful parsing of command line.
63 return 0;
66 int
67 read_IOR_from_file ()
69 // Open the file for reading.
70 ACE_HANDLE f_handle =
71 ACE_OS::open (IOR_file, 0);
73 if (f_handle == ACE_INVALID_HANDLE)
74 ACE_ERROR_RETURN ((LM_ERROR,
75 "Unable to open %s for reading\n",
76 IOR_file),
77 -1);
79 ACE_Read_Buffer ior_buffer (f_handle);
80 char *data = ior_buffer.read ();
82 if (data == 0)
83 ACE_ERROR_RETURN ((LM_ERROR,
84 "Unable to read ior\n"),
85 -1);
87 IOR = ACE_OS::strdup (data);
88 ior_buffer.alloc ()->free (data);
90 ACE_OS::close (f_handle);
92 return 0;
95 int
96 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
98 char str [255];
99 // Initialize the ORB
102 ACE_OS::strcpy (str,
103 "CORBA::ORB_init");
104 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
106 // Parse the command-line arguments to get the location of the
107 // IOR
108 if (parse_args (argc, argv) == -1)
109 return -1;
111 if (IOR == 0)
113 int result = read_IOR_from_file ();
114 if (result != 0)
115 ACE_ERROR_RETURN ((LM_ERROR,
116 "Cannot read IOR from %s\n",
117 IOR_file),
118 -1);
121 ACE_OS::strcpy (str,
122 "CORBA::ORB::string_to_object");
124 // Get the object reference with the IOR
125 CORBA::Object_var object = orb->string_to_object (IOR);
127 ACE_OS::strcpy (str,
128 "Database::Agent::_narrow");
130 // Narrow the object reference to a Database::Agent
131 Database::Agent_var database_agent =
132 Database::Agent::_narrow (object.in ());
134 Database::NVPairSequence employee_attributes (2);
135 employee_attributes.length (2);
137 Database::NamedValue &first =
138 employee_attributes[0];
139 Database::NamedValue &second =
140 employee_attributes[1];
142 const char *name = "irfan";
143 CORBA::Long id = 555;
145 first.name = CORBA::string_dup ("name");
146 first.value <<= name;
147 second.name = CORBA::string_dup ("id");
148 second.value <<= id;
150 ACE_OS::strcpy (str,
151 "Database::Agent::create_entry");
153 // Create an employee
154 Database::Entry_var entry =
155 database_agent->create_entry ("irfan",
156 "Employee",
157 employee_attributes);
159 ACE_OS::strcpy (str, "Database::Employee::_narrow");
161 Database::Employee_var employee =
162 Database::Employee::_narrow (entry.in ());
166 * NOT IMPLEMENTED YET
171 #if 0
172 // Reset the id
173 ACE_OS::strcpy (str, "Database::Employee::id");
174 employee->id (666);
175 #endif /* 0 */
177 ACE_OS::strcpy (str, "Database::Entry::find");
178 // Find the employee
179 entry = database_agent->find_entry ("irfan",
180 "Employee");
182 ACE_OS::strcpy (str, "Database::Entry::destroy");
183 // Destroy the employee
184 database_agent->destroy_entry ("irfan",
185 "Employee");
187 ACE_OS::strcpy (str, "Shutdown server");
189 if (shutdown_server)
191 database_agent->shutdown ();
194 ACE_OS::free (IOR);
196 orb->destroy ();
198 catch (const CORBA::Exception& ex)
200 ex._tao_print_exception (str);
201 return -1;
204 return 0;