Skip to content
ControllerClient.h 31.6 KiB
Newer Older
Nicolas Metts's avatar
Nicolas Metts committed
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 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
#ifndef _CONTROLLER_CLIENT_H_
#define _CONTROLLER_CLIENT_H_
/**
 * \defgroup ControllerClient
 * Client controller code
 */
/**
 * \ingroup ControllerClient
 */
/**
 * \file  lighting_controller_client/inc/ControllerClient.h
 * This file provides definitions for the Lighting Controller Client
 */
/******************************************************************************
 * Copyright 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 <map>
#include <list>
#include <signal.h>

#include <alljoyn/BusAttachment.h>
#include <alljoyn/InterfaceDescription.h>
#include <alljoyn/Session.h>
#include <alljoyn/MessageReceiver.h>
#include <qcc/platform.h>

#include <ControllerClientDefs.h>
#include <LampManager.h>
#include <LampGroupManager.h>
#include <PresetManager.h>
#include <TransitionEffectManager.h>
#include <PulseEffectManager.h>
#include <SceneManager.h>
#include <MasterSceneManager.h>
#include <ControllerServiceManager.h>
#include <Rank.h>

namespace lsf {
/**
 * controller client version
 */
#define CONTROLLER_CLIENT_VERSION 1

/**
 * Abstract base class implemented by User Application Developers. \n
 * The callbacks defined in this class allow the User Application
 * to be informed about some Controller Client operations.
 */
class ControllerClientCallback {
  public:
    /**
     * Destructor
     */
    virtual ~ControllerClientCallback() { }

    /**
     * Indicates that the Controller Client has successfully set up an AllJoyn session
     * with a Controller Service
     */
    virtual void ConnectedToControllerServiceCB(const LSFString& controllerServiceDeviceID, const LSFString& controllerServiceName) { }

    /**
     * Indicates that the Controller Client was unsuccessful in setting up an AllJoyn session
     * with a Controller Service
     */
    virtual void ConnectToControllerServiceFailedCB(const LSFString& controllerServiceDeviceID, const LSFString& controllerServiceName) { }

    /**
     * Indicates that the Controller Client has disconnected from a Controller Service
     */
    virtual void DisconnectedFromControllerServiceCB(const LSFString& controllerServiceDeviceID, const LSFString& controllerServiceName) { }

    /**
     *  Indicates that an internal error has occurred in the Controller Client
     *
     *  @param errorCodeList   List of Error Codes
     */
    virtual void ControllerClientErrorCB(const ErrorCodeList& errorCodeList) { }
};

/**
 * This class allows the User Application to initialize the Lighting
 * Controller Client operations
 */
class ControllerClient : public ajn::MessageReceiver {

    friend class ControllerServiceManager;
    friend class LampManager;
    friend class LampGroupManager;
    friend class PresetManager;
    friend class SceneManager;
    friend class MasterSceneManager;
    friend class TransitionEffectManager;
    friend class PulseEffectManager;

    class ControllerClientBusHandler;

  public:

    /**
     * Constructor
     * @param bus - reference to bus attachment object
     * @param clientCB - reference to ControllerClientCallback
     */
    ControllerClient(
        ajn::BusAttachment& bus,
        ControllerClientCallback& clientCB);

    /**
     * Destructor
     */
    ~ControllerClient();

    /**
     *  Get the version of the Lighting Controller Client.
     *
     *  @return The Lighting Controller Client version
     */
    uint32_t GetVersion(void);

    /**
     *  Stop the Lighting Controller Client. This will cause the
     *  Controller Client to tear-down any existent session with
     *  a Controller Service, clear the leadersMap contents and
     *  unregister announce handlers. The user application should
     *  call this API when it detects that the device has disconnected
     *  from Wi-Fi and/or the data network
     */
    ControllerClientStatus Stop(void);

    /**
     *  Start the Lighting Controller Client. This will cause the
     *  Controller Client to register for announce handlers.
     *  The user application should call this API at system start
     *  after confirming that the device is connected to Wi-Fi and/or
     *  data network
     *  OR
     *  when it detects that the device has re-connected to Wi-Fi and/or
     *  the data network after it was disconnected
     */
    ControllerClientStatus Start(void);

