Next Topic

Previous Topic

Book Contents

Advanced script

The advanced script model gives the script author new powerful tools to control parameters giving as arguments to the script. This makes it possible to make Lua scripts that have the same look and feel like native monitor types.

Reserved function names

There are two reserved function names, used by Network Monitor to query information. These function names can not be used for any other purpose.

OnConfigure

This function is called by Network Monitor to have the script populate a LuaScriptConfigurator class instance. The information is then used to create a user interface for the script. The name of the instance must be "Config" (note that casing) so Network Monitor may find it in the Lua stack when the function returns.

OnEnumerate

Each field in the user interface can be enumerated, Network Monitor calls the OnEnumerate function to have the script populate a data strcture, LuaScriptEnumResult, with values the user can select. The OnEnumerate have one parameter, sFieldToEnum, that is used by the script to determin which field/argument to provide enumeration results for. The returned instance must be named "Enum" (note the casing).

The entry point

The advanced script model requires the OnConfigure function to set the name of the entry point function. This function is called by Network Monitor to start the execution of the script. The name of the entry point is by default "main" but can be set by the programmer to any name except the reserved function names.

Example

--This function is called by KNM when enumerating a field

function OnEnumerate(sFieldToEnum)

	--The variable returned must be called "Config" so KNM can find it.
	Enum = LuaScriptEnumResult()

	--Second argument
	if sFieldToEnum == "Argument 2" then
		Enum:Add("First value")
		Enum:Add("Second value")
		Enum:Add("Third value") end
	return Enum
end

--This function is called by KNM to retrieve a script configuration

function OnConfigure()

	--The variable returned must be called "Config" so KNM can find it.
	Config = LuaScriptConfigurator()

	--Author.
	Config:SetAuthor("My name")

	--Description.
	Config:SetDescription("Description of the script, including usage, parameters etc")
	
	--Minimum build version of KNM, set to zero for if no specificbuild version is required.
	Config:SetMinBuildVersion(0)

	--Script version (major/minor)
	Config:SetScriptVersion(1,0)

	--A parameter configuration, add them in the order the script isextracting them.
	Config:AddArgument("Argument 1","This is the description of thefirst argument",LuaScriptConfigurator.CHECK_NOT_EMPTY)

	--Add another parameter, a select box with 3 values.
	Config:AddArgument("Argument 2","This is the description of thesecond argument",LuaScriptConfigurator.CHECK_NOT_EMPTY+LuaScriptConfigurator.ENUM_AVAIL)

	--Set the entry point, this is the function called by KNM
Config:SetEntryPoint("main")

	--Done with configuration, return the object
	return Config
end

--This is the entry point

function main()

	sFirstArgument = GetArgument(0)
	sSecondArgument = GetArgument(1)

	SetExitStatus("OK",true)

end