RDK Documentation (Open Sourced RDK Components)
ReservationTimerTask.cpp
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 2016 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 
21 /**
22 * @defgroup trm
23 * @{
24 * @defgroup qtapp
25 * @{
26 **/
27 
28 
29 #include "trm/TRM.h"
30 #include "trm/JsonEncoder.h"
31 
32 #include "Manager.h"
33 #include "Server.h"
34 #include "Util.h"
35 #include "ReservationTimerTask.h"
36 #include "Connection.h"
37 
38 extern void printThread(const char *func);
39 extern TRM::Server *serverInstance;
40 
41 TRM_BEGIN_NAMESPACE
42 
43 ReservationExpirationTimerTask::ReservationExpirationTimerTask(Timer &timer, const std::string &reservationToken)
44 : timer(timer), reservationToken(reservationToken) {}
45 
46 ReservationExpirationTimerTask::~ReservationExpirationTimerTask(void) {}
47 
48 void ReservationExpirationTimerTask::run(void)
49 {
50  Log() << "Reservation " << reservationToken << " Timer "
51  << timer.getToken()
52  << " Expired at " << time(0) << std::endl;
53 
54  TunerReservation &expired = Manager::getInstance().getReservation(reservationToken);
55  try
56  {
57  Assert(expired.state == TunerReservation::ACTIVE);
58  const ReservationAttributes &attrs = Manager::getInstance().getReservationAttributes(reservationToken);
59  try {
60  const Connection &connection = ::serverInstance->getConnection(attrs.clientId);
61  NotifyTunerReservationRelease notification(GenerateUUID(), reservationToken, "expired");
62  {
63  std::vector<uint8_t> out;
64  SerializeMessage(notification, attrs.clientId, out);
65  connection.sendAsync(out);
66  }
67  }
68  catch(...) {
69 
70  }
71  }
72  catch(...) {
73  Log() << "Reservation released failed: " << std::endl;
74  }
75  Log() << "Reservation Expired : " << std::endl;
76  expired.print();
77  expired.state = TunerReservation::EXPIRED;
78  Manager::getInstance().releaseReservation(reservationToken);
79 };
80 
81 Timer & ReservationExpirationTimerTask::getTimer(void)
82 {
83  return timer;
84 }
85 
86 
87 ReservationStartTimerTask::ReservationStartTimerTask(Timer &timer, const std::string &reservationToken)
88 : timer(timer), reservationToken(reservationToken) {}
89 
90 ReservationStartTimerTask::~ReservationStartTimerTask(void) {}
91 
92 void ReservationStartTimerTask::run(void)
93 {
94  Log() << "Reservation " << reservationToken << " Start Timer "
95  << timer.getToken()
96  << " Started at " << time(0) << std::endl;
97 
98  TunerReservation &started = Manager::getInstance().getReservation(reservationToken);
99  try
100  {
101  Assert(started.state == TunerReservation::IDLE);
102  printf("Starting Reservation %p\r\n", &started);
103 
104  /* Update the state of the tuner */
105  Tuner & tuner = Manager::getInstance().getTuner(Manager::getInstance().getParent(reservationToken));
106 #if 0
107  TunerState state = tuner.getState();
108  state = state + started.getActivity().getActivity();
109  tuner.setState(state);
110 #else
111  /*Tuner state is calculated dynamically based on the currentn token held at the time of getState() call. */
112 #endif
113 
114  started.state = TunerReservation::ACTIVE;
115  Log() << "New Tuner State is " << (const char *) tuner.getState().getState() << std::endl;
116 
117  /* If tuner state is hybrid, make sure the sourceIds of LIVE and RECORD are same */
118  if (tuner.getState().getState() == TunerState::kHybrid) {
119  Manager::getInstance().syncHybrid(tuner.getId(), reservationToken);
120  }
121 
122  Manager::getInstance().startReservation(reservationToken);
123  Manager::getInstance().adjustExpireTimer(reservationToken);
124  }
125  catch(...) {
126  Log() << "Reservation start failed : " << std::endl;
127  }
128  Log() << "Reservation started : " << std::endl;
129  started.print();
130 };
131 
132 Timer & ReservationStartTimerTask::getTimer(void)
133 {
134  return timer;
135 }
136 
137 
138 ReservationPreStartTimerTask::ReservationPreStartTimerTask(Timer &timer, const std::string &reservationToken)
139 : timer(timer), reservationToken(reservationToken) {}
140 
141 ReservationPreStartTimerTask::~ReservationPreStartTimerTask(void) {}
142 
143 void ReservationPreStartTimerTask::run(void)
144 {
145  Log() << "Reservation " << reservationToken << " PreStart Timer "
146  << timer.getToken()
147  << " Started at " << time(0) << std::endl;
148 
149  TunerReservation &started = Manager::getInstance().getReservation(reservationToken);
150  try
151  {
152  Assert(started.state == TunerReservation::IDLE);
153  printf("About to start Reservation %p\r\n", &started);
154 
155  /* Update the state of the tuner */
156  Tuner & tuner = Manager::getInstance().getTuner(Manager::getInstance().getParent(reservationToken));
157  Log() << "Current Tuner State is " << (const char *) tuner.getState().getState() << std::endl;
158 
159  /* If tuner state is hybrid, make sure the sourceIds of LIVE and RECORD are same */
160  if ( (started.getActivity().getActivity() == Activity::kRecord) && (tuner.getState().getState() == TunerState::kHybrid)) {
161  Manager::getInstance().prepareRecordHandover(tuner.getId(), reservationToken);
162  }
163  }
164  catch(...) {
165  Log() << "Reservation start failed : " << std::endl;
166  }
167  Log() << "Reservation is about to start: " << std::endl;
168  started.print();
169 };
170 
171 Timer & ReservationPreStartTimerTask::getTimer(void)
172 {
173  return timer;
174 }
175 
176 PendingRequestTimeoutTimerTask::PendingRequestTimeoutTimerTask(Timer &timer, const std::string &uuid)
177 : timer(timer), uuid(uuid) {}
178 
179 PendingRequestTimeoutTimerTask::~PendingRequestTimeoutTimerTask(void) {}
180 
181 void PendingRequestTimeoutTimerTask::run(void)
182 {
183  /* If PendingReservation is record, grant reservation */
184  try
185  {
186 
187  PendingRequestProcessor &pending = Manager::getInstance().getPendingRequest(timer.getToken());
188 
189  Log() << "PendingRequest [type=" << pending.getType() << "] " << "uuid=" << pending.getUUID() << " "
190  << "Timer " << timer.getToken() << " timeout" << std::endl;
191 
192  pending.timeout();
193  Manager::getInstance().removePendingRequest(pending.getUUID());
194  delete &pending;
195  }
196  catch(...) {
197  }
198 };
199 
200 Timer & PendingRequestTimeoutTimerTask::getTimer(void)
201 {
202  return timer;
203 }
204 TRM_END_NAMESPACE
205 
206 
207 /** @} */
208 /** @} */
Connection
The connection class provides APIs that allows the application to access CEC Bus. A connection is a t...
Definition: Connection.hpp:57
Connection::sendAsync
void sendAsync(const CECFrame &frame)
This function is used to send the CEC frame to physical CEC Bus using asynchronous method.
Definition: Connection.cpp:283
Timer
Definition: timer.h:42
TRM::Server
Definition: Server.h:42
Tuner
Definition: Tuner.h:43