  private:

    void DoLeaveSessionAsync(ajn::SessionId sessionId);

    void LeaveSessionAsyncReplyHandler(ajn::Message& message, void* context);

    /**
     * Internal callback invoked when an announcement is received from a Controller
     * Service Leader.
     */
    void OnAnnounced(ajn::SessionPort port, const char* busName, const char* deviceID, const char* deviceName, Rank rank);

    /**
     * Internal callback invoked when a session with a Controller
     * Service Leader is lost.
     */
    void OnSessionLost(ajn::SessionId sessionID);

    void OnSessionMemberRemoved(ajn::SessionId sessionId, const char* uniqueName);

    void SignalWithArgDispatcher(const ajn::InterfaceDescription::Member* member, const char* sourcePath, ajn::Message& message);

    void NameChangedSignalDispatcher(const ajn::InterfaceDescription::Member* member, const char* sourcePath, ajn::Message& message);

    void StateChangedSignalDispatcher(const ajn::InterfaceDescription::Member* member, const char* sourcePath, ajn::Message& message);

    void SignalWithoutArgDispatcher(const ajn::InterfaceDescription::Member* member, const char* sourcePath, ajn::Message& message);

    void HandlerForMethodReplyWithResponseCodeAndListOfIDs(ajn::Message& message, void* context);

    void HandlerForMethodReplyWithResponseCodeIDAndName(ajn::Message& message, void* context);

    void HandlerForMethodReplyWithResponseCodeAndID(ajn::Message& message, void* context);

    void HandlerForMethodReplyWithUint32Value(ajn::Message& message, void* context);

    void HandlerForMethodReplyWithResponseCodeIDLanguageAndName(ajn::Message& message, void* context);

    /**
     * Internal callback invoked when a session is joined with a Controller
     * Service Leader.
     */
    void OnSessionJoined(QStatus status, ajn::SessionId sessionID, void* context);

    /**
     * Helper template to invoke MethodCallAsync on the AllJoyn ProxyBusObject.
     */
    template <typename OBJ>
    ControllerClientStatus MethodCallAsync(const char* ifaceName,
                                           const char* methodName,
                                           OBJ * receiver,
                                           void (OBJ::* replyFunc)(ajn::Message & message),
                                           const ajn::MsgArg * args = NULL,
                                           size_t numArgs = 0);

    /**
     * Helper template to invoke MethodCallAsync on the AllJoyn ProxyBusObject.
     */
    ControllerClientStatus MethodCallAsyncForReplyWithResponseCodeAndListOfIDs(
        const char* ifaceName,
        const char* methodName,
        const ajn::MsgArg* args = NULL,
        size_t numArgs = 0);

    /**
     * Helper template to invoke MethodCallAsync on the AllJoyn ProxyBusObject.
     */
    ControllerClientStatus MethodCallAsyncForReplyWithResponseCodeIDAndName(
        const char* ifaceName,
        const char* methodName,
        const ajn::MsgArg* args = NULL,
        size_t numArgs = 0);

    /**
     * Helper template to invoke MethodCallAsync on the AllJoyn ProxyBusObject.
     */
    ControllerClientStatus MethodCallAsyncForReplyWithResponseCodeAndID(
        const char* ifaceName,
        const char* methodName,
        const ajn::MsgArg* args = NULL,
        size_t numArgs = 0);

    /**
     * Helper template to invoke MethodCallAsync on the AllJoyn ProxyBusObject.
     */
    ControllerClientStatus MethodCallAsyncForReplyWithUint32Value(
        const char* ifaceName,
        const char* methodName,
        const ajn::MsgArg* args = NULL,
        size_t numArgs = 0);

    /**
     * Helper template to invoke MethodCallAsync on the AllJoyn ProxyBusObject.
     */
    ControllerClientStatus MethodCallAsyncForReplyWithResponseCodeIDLanguageAndName(
        const char* ifaceName,
        const char* methodName,
        const ajn::MsgArg* args = NULL,
        size_t numArgs = 0);

    /**
     * Check if the receivedNumArgs is same as expectedNumArgs and return a response
     * code accordingly
     */
    LSFResponseCode CheckNumArgsInMessage(uint32_t receivedNumArgs, uint32_t expectedNumArgs);

