~ubuntu-branches/ubuntu/quantal/zeroc-ice/quantal

« back to all changes in this revision

Viewing changes to demo/Ice/hello/Client.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Francisco Moya
  • Date: 2008-02-12 16:28:20 UTC
  • mfrom: (4.1.5 hardy)
  • Revision ID: james.westby@ubuntu.com-20080212162820-x3e046s7nmabeswv
Tags: 3.2.1-8
Added -g to global compilation flags (Closes: #465074).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// **********************************************************************
2
2
//
3
 
// Copyright (c) 2003-2006 ZeroC, Inc. All rights reserved.
 
3
// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.
4
4
//
5
5
// This copy of Ice is licensed to you under the terms described in the
6
6
// ICE_LICENSE file included in this distribution.
7
7
//
8
8
// **********************************************************************
9
9
 
10
 
#include <Ice/Application.h>
 
10
#include <Ice/Ice.h>
11
11
#include <Hello.h>
12
12
 
13
13
using namespace std;
18
18
public:
19
19
 
20
20
    virtual int run(int, char*[]);
 
21
    virtual void interruptCallback(int);
21
22
 
22
23
private:
23
24
 
34
35
int
35
36
HelloClient::run(int argc, char* argv[])
36
37
{
37
 
    Ice::PropertiesPtr properties = communicator()->getProperties();
38
 
    const char* proxyProperty = "Hello.Proxy";
39
 
    string proxy = properties->getProperty(proxyProperty);
40
 
    if(proxy.empty())
41
 
    {
42
 
        cerr << argv[0] << ": property `" << proxyProperty << "' not set" << endl;
43
 
        return EXIT_FAILURE;
44
 
    }
 
38
    //
 
39
    // Since this is an interactive demo we want the custom interrupt
 
40
    // callback to be called when the process is interrupted.
 
41
    //
 
42
    callbackOnInterrupt();
45
43
 
46
44
    HelloPrx twoway = HelloPrx::checkedCast(
47
 
        communicator()->stringToProxy(proxy)->ice_twoway()->ice_timeout(-1)->ice_secure(false));
 
45
        communicator()->propertyToProxy("Hello.Proxy")->ice_twoway()->ice_timeout(-1)->ice_secure(false));
48
46
    if(!twoway)
49
47
    {
50
 
        cerr << argv[0] << ": invalid proxy" << endl;
51
 
        return EXIT_FAILURE;
 
48
        cerr << argv[0] << ": invalid proxy" << endl;
 
49
        return EXIT_FAILURE;
52
50
    }
53
51
    HelloPrx oneway = HelloPrx::uncheckedCast(twoway->ice_oneway());
54
52
    HelloPrx batchOneway = HelloPrx::uncheckedCast(twoway->ice_batchOneway());
57
55
 
58
56
    bool secure = false;
59
57
    int timeout = -1;
 
58
    int delay = 0;
60
59
 
61
60
    menu();
62
61
 
63
62
    char c;
64
63
    do
65
64
    {
66
 
        try
67
 
        {
68
 
            cout << "==> ";
69
 
            cin >> c;
70
 
            if(c == 't')
71
 
            {
72
 
                twoway->sayHello();
73
 
            }
74
 
            else if(c == 'o')
75
 
            {
76
 
                oneway->sayHello();
77
 
            }
78
 
            else if(c == 'O')
79
 
            {
80
 
                batchOneway->sayHello();
81
 
            }
82
 
            else if(c == 'd')
83
 
            {
84
 
                if(secure)
85
 
                {
86
 
                    cout << "secure datagrams are not supported" << endl;
87
 
                }
88
 
                else
89
 
                {
90
 
                    datagram->sayHello();
91
 
                }
92
 
            }
93
 
            else if(c == 'D')
94
 
            {
95
 
                if(secure)
96
 
                {
97
 
                    cout << "secure datagrams are not supported" << endl;
98
 
                }
99
 
                else
100
 
                {
101
 
                    batchDatagram->sayHello();
102
 
                }
103
 
            }
104
 
            else if(c == 'f')
105
 
            {
106
 
                communicator()->flushBatchRequests();
107
 
            }
108
 
            else if(c == 'T')
109
 
            {
110
 
                if(timeout == -1)
111
 
                {
112
 
                    timeout = 2000;
113
 
                }
114
 
                else
115
 
                {
116
 
                    timeout = -1;
117
 
                }
118
 
                
119
 
                twoway = HelloPrx::uncheckedCast(twoway->ice_timeout(timeout));
120
 
                oneway = HelloPrx::uncheckedCast(oneway->ice_timeout(timeout));
121
 
                batchOneway = HelloPrx::uncheckedCast(batchOneway->ice_timeout(timeout));
122
 
                
123
 
                if(timeout == -1)
124
 
                {
125
 
                    cout << "timeout is now switched off" << endl;
126
 
                }
127
 
                else
128
 
                {
129
 
                    cout << "timeout is now set to 2000ms" << endl;
130
 
                }
131
 
            }
132
 
            else if(c == 'S')
133
 
            {
134
 
                secure = !secure;
135
 
                
136
 
                twoway = HelloPrx::uncheckedCast(twoway->ice_secure(secure));
137
 
                oneway = HelloPrx::uncheckedCast(oneway->ice_secure(secure));
138
 
                batchOneway = HelloPrx::uncheckedCast(batchOneway->ice_secure(secure));
139
 
                datagram = HelloPrx::uncheckedCast(datagram->ice_secure(secure));
140
 
                batchDatagram = HelloPrx::uncheckedCast(batchDatagram->ice_secure(secure));
141
 
                
142
 
                if(secure)
143
 
                {
144
 
                    cout << "secure mode is now on" << endl;
145
 
                }
146
 
                else
147
 
                {
148
 
                    cout << "secure mode is now off" << endl;
149
 
                }
150
 
            }
151
 
            else if(c == 's')
152
 
            {
153
 
                twoway->shutdown();
154
 
            }
155
 
            else if(c == 'x')
156
 
            {
157
 
                // Nothing to do
158
 
            }
159
 
            else if(c == '?')
160
 
            {
161
 
                menu();
162
 
            }
163
 
            else
164
 
            {
165
 
                cout << "unknown command `" << c << "'" << endl;
166
 
                menu();
167
 
            }
168
 
        }
169
 
        catch(const Ice::Exception& ex)
170
 
        {
171
 
            cerr << ex << endl;
172
 
        }
 
65
        try
 
66
        {
 
67
            cout << "==> ";
 
68
            cin >> c;
 
69
            if(c == 't')
 
70
            {
 
71
                twoway->sayHello(delay);
 
72
            }
 
73
            else if(c == 'o')
 
74
            {
 
75
                oneway->sayHello(delay);
 
76
            }
 
77
            else if(c == 'O')
 
78
            {
 
79
                batchOneway->sayHello(delay);
 
80
            }
 
81
            else if(c == 'd')
 
82
            {
 
83
                if(secure)
 
84
                {
 
85
                    cout << "secure datagrams are not supported" << endl;
 
86
                }
 
87
                else
 
88
                {
 
89
                    datagram->sayHello(delay);
 
90
                }
 
91
            }
 
92
            else if(c == 'D')
 
93
            {
 
94
                if(secure)
 
95
                {
 
96
                    cout << "secure datagrams are not supported" << endl;
 
97
                }
 
98
                else
 
99
                {
 
100
                    batchDatagram->sayHello(delay);
 
101
                }
 
102
            }
 
103
            else if(c == 'f')
 
104
            {
 
105
                communicator()->flushBatchRequests();
 
106
            }
 
107
            else if(c == 'T')
 
108
            {
 
109
                if(timeout == -1)
 
110
                {
 
111
                    timeout = 2000;
 
112
                }
 
113
                else
 
114
                {
 
115
                    timeout = -1;
 
116
                }
 
117
                
 
118
                twoway = HelloPrx::uncheckedCast(twoway->ice_timeout(timeout));
 
119
                oneway = HelloPrx::uncheckedCast(oneway->ice_timeout(timeout));
 
120
                batchOneway = HelloPrx::uncheckedCast(batchOneway->ice_timeout(timeout));
 
121
                
 
122
                if(timeout == -1)
 
123
                {
 
124
                    cout << "timeout is now switched off" << endl;
 
125
                }
 
126
                else
 
127
                {
 
128
                    cout << "timeout is now set to 2000ms" << endl;
 
129
                }
 
130
            }
 
131
            else if(c == 'P')
 
132
            {
 
133
                if(delay == 0)
 
134
                {
 
135
                    delay = 2500;
 
136
                }
 
137
                else
 
138
                {
 
139
                    delay = 0;
 
140
                }
 
141
                
 
142
                if(delay == 0)
 
143
                {
 
144
                    cout << "server delay is now disabled" << endl;
 
145
                }
 
146
                else
 
147
                {
 
148
                    cout << "server delay is now set to 2500ms" << endl;
 
149
                }
 
150
            }
 
151
            else if(c == 'S')
 
152
            {
 
153
                secure = !secure;
 
154
                
 
155
                twoway = HelloPrx::uncheckedCast(twoway->ice_secure(secure));
 
156
                oneway = HelloPrx::uncheckedCast(oneway->ice_secure(secure));
 
157
                batchOneway = HelloPrx::uncheckedCast(batchOneway->ice_secure(secure));
 
158
                datagram = HelloPrx::uncheckedCast(datagram->ice_secure(secure));
 
159
                batchDatagram = HelloPrx::uncheckedCast(batchDatagram->ice_secure(secure));
 
160
                
 
161
                if(secure)
 
162
                {
 
163
                    cout << "secure mode is now on" << endl;
 
164
                }
 
165
                else
 
166
                {
 
167
                    cout << "secure mode is now off" << endl;
 
168
                }
 
169
            }
 
170
            else if(c == 's')
 
171
            {
 
172
                twoway->shutdown();
 
173
            }
 
174
            else if(c == 'x')
 
175
            {
 
176
                // Nothing to do
 
177
            }
 
178
            else if(c == '?')
 
179
            {
 
180
                menu();
 
181
            }
 
182
            else
 
183
            {
 
184
                cout << "unknown command `" << c << "'" << endl;
 
185
                menu();
 
186
            }
 
187
        }
 
188
        catch(const Ice::Exception& ex)
 
189
        {
 
190
            cerr << ex << endl;
 
191
        }
173
192
    }
174
193
    while(cin.good() && c != 'x');
175
194
 
177
196
}
178
197
 
