From fdd0117d6f5d4043e195f3abcf8f6856d16d613f Mon Sep 17 00:00:00 2001 From: nebojsa Date: Wed, 4 Nov 2009 21:59:05 +0000 Subject: [PATCH] prepairing infrastructure for scheduler immediate start feature --- .../EventScheduler/EventContainerInterface.h | 11 ++++ .../eventScheduler/src/SchedulerThread.cxx | 26 ++++++++- .../eventScheduler/src/SchedulerThread.h | 8 +++ .../scheduler/src/PlaylistEventContainer.cxx | 20 +++++++ .../scheduler/src/PlaylistEventContainer.h | 11 ++++ .../scheduler/src/PostgresqlSchedule.cxx | 57 +++++++++++++++++++ .../scheduler/src/PostgresqlSchedule.h | 16 ++++++ .../scheduler/src/ScheduleInterface.h | 13 +++++ 8 files changed, 161 insertions(+), 1 deletion(-) diff --git a/campcaster/src/modules/eventScheduler/include/LiveSupport/EventScheduler/EventContainerInterface.h b/campcaster/src/modules/eventScheduler/include/LiveSupport/EventScheduler/EventContainerInterface.h index b70b28c87..4e2749ede 100644 --- a/campcaster/src/modules/eventScheduler/include/LiveSupport/EventScheduler/EventContainerInterface.h +++ b/campcaster/src/modules/eventScheduler/include/LiveSupport/EventScheduler/EventContainerInterface.h @@ -89,6 +89,17 @@ class EventContainerInterface */ virtual Ptr::Ref getNextEvent(Ptr::Ref when) throw () = 0; + + + /** + * Return current event + * + * @param + * @return the first event to schedule at this point in time + * may be a reference to 0, if there are no known events at this time + */ + virtual Ptr::Ref + getCurrentEvent() throw () = 0; }; diff --git a/campcaster/src/modules/eventScheduler/src/SchedulerThread.cxx b/campcaster/src/modules/eventScheduler/src/SchedulerThread.cxx index 662f21776..8dd22e385 100644 --- a/campcaster/src/modules/eventScheduler/src/SchedulerThread.cxx +++ b/campcaster/src/modules/eventScheduler/src/SchedulerThread.cxx @@ -95,6 +95,29 @@ SchedulerThread :: getNextEvent(Ptr::Ref when) throw () } } +/*------------------------------------------------------------------------------ + * Get the next event from the eventContainer + *----------------------------------------------------------------------------*/ +void +SchedulerThread :: getCurrentEvent() throw () +{ + //DEBUG_FUNC_INFO + + nextEvent = eventContainer->getCurrentEvent(); + if (nextEvent.get()) { + nextEventTime = TimeConversion::now(); + nextInitTime.reset(new ptime(*nextEventTime)); + nextEventEnd.reset(new ptime(*nextEvent->getScheduledTime() + + *nextEvent->eventLength())); + debug() << "::getCurrentEvent() - nextInitTime: " + << to_simple_string(*nextInitTime) << endl; + debug() << " - nextEventTime: " + << to_simple_string(*nextEventTime) << endl; + debug() << " - nextEventEnd: " + << to_simple_string(*nextEventEnd) << endl; + } +} + /*------------------------------------------------------------------------------ * The main execution body of the thread. @@ -158,7 +181,8 @@ SchedulerThread :: run(void) throw () //DEBUG_FUNC_INFO shouldRun = true; - getNextEvent(TimeConversion::now()); +// getCurrentEvent(); + getNextEvent(); while (shouldRun) { Ptr::Ref start = TimeConversion::now(); diff --git a/campcaster/src/modules/eventScheduler/src/SchedulerThread.h b/campcaster/src/modules/eventScheduler/src/SchedulerThread.h index f72cc74e4..f4e35323c 100644 --- a/campcaster/src/modules/eventScheduler/src/SchedulerThread.h +++ b/campcaster/src/modules/eventScheduler/src/SchedulerThread.h @@ -153,6 +153,14 @@ class SchedulerThread : public virtual RunnableInterface void getNextEvent(Ptr::Ref when) throw (); + /** + * Get the current event. + * + * @param + */ + void + getCurrentEvent() throw (); + /** * Tell if the specified time falls within now and the next * waking up. Basically tells if it is within now and diff --git a/campcaster/src/products/scheduler/src/PlaylistEventContainer.cxx b/campcaster/src/products/scheduler/src/PlaylistEventContainer.cxx index c22520a9e..ac2c091b9 100644 --- a/campcaster/src/products/scheduler/src/PlaylistEventContainer.cxx +++ b/campcaster/src/products/scheduler/src/PlaylistEventContainer.cxx @@ -98,3 +98,23 @@ PlaylistEventContainer :: getNextEvent(Ptr::Ref when) throw () return event; } +/*------------------------------------------------------------------------------ + * Return the first scheduled event after the specified timepoint + *----------------------------------------------------------------------------*/ +Ptr::Ref +PlaylistEventContainer :: getCurrentEvent() throw () +{ + Ptr::Ref entry = schedule->getCurrentEntry(); + Ptr::Ref event; + + if (entry.get()) { + event.reset(new PlaylistEvent(sessionId, + audioPlayer, + storage, + playLog, + entry)); + } + + return event; +} + diff --git a/campcaster/src/products/scheduler/src/PlaylistEventContainer.h b/campcaster/src/products/scheduler/src/PlaylistEventContainer.h index d8d7b3bd9..ba2f0def8 100644 --- a/campcaster/src/products/scheduler/src/PlaylistEventContainer.h +++ b/campcaster/src/products/scheduler/src/PlaylistEventContainer.h @@ -142,6 +142,17 @@ class PlaylistEventContainer : public virtual EventContainerInterface */ virtual Ptr::Ref getNextEvent(Ptr::Ref when) throw (); + + + /** + * Return current event + * + * @param + * @return the first event to schedule at this point in time + * may be a reference to 0, if there are no known events at this time + */ + virtual Ptr::Ref + getCurrentEvent() throw (); }; diff --git a/campcaster/src/products/scheduler/src/PostgresqlSchedule.cxx b/campcaster/src/products/scheduler/src/PostgresqlSchedule.cxx index 948f445d8..83a79c2c5 100644 --- a/campcaster/src/products/scheduler/src/PostgresqlSchedule.cxx +++ b/campcaster/src/products/scheduler/src/PostgresqlSchedule.cxx @@ -161,6 +161,16 @@ const std::string PostgresqlSchedule::getNextEntryStmt = "SELECT id, playlist, starts, ends FROM schedule WHERE ? < starts " "ORDER BY starts"; +/*------------------------------------------------------------------------------ + * The SQL statement for querying current scheduled entry + * The parameters for this call are: from + * and returns the properties: id, playlist, starts, ends for the current + * schedule entry + *----------------------------------------------------------------------------*/ +const std::string PostgresqlSchedule::getCurrentEntryStmt = + "SELECT id, playlist, starts, ends FROM schedule WHERE starts <= ? AND ? < ends " + "ORDER BY starts"; + /*------------------------------------------------------------------------------ * The SQL statement for querying if a schedule entry exists. * Expects a single argument, the id of the schedule to check. @@ -542,6 +552,53 @@ PostgresqlSchedule :: getNextEntry(Ptr::Ref fromTime) } +/*------------------------------------------------------------------------------ + * Get current schedule entry + *----------------------------------------------------------------------------*/ +Ptr::Ref +PostgresqlSchedule :: getCurrentEntry() + throw () +{ + Ptr::Ref conn; + Ptr::Ref result; + + try { + conn = cm->getConnection(); + Ptr::Ref timestamp; + Ptr::Ref pstmt(conn->prepareStatement( + getCurrentEntryStmt)); + timestamp = Conversion::ptimeToTimestamp(TimeConversion::now(), + Conversion::roundDown); + + pstmt->setTimestamp(1, *timestamp); + pstmt->setTimestamp(2, *timestamp); + + Ptr::Ref rs(pstmt->executeQuery()); + if (rs->next()) { + Ptr::Ref id(new UniqueId(rs->getLong(1))); + Ptr::Ref playlistId(new UniqueId(rs->getLong(2))); + + *timestamp = rs->getTimestamp(3); + Ptr::Ref startTime = Conversion::timestampToPtime(timestamp); + + *timestamp = rs->getTimestamp(4); + Ptr::Ref endTime = Conversion::timestampToPtime(timestamp); + + result.reset(new ScheduleEntry(id, playlistId, startTime, endTime)); + } + + cm->returnConnection(conn); + } catch (std::exception &e) { + if (conn) { + cm->returnConnection(conn); + } + // TODO: report error + return result; + } + + return result; +} + /*------------------------------------------------------------------------------ * Tell if a schedule entry exists. *----------------------------------------------------------------------------*/ diff --git a/campcaster/src/products/scheduler/src/PostgresqlSchedule.h b/campcaster/src/products/scheduler/src/PostgresqlSchedule.h index 40ebf5147..0337acdc2 100644 --- a/campcaster/src/products/scheduler/src/PostgresqlSchedule.h +++ b/campcaster/src/products/scheduler/src/PostgresqlSchedule.h @@ -154,6 +154,11 @@ class PostgresqlSchedule : public Configurable, */ static const std::string getNextEntryStmt; + /** + * The SQL statement for getting current schedule entry + */ + static const std::string getCurrentEntryStmt; + /** * The SQL statement for telling if a schedule entry exists. */ @@ -320,6 +325,17 @@ class PostgresqlSchedule : public Configurable, getNextEntry(Ptr::Ref fromTime) throw (); + /** + * Return current schedule entry + * + * @param + * + * @return the first schedule entry at this point in time + */ + virtual Ptr::Ref + getCurrentEntry() + throw (); + /** * Tell if a schedule entry exists by the give name. * diff --git a/campcaster/src/products/scheduler/src/ScheduleInterface.h b/campcaster/src/products/scheduler/src/ScheduleInterface.h index 5f5ee644d..95a716ffc 100644 --- a/campcaster/src/products/scheduler/src/ScheduleInterface.h +++ b/campcaster/src/products/scheduler/src/ScheduleInterface.h @@ -178,6 +178,19 @@ class ScheduleInterface throw () = 0; + /** + * Return the current schedule entry. + * + * + * @param + * + * @return the current schedule entry + */ + virtual Ptr::Ref + getCurrentEntry() + throw () + = 0; + /** * Tell if a schedule entry exists by the give name. *