aboutsummaryrefslogtreecommitdiffstats
path: root/alljoyn_core/samples/about/AboutClient.cc
blob: 25dc076cc9c4932c50f9ab3efbc8ba681328829c (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/******************************************************************************
 *    Copyright (c) Open Connectivity Foundation (OCF), AllJoyn Open Source
 *    Project (AJOSP) Contributors and others.
 *
 *    SPDX-License-Identifier: Apache-2.0
 *
 *    All rights reserved. This program and the accompanying materials are
 *    made available under the terms of the Apache License, Version 2.0
 *    which accompanies this distribution, and is available at
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Copyright (c) Open Connectivity Foundation and Contributors to AllSeen
 *    Alliance. All rights reserved.
 *
 *    Permission to use, copy, modify, and/or distribute this software for
 *    any purpose with or without fee is hereby granted, provided that the
 *    above copyright notice and this permission notice appear in all
 *    copies.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
 *    WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
 *    WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
 *    AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 *    DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 *    PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 *    TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 *    PERFORMANCE OF THIS SOFTWARE.
 ******************************************************************************/

#include <alljoyn/AboutData.h>
#include <alljoyn/AboutListener.h>
#include <alljoyn/AboutObjectDescription.h>
#include <alljoyn/AboutProxy.h>
#include <alljoyn/BusAttachment.h>
#include <alljoyn/Init.h>
#include <alljoyn/Session.h>
#include <alljoyn/SessionListener.h>

#include <qcc/Mutex.h>

#include <signal.h>
#include <stdio.h>
#include <queue>

using namespace ajn;

static volatile sig_atomic_t s_interrupt = false;

/* The interface name should be the only thing required to find and form a
   connection between the service and the client using the about feature. */
static const char* INTERFACE_NAME = "com.example.about.feature.interface.sample";

static void CDECL_CALL SigIntHandler(int sig) {
    QCC_UNUSED(sig);
    s_interrupt = true;
}

BusAttachment* g_bus = nullptr;

struct JoinedSessionData {
    JoinedSessionData(const qcc::String& hostBusName, SessionId sessionId)
        : hostBusName(hostBusName), sessionId(sessionId) {
    }

    qcc::String hostBusName;
    SessionId sessionId;
};

static std::queue<JoinedSessionData> s_joinedSessions;
static qcc::Mutex* s_joinedSessionsLock = nullptr;

void printHorizontalLine() {
    const qcc::String horizontalLine = "*********************************************************************************";
    std::cout << horizontalLine << std::endl;
}

/* Print out the fields found in the AboutData. Only fields with known signatures
   are printed out. All others will be treated as an unknown field. */
void printAboutDataForLanguage(AboutData& aboutData, const char* language, int tabLevel)
{
    std::ios::fmtflags initialFlags(std::cout.flags());
    size_t numFields = aboutData.GetFields();

    const char** fields = new const char*[numFields];
    aboutData.GetFields(fields, numFields);

    for (size_t i = 0; i < numFields; ++i) {
        for (int j = 0; j < tabLevel; ++j) {
            std::cout << "\t";
        }
        std::cout << "Key: " << fields[i];

        MsgArg* fieldArg;
        QStatus status = aboutData.GetField(fields[i], fieldArg, language);
        if (status != ER_OK) {
            std::cout << "printAboutDataForLanguage: AboutData.GetField failed for ";
            if (language == nullptr) {
                std::cout << "default language";
            } else {
                std::cout << "language " << language;
            }
            std::cout << " (" << status << ")" << std::endl;
            continue;
        }
        std::cout << "\t";
        if (fieldArg->Signature() == "s") {
            const char* toPrint;
            fieldArg->Get("s", &toPrint);
            std::cout << toPrint;
        } else if (fieldArg->Signature() == "as") {
            size_t asLen;
            MsgArg* asArg;
            fieldArg->Get("as", &asLen, &asArg);
            for (size_t j = 0; j < asLen; ++j) {
                const char* toPrint;
                asArg[j].Get("s", &toPrint);
                std::cout << toPrint << " ";
            }
        } else if (fieldArg->Signature() == "ay") {
            size_t ayLen;
            uint8_t* toPrint;
            fieldArg->Get("ay", &ayLen, &toPrint);
            for (size_t j = 0; j < ayLen; ++j) {
                std::cout << std::hex << static_cast<int>(toPrint[j]) << " ";
            }
        } else {
            std::cout << "User Defined Value\tSignature: " << fieldArg->Signature();
        }
        std::cout << std::endl;
    }
    std::cout.flags(initialFlags);
    delete [] fields;
}

void printObjectDescription(const AboutObjectDescription& objectDescription, size_t tabLevel) {
    size_t pathCount = objectDescription.GetPaths(nullptr, 0);
    const char** paths = new const char*[pathCount];
    objectDescription.GetPaths(paths, pathCount);

    for (size_t p = 0; p < pathCount; ++p) {
        for (size_t t = 0; t < tabLevel; ++t) {
            std::cout << "\t";
        }
        std::cout << paths[p] << std::endl;

        size_t intfCount = objectDescription.GetInterfaces(paths[p], nullptr, 0);
        const char** intfs = new const char*[intfCount];
        objectDescription.GetInterfaces(paths[p], intfs, intfCount);
        for (size_t i = 0; i < intfCount; ++i) {
            for (size_t t = 0; t < tabLevel + 1; ++t) {
                std::cout << "\t";
            }
            std::cout << intfs[i] << std::endl;
        }
        delete [] intfs;
    }
    delete [] paths;
}

QStatus printObjectDescription(AboutProxy& aboutProxy) {
    MsgArg objArg;
    QStatus status = aboutProxy.GetObjectDescription(objArg);
    if (status != ER_OK) {
        std::cout << "AboutProxy.GetObjectDescription failed(" << status << ")" << std::endl;
        return status;
    }

    AboutObjectDescription aboutObjectDescription(objArg);
    std::cout << "AboutProxy.GetObjectDescription:" << std::endl;
    printObjectDescription(aboutObjectDescription, 1);
    return ER_OK;
}

QStatus printAboutData(AboutProxy& aboutProxy) {
    MsgArg aboutArg;
    QStatus status = aboutProxy.GetAboutData(nullptr, aboutArg);
    if (status != ER_OK) {
        std::cout << "printAboutData: AboutProxy.GetAboutData for default language failed "
                  << "(" << status << ")" << std::endl;
        return status;
    }

    AboutData defaultLangAboutData(aboutArg);
    std::cout << "AboutProxy.GetAboutData: (Default Language)" << std::endl;
    printAboutDataForLanguage(defaultLangAboutData, nullptr, 1);
    size_t langCount;
    langCount = defaultLangAboutData.GetSupportedLanguages();
    /* If the langCount == 1 we only have a default language. */
    if (langCount > 1) {
        const char** langs = new const char*[langCount];
        defaultLangAboutData.GetSupportedLanguages(langs, langCount);
        char* defaultLanguage;
        QStatus defaultLanguageStatus = defaultLangAboutData.GetDefaultLanguage(&defaultLanguage);
        if (defaultLanguageStatus != ER_OK) {
            std::cout << "printAboutData: AboutData.GetDefaultLanguage failed "
                      << "(" << defaultLanguageStatus << ")" << std::endl;
        }
        /* Print out the AboutData for every language but the
           default as it has already been printed. */
        for (size_t l = 0; l < langCount; ++l) {
            if (defaultLanguageStatus == ER_OK && strcmp(defaultLanguage, langs[l]) == 0) {
                continue;
            }
            status = aboutProxy.GetAboutData(langs[l], aboutArg);
            if (status != ER_OK) {
                std::cout << "printAboutData: AboutProxy.GetAboutData for language "
                          << langs[l] << " failed (" << status << ")" << std::endl;
                continue;
            }

            AboutData nonDefaultAboutData;
            nonDefaultAboutData.CreatefromMsgArg(aboutArg, langs[l]);
            std::cout << "AboutProxy.GetAboutData: (" << langs[l] << ")" << std::endl;
            printAboutDataForLanguage(nonDefaultAboutData, langs[l], 1);
        }
        delete [] langs;
    }
    return ER_OK;
}

QStatus printVersion(AboutProxy& aboutProxy) {
    uint16_t ver;
    QStatus status = aboutProxy.GetVersion(ver);
    if (status != ER_OK) {
        std::cout << "printVersion: AboutProxy.GetVersion failed(" << status << ")" << std::endl;
        return status;
    }

    std::cout << "AboutProxy.GetVersion: " << ver << std::endl;
    return ER_OK;
}

QStatus callEchoMethod(AboutProxy& aboutProxy, const JoinedSessionData& joinedSession) {
    MsgArg objArg;
    QStatus status = aboutProxy.GetObjectDescription(objArg);
    if (status != ER_OK) {
        std::cout << "callEchoMethod: AboutProxy.GetObjectDescription failed(" << status << ")" << std::endl;
        return status;
    }

    AboutObjectDescription aboutObjectDescription(objArg);
    const char* path;
    aboutObjectDescription.GetInterfacePaths(INTERFACE_NAME, &path, 1);

    ProxyBusObject proxyObject(*g_bus, joinedSession.hostBusName.c_str(), path, joinedSession.sessionId);
    status = proxyObject.IntrospectRemoteObject();
    if (status != ER_OK) {
        std::cout << "callEchoMethod: Failed to introspect remote object." << std::endl;
        return status;
    }

    MsgArg arg("s", "ECHO Echo echo...\n");
    Message replyMsg(*g_bus);
    std::cout << "Calling " << path << "/" << INTERFACE_NAME << std::endl;
    status = proxyObject.MethodCall(INTERFACE_NAME, "Echo", &arg, 1, replyMsg);
    if (status != ER_OK) {
        std::cout << "callEchoMethod: Failed to call Echo method." << std::endl;
        return status;
    }
    char* echoReply;
    status = replyMsg->GetArg(0)->Get("s", &echoReply);
    if (status != ER_OK) {
        std::cout << "callEchoMethod: Failed to read Echo method reply." << std::endl;
        return status;
    }
    std::cout << "Echo method reply: " << echoReply << std::endl;
    return ER_OK;
}

QStatus processJoinedSession(const JoinedSessionData& joinedSession) {
    MsgArg objArg;
    AboutProxy aboutProxy(*g_bus, joinedSession.hostBusName.c_str(), joinedSession.sessionId);

    printHorizontalLine();
    QStatus status = printObjectDescription(aboutProxy);
    if (status != ER_OK) {
        return status;
    }
    printHorizontalLine();
    status = printAboutData(aboutProxy);
    if (status != ER_OK) {
        return status;
    }
    printHorizontalLine();
    status = printVersion(aboutProxy);
    if (status != ER_OK) {
        return status;
    }
    printHorizontalLine();
    status = callEchoMethod(aboutProxy, joinedSession);
    return status;
}

class MySessionListener : public SessionListener {
    void SessionLost(SessionId sessionId, SessionLostReason reason) {
        std::cout << "SessionLost. SessionId = " << sessionId << ", Reason = " << reason << std::endl;
    }
};

class MyJoinCallback : public BusAttachment::JoinSessionAsyncCB {
    void JoinSessionCB(QStatus status, SessionId sessionId, const SessionOpts& opts, void* context) {
        QCC_UNUSED(opts);
        qcc::String* busName = reinterpret_cast<qcc::String*>(context);

        printHorizontalLine();
        std::cout << "SessionJoined sessionId = " << sessionId << ", status = " << QCC_StatusText(status) << std::endl;
        /* Instead of processing the joined session data in the callback thread, we add the data to the
           application's work queue (s_joinedSessions). This way, the processing will be
           delegated to the application thread and the callback thread will not be overloaded with
           time-consuming work.
           See also the documentation for BusAttachment::EnableConcurrentCallbacks(). */
        s_joinedSessionsLock->Lock(MUTEX_CONTEXT);
        s_joinedSessions.push(JoinedSessionData(*busName, sessionId));
        s_joinedSessionsLock->Unlock(MUTEX_CONTEXT);
        delete busName;
    }
};

void printAnnouncedParameters(const char* busName, uint16_t version, SessionPort port) {
    std::cout << "Announce signal discovered" << std::endl;
    std::cout << "\tFrom bus " << busName << std::endl;
    std::cout << "\tAbout version " << version << std::endl;
    std::cout << "\tSessionPort " << port << std::endl;
}

void printAnnouncedObjectDescription(const MsgArg& objectDescriptionArg) {
    AboutObjectDescription objectDescription(objectDescriptionArg);
    std::cout << "\tObjectDescription:" << std::endl;
    const size_t tabLevel = 2;
    printObjectDescription(objectDescription, tabLevel);
}

void printAnnouncedAboutData(const MsgArg& aboutDataArg) {
    std::cout << "\tAboutData:" << std::endl;
    AboutData aboutData(aboutDataArg);
    const size_t tabLevel = 2;
    printAboutDataForLanguage(aboutData, nullptr, tabLevel);
}

class MyAboutListener : public AboutListener {
    void Announced(const char* busName, uint16_t version, SessionPort port, const MsgArg& objectDescriptionArg, const MsgArg& aboutDataArg) {
        AboutObjectDescription objectDescription;
        objectDescription.CreateFromMsgArg(objectDescriptionArg);

        printHorizontalLine();
        printAnnouncedParameters(busName, version, port);
        printAnnouncedObjectDescription(objectDescriptionArg);
        printAnnouncedAboutData(aboutDataArg);

        QStatus status;
        if (g_bus != nullptr) {
            SessionOpts opts(SessionOpts::TRAFFIC_MESSAGES, false, SessionOpts::PROXIMITY_ANY, TRANSPORT_ANY);
            /* Use the asynchronous variant of JoinSession. Calling a synchronous remote method call from
               a callback (and Announced() is one) would require using the BusAttachment::EnableConcurrentCallbacks()
               functionality which is resource-consuming and may lead to a deadlock if there are not enough
               callback-processing threads.
               For details, see the documentation for BusAttachment::EnableConcurrentCallbacks(). */
            status = g_bus->JoinSessionAsync(busName, port, &sessionListener, opts, &joinCb, new qcc::String(busName));
            if (status != ER_OK) {
                std::cout << "Error joining session: " << QCC_StatusText(status) << std::endl;
            }
        } else {
            std::cout << "BusAttachment is NULL" << std::endl;
        }
    }

  private:
    MySessionListener sessionListener;
    MyJoinCallback joinCb;
};

QStatus processJoinedSessions() {
    if (!s_joinedSessions.empty()) {
        /* Process a single entry from the application's work queue:
           print out a joined session's About data and try to do a remote method call,
           then leave the session. */
        s_joinedSessionsLock->Lock(MUTEX_CONTEXT);
        JoinedSessionData joinedSession = s_joinedSessions.front();
        s_joinedSessions.pop();
        s_joinedSessionsLock->Unlock(MUTEX_CONTEXT);
        QStatus status = processJoinedSession(joinedSession);
        if (status != ER_OK) {
            std::cout << "processJoinedSessions: processJoinedSession failed (" << status << ")" << std::endl;
            return status;
        }

        std::cout << "Leaving session id = " << joinedSession.sessionId << " with " << joinedSession.hostBusName.c_str() << " status: " << QCC_StatusText(status) << std::endl;
        status = g_bus->LeaveSession(joinedSession.sessionId);
        if (status != ER_OK) {
            std::cout << "processJoinedSessions: LeaveSession failed (" << status << ")" << std::endl;
            return status;
        }
    }
    return ER_OK;
}

int CDECL_CALL main(int argc, char** argv)
{
    QCC_UNUSED(argc);
    QCC_UNUSED(argv);

    if (AllJoynInit() != ER_OK) {
        return 1;
    }
#ifdef ROUTER
    if (AllJoynRouterInit() != ER_OK) {
        AllJoynShutdown();
        return 1;
    }
#endif

    /* Install SIGINT handler so Ctrl + C deallocates memory properly. */
    signal(SIGINT, SigIntHandler);

    g_bus = new BusAttachment("AboutServiceTest", true);

    QStatus status = g_bus->Start();
    if (ER_OK == status) {
        std::cout << "BusAttachment started." << std::endl;
    } else {
        std::cout << "FAILED to start BusAttachment (" << QCC_StatusText(status) << ")" << std::endl;
        delete g_bus;
        exit(1);
    }

    status = g_bus->Connect();
    if (ER_OK == status) {
        std::cout << "BusAttachment connect succeeded." << std::endl;
    } else {
        std::cout << "FAILED to connect to router node (" << QCC_StatusText(status) << ")" << std::endl;
        delete g_bus;
        exit(1);
    }

    s_joinedSessionsLock = new qcc::Mutex();

    MyAboutListener aboutListener;
    g_bus->RegisterAboutListener(aboutListener);

    const char* interfaces[] = { INTERFACE_NAME };
    status = g_bus->WhoImplements(interfaces, sizeof(interfaces) / sizeof(interfaces[0]));
    if (ER_OK == status) {
        std::cout << "WhoImplements called." << std::endl;
    } else {
        std::cout << "WhoImplements call FAILED with status " << QCC_StatusText(status) << std::endl;
        delete g_bus;
        delete s_joinedSessionsLock;
        exit(1);
    }

    /* Perform the service asynchronously until the user signals for an exit. */
    if (ER_OK == status) {
        while (s_interrupt == false) {
#ifdef _WIN32
            Sleep(100);
#else
            usleep(100 * 1000);
#endif
            /* Process the work queue. */
            status = processJoinedSessions();
            if (status != ER_OK) {
                std::cout << "processJoinedSessions failed (" << status << ")" << std::endl;
            }
        }
    }

    g_bus->UnregisterAboutListener(aboutListener);
    delete s_joinedSessionsLock;
    delete g_bus;
#ifdef ROUTER
    AllJoynRouterShutdown();
#endif
    AllJoynShutdown();
    return 0;
}