AllJoynProvider.h 2.25 KB
Newer Older
1 2
#pragma once

3 4
#include "DeviceProviders/AllJoynStatus.h"

5 6 7
#include <alljoyn/AboutListener.h>

#include <map>
8
#include <memory>
9 10 11 12 13 14
#include <string>
#include <vector>
#include <queue>

class AllJoynService;

15
class AllJoynProvider : std::enable_shared_from_this<AllJoynProvider>
16 17 18 19 20 21 22 23 24 25 26 27 28
{
  friend class AboutListener;

public:
  AllJoynProvider(std::string const& appname);
  virtual ~AllJoynProvider();

  AllJoynStatus Start();
  void Shutdown();

  inline ajn::BusAttachment* GetBusAttachment() const
    { return m_bus; }

29
  void RemoveSession(std::shared_ptr<AllJoynService> const& service);
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

private:
  void AnnounceDiscovery(char const* serviceName, uint16_t version, ajn::SessionPort port,
    ajn::MsgArg const& objectDescriptionArg, ajn::MsgArg const& aboutDataArg);

  class AboutListener : public ajn::AboutListener
  {
  public:
    AboutListener(AllJoynProvider* provider)
      : m_parent(provider) { }

    virtual ~AboutListener() { }

    virtual void Announced(char const* busName, uint16_t version, ajn::SessionPort port,
        ajn::MsgArg const& objectDescriptionArg, ajn::MsgArg const& aboutDataArg);
  private:
    AllJoynProvider* m_parent;
  };

  void ProcessAboutAnnouncementQueue();

  static void* RunAboutHandlerProcessing(void* argp)
    { ((AllJoynProvider *) argp)->ProcessAboutAnnouncementQueue(); return 0;}

private:
  ajn::BusAttachment*   m_bus;
  bool                  m_alljoynInitialized;
  bool                  m_isRegistered;
  bool                  m_isListening;
  std::string           m_applicationName;
  AboutListener*        m_aboutListener;

  struct WorkItem
  {
    ajn::MsgArg ObjectDescriptionArg;
    ajn::MsgArg AboutDataArg;
    ajn::SessionPort Port;
    std::string ServiceName;

    void Reset()
    {
      ObjectDescriptionArg.Clear();
      AboutDataArg.Clear();
      Port = 0;
      ServiceName.clear();
    }
  };

  bool                  m_aboutQueueRun;
  pthread_t             m_thread;
  pthread_mutex_t       m_mutex;
  pthread_cond_t        m_cond;
  std::queue<WorkItem>  m_aboutHandlerQueue;

84 85
  typedef std::vector< std::shared_ptr<AllJoynService> > service_vector_type;
  typedef std::map< std::string, std::shared_ptr<AllJoynService> > service_map_type;
86 87 88 89 90

  service_map_type m_servicesMap;
  service_vector_type m_servicesVector;
};