    /**
     * Reference to the AllJoyn BusAttachment received from the
     * User Application.
     */
    ajn::BusAttachment& bus;

    /**
     * Handler used to receive messages from the AllJoyn Bus.
     */
    ControllerClientBusHandler* busHandler;

    /**
     * ControllerClientCallback sends the Controller Client specific callbacks
     * to the User Application.
     */
    ControllerClientCallback& callback;

    /*
     * The ID of the CS we are currently trying to connect to.
     */
    struct ControllerEntry {
        ajn::SessionPort port;
        qcc::String busName;
        Rank rank;
        LSFString deviceID;
        LSFString deviceName;
        uint64_t announcementTimestamp;

        void Clear(void) {
            busName.clear();
            rank = Rank();
            port = 0;
            deviceID.clear();
            deviceName.clear();
            announcementTimestamp = 0;
        }
    };

    typedef std::map<Rank, ControllerEntry> Leaders;

    Leaders leadersMap;
    Mutex leadersMapLock;

    typedef struct _CurrentLeader {
        ControllerEntry controllerDetails;
        ajn::ProxyBusObject proxyObject;
        ajn::SessionId sessionId;

        void Clear(void) {
            controllerDetails.Clear();
            proxyObject = ProxyBusObject();
            sessionId = 0;
        }
    } CurrentLeader;

    CurrentLeader currentLeader;
    Mutex currentLeaderLock;

    bool JoinSessionWithAnotherLeader(Rank currentLeaderRank = Rank(), uint64_t timestamp = 0);

    /**
     * Pointer to the Controller Service Manager
     */
    ControllerServiceManager* controllerServiceManagerPtr;

    /**
     * Pointer to the Lamp Manager
     */
    LampManager* lampManagerPtr;

    /**
     * Pointer to the Lamp Group Manager
     */
    LampGroupManager* lampGroupManagerPtr;

    /**
     * Pointer to the Preset Manager
     */
    PresetManager* presetManagerPtr;

    /**
     * Pointer to the Scene Manager
     */
    SceneManager* sceneManagerPtr;

    /**
     * Pointer to the Scene Manager
     */
    MasterSceneManager* masterSceneManagerPtr;

    /**
     * Pointer to the Transition Effect Manager
     */
    TransitionEffectManager* transitionEffectManagerPtr;

    /**
     * Pointer to the Pulse Effect Manager
     */
    PulseEffectManager* pulseEffectManagerPtr;

    /**
     * Constructor
     */
    ControllerClient();

    /**
     * Constructor
     */
    ControllerClient(const ControllerClient&);

    /**
     * Operator
     */
    ControllerClient& operator=(ControllerClient&);

    /**
     * Helper function to invoke MethodCallAsync on the AllJoyn ProxyBusObject.
     */
    ControllerClientStatus MethodCallAsyncHelper(
        const char* ifaceName,
        const char* methodName,
        ajn::MessageReceiver* handler,
        ajn::MessageReceiver::ReplyHandler callback,
        const ajn::MsgArg* args = NULL,
        size_t numArgs = 0,
        void* context = NULL);

    /**
     * Template for AllJoyn MessageReceiver
     */
    template <typename OBJ, typename T>
    class TypeHandler;

    void AddSignalHandlers();

    void RemoveSignalHandlers();

    /**
     * Template for AllJoyn MessageReceiver
     */
    template <typename OBJ>
    class TypeHandler<OBJ, void> : public ajn::MessageReceiver {
        typedef void (OBJ::* ReplyHandler)(ajn::Message& message);
      public:

        TypeHandler(OBJ* receiver, ReplyHandler handler, ControllerClient* controllerClientPtr) :
            receiver(receiver), handler(handler), controllerClientPtr(controllerClientPtr)
        { }

        void MessageHandler(ajn::Message& message, void* context)
        {
            if (!controllerClientPtr->stopped) {
                controllerClientPtr->bus.EnableConcurrentCallbacks();
                if (message->GetType() == ajn::MESSAGE_METHOD_RET) {
                    (receiver->*(handler))(message);
                } else {
                    ErrorCodeList errorList;
                    errorList.push_back(ERROR_ALLJOYN_METHOD_CALL_TIMEOUT);
                    controllerClientPtr->callback.ControllerClientErrorCB(errorList);
                }
            }
            // nothing owns this object so we need to clean it up here
            delete this;
        }

