comparison nodejs/examples/mce/epg.js @ 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
children 18329b6f77a4
comparison
equal deleted inserted replaced
931:3a3734289523 932:bd9b0142fc7e
1 // -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*-
2 // vim: sw=4:ts=8:sts=4
3 var http = require('http');
4 var URL = require('url');
5 var sys = require("sys");
6 var fs = require("fs");
7 var os = require("child_process");
8 function EPG()
9 {
10 var epgsrv = http.createClient(8080, '211.23.50.144');
11 var cmd = '{"Protocol":"EPG-CSP","Command":"SearchRequest","ProgramCat":"MainCat"}';
12 var headers={
13 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
14 'Host':'211.23.50.144:8080',
15 'User-Agent':'MadButterfly',
16 'Content-Type':'application/x-www-form-urlencoded'
17 };
18 headers['Content-Length'] = cmd.length;
19 var request = epgsrv.request('POST', '/IPTV_EPG/EPGService.do?timestamp='+new Date().getTime(),headers);
20 var self = this;
21 sys.puts("aaaa");
22 var js = '';
23 request.write(cmd);
24 request.end();
25 request.on('response', function(res) {
26 sys.puts("connected");
27 res.on('data',function (data) {
28 js = js + data;
29 });
30 res.on('end', function () {
31 res = JSON.parse(js);
32 sys.puts("parsed");
33 self.onLoad(res);
34
35 });
36 });
37 }
38
39
40 /**
41 * Check if the file has been cached. Create a symbolic to link if it is cached already.
42 *
43 */
44 function isCached(cachepath,file,obj) {
45 var fields = cachepath.split('.');
46 try {
47 var ext = fields.pop();
48 var pngfile = cachepath;
49 sys.puts("ext="+ext);
50 if (ext != 'png') {
51 fields.push('png');
52 pngfile = fields.join('.');
53 }
54 var st = fs.statSync(pngfile);
55 try {
56 fs.unlinkSync(file);
57 } catch(e) {
58 }
59 fs.linkSync(pngfile, file);
60 obj.pend = obj.pend - 1;
61 if (obj.pend == 0) {
62 obj.onInitDone();
63 }
64 return 1;
65 } catch(e) {
66 sys.puts(e);
67 }
68 return 0;
69 }
70
71 /**
72 * Implement the mkdir -p to create the directory
73 */
74 function CreateDirectory(cachepath)
75 {
76 var fields = cachepath.split('/');
77 var p='';
78 for(i=0;i<fields.length-1;i++) {
79 p = p + fields[i]+'/';
80 try {
81 fs.mkdirSync(p,0777);
82 } catch(e) {
83 }
84 }
85 }
86 /*
87 * We will check the cache directory. If the file is available, we will create a symbolic link only. Otherwise,
88 * we will fetch it before create the symbolic link.
89 */
90 function httpGetFile(url,file,obj)
91 {
92 sys.puts("fetch "+ file);
93 var u = URL.parse(url);
94 var cachepath = 'cache/'+u.pathname;
95 if (isCached(cachepath,file,obj)) return;
96 CreateDirectory(cachepath);
97
98 // Fetch file from the server and convert it tyo PNG if it is not PNG format.
99 var f = fs.openSync(cachepath,'w');
100 sys.puts("f="+f);
101 var headers={
102 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
103 'Host':'211.23.50.144:8080',
104 'User-Agent':'MadButterfly',
105 'Content-Type':'application/x-www-form-urlencoded'
106 };
107 sys.puts("host="+u.host+' '+u.port+' '+u.pathname);
108 for(k in u) {
109 sys.puts(k+"--->"+u[k]);
110 }
111 var c = http.createClient(8080,'211.23.50.144');
112 var req = c.request('GET',u.pathname,headers);
113 req.end();
114 req.on('response', function(res) {
115 res.on('data',function(data) {
116 fs.writeSync(f,data,0,data.length);
117 });
118 res.on('end',function() {
119 fs.close(f);
120 var fields = cachepath.split('.');
121 var ext = fields.pop();
122 if (ext != "png") {
123 fields.push("png");
124 newf = fields.join(".");
125 os.spawn("convert",[file,newf]);
126 } else {
127 newf = cachepath;
128 }
129 try {
130 fs.unlinkSync(file);
131 } catch(e) {
132 }
133 sys.puts("end of "+cachepath+" to "+file);
134 fs.symlinkSync(newf, file);
135 obj.pend = obj.pend - 1;
136 if (obj.pend == 0) {
137 obj.onInitDone();
138 }
139
140 });
141
142 });
143 }
144
145 EPG.prototype.onLoad = function(res) {
146 cats = res['ProgramCat'];
147 this.pend = cats.length;
148 for (i in cats) {
149 c = cats[i];
150 httpGetFile(c['ProgramPIC'],'cat'+i+'.png',this);
151 }
152 }
153
154
155 EPG.prototype.onInitDone=function() {
156 if (this.loadCallback)
157 this.loadCallback();
158 }
159
160 EPG.prototype.registerInitDone=function(cb) {
161 this.loadCallback = cb;
162 }
163
164 exports.EPG = EPG;