blob: a6f2f528276cbe153046459fa3a0cf28a0f9a1fd (
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
// **********************************************************************
//
// Copyright (c) 2003-2004 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <Ice/Ice.h>
#include <IcePack/Query.h>
#include <Hello.h>
using namespace std;
void
menu()
{
cout <<
"usage:\n"
"c: create a hello object\n"
"d: destroy the current hello object\n"
"s: set the current hello object\n"
"r: set the current hello object to a random hello object\n"
"S: show the name of the current hello object\n"
"t: send greeting\n"
"x: exit\n"
"?: help\n";
}
int
run(int argc, char* argv[], const Ice::CommunicatorPtr& communicator)
{
Ice::PropertiesPtr properties = communicator->getProperties();
IcePack::QueryPrx query = IcePack::QueryPrx::checkedCast(communicator->stringToProxy("IcePack/Query"));
//
// Get an object implementing the HelloFactory interface.
//
HelloFactoryPrx factory = HelloFactoryPrx::checkedCast(query->findObjectByType("::HelloFactory"));
//
// By default we create a Hello object named 'Foo'.
//
HelloPrx hello;
try
{
hello = factory->find("Foo");
}
catch(const NameNotExistException&)
{
hello = factory->create("Foo");
}
menu();
char c;
do
{
try
{
cout << "==> ";
cin >> c;
if(c == 't')
{
hello->sayHello();
}
else if(c == 'c')
{
string name;
cout << "name: ";
cin >> name;
if(!name.empty())
{
try
{
hello = factory->find(name);
cout << "Hello object named '" << name << "' already exists" << endl;
}
catch(const NameNotExistException&)
{
factory = HelloFactoryPrx::checkedCast(query->findObjectByType("::HelloFactory"));
hello = factory->create(name);
}
}
}
else if(c == 'd')
{
if(Ice::identityToString(hello->ice_getIdentity()) == "Foo")
{
cout << "Can't delete the default Hello object named 'Foo'" << endl;
}
else
{
hello->destroy();
try
{
hello = factory->find("Foo");
}
catch(const NameNotExistException&)
{
hello = factory->create("Foo");
}
}
}
else if(c == 's')
{
string name;
cout << "name: ";
cin >> name;
try
{
hello = HelloPrx::checkedCast(factory->find(name));
}
catch(const NameNotExistException&)
{
cout << "This name doesn't exist" << endl;
}
}
else if(c == 'r')
{
hello = HelloPrx::checkedCast(query->findObjectByType("::Hello"));
}
else if(c == 'S')
{
cout << Ice::identityToString(hello->ice_getIdentity()) << endl;
}
else if(c == 'x')
{
// Nothing to do
}
else if(c == '?')
{
menu();
}
else
{
cout << "unknown command `" << c << "'" << endl;
menu();
}
}
catch(const Ice::Exception& ex)
{
cerr << ex << endl;
}
}
while(cin.good() && c != 'x');
return EXIT_SUCCESS;
}
int
main(int argc, char* argv[])
{
int status;
Ice::CommunicatorPtr communicator;
try
{
Ice::PropertiesPtr properties = Ice::createProperties(argc, argv);
properties->load("config");
communicator = Ice::initializeWithProperties(argc, argv, properties);
status = run(argc, argv, communicator);
}
catch(const Ice::Exception& ex)
{
cerr << ex << endl;
status = EXIT_FAILURE;
}
if(communicator)
{
try
{
communicator->destroy();
}
catch(const Ice::Exception& ex)
{
cerr << ex << endl;
status = EXIT_FAILURE;
}
}
return status;
}
|