        OBJ* receiver;
        ReplyHandler handler;
        ControllerClient* controllerClientPtr;
    };

    typedef struct {
        const ajn::InterfaceDescription::Member* member;  /**< Pointer to method's member */
        ajn::MessageReceiver::SignalHandler handler;      /**< Signal implementation */
    } SignalEntry;

    template <typename OBJ>
    void AddSignalHandler(const std::string& signalName, OBJ* obj, void (OBJ::* signal)(LSFStringList &))
    {
        SignalHandlerBase* handler = new SignalHandler<OBJ>(obj, signal);
        std::pair<SignalDispatcherMap::iterator, bool> ins = signalHandlers.insert(std::make_pair(signalName, handler));
        if (ins.second == false) {
            // if this was already there, overwrite and delete the old handler
            delete ins.first->second;
            ins.first->second = handler;
        }
    }

    class SignalHandlerBase {
      public:
        virtual ~SignalHandlerBase() { }
        virtual void Handle(LSFStringList& msg) = 0;
    };

    template <typename OBJ>
    class SignalHandler : public SignalHandlerBase {
        typedef void (OBJ::* HandlerFunction)(LSFStringList&);

      public:
        SignalHandler(OBJ* obj, HandlerFunction handleFunc) :
            object(obj), handler(handleFunc) { }

        virtual ~SignalHandler() { }

        virtual void Handle(LSFStringList& list) {
            (object->*(handler))(list);
        }

        OBJ* object;
        HandlerFunction handler;
    };

    typedef std::map<std::string, SignalHandlerBase*> SignalDispatcherMap;
    SignalDispatcherMap signalHandlers;

    void DeleteSignalHandlers(void)
    {
        for (SignalDispatcherMap::iterator it = signalHandlers.begin(); it != signalHandlers.end(); it++) {
            delete it->second;
        }
        signalHandlers.clear();
    }

    template <typename OBJ>
    void AddNameChangedSignalHandler(const std::string& nameChangedSignalName, OBJ* obj, void (OBJ::* nameChangedSignal)(LSFString &, LSFString &))
    {
        NameChangedSignalHandlerBase* handler = new NameChangedSignalHandler<OBJ>(obj, nameChangedSignal);
        std::pair<NameChangedSignalDispatcherMap::iterator, bool> ins = nameChangedSignalHandlers.insert(std::make_pair(nameChangedSignalName, handler));
        if (ins.second == false) {
            // if this was already there, overwrite and delete the old handler
            delete ins.first->second;
            ins.first->second = handler;
        }
    }

    class NameChangedSignalHandlerBase {
      public:
        virtual ~NameChangedSignalHandlerBase() { }
        virtual void Handle(LSFString& id, LSFString& name) = 0;
    };

    template <typename OBJ>
    class NameChangedSignalHandler : public NameChangedSignalHandlerBase {
        typedef void (OBJ::* HandlerFunction)(LSFString&, LSFString&);

      public:
        NameChangedSignalHandler(OBJ* obj, HandlerFunction handleFunc) :
            object(obj), handler(handleFunc) { }

        virtual ~NameChangedSignalHandler() { }

        virtual void Handle(LSFString& id, LSFString& name) {
            (object->*(handler))(id, name);
        }

        OBJ* object;
        HandlerFunction handler;
    };

    typedef std::map<std::string, NameChangedSignalHandlerBase*> NameChangedSignalDispatcherMap;
    NameChangedSignalDispatcherMap nameChangedSignalHandlers;

    void DeleteNameChangedSignalHandlers(void)
    {
        for (NameChangedSignalDispatcherMap::iterator it = nameChangedSignalHandlers.begin(); it != nameChangedSignalHandlers.end(); it++) {
            delete it->second;
        }
        nameChangedSignalHandlers.clear();
    }

