Skip to content

Commit

Permalink
rewrite io service log
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Mar 11, 2024
1 parent 3b297a5 commit 51db49b
Showing 1 changed file with 77 additions and 63 deletions.
140 changes: 77 additions & 63 deletions engine/firmware/io.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,30 @@ local config, fddata = io_req:bpop()
local QUIT = false
local OFFLINE = false

local _print = _G.print
if platform.os == 'android' then
local android = require "android"
_print = function (text)
android.rawlog("info", "IO", text)
local LOG; do
local fs = require "bee.filesystem"
local AppPath
if platform.os == "ios" then
local ios = require "ios"
AppPath = fs.path(ios.directory(ios.NSDocumentDirectory)):string()
elseif platform.os == 'android' then
local android = require "android"
AppPath = fs.path(android.directory(android.ExternalDataPath)):string()
else
AppPath = fs.current_path():string()
end
local logfile = AppPath .. "/io_thread.log"
fs.create_directories(AppPath)
if fs.exists(logfile) then
fs.rename(logfile, AppPath .. "/io_thread_1.log")
end
local function LOGRAW(data)
local f <close> = io.open(logfile, "a+")
if f then
f:write(data)
f:write("\n")
end
end
end

local channelfd = socket.fd(fddata)
local channelfd_init = false

thread.setname "ant - IO thread"

local vfs = assert(fw.loadfile "vfs.lua")()
local repo = vfs.new(config.vfs)

local connection = {
request = {},
sendq = {},
recvq = {},
fd = nil,
flags = 0,
}

local function connection_send(...)
local pack = string.pack("<s2", serialization.packstring(...))
table.insert(connection.sendq, 1, pack)
end

local function init_channels()
io_req = thread.channel "IOreq"

local origin = os.time() - os.clock()
local function os_date(fmt)
Expand All @@ -91,29 +84,50 @@ local function init_channels()
end
return table.concat(t, '\t')
end
function _G.print(...)
function LOG(...)
local info = debug.getinfo(2, 'Sl')
local text = ('[%s][IO ](%s:%3d) %s'):format(os_date('%Y-%m-%d %H:%M:%S:{ms}'), info.short_src, info.currentline, packstring(...))
if OFFLINE then
_print(text)
else
connection_send("LOG", text)
end
LOGRAW(text)
end
end

local channelfd = socket.fd(fddata)
local channelfd_init = false

thread.setname "ant - IO thread"

local vfs = assert(fw.loadfile "vfs.lua")()
local repo = vfs.new(config.vfs)

local connection = {
request = {},
sendq = {},
recvq = {},
fd = nil,
flags = 0,
}

local function connection_send(...)
local pack = string.pack("<s2", serialization.packstring(...))
table.insert(connection.sendq, 1, pack)
end

local function init_channels()
io_req = thread.channel "IOreq"
end

local function connect_server(address, port)
print("Connecting", address, port)
LOG("Connecting", address, port)
local fd, err = socket "tcp"
if not fd then
_print("[ERROR]: "..err)
LOG("[ERROR]: "..err)
return
end
local ok
ok, err = fd:connect(address, port)
if ok == nil then
fd:close()
_print("[ERROR]: "..err)
LOG("[ERROR]: "..err)
return
end
if ok == false then
Expand All @@ -124,30 +138,30 @@ local function connect_server(address, port)
local ok, err = fd:status()
if not ok then
fd:close()
_print("[ERROR]: "..err)
LOG("[ERROR]: "..err)
return
end
print("Connected")
LOG("Connected")
return fd
end

local function listen_server(address, port)
print("Listening", address, port)
LOG("Listening", address, port)
local fd, err = socket "tcp"
if not fd then
_print("[ERROR] socket: "..err)
LOG("[ERROR] socket: "..err)
return
end
fd:option("reuseaddr", 1)
local ok
ok, err = fd:bind(address, port)
if not ok then
_print("[ERROR] bind: "..err)
LOG("[ERROR] bind: "..err)
return
end
ok, err = fd:listen()
if not ok then
_print("[ERROR] listen: "..err)
LOG("[ERROR] listen: "..err)
return
end
selector:event_add(fd, SELECT_READ)
Expand All @@ -156,15 +170,15 @@ local function listen_server(address, port)
if not newfd then
selector:event_del(fd)
fd:close()
_print("[ERROR] accept: "..err)
LOG("[ERROR] accept: "..err)
return
end
print("Accepted")
LOG("Accepted")
selector:event_del(fd)
fd:close()
return newfd
end
_print("[ERROR] select: timeout")
LOG("[ERROR] select: timeout")
selector:event_del(fd)
fd:close()
end
Expand All @@ -190,7 +204,7 @@ local function response_id(id, ...)
end

