RDK Documentation (Open Sourced RDK Components)
rtevents.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 #ifndef _COMMON_RT_EVENTS_H_
20 #define _COMMON_RT_EVENTS_H_
21 
22 #include <queue>
23 #include <glib.h>
24 #include <rtRemote.h>
25 #include "logger.h"
26 
27 class Event
28 {
29 protected:
30  rtObjectRef m_object;
31  rtString name() const
32  {
33  return m_object.get<rtString>("name");
34  }
35  rtObjectRef object() const
36  {
37  return m_object;
38  }
39  Event(rtObjectRef o) : m_object(o)
40  {
41  }
42 public:
43  Event(const char* eventName) : m_object(new rtMapObject)
44  {
45  m_object.set("name", eventName);
46  }
47  virtual ~Event() { }
48  friend class EventEmitter;
49 };
50 
52 {
53 public:
54  EventEmitter()
55  : m_emit(new rtEmit)
56  , m_timeoutId(0)
57  { }
58 
59  ~EventEmitter() {
60  if (m_timeoutId != 0)
61  g_source_remove(m_timeoutId);
62  }
63 
64  rtError setListener(const char* eventName, rtIFunction* f)
65  {
66  return m_emit->addListener(eventName, f);//call addListener instead of setListener due to marksForDelete bug in rtObject.cpp
67  }
68  rtError delListener(const char* eventName, rtIFunction* f)
69  {
70  return m_emit->delListener(eventName, f);
71  }
72  rtError send(Event&& event);
73 private:
74  rtEmitRef m_emit;
75  std::queue<rtObjectRef> m_eventQueue;
76  int m_timeoutId;
77 };
78 
79 #endif
Event
class to DRM Event handle
Definition: opencdmsessionadapter.h:24
EventEmitter
Definition: rtevents.h:51