    template <typename OBJ>
    void AddStateChangedSignalHandler(const std::string& stateChangedSignalState, OBJ* obj, void (OBJ::* stateChangedSignal)(LSFString &, LampState &))
    {
        StateChangedSignalHandlerBase* handler = new StateChangedSignalHandler<OBJ>(obj, stateChangedSignal);
        std::pair<StateChangedSignalDispatcherMap::iterator, bool> ins = stateChangedSignalHandlers.insert(std::make_pair(stateChangedSignalState, handler));
        if (ins.second == false) {
            // if this was already there, overwrite and delete the old handler
            delete ins.first->second;
            ins.first->second = handler;
        }
    }

    class StateChangedSignalHandlerBase {
      public:
        virtual ~StateChangedSignalHandlerBase() { }
        virtual void Handle(LSFString& id, LampState& state) = 0;
    };

    template <typename OBJ>
    class StateChangedSignalHandler : public StateChangedSignalHandlerBase {
        typedef void (OBJ::* HandlerFunction)(LSFString&, LampState&);

      public:
        StateChangedSignalHandler(OBJ* obj, HandlerFunction handleFunc) :
            object(obj), handler(handleFunc) { }

        virtual ~StateChangedSignalHandler() { }

        virtual void Handle(LSFString& id, LampState& state) {
            (object->*(handler))(id, state);
        }

        OBJ* object;
        HandlerFunction handler;
    };

    typedef std::map<std::string, StateChangedSignalHandlerBase*> StateChangedSignalDispatcherMap;
    StateChangedSignalDispatcherMap stateChangedSignalHandlers;

    void DeleteStateChangedSignalHandlers(void)
    {
        for (StateChangedSignalDispatcherMap::iterator it = stateChangedSignalHandlers.begin(); it != stateChangedSignalHandlers.end(); it++) {
            delete it->second;
        }
        stateChangedSignalHandlers.clear();
    }

    template <typename OBJ>
    void AddNoArgSignalHandler(const std::string& signalName, OBJ* obj, void (OBJ::* signal)(void))
    {
        NoArgSignalHandlerBase* handler = new NoArgSignalHandler<OBJ>(obj, signal);
        std::pair<NoArgSignalDispatcherMap::iterator, bool> ins = noArgSignalHandlers.insert(std::make_pair(signalName, handler));
        if (ins.second == false) {
            // if this was already there, overwrite and delete the old handler
            delete ins.first->second;
            ins.first->second = handler;
        }
    }

    class NoArgSignalHandlerBase {
      public:
        virtual ~NoArgSignalHandlerBase() { }
        virtual void Handle(void) = 0;
    };

    template <typename OBJ>
    class NoArgSignalHandler : public NoArgSignalHandlerBase {
        typedef void (OBJ::* HandlerFunction)(void);

      public:
        NoArgSignalHandler(OBJ* obj, HandlerFunction handleFunc) :
            object(obj), handler(handleFunc) { }

        virtual ~NoArgSignalHandler() { }

        virtual void Handle(void) {
            (object->*(handler))();
        }

        OBJ* object;
        HandlerFunction handler;
    };

    typedef std::map<std::string, NoArgSignalHandlerBase*> NoArgSignalDispatcherMap;
    NoArgSignalDispatcherMap noArgSignalHandlers;

    void DeleteNoArgSignalHandlers(void)
    {
        for (NoArgSignalDispatcherMap::iterator it = noArgSignalHandlers.begin(); it != noArgSignalHandlers.end(); it++) {
            delete it->second;
        }
        noArgSignalHandlers.clear();
    }

    void AddMethodHandlers();

    template <typename OBJ>
    void AddMethodReplyWithResponseCodeAndListOfIDsHandler(const std::string& methodName, OBJ* obj, void (OBJ::* methodReply)(LSFResponseCode &, LSFStringList &))
    {
        MethodReplyWithResponseCodeAndListOfIDsHandlerBase* handler = new MethodReplyWithResponseCodeAndListOfIDsHandler<OBJ>(obj, methodReply);
        std::pair<MethodReplyWithResponseCodeAndListOfIDsDispatcherMap::iterator, bool> ins = methodReplyWithResponseCodeAndListOfIDsHandlers.insert(std::make_pair(methodName, handler));
        if (ins.second == false) {
            // if this was already there, overwrite and delete the old handler
            delete ins.first->second;
            ins.first->second = handler;
        }
    }

