summaryrefslogtreecommitdiff
path: root/cpp/demo/Ice/async/Publisher.cpp
blob: e73796fc45bfafa446fcc45833f2e56d198a08bc (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
// **********************************************************************
//
// Copyright (c) 2003-2007 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/Application.h>
#include <Queue.h>

using namespace std;
using namespace Demo;

class QueuePublisher : public Ice::Application
{
public:

    virtual int run(int, char*[]);

private:

    void menu();
    string trim(const string& s);
};

int
main(int argc, char* argv[])
{
    QueuePublisher app;
    return app.main(argc, argv, "config.client");
}

int
QueuePublisher::run(int argc, char* argv[])
{
    QueuePrx queue = QueuePrx::checkedCast(communicator()->propertyToProxy("Queue.Proxy"));
    if(!queue)
    {
	cerr << argv[0] << ": invalid proxy" << endl;
	return EXIT_FAILURE;
    }

    cout << "Type a message and hit return to queue a message." << endl;
    menu();

    try
    {
        do
        {
            string s;
            cout << "==> ";
            getline(cin, s);
            s = trim(s);
            if(!s.empty())
            {
                if(s[0] == '/')
                {
                    if(s == "/quit")
                    {
                        break;
                    }
                    menu();
                }
                else
                {
                    queue->add(s);
                }
            }
        }
        while(cin.good());
    }
    catch(const Ice::Exception& ex)
    {
        cerr << ex << endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

void
QueuePublisher::menu()
{
    cout << "Enter /quit to exit." << endl;
}

string
QueuePublisher::trim(const string& s)
{
    static const string delims = "\t\r\n ";
    string::size_type last = s.find_last_not_of(delims);
    if(last != string::npos)
    {
        return s.substr(s.find_first_not_of(delims), last+1);
    }
    return s;
}