2 //=============================================================================
6 * A client program for the Database IDL module
8 * @author Irfan Pyarali
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"
21 static const ACE_TCHAR
*IOR_file
= 0;
22 static int shutdown_server
= 0;
25 parse_args (int argc
, ACE_TCHAR
**argv
)
27 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("xk:f:"));
30 while ((c
= get_opts ()) != -1)
34 IOR
= ACE_OS::strdup (ACE_TEXT_ALWAYS_CHAR (get_opts
.opt_arg ()));
38 IOR_file
= get_opts
.opt_arg ();
47 ACE_ERROR_RETURN ((LM_ERROR
,
51 "-x [for shutting down the server] "
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"),
62 // Indicates successful parsing of command line.
69 // Open the file for reading.
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",
79 ACE_Read_Buffer
ior_buffer (f_handle
);
80 char *data
= ior_buffer
.read ();
83 ACE_ERROR_RETURN ((LM_ERROR
,
84 "Unable to read ior\n"),
87 IOR
= ACE_OS::strdup (data
);
88 ior_buffer
.alloc ()->free (data
);
90 ACE_OS::close (f_handle
);
96 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
104 CORBA::ORB_var orb
= CORBA::ORB_init (argc
, argv
);
106 // Parse the command-line arguments to get the location of the
108 if (parse_args (argc
, argv
) == -1)
113 int result
= read_IOR_from_file ();
115 ACE_ERROR_RETURN ((LM_ERROR
,
116 "Cannot read IOR from %s\n",
122 "CORBA::ORB::string_to_object");
124 // Get the object reference with the IOR
125 CORBA::Object_var object
= orb
->string_to_object (IOR
);
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");
151 "Database::Agent::create_entry");
153 // Create an employee
154 Database::Entry_var entry
=
155 database_agent
->create_entry ("irfan",
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
173 ACE_OS::strcpy (str
, "Database::Employee::id");
177 ACE_OS::strcpy (str
, "Database::Entry::find");
179 entry
= database_agent
->find_entry ("irfan",
182 ACE_OS::strcpy (str
, "Database::Entry::destroy");
183 // Destroy the employee
184 database_agent
->destroy_entry ("irfan",
187 ACE_OS::strcpy (str
, "Shutdown server");
191 database_agent
->shutdown ();
198 catch (const CORBA::Exception
& ex
)
200 ex
._tao_print_exception (str
);