    class MethodReplyWithResponseCodeAndListOfIDsHandlerBase {
      public:
        virtual ~MethodReplyWithResponseCodeAndListOfIDsHandlerBase() { }
        virtual void Handle(LSFResponseCode& responseCode, LSFStringList& idList) = 0;
    };

    template <typename OBJ>
    class MethodReplyWithResponseCodeAndListOfIDsHandler : public MethodReplyWithResponseCodeAndListOfIDsHandlerBase {
        typedef void (OBJ::* HandlerFunction)(LSFResponseCode&, LSFStringList&);

      public:
        MethodReplyWithResponseCodeAndListOfIDsHandler(OBJ* obj, HandlerFunction handleFunc) :
            object(obj), handler(handleFunc) { }

        virtual ~MethodReplyWithResponseCodeAndListOfIDsHandler() { }

        virtual void Handle(LSFResponseCode& responseCode, LSFStringList& idList) {
            (object->*(handler))(responseCode, idList);
        }

        OBJ* object;
        HandlerFunction handler;
    };

    typedef std::map<std::string, MethodReplyWithResponseCodeAndListOfIDsHandlerBase*> MethodReplyWithResponseCodeAndListOfIDsDispatcherMap;
    MethodReplyWithResponseCodeAndListOfIDsDispatcherMap methodReplyWithResponseCodeAndListOfIDsHandlers;

    template <typename OBJ>
    void AddMethodReplyWithResponseCodeIDAndNameHandler(const std::string& methodName, OBJ* obj, void (OBJ::* methodReply)(LSFResponseCode &, LSFString &, LSFString &))
    {
        MethodReplyWithResponseCodeIDAndNameHandlerBase* handler = new MethodReplyWithResponseCodeIDAndNameHandler<OBJ>(obj, methodReply);
        std::pair<MethodReplyWithResponseCodeIDAndNameDispatcherMap::iterator, bool> ins = methodReplyWithResponseCodeIDAndNameHandlers.insert(std::make_pair(methodName, handler));
        if (ins.second == false) {
            // if this was already there, overwrite and delete the old handler
            delete ins.first->second;
            ins.first->second = handler;
        }
    }

    class MethodReplyWithResponseCodeIDAndNameHandlerBase {
      public:
        virtual ~MethodReplyWithResponseCodeIDAndNameHandlerBase() { }
        virtual void Handle(LSFResponseCode& responseCode, LSFString& lsfId, LSFString& lsfName) = 0;
    };

    template <typename OBJ>
    class MethodReplyWithResponseCodeIDAndNameHandler : public MethodReplyWithResponseCodeIDAndNameHandlerBase {
        typedef void (OBJ::* HandlerFunction)(LSFResponseCode&, LSFString&, LSFString&);

      public:
        MethodReplyWithResponseCodeIDAndNameHandler(OBJ* obj, HandlerFunction handleFunc) :
            object(obj), handler(handleFunc) { }

        virtual ~MethodReplyWithResponseCodeIDAndNameHandler() { }

        virtual void Handle(LSFResponseCode& responseCode, LSFString& lsfId, LSFString& lsfName) {
            (object->*(handler))(responseCode, lsfId, lsfName);
        }

        OBJ* object;
        HandlerFunction handler;
    };

    typedef std::map<std::string, MethodReplyWithResponseCodeIDAndNameHandlerBase*> MethodReplyWithResponseCodeIDAndNameDispatcherMap;
    MethodReplyWithResponseCodeIDAndNameDispatcherMap methodReplyWithResponseCodeIDAndNameHandlers;

    template <typename OBJ>
    void AddMethodReplyWithResponseCodeAndIDHandler(const std::string& methodName, OBJ* obj, void (OBJ::* methodReply)(LSFResponseCode &, LSFString &))
    {
        MethodReplyWithResponseCodeAndIDHandlerBase* handler = new MethodReplyWithResponseCodeAndIDHandler<OBJ>(obj, methodReply);
        std::pair<MethodReplyWithResponseCodeAndIDDispatcherMap::iterator, bool> ins = methodReplyWithResponseCodeAndIDHandlers.insert(std::make_pair(methodName, handler));
        if (ins.second == false) {
            // if this was already there, overwrite and delete the old handler
            delete ins.first->second;
            ins.first->second = handler;
        }
    }

