1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* DreemGL is a collaboration between Teeming Society & Samsung Electronics, sponsored by Samsung and others.
   Copyright 2015-2016 Teeming Society. Licensed under the Apache License, Version 2.0 (the "License"); You may not use this file except in compliance with the License.
   You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and limitations under the License.*/

// parse a color string into a [r,g,b] 0-1 float array

define.class(function(require){

        var fs = require('fs')

        var FileWatcher = require('./filewatcher')

        var BusServer = require('$system/rpc/busserver')

        this.atConstructor = function(
                args, //Object: Process arguments
                compname, //String: name of the composition
                rootserver){ //TeemServer: teem server object

                this.rootserver = rootserver
                 this.args = args
                this.compname = compname

                this.busserver = new BusServer()

                this.slow_watcher = new FileWatcher()

                // lets give it a session
                this.session = Math.random() * 1000000

                this.slow_watcher.atChange = function(){
                        // lets reload this app
                        this.reload()
                }.bind(this)

                this.components = {}

                this.paths = ""
                this.pathset = '{'

                for(var key in define.paths){
                        if(this.paths) this.paths += ',\n\t\t', this.pathset += ','
                        this.paths += '$'+key+':"$root/'+key+'"'
                        this.pathset += '"'+key+'":1'
                }
                this.pathset += '}'


                // lets compile and run the dreem composition
                define.atRequire = function(filename){
                        this.slow_watcher.watch(filename)
                }.bind(this)
                //
                this.reload()
        }

        // Called when any of the dependent files change for this composition
        this.atChange = function(){
        }

        // Destroys all objects maintained by the composition
        this.destroy = function(){
                if(this.mycomposition && this.mycomposition.destroy) this.mycomposition.destroy()
                this.mycomposition = undefined
        }

        this.loadComposition = function(){
                console.log("Reloading composition "+this.filename)
                require.clearCache()
                var Composition = require(define.expandVariables(this.filename))
                this.composition = new Composition(this.busserver, this.session, this.composition)
        }

        this.reload = function(){
                this.destroy()

                // lets fill
                require.clearCache()

                this.title = define.fileName(this.compname)
                // lets see if our composition is a dir or a jsfile
                var jsname = this.compname+'.js'
                try{
                        if(fs.existsSync(define.expandVariables(jsname))){
                                this.filename = jsname
                                return this.loadComposition()
                        }
                        else{
                                var jsname = this.compname + '/index.js'
                                if(fs.existsSync(define.expandVariables(jsname))){
                                        this.filename = jsname
                                        return this.loadComposition()
                                }
                        }
                }
                catch(e){
                        console.log(e.stack)
                }
                finally{
                }
        }

        this.loadHTML = function(title, boot, paths, pathset){

                // These rpc attributes we will write directly into the header so that they are available even before the screen connects
                var preloadattrs = {};

                var additionalHeader = "";

                if (this.composition) {

                        // preload_rpc_attributes can be `true` to include everything or an array of `rpcobj_rpcattr` identifiers
                        var preload = this.composition.preload_rpc_attributes;
                        if (preload === true) {
                                preloadattrs = this.composition.server_attributes;
                        } else if (Array.isArray(preload)) {
                                for (var i = 0; i< preload.length;i++) {
                                        var key = preload[i];
                                        var attr = this.composition.server_attributes[key];
                                        if (attr) {
                                                preloadattrs[key] = attr
                                        }
                                }
                        }
                        additionalHeader = this.composition.headHTML || "";
                }


                return '<html lang="en">\n'+
                        ' <head>\n'+
                        '  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n'+
                        '  <meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">\n'+
                        '  <meta name="apple-mobile-web-app-capable" content="yes">\n'+
                        '  <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">\n'+
                        '  <meta name="format-detection" content="telephone=no">\n'+
                        '  <title>' + title + '</title>\n'+
                        '  <style>\n'+
                        '    .unselectable{\n'+
                        '                 -webkit-user-select: none;\n'+
                          '                -moz-user-select: none;\n'+
                          '                -user-select: none;\n'+
                          '    }\n'+
                        '    body {background-color:white;margin:0;padding:0;height:100%;overflow:hidden;}\n'+
                        '  </style>'+
                        '  <script type="text/javascript">\n'+
                        '    window.define = {\n'+
                        '           $platform:"webgl",\n'+
                        '      paths:'+pathset+',\n'+
                        '     '+paths+',\n'+
                        '      main:["$system/base/math", "' + boot + '"],\n'+
                        '      atMain:function(require, modules){\n'+
                        '        define.endLoader()\n'+
                        '                 require(modules[0])\n'+
                        '                 var Composition = require(modules[1])\n'+
                        '                 var serverattrs = ' + JSON.stringify(preloadattrs) + '\n'+
                        '                 var renderTarget;' + '\n'+
                        '        define.rootComposition = new Composition(define.rootComposition, undefined, serverattrs, renderTarget)\n'+
                        '      },\n'+
                        '           atEnd:function(){\n'+
                        '         define.startLoader()\n'+
                        '      }\n'+
                        '    }\n'+
                        '  </script>\n'+
                        '  <script type="text/javascript" src="/system/base/define.js"></script>\n'+
                        additionalHeader +
                        ' </head>\n'+
                        ' <body class="unselectable">\n'+
                        ' </body>\n'+
                        '</html>\n'
        }

        this.request = function(req, res){
                var base = req.url.split('?')[0]
                var app = base.split('/')[2] || 'browser'
                // ok lets serve our Composition device

                if(req.method == 'POST'){
                        // lets do an RPC call

                        if(!define.$unsafeorigin && this.rootserver.addresses.indexOf(req.headers.origin) === -1){
                                console.log("WRONG ORIGIN POST API RECEIVED" + req.headers.origin)
                                res.end()
                                return false
                        }

                        var boundary;
                        if (req.headers && req.headers['content-type']) {
                                var type = req.headers['content-type'];
                                if (type) {
                                        var m = /^multipart\/form-data; boundary=(.*)$/.exec(type)

                                        if (m) {
                                                boundary = m[1];
                                        }
                                }
                        }

                        var buffer;
                        req.on('data', function(data){
                                if (boundary) {
                                        if (!buffer) {
                                                buffer = new Buffer(data)
                                        } else {
                                                buffer = Buffer.concat([buffer, data])
                                        }
                                }
                        })
                        req.on('end', function(){

                                if (boundary && buffer && buffer.indexOf) {

                                        var cursor = buffer.indexOf(boundary) + boundary.length;

                                        cursor = buffer.indexOf("filename=", cursor) + "filename=".length;
                                        var q = buffer[cursor];
                                        var filename = buffer.slice(cursor + 1, buffer.indexOf(q, cursor + 1)).toString();
                                        cursor = buffer.indexOf("\r\n\r\n", cursor) + "\r\n\r\n".length;

                                        var filedata = buffer.slice(cursor, buffer.indexOf("\r\n--" + boundary));

                                        var compfile = this.composition.constructor.module.filename;
                                        var compdir = compfile.substring(0, compfile.lastIndexOf('/'));

                                        filename = compdir + "/" + filename.replace(/[^A-Za-z0-9_.-]/g,'');

                                        if (!define.$writefile){
                                                console.log("writefile api disabled, use -writefile to turn it on. Writefile api is always limited to localhost origins.")
                                                res.writeHead(501);
                                        } else {
                                                try{
                                                        var fullname = define.expandVariables(filename);
                                                        fs.writeFile(fullname, filedata);
                                                        console.log("[UPLOAD] Wrote", filedata.length, "bytes to", fullname);
                                                        res.writeHead(200);
                                                }
                                                catch(e){
                                                        res.writeHead(503);
                                                }
                                        }

                                        res.end();
                                        return
                                } else if (buffer) {
                                        try{
                                                var json = JSON.parse(buffer.toString())
                                                this.composition.postAPI(json, {send:function(msg){
                                                        res.writeHead(200, {"Content-Type":"text/json"})
                                                        res.write(JSON.stringify(msg))
                                                        res.end()
                                                }})
                                        }
                                        catch(e){
                                                res.writeHead(500, {"Content-Type": "text/html"})
                                                res.write('FAIL')
                                                res.end()
                                                return
                                        }
                                } else {
                                        res.writeHead(500, {"Content-Type": "text/html"})
                                        res.write('FAIL')
                                        res.end()
                                        return
                                }
                        }.bind(this))
                        return
                }

                var header = {
                        "Cache-control": "max-age=0",
                        "Content-Type": "text/html"
                }
                //var screen = this.screens[app]

                // nodejs root
                if(req.headers['client-type'] === 'nodejs'){
                        res.writeHead(200, {"Content-type":"text/json"})
                        res.write(JSON.stringify({title:this.title, boot:this.filename, paths:define.paths }))
                        res.end()
                        return
                }

                if (! this.filename) {
                        res.writeHead(404, header)
                        res.write('<body>Sorry, we could not find an application at that URL. <a href="/docs/api/index.html">Try reading the documentation?</a></body>')
                        res.end()
                        return
                }
                var html = this.loadHTML(this.title, this.filename, this.paths, this.pathset)
                res.writeHead(200, header)
                res.write(html)
                res.end()
        }
})