local function response_err(id, msg)
print("[ERROR]", msg)
LOG("[ERROR]", msg)
response_id(id, nil)
end

Expand Down Expand Up @@ -234,7 +248,7 @@ end

local function request_start_with_token(req, args, token, promise)
if OFFLINE then
print("[ERROR] " .. req .. " failed in offline mode.")
LOG("[ERROR] " .. req .. " failed in offline mode.")
promise.reject()
return
end
Expand Down Expand Up @@ -296,11 +310,11 @@ local response = {}

function response.ROOT(hash)
if hash == '' then
_print("[ERROR] INVALID ROOT")
LOG("[ERROR] INVALID ROOT")
os.exit(-1, true)
return
end
print("[response] ROOT", hash)
LOG("[response] ROOT", hash)
local resources = repo:init(hash)
for path in pairs(resources) do
CMD.LIST(nil, path)
Expand All @@ -311,31 +325,31 @@ end
-- It's rare because the file name is sha1 of file content. We don't need update the file.
-- Client may not request the file already exist.
function response.BLOB(hash, data)
print("[response] BLOB", hash, #data)
LOG("[response] BLOB", hash, #data)
if repo:write_blob(hash, data) then
request_resolve(hash)
end
end

function response.FILE(hash, size)
print("[response] FILE", hash, size)
LOG("[response] FILE", hash, size)
repo:write_file(hash, size)
end

function response.MISSING(hash)
print("[response] MISSING", hash)
LOG("[response] MISSING", hash)
request_reject(hash, "MISSING "..hash)
end

function response.SLICE(hash, offset, data)
print("[response] SLICE", hash, offset, #data)
LOG("[response] SLICE", hash, offset, #data)
if repo:write_slice(hash, offset, data) then
request_resolve(hash)
end
end

function response.RESOURCE(fullpath, hash)
print("[response] RESOURCE", fullpath, hash)
LOG("[response] RESOURCE", fullpath, hash)
repo:add_resource(fullpath, hash)
request_resolve(fullpath)
end
Expand All @@ -345,7 +359,7 @@ local ListNeedResource <const> = 4

function CMD.LIST(id, fullpath)
fullpath = fullpath:gsub("|", "/")
-- print("[request] LIST", path)
-- LOG("[request] LIST", path)
local dir, r, hash = repo:list(fullpath)
if dir then
response_id(id, dir)
Expand All @@ -364,7 +378,7 @@ end

function CMD.TYPE(id, fullpath)
fullpath = fullpath:gsub("|", "/")
-- print("[request] TYPE", fullpath)
-- LOG("[request] TYPE", fullpath)
if fullpath == "/" then
response_id(id, "dir")
return
Expand Down Expand Up @@ -429,7 +443,7 @@ function CMD.READ(id, fullpath)
end

function CMD.RESOURCE_SETTING(_, setting)
-- print("[request] RESOURCE_SETTING", setting)
-- LOG("[request] RESOURCE_SETTING", setting)
repo:resource_setting(setting)
request_send("RESOURCE_SETTING", setting)
end
Expand All @@ -451,7 +465,7 @@ end
local function dispatch_net(cmd, ...)
local f = response[cmd]
if not f then
print("[ERROR] Unsupport net command", cmd)
LOG("[ERROR] Unsupport net command", cmd)
return
end
f(...)
Expand All @@ -464,7 +478,7 @@ local function dispatch(ok, id, cmd, ...)
end
local f = CMD[cmd]
if not f then
print("[ERROR] Unsupported command : ", cmd)
LOG("[ERROR] Unsupported command : ", cmd)
else
f(id, ...)
end
Expand Down Expand Up @@ -695,7 +709,7 @@ local function main()
if QUIT then
return
end
print("Working offline")
LOG("Working offline")
work_offline()
end

Expand Down

0 comments on commit 51db49b

Please sign in to comment.