Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / orbsvcs / tests / ImplRepo / nestea_client_i.cpp
blob600d36516af8a4d1a573c7d6d6ffe3a4cc38943b
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"
8 // Constructor.
9 Nestea_Client_i::Nestea_Client_i ()
10 : argc_ (0)
11 , argv_ (0)
12 , server_key_ (ACE::strnew (ACE_TEXT("key0")))
13 , server_ (Nestea_Bookshelf::_nil ())
14 , shutdown_server_(false)
19 // Parses the command line arguments and returns an error status.
21 int
22 Nestea_Client_i::parse_args ()
24 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("dsn:k:"));
25 int c;
27 while ((c = get_opts ()) != -1)
28 switch (c)
30 case 'd': // debug flag
31 TAO_debug_level++;
32 break;
33 case 'k': // ior provide on command line
34 delete [] this->server_key_;
35 this->server_key_ = ACE::strnew (get_opts.opt_arg ());
36 break;
37 case 's': // shutdown server before exiting
38 this->shutdown_server_ = true;
39 break;
40 case '?':
41 default:
42 ACE_ERROR_RETURN ((LM_ERROR,
43 "usage: %s"
44 " [-d]"
45 " [-n loopcount]"
46 " [-s]"
47 " [-k server-object-key]"
48 "\n",
49 this->argv_ [0]),
50 -1);
53 // Indicates successful parsing of command line.
54 return 0;
57 // Execute client example code.
59 int
60 Nestea_Client_i::run ()
62 this->server_->drink (40);
63 this->server_->drink (100);
65 CORBA::String_var praise = this->server_->get_praise ();
66 ACE_DEBUG ((LM_DEBUG, "Cans: %d\n"
67 "Praise: %s\n",
68 this->server_->bookshelf_size (),
69 praise.in ()));
71 this->server_->drink (500);
72 this->server_->crush (200);
74 praise = this->server_->get_praise ();
75 ACE_DEBUG ((LM_DEBUG, "Cans: %d\n"
76 "Praise: %s\n",
77 this->server_->bookshelf_size (),
78 praise.in ()));
80 if (shutdown_server_)
81 server_->shutdown();
83 return 0;
86 Nestea_Client_i::~Nestea_Client_i ()
88 // Free resources
89 CORBA::release (this->server_);
91 delete [] this->server_key_;
95 int
96 Nestea_Client_i::init (int argc, ACE_TCHAR **argv)
98 this->argc_ = argc;
99 this->argv_ = argv;
103 // Retrieve the ORB.
104 this->orb_ = CORBA::ORB_init (this->argc_,
105 this->argv_,
106 "internet");
108 // Parse command line and verify parameters.
109 if (this->parse_args () == -1)
110 return -1;
112 if (this->server_key_ == 0)
113 ACE_ERROR_RETURN ((LM_ERROR,
114 "%s: no server key specified\n",
115 this->argv_[0]),
116 -1);
118 CORBA::Object_var server_object =
119 this->orb_->string_to_object (this->server_key_);
121 this->server_ = Nestea_Bookshelf::_narrow (server_object.in());
123 if (CORBA::is_nil (server_object.in ()))
124 ACE_ERROR_RETURN ((LM_ERROR,
125 "Error: invalid server key <%s>\n", this->server_key_), -1);
127 catch (const CORBA::Exception& ex)
129 ex._tao_print_exception ("Nestea_Client_i::init");
130 return -1;
133 return 0;