Skip to content

Commit

Permalink
Cleanup and move to jls serial worker
Browse files Browse the repository at this point in the history
  • Loading branch information
javalikescript committed Jan 22, 2022
1 parent 9d6fdd0 commit 8d83c38
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 142 deletions.
40 changes: 21 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ endif

LUACLIBS := ../luaclibs/dist-$(PLAT)
FCUT_DIST := dist
FCUT_DIST_CLUA := $(FCUT_DIST)/bin
FCUT_DIST_LUA := $(FCUT_DIST)/lua

SO_windows=dll
EXE_windows=.exe
Expand Down Expand Up @@ -48,29 +50,29 @@ show:
@echo UNAME_M: $(UNAME_M)

dist-copy-linux:
-cp -u $(LUACLIBS)/linux.$(SO) $(FCUT_DIST)/bin/
-cp -u $(LUACLIBS)/linux.$(SO) $(FCUT_DIST_CLUA)/
cp -u fcut.sh $(FCUT_DIST)/

dist-copy-windows:
-cp -u $(LUACLIBS)/winapi.$(SO) $(FCUT_DIST)/bin/
-cp -u $(LUACLIBS)/win32.$(SO) $(FCUT_DIST)/bin/
cp -u $(LUACLIBS)/lua*.$(SO) $(FCUT_DIST)/bin/
-cp -u $(LUACLIBS)/wlua$(EXE) $(FCUT_DIST)/bin/
cp -u $(LUACLIBS)/WebView2Loader.dll $(FCUT_DIST)/bin/
-cp -u $(LUACLIBS)/winapi.$(SO) $(FCUT_DIST_CLUA)/
-cp -u $(LUACLIBS)/win32.$(SO) $(FCUT_DIST_CLUA)/
cp -u $(LUACLIBS)/lua*.$(SO) $(FCUT_DIST_CLUA)/
-cp -u $(LUACLIBS)/wlua$(EXE) $(FCUT_DIST_CLUA)/
cp -u $(LUACLIBS)/WebView2Loader.dll $(FCUT_DIST_CLUA)/
cp -u fcut.bat $(FCUT_DIST)/

dist-copy: dist-copy-$(PLAT)
cp -u $(LUACLIBS)/lua$(EXE) $(FCUT_DIST)/bin/
cp -u $(LUACLIBS)/cjson.$(SO) $(FCUT_DIST)/bin/
cp -u $(LUACLIBS)/luv.$(SO) $(FCUT_DIST)/bin/
cp -u $(LUACLIBS)/zlib.$(SO) $(FCUT_DIST)/bin/
cp -u $(LUACLIBS)/webview.$(SO) $(FCUT_DIST)/bin/
cp -ru $(LUACLIBS)/sha1/ $(FCUT_DIST)/lua/
cp -u $(LUACLIBS)/XmlParser.lua $(FCUT_DIST)/lua/
cp -u $(LUACLIBS)/sha1.lua $(FCUT_DIST)/lua/
cp -ru $(LUACLIBS)/jls/ $(FCUT_DIST)/lua/
cp -u *.lua $(FCUT_DIST)/lua/
mv $(FCUT_DIST)/lua/fcut.lua $(FCUT_DIST)/
cp -u $(LUACLIBS)/lua$(EXE) $(FCUT_DIST_CLUA)/
cp -u $(LUACLIBS)/cjson.$(SO) $(FCUT_DIST_CLUA)/
cp -u $(LUACLIBS)/luv.$(SO) $(FCUT_DIST_CLUA)/
cp -u $(LUACLIBS)/zlib.$(SO) $(FCUT_DIST_CLUA)/
cp -u $(LUACLIBS)/webview.$(SO) $(FCUT_DIST_CLUA)/
cp -ru $(LUACLIBS)/sha1/ $(FCUT_DIST_LUA)/
cp -u $(LUACLIBS)/XmlParser.lua $(FCUT_DIST_LUA)/
cp -u $(LUACLIBS)/sha1.lua $(FCUT_DIST_LUA)/
cp -ru $(LUACLIBS)/jls/ $(FCUT_DIST_LUA)/
cp -u *.lua $(FCUT_DIST_LUA)/
mv $(FCUT_DIST_LUA)/fcut.lua $(FCUT_DIST)/
cp -ru htdocs/ $(FCUT_DIST)/
-rm -f assets.tmp.zip
cd assets/ && zip -r ../assets.tmp.zip *
Expand All @@ -81,8 +83,8 @@ dist-clean:

