Call Lua business scripts in Go or C to implement hot update solutions for terminal applications

With the popularization of 5G networks, the two directions of future terminal architecture development are boldly predicted, and terminal equipment functions will be more powerful and intelligent.

First, with the popularization of 5G, online services and services on terminal equipment will be lighter, that is, the idea of ​​thin clients. The business is realized in the background, and the terminal only becomes the medium of the operating entity, so the amount of maintenance will be greatly reduced, and the business is in the background, which is more conducive to upgrade iteration and maintenance.

The second is offline, idle online services, the terminal can be lightweight, hot update applications. Modularize, componentize, and script your business. Every time you upgrade, you don’t need to upgrade the whole thing. You may only need to write a good business script and send a lightweight script of several K to realize the hot update of the application, and the user has no perception.

Borrowing the icon of docker, its icon is very vivid, a large ship drags many containers. By analogy with the real world, other functions of embedded applications are equivalent to large ships, providing basic services, and business equivalent to containers. Terminals in all places are good for maintaining a big ship, which is conducive to iteration and stability, and business scripting is more conducive to reuse and hot updates, lightweight upgrades, users have no perception, and fast iterative services. This will greatly improve user experience, reduce operation and maintenance costs, and improve development efficiency, which will become possible in the future.

Associated with the hardware driver, a layer of encapsulation and a layering are performed. In this way, no matter how the bottom layer changes, the upper layer applications will not be affected.

And fully consider modularization and componentization. Technical components can be reused, business modules can be reused. The bottom layer provides a stable and powerful infrastructure, and applications will be as agile and rapid as building blocks, focusing on business and more innovative things.

Of course, the use of all technologies is not a silver bullet, but is used to solve pain points and a certain practical problem. It's not for grandstanding, for the sake of technology.

When making a choice, weigh the pros and cons. Do the advantages outweigh the disadvantages, or the disadvantages outweigh the advantages. For example, if the terminal function is very simple, the business is very simple, does not change frequently, does not iterate frequently, and does not involve reuse, then there is no need to do this. How simple it is.

And it is not difficult to do so. The learning cost involved is high. Golang, Lua, and C are all involved. The foundation and wheel building in the early stage requires a little effort, and the subsequent business implementation may be very simple, that is, writing business scripts in a large container environment, similar to building blocks to achieve business flexibly and quickly update iterations.

The following is an attempt to call Lua business scripts in Go applications: (Note, it has been verified that it is feasible on both the computer and the ARM board)

The following is one of my verifications:

package main

import (
	"fmt"
	"github.com/sryanyuan/luago"
	"strconv"
	"sync"
)

func export(L luago.Lua_Handle) int {
	//	get args from lua
	num := int(luago.Lua_tonumber(L, -2))
	str := luago.Lua_tostring(L, -1)

	fmt.Printf("this is a luago func\n")
	//	push value to lua
	val := str + strconv.Itoa(num)
	luago.Lua_pushstring(L, val)
	return 1
}

func main() {
	var wg sync.WaitGroup
	wg.Add(1) //

	L := luago.LuaGo_newState()
	fmt.Printf("lua ver:%#v\n", luago.Lua_version(L.GetHandle()))
	L.OpenStdLibs()

	L.LuaGo_PushGoFunction("export", export)

	luago.LuaGo_stackDump(L.GetHandle())
	ret := luago.LuaL_dofile(L.GetHandle(), "test1.lua")

	fmt.Printf("ret:%d\n", ret)
	//	invoke
	ret = luago.LuaL_dostring(L.GetHandle(), ` 
		local val = export(1, "hello luago")
		print(val)
		shanghu = string.format("%012u",123456789011) 
		print(shanghu)
	`)

	fmt.Printf("ret:%d\n", ret)

	luago.LuaGo_stackDump(L.GetHandle())

	ret = luago.LuaL_dofile(L.GetHandle(), "test.lua")

	luago.LuaGo_stackDump(L.GetHandle())
	//wg.Wait() // 等待
}

Attached test1.lua script:

--this is a demo:
----------------------------------------------------

print("this is a lua script,begin:")
--通信服务地址
ip= "127.0.0.1"
--通信服务端口
port = 5050

function CONNECT( ip,port )
	-- body
	print("this is CONNECT,"..ip..port)
end

function TxData( tx )
	-- body
	print("this is TxData,"..tx)
end

function DISCONNECT( ... )
	-- body
	print("this is DISCONNECT")
end

--连接到通信前置服务
ret = CONNECT(ip,port)           --建立连接
-------------------------------------------  
--通讯报文规范是基于TCP/IP 通讯协议而设计,支持二进制字节流。采用统一的消息类型标识识别方式进行处理
--通信协议格式: MTI+STI+DBL+DATA
--签到报文 ,
MTI = 'B001'    --消息类型标识
STI = '52'      --请求类型标识
DBL = '0000001C'      --报文体长度
DATA = ''       --数据域

shanghu = string.format("%012d",3456789012) 
poscode = string.format("%016d",34567890112233)
samcode = '313233343536'
optcode = '3132333435363738'

DATA = shanghu..poscode..samcode..optcode
DBL = string.format("%08x",string.len(DATA)/2)
TX = MTI .. STI .. DBL .. DATA

ret,rcv = TxData(TX)      --调用接口TxData(str),实际会以二进制的字节流发送报文
--print(rcv)

--黑名单下载
MTI = 'B004'
blackver = '00000000'
DATA = blackver .. poscode
DBL = string.format("%02x",string.len(DATA)/2)
TX = MTI .. STI .. DBL .. DATA

ret,rcv = TxData(TX)           
print(rcv)

----lua test go function 
print("this is go function test by lua")

--LtestGofunc0(7,8,9)

--LtestGofunc1()


DISCONNECT()                          --断开连接

print("lua script over")

 

Guess you like

Origin blog.csdn.net/qq8864/article/details/108502558