summaryrefslogtreecommitdiff
path: root/TAO/examples/POA/DSI/client.cpp
blob: 88472c14903a13654c78719b97f2b0d6931bf0f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// $Id$

//===================================================================
//  = LIBRARY
//      TAO/tests/POA/DSI/client
//
//  = FILENAME
//     client.cpp
//
//  = DESCRIPTION
//      A client program for the Database IDL module
//
//  = AUTHOR
//     Irfan Pyarali
//
//====================================================================

#include "ace/Get_Opt.h"
#include "ace/Read_Buffer.h"
#include "DatabaseC.h"

ACE_RCSID(DSI, client, "$Id$")

static char *ior = 0;
static char *iorfile = 0;
static int
parse_args (int argc, char **argv)
{
  ACE_Get_Opt get_opts (argc, argv, "k:d");
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'k':
        iorfile = ACE_OS::strdup (get_opts.optarg);
        break;
      case 'd':
	TAO_debug_level++;
	break; 
      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s"
                           "[-k <iorfile>]"
                           "\n",
                           argv [0]),
                          -1);
      }

  if (iorfile == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Please specify the IOR for the servant\n"), -1);

  // Indicates successful parsing of command line.
  return 0;
}

int
main (int argc, char **argv)
{
  CORBA::Environment env;

  // Initialize the ORB
  CORBA::ORB_var orb = CORBA::ORB_init (argc, argv, 0, env);
  if (env.exception () != 0)
    {
      env.print_exception ("CORBA::ORB_init");
      return -1;
    }

  // Parse the command-line arguments to get the location of the IOR 
  if (parse_args (argc, argv) == -1)
    return -1;

    // Read the file, and get the IOR
  ACE_HANDLE input_file = ACE_OS::open (iorfile, 0);
  if (input_file == ACE_INVALID_HANDLE)
    ACE_ERROR_RETURN ((LM_DEBUG,
		       "Cannot open input file for reading IOR: %s\n", 
		       iorfile),
		      -1);
  ACE_Read_Buffer ior_buffer (input_file);
  char *data = ior_buffer.read ();
  if (data == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
		       "Unable to read ior\n"),
		      -1);
  ior = ACE_OS::strdup (data);
  ior_buffer.alloc ()-> free (data);
  ACE_OS::close (input_file);
  
  // Get the object reference with the IOR
  CORBA::Object_var object = orb->string_to_object (ior, env);
  if (env.exception () != 0)
    {
      env.print_exception ("CORBA::ORB::string_to_object");
      return -1;
    }
  
  // Narrow the object reference to a Database::Agent
  Database::Agent_var database_agent = Database::Agent::_narrow (object.in (), env);
  if (env.exception () != 0)
    {
      env.print_exception ("Database::Agent::_narrow");
      return -1;
    }

  Database::NVPairSequence employee_attributes (2);
  employee_attributes.length (2);

  Database::NamedValue &first = employee_attributes[0];    
  Database::NamedValue &second = employee_attributes[1];    
      
  char *name = "irfan";
  CORBA::Long id = 555;

  first.name = CORBA::string_dup ("name");
  first.value <<= name;
  second.name = CORBA::string_dup ("id");
  second.value <<= id;
  
  // Create an employee
  Database::Entry_var entry = database_agent->create_entry ("irfan",
                                                            "Employee",
                                                            employee_attributes,
                                                            env);
  if (env.exception () != 0)
    {
      env.print_exception ("Database::Agent::create_entry");
      return -1;
    }

  Database::Employee_var employee = Database::Employee::_narrow (entry.in (), env);
  if (env.exception () != 0)
    {
      env.print_exception ("Database::Employee::_narrow");
      return -1;
    }

  /*
   * 
   *  NOT IMPLEMENTED YET 
   * 
   *
   */
   
#if 0
  // Reset the id
  employee->id (666, env);  
  if (env.exception () != 0)
    {
      env.print_exception ("Database::Employee::id");
      return -1;
    }
#endif /* 0 */

  // Destroy the employee
  database_agent->destroy_entry ("irfan",
                                 "Employee",
                                 env);
  if (env.exception () != 0)
    {
      env.print_exception ("Database::Entry::destroy");
      return -1;
    }

  return 0;
}