    class MethodReplyWithResponseCodeAndIDHandlerBase {
      public:
        virtual ~MethodReplyWithResponseCodeAndIDHandlerBase() { }
        virtual void Handle(LSFResponseCode& responseCode, LSFString& lsfId) = 0;
    };

    template <typename OBJ>
    class MethodReplyWithResponseCodeAndIDHandler : public MethodReplyWithResponseCodeAndIDHandlerBase {
        typedef void (OBJ::* HandlerFunction)(LSFResponseCode&, LSFString&);

      public:
        MethodReplyWithResponseCodeAndIDHandler(OBJ* obj, HandlerFunction handleFunc) :
            object(obj), handler(handleFunc) { }

        virtual ~MethodReplyWithResponseCodeAndIDHandler() { }

        virtual void Handle(LSFResponseCode& responseCode, LSFString& lsfId) {
            (object->*(handler))(responseCode, lsfId);
        }

        OBJ* object;
        HandlerFunction handler;
    };

    typedef std::map<std::string, MethodReplyWithResponseCodeAndIDHandlerBase*> MethodReplyWithResponseCodeAndIDDispatcherMap;
    MethodReplyWithResponseCodeAndIDDispatcherMap methodReplyWithResponseCodeAndIDHandlers;

    template <typename OBJ>
    void AddMethodReplyWithUint32ValueHandler(const std::string& methodName, OBJ* obj, void (OBJ::* methodReply)(uint32_t &))
    {
        MethodReplyWithUint32ValueHandlerBase* handler = new MethodReplyWithUint32ValueHandler<OBJ>(obj, methodReply);
        std::pair<MethodReplyWithUint32ValueDispatcherMap::iterator, bool> ins = methodReplyWithUint32ValueHandlers.insert(std::make_pair(methodName, handler));
        if (ins.second == false) {
            // if this was already there, overwrite and delete the old handler
            delete ins.first->second;
            ins.first->second = handler;
        }
    }

    class MethodReplyWithUint32ValueHandlerBase {
      public:
        virtual ~MethodReplyWithUint32ValueHandlerBase() { }
        virtual void Handle(uint32_t& value) = 0;
    };

    template <typename OBJ>
    class MethodReplyWithUint32ValueHandler : public MethodReplyWithUint32ValueHandlerBase {
        typedef void (OBJ::* HandlerFunction)(uint32_t& value);

      public:
        MethodReplyWithUint32ValueHandler(OBJ* obj, HandlerFunction handleFunc) :
            object(obj), handler(handleFunc) { }

        virtual ~MethodReplyWithUint32ValueHandler() { }

        virtual void Handle(uint32_t& value) {
            (object->*(handler))(value);
        }

        OBJ* object;
        HandlerFunction handler;
    };

    typedef std::map<std::string, MethodReplyWithUint32ValueHandlerBase*> MethodReplyWithUint32ValueDispatcherMap;
    MethodReplyWithUint32ValueDispatcherMap methodReplyWithUint32ValueHandlers;

    template <typename OBJ>
    void AddMethodReplyWithResponseCodeIDLanguageAndNameHandler(const std::string& methodName, OBJ* obj, void (OBJ::* methodReply)(LSFResponseCode &, LSFString &, LSFString &, LSFString &))
    {
        MethodReplyWithResponseCodeIDLanguageAndNameHandlerBase* handler = new MethodReplyWithResponseCodeIDLanguageAndNameHandler<OBJ>(obj, methodReply);
        std::pair<MethodReplyWithResponseCodeIDLanguageAndNameDispatcherMap::iterator, bool> ins = methodReplyWithResponseCodeIDLanguageAndNameHandlers.insert(std::make_pair(methodName, handler));
        if (ins.second == false) {
            // if this was already there, overwrite and delete the old handler
            delete ins.first->second;
            ins.first->second = handler;
        }
    }

