Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / tests / ImplRepo / nestea_client_i.cpp
blob406f52b8c106e805af95d017ed398f80a58be634
1 #include "nestea_client_i.h"
2 #include "tao/debug.h"
3 #include "ace/Get_Opt.h"
4 #include "ace/Read_Buffer.h"
5 #include "ace/ACE.h"
9 // Constructor.
10 Nestea_Client_i::Nestea_Client_i (void)
11 : argc_ (0)
12 , argv_ (0)
13 , server_key_ (ACE::strnew (ACE_TEXT("key0")))
14 , server_ (Nestea_Bookshelf::_nil ())
15 , shutdown_server_(false)
20 // Parses the command line arguments and returns an error status.
22 int
23 Nestea_Client_i::parse_args (void)
25 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("dsn:k:"));
26 int c;
28 while ((c = get_opts ()) != -1)
29 switch (c)
31 case 'd': // debug flag
32 TAO_debug_level++;
33 break;
34 case 'k': // ior provide on command line
35 delete [] this->server_key_;
36 this->server_key_ = ACE::strnew (get_opts.opt_arg ());
37 break;
38 case 's': // shutdown server before exiting
39 this->shutdown_server_ = true;
40 break;
41 case '?':
42 default:
43 ACE_ERROR_RETURN ((LM_ERROR,
44 "usage: %s"
45 " [-d]"
46 " [-n loopcount]"
47 " [-s]"
48 " [-k server-object-key]"
49 "\n",
50 this->argv_ [0]),
51 -1);
54 // Indicates successful parsing of command line.
55 return 0;
58 // Execute client example code.
60 int
61 Nestea_Client_i::run ()
63 this->server_->drink (40);
64 this->server_->drink (100);
66 CORBA::String_var praise = this->server_->get_praise ();
67 ACE_DEBUG ((LM_DEBUG, "Cans: %d\n"
68 "Praise: %s\n",
69 this->server_->bookshelf_size (),
70 praise.in ()));
72 this->server_->drink (500);
73 this->server_->crush (200);
75 praise = this->server_->get_praise ();
76 ACE_DEBUG ((LM_DEBUG, "Cans: %d\n"
77 "Praise: %s\n",
78 this->server_->bookshelf_size (),
79 praise.in ()));
81 if (shutdown_server_)
82 server_->shutdown();
84 return 0;
87 Nestea_Client_i::~Nestea_Client_i (void)
89 // Free resources
90 CORBA::release (this->server_);
92 delete [] this->server_key_;
96 int
97 Nestea_Client_i::init (int argc, ACE_TCHAR **argv)
99 this->argc_ = argc;
100 this->argv_ = argv;
104 // Retrieve the ORB.
105 this->orb_ = CORBA::ORB_init (this->argc_,
106 this->argv_,
107 "internet");
109 // Parse command line and verify parameters.
110 if (this->parse_args () == -1)
111 return -1;
113 if (this->server_key_ == 0)
114 ACE_ERROR_RETURN ((LM_ERROR,
115 "%s: no server key specified\n",
116 this->argv_[0]),
117 -1);
119 CORBA::Object_var server_object =
120 this->orb_->string_to_object (this->server_key_);
122 this->server_ = Nestea_Bookshelf::_narrow (server_object.in());
124 if (CORBA::is_nil (server_object.in ()))
125 ACE_ERROR_RETURN ((LM_ERROR,
126 "Error: invalid server key <%s>\n", this->server_key_), -1);
128 catch (const CORBA::Exception& ex)
130 ex._tao_print_exception ("Nestea_Client_i::init");
131 return -1;
134 return 0;