[Redbutton-devel] SF.net SVN: redbutton: [227]
Brought to you by:
skilvington
|
From: <ski...@us...> - 2007-02-19 17:16:26
|
Revision: 227
http://svn.sourceforge.net/redbutton/?rev=227&view=rev
Author: skilvington
Date: 2007-02-19 09:16:11 -0800 (Mon, 19 Feb 2007)
Log Message:
-----------
cache PAT and PMTs
Modified Paths:
--------------
redbutton-download/trunk/Makefile
redbutton-download/trunk/TODO
redbutton-download/trunk/findmheg.c
redbutton-download/trunk/listen.c
redbutton-download/trunk/rb-download.c
www/index.html
Added Paths:
-----------
redbutton-download/trunk/cache.c
redbutton-download/trunk/cache.h
Modified: redbutton-download/trunk/Makefile
===================================================================
--- redbutton-download/trunk/Makefile 2007-02-19 13:30:26 UTC (rev 226)
+++ redbutton-download/trunk/Makefile 2007-02-19 17:16:11 UTC (rev 227)
@@ -17,6 +17,7 @@
biop.o \
fs.o \
channels.o \
+ cache.o \
utils.o
LIBS=-lz
Modified: redbutton-download/trunk/TODO
===================================================================
--- redbutton-download/trunk/TODO 2007-02-19 13:30:26 UTC (rev 226)
+++ redbutton-download/trunk/TODO 2007-02-19 17:16:11 UTC (rev 227)
@@ -1,5 +1,3 @@
-cache PAT and PMTs
-
need to kill all existing command connections on retune
(listen_data->carousel is stale for them)
Added: redbutton-download/trunk/cache.c
===================================================================
--- redbutton-download/trunk/cache.c (rev 0)
+++ redbutton-download/trunk/cache.c 2007-02-19 17:16:11 UTC (rev 227)
@@ -0,0 +1,125 @@
+/*
+ * cache.c
+ */
+
+/*
+ * Copyright (C) 2007, Simon Kilvington
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdbool.h>
+#include <dirent.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include "cache.h"
+#include "table.h"
+#include "utils.h"
+
+/*
+ * can't just cache tables in memory because the PMTs are mostly read when we
+ * execute "avstream <service_id> ..." commands
+ * each command is run in a child process, so the memory is lost when the
+ * process ends
+ * so we cache the tables in the file system
+ * all cache items are MAX_TABLE_LEN bytes long
+ */
+
+#define CACHE_DIR "cache"
+
+bool
+cache_init(void)
+{
+ if(mkdir(CACHE_DIR, 0755) < 0 && errno != EEXIST)
+ fatal("Unable to create cache directory '%s': %s", CACHE_DIR, strerror(errno));
+
+ cache_flush();
+
+ return true;
+}
+
+/*
+ * returns false if the item is not in the cache
+ */
+
+bool
+cache_load(char *item, unsigned char *out)
+{
+ char filename[PATH_MAX];
+ FILE *f;
+ size_t nread;
+
+ snprintf(filename, sizeof(filename), "%s/%s", CACHE_DIR, item);
+
+ if((f = fopen(filename, "r")) == NULL)
+ return false;
+
+ nread = fread(out, 1, MAX_TABLE_LEN, f);
+
+ fclose(f);
+
+ return (nread == MAX_TABLE_LEN);
+}
+
+void
+cache_save(char *item, unsigned char *data)
+{
+ char filename[PATH_MAX];
+ FILE *f;
+
+ snprintf(filename, sizeof(filename), "%s/%s", CACHE_DIR, item);
+
+ if((f = fopen(filename, "w")) == NULL)
+ return;
+
+ /* if we don't write it all, we'll find out when we try to load it */
+ fwrite(data, 1, MAX_TABLE_LEN, f);
+
+ fclose(f);
+
+ return;
+}
+
+void
+cache_flush(void)
+{
+ DIR *d;
+ struct dirent *item;
+ char filename[PATH_MAX];
+
+ if((d = opendir(CACHE_DIR)) == NULL)
+ return;
+
+ while((item = readdir(d)) != NULL)
+ {
+ /* skip . and .. */
+ if(strcmp(item->d_name, ".") == 0
+ || strcmp(item->d_name, "..") == 0)
+ continue;
+ snprintf(filename, sizeof(filename), "%s/%s", CACHE_DIR, item->d_name);
+ unlink(filename);
+ }
+
+ closedir(d);
+
+ return;
+}
+
+
Added: redbutton-download/trunk/cache.h
===================================================================
--- redbutton-download/trunk/cache.h (rev 0)
+++ redbutton-download/trunk/cache.h 2007-02-19 17:16:11 UTC (rev 227)
@@ -0,0 +1,36 @@
+/*
+ * cache.h
+ */
+
+/*
+ * Copyright (C) 2007, Simon Kilvington
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef __CACHE_H__
+#define __CACHE_H__
+
+#include <stdbool.h>
+
+bool cache_init(void);
+
+bool cache_load(char *, unsigned char *);
+void cache_save(char *, unsigned char *);
+
+void cache_flush(void);
+
+#endif /* __CACHE_H__ */
+
Modified: redbutton-download/trunk/findmheg.c
===================================================================
--- redbutton-download/trunk/findmheg.c 2007-02-19 13:30:26 UTC (rev 226)
+++ redbutton-download/trunk/findmheg.c 2007-02-19 17:16:11 UTC (rev 227)
@@ -30,6 +30,7 @@
#include "findmheg.h"
#include "table.h"
#include "assoc.h"
+#include "cache.h"
#include "utils.h"
/* Programme Association Table and Section */
@@ -81,6 +82,7 @@
static struct avstreams *find_current_avstreams(struct carousel *, int, int);
static struct avstreams *find_service_avstreams(struct carousel *, int, int, int);
+static bool read_pat(char *, unsigned int, unsigned char *);
static bool read_pmt(char *, uint16_t, unsigned int, unsigned char *);
bool
@@ -405,9 +407,35 @@
* returns false if it timesout
*/
-bool
+static bool
+read_pat(char *demux, unsigned int timeout, unsigned char *out)
+{
+ /* is it in the cache */
+ if(cache_load("pat", out))
+ return true;
+
+ /* read it from the DVB card */
+ if(!read_table(demux, PID_PAT, TID_PAT, timeout, out))
+ {
+ error("Unable to read PAT");
+ return false;
+ }
+
+ /* cache it */
+ cache_save("pat", out);
+
+ return true;
+}
+
+/*
+ * output buffer must be at least MAX_TABLE_LEN bytes
+ * returns false if it timesout
+ */
+
+static bool
read_pmt(char *demux, uint16_t service_id, unsigned int timeout, unsigned char *out)
{
+ char cache_item[PATH_MAX];
unsigned char pat[MAX_TABLE_LEN];
uint16_t section_length;
uint16_t offset;
@@ -415,12 +443,14 @@
bool found;
bool rc;
+ /* is it in the cache */
+ snprintf(cache_item, sizeof(cache_item), "pmt-%u", service_id);
+ if(cache_load(cache_item, out))
+ return true;
+
/* get the PAT */
- if(!read_table(demux, PID_PAT, TID_PAT, timeout, pat))
- {
- error("Unable to read PAT");
+ if(!read_pat(demux, timeout, pat))
return false;
- }
section_length = 3 + (((pat[1] & 0x0f) << 8) + pat[2]);
@@ -447,7 +477,12 @@
vverbose("PMT PID: %u", map_pid);
/* get the PMT */
- if(!(rc = read_table(demux, map_pid, TID_PMT, timeout, out)))
+ rc = read_table(demux, map_pid, TID_PMT, timeout, out);
+
+ /* cache it */
+ if(rc)
+ cache_save(cache_item, out);
+ else
error("Unable to read PMT");
return rc;
Modified: redbutton-download/trunk/listen.c
===================================================================
--- redbutton-download/trunk/listen.c 2007-02-19 13:30:26 UTC (rev 226)
+++ redbutton-download/trunk/listen.c 2007-02-19 17:16:11 UTC (rev 227)
@@ -20,6 +20,7 @@
#include "findmheg.h"
#include "carousel.h"
#include "channels.h"
+#include "cache.h"
#include "utils.h"
/* listen() backlog, 5 is max for BSD apparently */
@@ -177,6 +178,7 @@
verbose("Retune to service_id %d", retune_id);
/* kill the current downloader process and start a new one */
kill(listen_data.carousel->downloader, SIGKILL);
+ cache_flush();
listen_data.carousel = start_downloader(adapter, timeout, retune_id, -1);
retune_id = -1;
}
Modified: redbutton-download/trunk/rb-download.c
===================================================================
--- redbutton-download/trunk/rb-download.c 2007-02-19 13:30:26 UTC (rev 226)
+++ redbutton-download/trunk/rb-download.c 2007-02-19 17:16:11 UTC (rev 227)
@@ -71,6 +71,7 @@
#include "carousel.h"
#include "listen.h"
#include "channels.h"
+#include "cache.h"
#include "utils.h"
/* seconds before we assume no DSMCC data is available on this PID */
@@ -146,6 +147,9 @@
}
}
+ if(!cache_init())
+ fatal("Unable to initialise cache");
+
/* initialise channels.conf */
if(!init_channels_conf(channels_file))
error("Unable to open channels.conf file");
Modified: www/index.html
===================================================================
--- www/index.html 2007-02-19 13:30:26 UTC (rev 226)
+++ www/index.html 2007-02-19 17:16:11 UTC (rev 227)
@@ -90,6 +90,10 @@
</PRE>
where <PID> is the PID the carousel was downloaded from
and <CID> is the Carousel ID.
+<PRE>
+./cache/
+</PRE>
+Temporary cache of DVB tables to stop us having to wait for them to be retransmitted everytime we need them.
<P>
Just leave it running and any updated files will be downloaded as they are broadcast.
<H2>Notes</H2>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|