    class MethodReplyWithResponseCodeIDLanguageAndNameHandlerBase {
      public:
        virtual ~MethodReplyWithResponseCodeIDLanguageAndNameHandlerBase() { }
        virtual void Handle(LSFResponseCode& responseCode, LSFString& lsfId, LSFString& language, LSFString& name) = 0;
    };

    template <typename OBJ>
    class MethodReplyWithResponseCodeIDLanguageAndNameHandler : public MethodReplyWithResponseCodeIDLanguageAndNameHandlerBase {
        typedef void (OBJ::* HandlerFunction)(LSFResponseCode& responseCode, LSFString& lsfId, LSFString& language, LSFString& name);

      public:
        MethodReplyWithResponseCodeIDLanguageAndNameHandler(OBJ* obj, HandlerFunction handleFunc) :
            object(obj), handler(handleFunc) { }

        virtual ~MethodReplyWithResponseCodeIDLanguageAndNameHandler() { }

        virtual void Handle(LSFResponseCode& responseCode, LSFString& lsfId, LSFString& language, LSFString& name) {
            (object->*(handler))(responseCode, lsfId, language, name);
        }

        OBJ* object;
        HandlerFunction handler;
    };

    typedef std::map<std::string, MethodReplyWithResponseCodeIDLanguageAndNameHandlerBase*> MethodReplyWithResponseCodeIDLanguageAndNameDispatcherMap;
    MethodReplyWithResponseCodeIDLanguageAndNameDispatcherMap methodReplyWithResponseCodeIDLanguageAndNameHandlers;

    void RemoveMethodHandlers() {
        for (MethodReplyWithUint32ValueDispatcherMap::iterator it = methodReplyWithUint32ValueHandlers.begin(); it != methodReplyWithUint32ValueHandlers.end(); it++) {
            delete it->second;
        }
        methodReplyWithUint32ValueHandlers.clear();

        for (MethodReplyWithResponseCodeAndListOfIDsDispatcherMap::iterator it = methodReplyWithResponseCodeAndListOfIDsHandlers.begin(); it != methodReplyWithResponseCodeAndListOfIDsHandlers.end(); it++) {
            delete it->second;
        }
        methodReplyWithResponseCodeAndListOfIDsHandlers.clear();

        for (MethodReplyWithResponseCodeIDAndNameDispatcherMap::iterator it = methodReplyWithResponseCodeIDAndNameHandlers.begin(); it != methodReplyWithResponseCodeIDAndNameHandlers.end(); it++) {
            delete it->second;
        }
        methodReplyWithResponseCodeIDAndNameHandlers.clear();

        for (MethodReplyWithResponseCodeIDLanguageAndNameDispatcherMap::iterator it = methodReplyWithResponseCodeIDLanguageAndNameHandlers.begin(); it != methodReplyWithResponseCodeIDLanguageAndNameHandlers.end(); it++) {
            delete it->second;
        }
        methodReplyWithResponseCodeIDLanguageAndNameHandlers.clear();

        for (MethodReplyWithResponseCodeAndIDDispatcherMap::iterator it = methodReplyWithResponseCodeAndIDHandlers.begin(); it != methodReplyWithResponseCodeAndIDHandlers.end(); it++) {
            delete it->second;
        }
        methodReplyWithResponseCodeAndIDHandlers.clear();
    }

    volatile sig_atomic_t stopped;

    uint64_t timeStopped;
};

template <typename OBJ>
ControllerClientStatus ControllerClient::MethodCallAsync(
    const char* ifaceName,
    const char* methodName,
    OBJ* receiver,
    void (OBJ::* replyFunc)(ajn::Message & message),
    const ajn::MsgArg* args,
    size_t numArgs)
{
    typedef TypeHandler<OBJ, void> HANDLER;
    HANDLER* handler = new HANDLER(receiver, replyFunc, this);
    ControllerClientStatus status = MethodCallAsyncHelper(
        ifaceName,
        methodName,
        handler,
        static_cast<ajn::MessageReceiver::ReplyHandler>(&HANDLER::MessageHandler),
        args,
        numArgs);

    if (status != CONTROLLER_CLIENT_OK) {
        delete handler;
        handler = NULL;
    }

    return status;
}

} // namespace lsf

#endif