summaryrefslogtreecommitdiff
path: root/CommonAPI-Examples/E04PhoneBook/src/E04PhoneBookClient.cpp
blob: d1b9d84699e40d2c89c20265c9c4799c8910a004 (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
192
193
194
195
196
197
198
199
200
201
/* Copyright (C) 2014, 2015 BMW Group
 * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
 * Author: Juergen Gehring (juergen.gehring@bmw.de)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef WIN32
#include <unistd.h>
#endif

#include <map>
#include <iostream>

#include <CommonAPI/CommonAPI.hpp>
#include <commonapi/examples/E04PhoneBook.hpp>
#include <commonapi/examples/E04PhoneBookProxy.hpp>

using namespace commonapi::examples;

// Utility functions only for printing results to the console

std::string phoneNumberType2String(E04PhoneBook::phoneNumberEnum phoneNumberType) {

    switch (phoneNumberType) {
        case 0:
            return "WORK";
            break;
        case 1:
            return "HOME";
            break;
        case 2:
            return "MOBILE1";
            break;
        case 3:
            return "MOBILE2";
            break;
        default:
            return "";
            break;
    }
}

void printPhoneBook(const std::vector<E04PhoneBook::phoneBookStruct>& myPhoneBook) {
    std::vector<E04PhoneBook::phoneBookStruct>::const_iterator myIterator;

    std::cout << "Actual phoneBook content: " << std::endl;
    for (myIterator = myPhoneBook.begin(); myIterator != myPhoneBook.end(); myIterator++) {

        std::cout << "Name: " << myIterator->getName() << std::endl;
        std::cout << "Forename: " << myIterator->getForename() << std::endl;
        std::cout << "Organisation: " << myIterator->getOrganisation() << std::endl;
        std::cout << "Address: " << myIterator->getAddress() << std::endl;
        std::cout << "EMail: " << myIterator->getEmail() << std::endl;

        for (E04PhoneBook::phoneNumberMap::const_iterator myPhoneNumberIterator = myIterator->getPhoneNumber().begin();
                        myPhoneNumberIterator != myIterator->getPhoneNumber().end();
                        myPhoneNumberIterator++) {
            std::cout << "phoneNumber[" << phoneNumberType2String(myPhoneNumberIterator->first) << "]: ";
            std::cout << myPhoneNumberIterator->second << std::endl;
        }
        std::cout << std::endl;
    }
}

void printFilterResult(const std::vector<E04PhoneBook::phoneBookDataElementMap>& phoneBookDataSet, std::string proxy) {
    std::vector<E04PhoneBook::phoneBookDataElementMap>::const_iterator it0;

    std::cout << "Actual phoneBookDataSet for proxy " << proxy << ": " << std::endl;
    for (it0 = phoneBookDataSet.begin(); it0 != phoneBookDataSet.end(); it0++) {
        E04PhoneBook::phoneBookDataElementMap::const_iterator it1;

        for (it1 = (*it0).begin(); it1 != (*it0).end(); it1++) {
            switch (it1->first) {
                case E04PhoneBook::phoneBookDataElementEnum::NAME: {
                    std::string name = (std::static_pointer_cast < E04PhoneBook::phoneBookDataElementString
                                    > (it1->second))->getContent();
                    std::cout << "Name = " << name << std::endl;
                }
                break;

                case E04PhoneBook::phoneBookDataElementEnum::FORENAME: {
                    std::string forename = (std::static_pointer_cast < E04PhoneBook::phoneBookDataElementString
                                    > (it1->second))->getContent();
                    std::cout << "Forename = " << forename << std::endl;
                }
                break;

                case E04PhoneBook::phoneBookDataElementEnum::ORGANISATION: {
                    std::string organisation = (std::static_pointer_cast < E04PhoneBook::phoneBookDataElementString
                                    > ((*it0).at(E04PhoneBook::phoneBookDataElementEnum::ORGANISATION)))->getContent();
                    std::cout << "Organisation = " << organisation << std::endl;
                }
                break;

                case E04PhoneBook::phoneBookDataElementEnum::ADDRESS: {
                    std::string address = (std::static_pointer_cast < E04PhoneBook::phoneBookDataElementString
                                    > ((*it0).at(E04PhoneBook::phoneBookDataElementEnum::ADDRESS)))->getContent();
                    std::cout << "Address = " << address << std::endl;
                }
                break;

                case E04PhoneBook::phoneBookDataElementEnum::EMAIL: {
                    std::string email = (std::static_pointer_cast < E04PhoneBook::phoneBookDataElementString
                                    > ((*it0).at(E04PhoneBook::phoneBookDataElementEnum::EMAIL)))->getContent();
                    std::cout << "EMail = " << email << std::endl;
                }
                break;

                case E04PhoneBook::phoneBookDataElementEnum::PHONENUMBER: {
                    E04PhoneBook::phoneNumberMap phoneNumber = (std::static_pointer_cast
                                    < E04PhoneBook::phoneBookDataElementPhoneNumber
                                    > ((*it0).at(E04PhoneBook::phoneBookDataElementEnum::PHONENUMBER)))->getContent();
                    for (E04PhoneBook::phoneNumberMap::iterator myPhoneNumberIterator = phoneNumber.begin();
                                    myPhoneNumberIterator != phoneNumber.end();
                                    myPhoneNumberIterator++) {
                        std::cout << "PhoneNumber[" << phoneNumberType2String(myPhoneNumberIterator->first) << "] = ";
                        std::cout << myPhoneNumberIterator->second << std::endl;
                    }
                }
                break;

                default: {
                    std::cout << "No result." << std::endl;
                }
                break;
            }
        }
    }
}

int main() {
    CommonAPI::Runtime::setProperty("LogContext", "E04C");
    CommonAPI::Runtime::setProperty("LibraryBase", "E04PhoneBook");

    std::shared_ptr<CommonAPI::Runtime> runtime = CommonAPI::Runtime::get();

    const std::string &domain = "local";
    const std::string &instance = "commonapi.examples.PhoneBook";

    std::shared_ptr < E04PhoneBookProxy<> > myProxyA = runtime->buildProxy < E04PhoneBookProxy > (domain, instance);
    while (!myProxyA->isAvailable()) {
        usleep(10);
    }

    const CommonAPI::ConnectionId_t otherConnectionId = "42";

    std::shared_ptr < E04PhoneBookProxy<> > myProxyB = runtime->buildProxy < E04PhoneBookProxy > (domain, instance, otherConnectionId);
    while (!myProxyB->isAvailable()) {
        usleep(10);
    }

    // Subscribe A to broadcast
    myProxyA->getPhoneBookDataSetSelectiveEvent().subscribe(
                    [&](const std::vector<E04PhoneBook::phoneBookDataElementMap>& phoneBookDataSet) {
                        printFilterResult(phoneBookDataSet, "A");
                    });

    // Subscribe B to broadcast
    myProxyB->getPhoneBookDataSetSelectiveEvent().subscribe(
                    [&](const std::vector<E04PhoneBook::phoneBookDataElementMap>& phoneBookDataSet) {
                        printFilterResult(phoneBookDataSet, "B");
                    });

    // Get actual phoneBook from service
    CommonAPI::CallStatus myCallStatus;
    std::vector<E04PhoneBook::phoneBookStruct> myValue;

    myProxyA->getPhoneBookAttribute().getValue(myCallStatus, myValue);
    if (myCallStatus != CommonAPI::CallStatus::SUCCESS)
        std::cerr << "Remote call getPhoneBookAttribute failed!\n";
    else
        printPhoneBook (myValue);

    // Synchronous call setPhoneBookDataFilter
    std::cout << "Call setPhoneBookDataFilter A ..." << std::endl;
    E04PhoneBook::elementFilterStruct lElementFilterA = {true, true, false, false, false, false};
    std::vector<E04PhoneBook::contentFilterStruct> lContentFilterA = { {E04PhoneBook::phoneBookDataElementEnum::NAME, "*"}};

    myProxyA->setPhoneBookDataFilter(lElementFilterA, lContentFilterA, myCallStatus);
    if (myCallStatus != CommonAPI::CallStatus::SUCCESS)
        std::cerr << "Remote call setPhoneBookDataFilter A failed: " << (int) myCallStatus << std::endl;
    else
    	std::cout << "Remote call setPhoneBookDataFilter A succeeded." << std::endl;

	std::cout << "Call setPhoneBookDataFilter B ..." << std::endl;
    E04PhoneBook::elementFilterStruct lElementFilterB = {true, false, false, false, false, true};
    std::vector<E04PhoneBook::contentFilterStruct> lContentFilterB = { {E04PhoneBook::phoneBookDataElementEnum::NAME, "*"}};

    myProxyB->setPhoneBookDataFilter(lElementFilterB, lContentFilterB, myCallStatus);
    if (myCallStatus != CommonAPI::CallStatus::SUCCESS)
        std::cerr << "Remote call setPhoneBookDataFilter B failed: " << (int) myCallStatus << std::endl;
    else
    	std::cout << "Remote call setPhoneBookDataFilter B succeeded." << std::endl;

    while (true) {
    	std::cout << "Now I am going to sleep for 5 seconds..." << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(5));
    }
    return 0;
}