Twitch SDK (Internal)
pubsubcomponent.h
Go to the documentation of this file.
1 /********************************************************************************************
2  * Twitch Broadcasting SDK
3  *
4  * This software is supplied under the terms of a license agreement with Twitch Interactive, Inc. and
5  * may not be copied or used except in accordance with the terms of that agreement
6  * Copyright (c) 2012-2017 Twitch Interactive, Inc.
7  *********************************************************************************************/
8 
9 #pragma once
10 
13 
14 #include <memory>
15 #include <vector>
16 
17 namespace ttv
18 {
19  class PubSubClient;
20  class PubSubComponentBase;
21  template <typename LISTENER> class PubSubComponent;
22 }
23 
24 
26 {
27 public:
28  using DisposerFunc = std::function<void()>;
29 public:
30  PubSubComponentBase(const std::shared_ptr<User>& user);
31  virtual ~PubSubComponentBase() = default;
32 
33  void SetDisposer(DisposerFunc&& func) { mDisposerFunc = func; }
34 
36 
37  // UserComponent overrides
38  virtual TTV_ErrorCode Initialize() override;
39  virtual TTV_ErrorCode Shutdown() override;
40  virtual std::string GetLoggerName() const override = 0;
41 
42  virtual void OnTopicSubscribeStateChanged(const std::string& topic, PubSubClient::SubscribeState::Enum state, TTV_ErrorCode ec) = 0;
43  virtual void OnTopicMessageReceived(const std::string& topic, const json::Value& msg) = 0;
44  virtual void OnTopicListenerRemoved(const std::string& topic, TTV_ErrorCode ec);
45 
46 protected:
47  // Component overrides
48  virtual bool CheckShutdown() override;
49  virtual void CompleteShutdown() override;
50 
51  virtual void AddTopic(const std::string& topic);
53 
55  {
56  public:
58  : mOwner(owner)
59  {}
60 
61  // PubSubClient::ITopicListener implementation
62  virtual void OnTopicSubscribeStateChanged(PubSubClient* source, const std::string& topic, PubSubClient::SubscribeState::Enum state, TTV_ErrorCode ec) override;
63  virtual void OnTopicMessageReceived(PubSubClient* source, const std::string& topic, const json::Value& msg) override;
64  virtual void OnTopicListenerRemoved(PubSubClient* source, const std::string& topic, TTV_ErrorCode ec) override;
65 
66  private:
68  };
69 
70  std::shared_ptr<PubSubClient> mPubSub;
71  std::shared_ptr<PubSubTopicListener> mPubSubTopicListener;
72  std::shared_ptr<PubSubTopicListenerHelper> mPubSubTopicListenerHelper;
74  std::vector<std::string> mPubSubTopics;
75 };
76 
77 
81 template <typename LISTENER>
83 {
84 public:
85  PubSubComponent(const std::shared_ptr<User>& user)
86  : PubSubComponentBase(user)
87  {
88  }
89  virtual ~PubSubComponent() = default;
90 
91  virtual void CompleteShutdown() override
92  {
94  mListener.reset();
95  }
96 
97  // Call this setter before Initialize()
98  void SetListener(const std::shared_ptr<LISTENER>& listener) { mListener = listener; }
99 
100 protected:
101  std::shared_ptr<LISTENER> mListener;
102 };
virtual void OnTopicSubscribeStateChanged(const std::string &topic, PubSubClient::SubscribeState::Enum state, TTV_ErrorCode ec)=0
virtual void OnTopicListenerRemoved(const std::string &topic, TTV_ErrorCode ec)
void SetListener(const std::shared_ptr< LISTENER > &listener)
Definition: pubsubcomponent.h:98
virtual TTV_ErrorCode Shutdown() override
Definition: pubsubcomponent.h:54
virtual void OnTopicMessageReceived(PubSubClient *source, const std::string &topic, const json::Value &msg) override
std::function< void()> DisposerFunc
Definition: pubsubcomponent.h:28
PubSubTopicListener(PubSubComponentBase *owner)
Definition: pubsubcomponent.h:57
Enum
Definition: pubsubclient.h:91
virtual TTV_ErrorCode SubscribeTopics()
std::shared_ptr< LISTENER > mListener
Definition: pubsubcomponent.h:101
virtual void OnTopicSubscribeStateChanged(PubSubClient *source, const std::string &topic, PubSubClient::SubscribeState::Enum state, TTV_ErrorCode ec) override
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
PubSubComponentBase * mOwner
Definition: pubsubcomponent.h:67
Represents a JSON value.
Definition: value.h:114
virtual void CompleteShutdown() override
Definition: pubsubclient.h:86
void SetDisposer(DisposerFunc &&func)
Definition: pubsubcomponent.h:33
virtual TTV_ErrorCode Initialize() override
TTV_ErrorCode Dispose()
virtual void OnTopicMessageReceived(const std::string &topic, const json::Value &msg)=0
PubSubComponent(const std::shared_ptr< User > &user)
Definition: pubsubcomponent.h:85
uint32_t TTV_ErrorCode
Definition: errortypes.h:30
Definition: component.h:87
std::shared_ptr< PubSubClient > mPubSub
Definition: pubsubcomponent.h:70
virtual void OnTopicListenerRemoved(PubSubClient *source, const std::string &topic, TTV_ErrorCode ec) override
virtual void CompleteShutdown() override
Definition: pubsubcomponent.h:91
Definition: pubsubclient.h:117
DisposerFunc mDisposerFunc
Definition: pubsubcomponent.h:73
Definition: pubsubcomponent.h:25
virtual void AddTopic(const std::string &topic)
virtual ~PubSubComponentBase()=default
std::vector< std::string > mPubSubTopics
Definition: pubsubcomponent.h:74
std::shared_ptr< PubSubTopicListener > mPubSubTopicListener
Definition: pubsubcomponent.h:71
virtual bool CheckShutdown() override
std::shared_ptr< PubSubTopicListenerHelper > mPubSubTopicListenerHelper
Definition: pubsubcomponent.h:72
Definition: pubsubcomponent.h:21
virtual std::string GetLoggerName() const override=0
PubSubComponentBase(const std::shared_ptr< User > &user)