179
198
void
 
199
HelloClient::interruptCallback(int)
 
200
{
 
201
    try
 
202
    {
 
203
        communicator()->destroy();
 
204
    }
 
205
    catch(const IceUtil::Exception& ex)
 
206
    {
 
207
        cerr << appName() << ": " << ex << endl;
 
208
    }
 
209
    catch(...)
 
210
    {
 
211
        cerr << appName() << ": unknown exception" << endl;
 
212
    }
 
213
    exit(EXIT_SUCCESS);
 
214
}
 
215
 
 
216
void
180
217
HelloClient::menu()
181
218
{
182
219
    cout <<
183
 
        "usage:\n"
184
 
        "t: send greeting as twoway\n"
185
 
        "o: send greeting as oneway\n"
186
 
        "O: send greeting as batch oneway\n"
187
 
        "d: send greeting as datagram\n"
188
 
        "D: send greeting as batch datagram\n"
189
 
        "f: flush all batch requests\n"
190
 
        "T: set a timeout\n"
191
 
        "S: switch secure mode on/off\n"
192
 
        "s: shutdown server\n"
193
 
        "x: exit\n"
194
 
        "?: help\n";
 
220
        "usage:\n"
 
221
        "t: send greeting as twoway\n"
 
222
        "o: send greeting as oneway\n"
 
223
        "O: send greeting as batch oneway\n"
 
224
        "d: send greeting as datagram\n"
 
225
        "D: send greeting as batch datagram\n"
 
226
        "f: flush all batch requests\n"
 
227
        "T: set a timeout\n"
 
228
        "P: set server delay\n"
 
229
        "S: switch secure mode on/off\n"
 
230
        "s: shutdown server\n"
 
231
        "x: exit\n"
 
232
        "?: help\n";
195
233
}