From 467a36b5318e8ca5ada01e980883f4b8c1038d4b Mon Sep 17 00:00:00 2001 From: Xuan Sang LE Date: Mon, 17 Sep 2018 19:43:24 +0200 Subject: [PATCH] new wvnc app --- apps/Makefile | 4 +- apps/assets/coffee/APIManager.coffee | 10 ++- apps/assets/coffee/BaseObject.coffee | 11 +++ apps/assets/coffee/WVNC.coffee | 57 +++++++++++++ apps/assets/coffee/bootstrap.coffee | 3 +- apps/assets/scripts/main.js | 117 ++++++++++++++++++++++++-- apps/controllers/MarkOnController.lua | 2 +- apps/controllers/WvncController.lua | 13 +++ apps/router.lua | 1 + apps/views/default/layout.ls | 8 +- apps/views/default/wvnc/index.ls | 1 + 11 files changed, 208 insertions(+), 19 deletions(-) create mode 100644 apps/assets/coffee/WVNC.coffee create mode 100644 apps/controllers/WvncController.lua create mode 100644 apps/views/default/wvnc/index.ls diff --git a/apps/Makefile b/apps/Makefile index 0ee7a77..a00d97a 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -5,7 +5,7 @@ coffees = assets/coffee/bootstrap.coffee \ assets/coffee/BaseObject.coffee \ assets/coffee/APIManager.coffee \ assets/coffee/MarkOn.coffee \ - + assets/coffee/WVNC.coffee main: js - mkdir -p $(BUILDDIR)/assets @@ -17,7 +17,7 @@ js: - rm assets/scripts/main.js for f in $(coffees); do (cat "$${f}"; echo) >> assets/scripts/main.coffee; done coffee --compile assets/scripts/main.coffee - - rm assets/scripts/main.coffee + -rm assets/scripts/main.coffee clean: rm -rf $(BUILDDIR)/* \ No newline at end of file diff --git a/apps/assets/coffee/APIManager.coffee b/apps/assets/coffee/APIManager.coffee index 05bb5f3..3421cd9 100644 --- a/apps/assets/coffee/APIManager.coffee +++ b/apps/assets/coffee/APIManager.coffee @@ -1,9 +1,11 @@ class APIManager extends window.classes.BaseObject - constructor: () -> + constructor: (@args) -> super "APIManager" - init: (cname) -> - console.log(cname) + init: () -> + me = @ + return console.error "No class found" unless @args and @args.length > 0 + cname = (@args.splice 0,1)[0].trim() @ready() .then () -> if mobilecheck() @@ -11,7 +13,7 @@ class APIManager extends window.classes.BaseObject # load the class return if not cname or cname is "" return console.error("Cannot find class ", cname) unless window.classes[cname] - (new window.classes[cname]).init() + (new window.classes[cname](me.args)).init() .catch ( m, s ) -> console.error(m, s) diff --git a/apps/assets/coffee/BaseObject.coffee b/apps/assets/coffee/BaseObject.coffee index dadf740..397a1d8 100644 --- a/apps/assets/coffee/BaseObject.coffee +++ b/apps/assets/coffee/BaseObject.coffee @@ -1,3 +1,14 @@ +# private function +require = (lib) -> + return new Promise (r, e) -> + return r() if window.libraries[lib] + $.getScript window.myuri + lib + .done (d) -> + window.libraries[lib] = true + r() + .fail (m, s) -> + e(m, s) + class BaseObject constructor: (@name) -> diff --git a/apps/assets/coffee/WVNC.coffee b/apps/assets/coffee/WVNC.coffee new file mode 100644 index 0000000..935fe3c --- /dev/null +++ b/apps/assets/coffee/WVNC.coffee @@ -0,0 +1,57 @@ +class WVNC extends window.classes.BaseObject + constructor: (@args) -> + super "WVNC" + @socket = undefined + @uri = undefined + @uri = @args[0] if @args and @args.length > 0 + + init: () -> + me = @ + @ready() + .then () -> + me.openSession() + .catch (m, s) -> + console.error(m, s) + + openSession: () -> + me = @ + @socket.close() if @socket + return unless @uri + @socket = new WebSocket @uri + @socket.binaryType = "arraybuffer" + @socket.onopen = () -> + console.log "socket opened" + me.initConnection() + + @socket.onmessage = (e) -> + me.consume e + @socket.onclose = () -> + me.socket = null + console.log "socket closed" + + initConnection: () -> + vncserver = "localhost:5901" + @socket.send(@buildCommand 0x01, vncserver) + + buildCommand: (hex, o) -> + data = undefined + switch typeof o + when 'string' + data = (new TextEncoder()).encode(o) + else + data = o + cmd = new Uint8Array data.length + 3 + cmd[0] = hex + cmd[2] = data.length >> 8 + cmd[1] = data.length & 0x0F + cmd.set data, 3 + console.log "the command is", cmd.buffer + return cmd.buffer + + + consume: (e) -> + console.log e +WVNC.dependencies = [ +] + +makeclass "WVNC", WVNC \ No newline at end of file diff --git a/apps/assets/coffee/bootstrap.coffee b/apps/assets/coffee/bootstrap.coffee index e8947f6..11d740b 100644 --- a/apps/assets/coffee/bootstrap.coffee +++ b/apps/assets/coffee/bootstrap.coffee @@ -8,6 +8,7 @@ window.mobilecheck = () -> window.makeclass = (n, o) -> window.classes[n] = o +### window.require = (lib) -> return new Promise (r, e) -> return r() if window.libraries[lib] @@ -16,4 +17,4 @@ window.require = (lib) -> window.libraries[lib] = true r() .fail (m, s) -> - e(m, s) + e(m, s) ### diff --git a/apps/assets/scripts/main.js b/apps/assets/scripts/main.js index f586ffc..c08870b 100644 --- a/apps/assets/scripts/main.js +++ b/apps/assets/scripts/main.js @@ -1,6 +1,6 @@ -// Generated by CoffeeScript 1.12.7 +// Generated by CoffeeScript 1.9.3 (function() { - var APIManager, BaseObject, MarkOn, + var APIManager, BaseObject, MarkOn, WVNC, require, extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, hasProp = {}.hasOwnProperty; @@ -21,7 +21,20 @@ return window.classes[n] = o; }; - window.require = function(lib) { + + /* + window.require = (lib) -> + return new Promise (r, e) -> + return r() if window.libraries[lib] + $.getScript window.myuri + lib + .done (d) -> + window.libraries[lib] = true + r() + .fail (m, s) -> + e(m, s) + */ + + require = function(lib) { return new Promise(function(r, e) { if (window.libraries[lib]) { return r(); @@ -84,12 +97,18 @@ APIManager = (function(superClass) { extend(APIManager, superClass); - function APIManager() { + function APIManager(args) { + this.args = args; APIManager.__super__.constructor.call(this, "APIManager"); } - APIManager.prototype.init = function(cname) { - console.log(cname); + APIManager.prototype.init = function() { + var cname, me; + me = this; + if (!(this.args && this.args.length > 0)) { + return console.error("No class found"); + } + cname = (this.args.splice(0, 1))[0].trim(); return this.ready().then(function() { if (mobilecheck()) { mobileConsole.init(); @@ -100,7 +119,7 @@ if (!window.classes[cname]) { return console.error("Cannot find class ", cname); } - return (new window.classes[cname]).init(); + return (new window.classes[cname](me.args)).init(); })["catch"](function(m, s) { return console.error(m, s); }); @@ -141,4 +160,88 @@ makeclass("MarkOn", MarkOn); + WVNC = (function(superClass) { + extend(WVNC, superClass); + + function WVNC(args) { + this.args = args; + WVNC.__super__.constructor.call(this, "WVNC"); + this.socket = void 0; + this.uri = void 0; + if (this.args && this.args.length > 0) { + this.uri = this.args[0]; + } + } + + WVNC.prototype.init = function() { + var me; + me = this; + return this.ready().then(function() { + return me.openSession(); + })["catch"](function(m, s) { + return console.error(m, s); + }); + }; + + WVNC.prototype.openSession = function() { + var me; + me = this; + if (this.socket) { + this.socket.close(); + } + if (!this.uri) { + return; + } + this.socket = new WebSocket(this.uri); + this.socket.binaryType = "arraybuffer"; + this.socket.onopen = function() { + console.log("socket opened"); + return me.initConnection(); + }; + this.socket.onmessage = function(e) { + return me.consume(e); + }; + return this.socket.onclose = function() { + me.socket = null; + return console.log("socket closed"); + }; + }; + + WVNC.prototype.initConnection = function() { + var vncserver; + vncserver = "localhost:5901"; + return this.socket.send(this.buildCommand(0x01, vncserver)); + }; + + WVNC.prototype.buildCommand = function(hex, o) { + var cmd, data; + data = void 0; + switch (typeof o) { + case 'string': + data = (new TextEncoder()).encode(o); + break; + default: + data = o; + } + cmd = new Uint8Array(data.length + 3); + cmd[0] = hex; + cmd[2] = data.length >> 8; + cmd[1] = data.length & 0x0F; + cmd.set(data, 3); + console.log("the command is", cmd.buffer); + return cmd.buffer; + }; + + WVNC.prototype.consume = function(e) { + return console.log(e); + }; + + return WVNC; + + })(window.classes.BaseObject); + + WVNC.dependencies = []; + + makeclass("WVNC", WVNC); + }).call(this); diff --git a/apps/controllers/MarkOnController.lua b/apps/controllers/MarkOnController.lua index e7b56b1..ef1b748 100644 --- a/apps/controllers/MarkOnController.lua +++ b/apps/controllers/MarkOnController.lua @@ -3,7 +3,7 @@ BaseController:subclass("MarkOnController", { }) function MarkOnController:index( ... ) - self.template:set("jsclass", "MarkOn") + self.template:set("args", "['MarkOn']") return true end diff --git a/apps/controllers/WvncController.lua b/apps/controllers/WvncController.lua new file mode 100644 index 0000000..01c615f --- /dev/null +++ b/apps/controllers/WvncController.lua @@ -0,0 +1,13 @@ +BaseController:subclass("WvncController", { + registry = {} +}) + +function WvncController:index( ... ) + self.template:set("args", "['WVNC', 'wss://localhost:9195/wvnc']") + return true +end + +function WvncController:actionnotfound(...) + self.template:setView("index") + return self:index(table.unpack({...})) +end \ No newline at end of file diff --git a/apps/router.lua b/apps/router.lua index 0cada1b..4f17091 100644 --- a/apps/router.lua +++ b/apps/router.lua @@ -24,6 +24,7 @@ local REGISTRY = {} REGISTRY.logger = Logger:new{ levels = {INFO = true, ERROR = true, DEBUG = true}} REGISTRY.db = DBHelper:new{db="iosapps"} REGISTRY.layout = 'default' +REGISTRY.fileaccess = true REGISTRY.db:open() local router = Router:new{registry = REGISTRY} diff --git a/apps/views/default/layout.ls b/apps/views/default/layout.ls index 144f4c5..0b4c7de 100644 --- a/apps/views/default/layout.ls +++ b/apps/views/default/layout.ls @@ -1,6 +1,6 @@ @@ -16,8 +16,8 @@ if jsclass == nil then jsclass = "" end diff --git a/apps/views/default/wvnc/index.ls b/apps/views/default/wvnc/index.ls new file mode 100644 index 0000000..7a2c8e9 --- /dev/null +++ b/apps/views/default/wvnc/index.ls @@ -0,0 +1 @@ +

VNC screen here

\ No newline at end of file