RDK Documentation (Open Sourced RDK Components)
RtUtils.h
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2018 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 #ifndef _RT_UTILS_H_
21 #define _RT_UTILS_H_
22 #include <queue>
23 
24 #include <rtRemote.h>
25 #include <rtError.h>
26 
27 class RtUtils {
28 public:
29  class Event
30  {
31  protected:
32  rtObjectRef m_object;
33  rtString name() const
34  {
35  return m_object.get<rtString>("name");
36  }
37  Event(rtObjectRef o) : m_object(o)
38  {
39  }
40  public:
41  Event(const char* eventName) : m_object(new rtMapObject)
42  {
43  m_object.set("name", eventName);
44  }
45  rtObjectRef object() const
46  {
47  return m_object;
48  }
49  virtual ~Event() { }
50  friend class EventEmitter;
51  };
52 
54  {
55  public:
56  EventEmitter()
57  : m_emit(new rtEmit)
58  , m_timeoutId(0)
59  , mRemoteReady(false) {
60  pthread_mutex_init(&mMutex, NULL);
61  }
62 
63  ~EventEmitter() {
64  pthread_mutex_destroy(&mMutex);
65  }
66 
67  rtError setListener(const char* eventName, rtIFunction* f)
68  {
69  return m_emit->setListener(eventName, f);
70  }
71  rtError delListener(const char* eventName, rtIFunction* f)
72  {
73  return m_emit->delListener(eventName, f);
74  }
75  void remoteObjectReady()
76  {
77  mRemoteReady = true;
78  processEvents();
79  }
80  rtError send(Event* event);
81  private:
82  void processEvents();
83  rtEmitRef m_emit;
84  std::queue<rtObjectRef> mEventQueue;
85  pthread_mutex_t mMutex;
86  int m_timeoutId;
87  bool mRemoteReady;
88  };
89 
91  {
92  pthread_t mThread;
93  pthread_cond_t mCond;
94  pthread_mutex_t mMutex;
95  bool mRunning;
96  };
97 
98  RtUtils();
99  virtual ~RtUtils();
100  virtual rtError initRt();
101 
102  static void rtRemoteCallback(void* ctx);
103  void setStarted(bool started);
104  void send(Event* event);
105  rtError setListener(rtString eventName, const rtFunctionRef& f);
106  rtError delListener(rtString eventName, const rtFunctionRef& f);
107  void remoteObjectReady();
108 
109 private:
110  void setDedicatedThreadRunning(bool running);
111  bool isDedicatedThreadRunning();
112  static void * RtMessageThread(void * ctx);
113  void ProcessRtItem();
114  RtProcessThreadData mThreadData;
115  bool mStarted;
116  EventEmitter mEventEmitter;
117  bool mRemoteReady;
118 };
119 
120 #endif /* _NF_RT_UTILS_H_ */
RtUtils::RtProcessThreadData
Definition: RtUtils.h:90
RtUtils
Definition: RtUtils.h:27
RtUtils::EventEmitter
Definition: RtUtils.h:53
RtUtils::Event
Definition: RtUtils.h:29