Skip to content

Commit

Permalink
Watching page now polling for events.
Browse files Browse the repository at this point in the history
- Added default poll time on client.
  • Loading branch information
mstead committed Nov 18, 2010
1 parent 1ce1a65 commit 8047a1b
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 191 deletions.
1 change: 0 additions & 1 deletion ted-server/src/main/java/nu/ted/event/EventRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public List<Event> getEvents(Date from) {
for (Event regEvent : events) {
cal.setTimeInMillis(regEvent.getRegisteredOn().getValue());
Date eventDate = cal.getTime();
System.err.println(eventDate + " ---> " + from);
if (eventDate.after(from)) {
matched.add(regEvent);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,49 @@
package nu.ted.domain;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import nu.ted.Server;
import nu.ted.SearcherTest.TestTorrentSource;
import nu.ted.generated.TDate;
import nu.ted.generated.Episode;
import nu.ted.generated.EpisodeStatus;
import nu.ted.generated.Event;
import nu.ted.generated.EventType;
import nu.ted.generated.Series;
import nu.ted.generated.TDate;
import nu.ted.guide.GuideFactory;
import nu.ted.guide.TestGuide;
import nu.ted.service.TedServiceImpl;

import static org.junit.Assert.*;

import org.junit.Test;

public class SeriesBackendWrapperTest {

@Test
public void ensureEventFiredWhenEpisodeAdded() throws Exception {
TDate lastPoll = new TDate(Calendar.getInstance().getTimeInMillis());
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, -2);
TDate lastPoll = new TDate(calendar.getTimeInMillis());

TestGuide seriesSource = new TestGuide();
GuideFactory.addGuide(seriesSource);
TedServiceImpl service = new TedServiceImpl(Server.createDefaultTed(), seriesSource);

List<Event> current = service.getEvents(lastPoll);

Series series = new Series();
series.setGuideName(seriesSource.getName());
SeriesBackendWrapper wrapper = new SeriesBackendWrapper(series);
wrapper.update(Calendar.getInstance());

List<Event> events = service.getEvents(lastPoll);
// Clear out events that existed before add (static registry).
events.removeAll(current);
assertEquals(1, events.size());

Event received = events.get(0);
Expand Down
5 changes: 2 additions & 3 deletions ted-server/src/test/java/nu/ted/event/EventRegistryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ public void ensureGetEventsReturnsOnlyEventsSinceLastCheckDate() {
}

@Test
public void ensureGetEventsIncludesEventsPostedOnExactLastPostTime() {
public void ensureGetEventsDoesNotIncludeEventsPostedOnExactLastPostTime() {
TestEventRegistry registry = new TestEventRegistry();
Date lastPoll = getTime(Calendar.MAY, 11, 2011);
registry.setNextRegisterDate(lastPoll);
registry.addEvent(EventFactory.createEpisodeAddedEvent(null, null));

List<Event> events = registry.getEvents(lastPoll);
assertEquals(1, events.size());
assertEquals(EventType.EPISODE_ADDED, events.get(0).getType());
assertEquals(0, events.size());
}

@Test
Expand Down
12 changes: 10 additions & 2 deletions ted-server/src/test/java/nu/ted/service/TedTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ public void ensureGetOverviewFromService() throws TException
@Test
public void shouldRegisterWatchedListChangedEventIfStartWatching() throws TException, InvalidOperation{
TedServiceImpl ted = new TedServiceImpl(Server.createDefaultTed(), new TestGuide());
TDate lastCheck = new TDate(Calendar.getInstance().getTimeInMillis());

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, -1);
TDate lastCheck = new TDate(calendar.getTimeInMillis());

List<Event> initialEvents = ted.getEvents(lastCheck);

ted.startWatching("E");
Expand Down Expand Up @@ -171,10 +175,14 @@ public void shouldRegisterWatchedListChangedEventIfStopWatching() throws TExcept
TedServiceImpl ted = new TedServiceImpl(Server.createDefaultTed(), new TestGuide());
short id = ted.startWatching("E");

TDate lastCheck = new TDate(Calendar.getInstance().getTimeInMillis());
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, -1);
TDate lastCheck = new TDate(calendar.getTimeInMillis());
List<Event> initialEvents = ted.getEvents(lastCheck);

ted.stopWatching(id);

// lastCheck = new TDate(calendar.getTimeInMillis());
List<Event> events = ted.getEvents(lastCheck);

// Remove the initial events as the Event Registry is a static
Expand Down
29 changes: 29 additions & 0 deletions ted-ui/php-ted/src/controller/WatchingController.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,34 @@ public function stopWatching($id) {
header('Location: /' . __SITE_ROOT . '/watching');
exit;
}

public function poll($lastPollTime) {
$latestEventDate = (int)$lastPollTime;

$pollDate = new TDate(array('value'=>$latestEventDate));
$events = $this->registry->client->getEvents($pollDate);
$eventsJson = array();
foreach ($events as $event) {
if ($event->type == EventType::WATCHED_SERIES_ADDED || $event->type == EventType::EPISODE_ADDED) {
ob_start();
$series = $event->series;
include 'views/watching_content.php';
$seriesHtml = ob_get_clean();
array_push($eventsJson, array('type'=>$event->type,
'uid'=>$event->series->uid,
'seriesHtml'=>$seriesHtml));
}
else if ($event->type == EventType::WATCHED_SERIES_REMOVED) {
array_push($eventsJson, array('type'=>$event->type, 'uid'=>$event->series->uid));
}

$eventMillis = $event->registeredOn->value;
if ($latestEventDate < $eventMillis) {
$latestEventDate = $eventMillis;
}
}

echo json_encode(array('lastPolled'=>$latestEventDate, 'events'=>$eventsJson));
}
}
?>
3 changes: 2 additions & 1 deletion ted-ui/php-ted/src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
$path_parts = explode('/', $site_path);
define ('__SITE_ROOT', $path_parts[sizeof($path_parts) - 1]);

/*** include the init.php file ***/
define ('__POLL_INTERVAL', 5000);

include 'includes/init.php';

$registry->router = new router($registry);
Expand Down
82 changes: 0 additions & 82 deletions ted-ui/php-ted/src/pages/watching.php

This file was deleted.

23 changes: 0 additions & 23 deletions ted-ui/php-ted/src/pages/watching_content.php

This file was deleted.

1 change: 1 addition & 0 deletions ted-ui/php-ted/src/views/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<head>
<title>TED - <?php echo $view_title; ?> </title>
<link href="views/ted.css" rel="stylesheet" type="text/css" />
<script src="lib/jquery.js" language="javascript" type="text/javascript"></script>
</head>
<body>
<div id="container">
Expand Down
78 changes: 74 additions & 4 deletions ted-ui/php-ted/src/views/watching.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,79 @@
<?php
if (sizeof($watch_list) == 0) {
echo "You are not watching any shows.";
function createNotWatchingDiv() {
return "<div id='not_watching'>You are not watching any shows.</div>";
}

foreach ($watch_list as $series) {
include 'watching_content.php';
// Create necessary JS functions.
$script = "
function showLocalDate(timestamp)
{
var dt = new Date(timestamp);
document.write(dt.toLocaleString());
}
function process(data) {
if (data.length <= 0) {
poll(new Date().getTime());
return;
}
var lastPollTime = data.lastPolled;
var i;
var events = data.events;
for (i in events) {
// Watch added.
if (1 == events[i].type) {
if ($('#not_watching').length) {
$('#not_watching').remove();
}
$('#watch_list').append(events[i].seriesHtml);
}
// Remove series.
else if (2 == events[i].type) {
$('#' + events[i].uid ).remove();
if( $('#watch_list').children().length == 0) {
$('#watch_list').append(\"" . createNotWatchingDiv() . "\");
}
}
// episode added.
else if (3 == events[i].type) {
if ($('#' + events[i].uid ).length) {
$('#' + events[i].uid ).replaceWith(events[i].seriesHtml);
}
else {
$('#watch_list').append(events[i].seriesHtml);
}
}
}
// Wait here. Since we share a connection to the ted
// server, waiting on the server ties up the connection,
// as the call is not asynchronous.
setTimeout('poll(' + lastPollTime + ')', " . __POLL_INTERVAL .");
}
function poll(lastPolled) {
$.post('watching/poll/' + lastPolled, {}, process, 'json');
}
";
include 'javascript.php';

echo "<div id='watch_list'>";
if (sizeof($watch_list) == 0) {
echo createNotWatchingDiv();
}
else {
foreach ($watch_list as $series) {
include 'watching_content.php';
}
}
echo "</div>";
?>

<?php
$script = "window.onload = poll(new Date().getTime());";
include 'javascript.php';
?>

0 comments on commit 8047a1b

Please sign in to comment.