dist-prepare:
-mkdir $(FCUT_DIST)
mkdir $(FCUT_DIST)/bin
mkdir $(FCUT_DIST)/lua
mkdir $(FCUT_DIST_CLUA)
mkdir $(FCUT_DIST_LUA)

dist: dist-clean dist-prepare dist-copy

Expand Down
57 changes: 0 additions & 57 deletions SerialWorker.lua

This file was deleted.

142 changes: 81 additions & 61 deletions fcut.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ local ZipFileHttpHandler = require('jls.net.http.handler.ZipFileHttpHandler')
local TableHttpHandler = require('jls.net.http.handler.TableHttpHandler')
local WebSocketUpgradeHandler = require('jls.net.http.ws').WebSocketUpgradeHandler

-- Project required modules

local FileChooser = require('FileChooser')
local Ffmpeg = require('Ffmpeg')
local CONFIG_SCHEMA = require('fcutSchema')

-- Helper functions

local function getUserDir()
if system.isWindows() then
return os.getenv('TEMP') or os.getenv('USERPROFILE')
Expand Down Expand Up @@ -58,7 +62,8 @@ local function startProcess(command, outputFile, callback)
fd = FileDescriptor.openSync(outputFile, 'w')
pb:redirectOutput(fd)
end
local ph = pb:start(function(exitCode)
local ph = pb:start()
ph:ended():next(function(exitCode)
if fd then
fd:close()
end
Expand All @@ -71,15 +76,43 @@ local function startProcess(command, outputFile, callback)
return ph
end

-- Extracts configuration from command line arguments
local config = tables.createArgumentTable(arg, {
helpPath = 'help',
configPath = 'config',
emptyPath = 'config',
schema = CONFIG_SCHEMA
});

-- Apply configured log level
logger:setLevel(config.loglevel)

-- Disable native open file dialog if necessary
if config.webview.native and config.webview.disable or not loader.tryRequire('win32') then
config.webview.native = false
end

-- Application local variables

local scriptFile = File:new(arg[0]):getAbsoluteFile()
local scriptDir = scriptFile:getParentFile()

local assetsHandler
local assetsDir = File:new(scriptDir, 'assets')
local assetsZip = File:new(scriptDir, 'assets.zip')
if assetsDir:isDirectory() then
assetsHandler = FileHttpHandler:new(assetsDir)
elseif assetsZip:isFile() then
assetsHandler = ZipFileHttpHandler:new(assetsZip)
end

local commandQueue = {}
local commandCount = 0
local processList = {}
local exportContexts = {}
local serialWorker

-- Set up the cache directory
local cacheDir = File:new(config.cache)
if not cacheDir:isAbsolute() then
local homeDir = File:new(getUserDir() or '.')
Expand All @@ -98,30 +131,25 @@ logger:info('Cache directory is '..cacheDir:getPath())
local ffmpeg = Ffmpeg:new(cacheDir)
ffmpeg:configure(config)

local scriptFile = File:new(arg[0]):getAbsoluteFile()
local scriptDir = scriptFile:getParentFile()

if config.webview.ie then
system.setenv('WEBVIEW2_WIN32_PATH', 'na')
end

if config.webview.native and config.webview.disable or not loader.tryRequire('win32') then
config.webview.native = false
end
-- Application local functions

local assetsHandler
local assetsDir = File:new(scriptDir, 'assets')
local assetsZip = File:new(scriptDir, 'assets.zip')
if assetsDir:isDirectory() then
assetsHandler = FileHttpHandler:new(assetsDir)
elseif assetsZip:isFile() then
assetsHandler = ZipFileHttpHandler:new(assetsZip)
local function terminate()
for _, ph in ipairs(processList) do
ph:destroy()
end
processList = {}
for _, exportContext in pairs(exportContexts) do
if exportContext and exportContext.process then
exportContext.process:destroy()
end
end
exportContexts = {}
if serialWorker then
serialWorker:close()
serialWorker = nil
end
end

local commandQueue = {}
local commandCount = 0
local processList = {}

local function wakeupCommandQueue()
if commandCount < config.processCount then
local command = table.remove(commandQueue, 1)
Expand Down Expand Up @@ -160,13 +188,13 @@ local function enqueueCommand(args, id, outputFile)
return promise
end

local exportContexts = {}

-- Create the HTTP contexts used by the web application
local function createHttpContexts(httpServer)
logger:info('HTTP Server bound on port '..tostring(select(2, httpServer:getAddress())))
httpServer:createContext('/(.*)', FileHttpHandler:new(File:new(scriptDir, 'htdocs'), nil, 'fcut.html'))
httpServer:createContext('/config/(.*)', TableHttpHandler:new(config, nil, true))
httpServer:createContext('/assets/(.*)', assetsHandler)
-- Context to retrieve and cache a movie image at a specific time
httpServer:createContext('/source/([^/]+)/(%d+)%.jpg', Map.assign(FileHttpHandler:new(cacheDir), {
getPath = function(_, exchange)
return string.sub(exchange:getRequest():getTargetPath(), 9)
Expand All @@ -179,6 +207,7 @@ local function createHttpContexts(httpServer)
end)
end
}))
-- Context to retrieve and cache a movie information
httpServer:createContext('/source/([^/]+)/info%.json', Map.assign(FileHttpHandler:new(cacheDir), {
getPath = function(_, exchange)
return string.sub(exchange:getRequest():getTargetPath(), 9)
Expand All @@ -191,6 +220,7 @@ local function createHttpContexts(httpServer)
end)
end
}))
-- Context for the application REST API
httpServer:createContext('/rest/(.*)', RestHttpHandler:new({
['getSourceId?method=POST'] = function(exchange)
local filename = exchange:getRequest():getBody()
Expand Down Expand Up @@ -255,6 +285,7 @@ local function createHttpContexts(httpServer)
return exportId
end,
}))
-- Context that handle the export commands and output to a WebSocket
httpServer:createContext('/console/(.*)', Map.assign(WebSocketUpgradeHandler:new(), {
onOpen = function(_, webSocket, exchange)
local exportId = exchange:getRequestArguments()
Expand Down Expand Up @@ -287,7 +318,8 @@ local function createHttpContexts(httpServer)
local pb = createProcessBuilder(command)
local p = Pipe:new()
pb:redirectError(p)
local ph = pb:start(function(exitCode)
local ph = pb:start()
ph:ended():next(function(exitCode)
if exitCode == 0 then
startNextCommand()
else
Expand All @@ -308,25 +340,7 @@ local function createHttpContexts(httpServer)
}))
end

local serialWorker

local function terminate()
for _, ph in ipairs(processList) do
ph:destroy()
end
processList = {}
for _, exportContext in pairs(exportContexts) do
if exportContext and exportContext.process then
exportContext.process:destroy()
end
end
exportContexts = {}
if serialWorker then
serialWorker:close()
serialWorker = nil
end
end

-- Start the application as an HTTP server or a WebView
if config.webview.disable then
local httpServer = require('jls.net.http.HttpServer'):new()
httpServer:bind(config.webview.address, config.webview.port):next(function()
Expand Down Expand Up @@ -368,27 +382,32 @@ else
end,
}))
if config.webview.native then
serialWorker = require('SerialWorker'):new(function()
serialWorker = require('jls.util.SerialWorker'):new()
serialWorker:call(function()
local win32 = require('win32')
win32.SetWindowOwner();
return function(message)
if message then
if message.save then
return {win32.GetSaveFileName()}
end
local names = table.pack(win32.GetOpenFileName(message.multiple))
local dir = table.remove(names, 1)
if #names == 0 then
return {dir}
end
local filenames = {}
for _, name in ipairs(names) do
table.insert(filenames, dir..'\\'..name)
end
return filenames
_G.win32 = win32
end)
local function getFileName(message)
if message then
if message.save then
return {win32.GetSaveFileName()}
end
local names = table.pack(win32.GetOpenFileName(message.multiple))
local dir = table.remove(names, 1)
if #names == 0 then
return {dir}
end
local filenames = {}
for _, name in ipairs(names) do
table.insert(filenames, dir..'\\'..name)
end
return filenames
end
end)
end
function serialWorker:process(order)
return self:call(getFileName, order)
end
end
return webview:getThread():ended()
end):next(function()
Expand All @@ -400,4 +419,5 @@ else
end)
end

-- Process events until the end
event:loop()
5 changes: 0 additions & 5 deletions fcutSchema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ return {
type = 'boolean',
default = false,
},
ie = {
title = 'Enable IE',
type = 'boolean',
default = false,
},
address = {
title = 'The binding address',
type = 'string',
Expand Down

0 comments on commit 8d83c38

Please sign in to comment.