changeset 932:bd9b0142fc7e

Update the sample application. We can use the ./test5 to test it now. It will download the image from the VOD server and render it in the screen.
author wycc
date Tue, 09 Nov 2010 07:41:52 +0800
parents 3a3734289523
children e45ad25a1462
files nodejs/examples/mce/background.png nodejs/examples/mce/epg.js nodejs/examples/mce/mainmenu.js nodejs/examples/mce/test
diffstat 4 files changed, 176 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
Binary file nodejs/examples/mce/background.png has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nodejs/examples/mce/epg.js	Tue Nov 09 07:41:52 2010 +0800
@@ -0,0 +1,164 @@
+// -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*-
+// vim: sw=4:ts=8:sts=4
+var http = require('http');
+var URL = require('url');
+var sys = require("sys");
+var fs = require("fs");
+var os = require("child_process");
+function EPG() 
+{
+    var epgsrv = http.createClient(8080, '211.23.50.144');
+    var cmd = '{"Protocol":"EPG-CSP","Command":"SearchRequest","ProgramCat":"MainCat"}';
+    var headers={
+        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
+        'Host':'211.23.50.144:8080',
+        'User-Agent':'MadButterfly',
+        'Content-Type':'application/x-www-form-urlencoded'
+    };
+    headers['Content-Length'] = cmd.length;
+    var request = epgsrv.request('POST', '/IPTV_EPG/EPGService.do?timestamp='+new Date().getTime(),headers);
+    var self = this;
+    sys.puts("aaaa");
+    var js = '';
+    request.write(cmd);
+    request.end();
+    request.on('response', function(res) {
+        sys.puts("connected");
+ 	res.on('data',function (data) {
+		js = js + data;
+	});
+	res.on('end', function () {
+		res = JSON.parse(js);
+		sys.puts("parsed");
+		self.onLoad(res);
+
+	});
+    });
+}
+
+
+/**
+ *   Check if the file has been cached. Create a symbolic to link if it is cached already.
+ *
+ */
+function isCached(cachepath,file,obj) {
+    var fields = cachepath.split('.');
+    try {
+        var ext = fields.pop();
+	var pngfile = cachepath;
+	sys.puts("ext="+ext);
+	if (ext != 'png') {
+	    fields.push('png');
+	    pngfile = fields.join('.');
+	}
+        var st = fs.statSync(pngfile);
+	try {
+	    fs.unlinkSync(file);
+	} catch(e) {
+	}
+	fs.linkSync(pngfile, file);
+        obj.pend = obj.pend - 1;
+        if (obj.pend == 0) {
+            obj.onInitDone();
+        }
+	return 1;
+    } catch(e) {
+	sys.puts(e);
+    }
+    return 0;
+}
+
+/**
+ *  Implement the mkdir -p to create the directory 
+ */
+function CreateDirectory(cachepath)
+{
+    var fields = cachepath.split('/');
+    var p='';
+    for(i=0;i<fields.length-1;i++) {
+        p = p + fields[i]+'/';
+	try {
+            fs.mkdirSync(p,0777);
+	} catch(e) {
+	}
+    }
+}
+/*
+ *  We will check the cache directory. If the file is available, we will create a symbolic link only. Otherwise, 
+ *  we will fetch it before create the symbolic link.
+ */
+function httpGetFile(url,file,obj)
+{
+    sys.puts("fetch "+ file);
+    var u = URL.parse(url);
+    var cachepath =  'cache/'+u.pathname;
+    if (isCached(cachepath,file,obj)) return;
+    CreateDirectory(cachepath);
+
+    // Fetch file from the server and convert it tyo PNG if it is not PNG format.
+    var f = fs.openSync(cachepath,'w');
+    sys.puts("f="+f);
+    var headers={
+        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
+        'Host':'211.23.50.144:8080',
+        'User-Agent':'MadButterfly',
+        'Content-Type':'application/x-www-form-urlencoded'
+    };
+    sys.puts("host="+u.host+' '+u.port+' '+u.pathname);
+    for(k in u) {
+        sys.puts(k+"--->"+u[k]);
+    }
+    var c = http.createClient(8080,'211.23.50.144');
+    var req = c.request('GET',u.pathname,headers);
+    req.end();
+    req.on('response', function(res) {
+        res.on('data',function(data) {
+            fs.writeSync(f,data,0,data.length);
+	});
+	res.on('end',function() {
+	    fs.close(f);
+	    var fields = cachepath.split('.');
+	    var ext = fields.pop();
+	    if (ext != "png") {
+	        fields.push("png");
+	        newf = fields.join(".");
+	        os.spawn("convert",[file,newf]);
+	    } else {
+	        newf = cachepath;
+	    }
+	    try {
+	        fs.unlinkSync(file);
+	    } catch(e) {
+	    }
+	    sys.puts("end of "+cachepath+" to "+file);
+	    fs.symlinkSync(newf, file);
+	    obj.pend = obj.pend - 1;
+	    if (obj.pend == 0) {
+	        obj.onInitDone();
+	    }
+
+	});
+
+    });
+}
+
+EPG.prototype.onLoad = function(res) {
+    cats = res['ProgramCat'];
+    this.pend = cats.length;
+    for (i in cats) {
+	c = cats[i];
+	httpGetFile(c['ProgramPIC'],'cat'+i+'.png',this);
+    }
+}
+
+
+EPG.prototype.onInitDone=function() {
+    if (this.loadCallback)
+        this.loadCallback();
+}
+
+EPG.prototype.registerInitDone=function(cb) {
+    this.loadCallback = cb;
+}
+
+exports.EPG = EPG;
--- a/nodejs/examples/mce/mainmenu.js	Sun Nov 07 11:36:12 2010 +0800
+++ b/nodejs/examples/mce/mainmenu.js	Tue Nov 09 07:41:52 2010 +0800
@@ -6,13 +6,17 @@
 var animate=require("animate");
 var fs = require("fs");
 var EPG = require('./epg');
-
+/**
+ *   We will fetch the EPG file from the server and fetch all images required for the main category from it.
+ *   If these files are cached, we will not fetch it again. Otherwise, we will fetch them. The EPG class is
+ *   responsible for the cache management.
+ */
 function MainMenu(app) 
 {
     var self = this;
-    //var epg = new EPG.EPG();
-    //epg.registerInitDone(function() { self.init();});
-    self.init();
+    var epg = new EPG.EPG();
+    epg.registerInitDone(function() { self.init();});
+    //self.init();
 }
 MainMenu.prototype.init=function()
 {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nodejs/examples/mce/test	Tue Nov 09 07:41:52 2010 +0800
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+export NODE_PATH=${PWD}/../../objs/default:${PWD}/../..
+node mbmce.js