Skip to content

Commit f1e01c3

Browse files
Added lua example
1 parent 295d528 commit f1e01c3

6 files changed

Lines changed: 84 additions & 3 deletions

File tree

examples/src/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,10 @@ if(BUILD_NIH_SWIG)
5454
add_subdirectory(networkInterfacesEnumeratorCSharp)
5555
endif()
5656
endif()
57+
58+
# If "lua" is listed in NIH_SWIG_LANGUAGES
59+
if("lua" IN_LIST NIH_SWIG_LANGUAGES)
60+
# Network interfaces enumerator using Lua bindings
61+
add_subdirectory(networkInterfacesEnumeratorLua)
62+
endif()
5763
endif()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Lua example
2+
3+
# Copy the Lua script to the build directory
4+
configure_file(networkInterfacesEnumerator.lua ${CMAKE_CURRENT_BINARY_DIR}/networkInterfacesEnumerator.lua COPYONLY)
5+
6+
# Custom target to copy the SWIG Lua library to the build directory so the script can find it
7+
add_custom_target(copy_lua_library ALL
8+
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:la_networkInterfaceHelper-lua> ${CMAKE_CURRENT_BINARY_DIR}/la_networkInterfaceHelper${CMAKE_SHARED_MODULE_SUFFIX}
9+
DEPENDS la_networkInterfaceHelper-lua
10+
COMMENT "Copying Lua SWIG library to example directory"
11+
)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env lua
2+
3+
-- Network Interfaces Enumerator Lua Example
4+
5+
-- Load the networkInterfaceHelper module
6+
local nih = require("la_networkInterfaceHelper")
7+
print("Using " .. nih.getLibraryName() .. " v" .. nih.getLibraryVersion())
8+
print(nih.getLibraryCopyright())
9+
10+
function displayInterfaces()
11+
print("Available interfaces:")
12+
13+
local intNum = 1
14+
local obs = nih.la.networkInterface.NetworkInterfaceHelper.DefaultedObserver()
15+
swig_override(obs, "onInterfaceAdded", function(self, intfc)
16+
print(intNum .. ": " .. intfc.id)
17+
print(" Description: " .. intfc.description)
18+
print(" Alias: " .. intfc.alias)
19+
print(" MacAddress: " .. nih.la.networkInterface.NetworkInterfaceHelper.macAddressToString(intfc.macAddress))
20+
print(" Type: " .. intfc.type)
21+
print(" Enabled: " .. (intfc.isEnabled and "YES" or "NO"))
22+
print(" Connected: " .. (intfc.isConnected and "YES" or "NO"))
23+
print(" Virtual: " .. (intfc.isVirtual and "YES" or "NO"))
24+
25+
if intfc.ipAddressInfos:size() > 0 then
26+
print(" IP Addresses:")
27+
for i = 0, intfc.ipAddressInfos:size() - 1 do
28+
local info = intfc.ipAddressInfos[i]
29+
if info.address:getType() == nih.la.networkInterface.IPAddress.Type_V4 then
30+
print(" " ..
31+
info.address:toString() ..
32+
" (" ..
33+
info.netmask:toString() ..
34+
") -> " .. info:getNetworkBaseAddress():toString() .. " / " .. info:getBroadcastAddress():toString())
35+
else
36+
-- IPv6
37+
print(" " ..
38+
info.address:toString() ..
39+
"/" ..
40+
nih.la.networkInterface.IPAddress.prefixLengthFromPackedV6(info.netmask:getIPV6Packed()) ..
41+
" -> " .. info:getNetworkBaseAddress():toString())
42+
end
43+
end
44+
end
45+
46+
if intfc.gateways:size() > 0 then
47+
print(" Gateways:")
48+
for i = 0, intfc.gateways:size() - 1 do
49+
local ip = intfc.gateways[i]
50+
print(" " .. ip:toString())
51+
end
52+
end
53+
54+
intNum = intNum + 1
55+
end)
56+
57+
local helper = nih.la.networkInterface.NetworkInterfaceHelper.getInstance()
58+
helper:registerObserver(obs)
59+
helper:unregisterObserver(obs)
60+
end
61+
62+
displayInterfaces()

scripts/bashUtils

src/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ if(BUILD_NIH_SWIG)
122122
include(${CU_ROOT_DIR}/cmake/cmakeUtils/helpers/GenerateNugetTarget.cmake REQUIRED)
123123
cu_generate_csharp_nuget_target(TARGET_NAME ${SWIG_TARGET_NAME} NUGET_SOURCE_URL "${NUGET_PUBLISH_SOURCE_URL}" NUGET_API_KEY "${NUGET_PUBLISH_API_KEY}" CS_SOURCE_FOLDERS "${SUPPORT_FILES_FOLDER_csharp}")
124124
elseif(${SWIG_LANG} STREQUAL "lua")
125-
target_link_libraries(${SWIG_TARGET_NAME} PRIVATE liblua)
125+
find_package(Lua 5.4 REQUIRED)
126+
target_include_directories(${SWIG_TARGET_NAME} PRIVATE ${LUA_INCLUDE_DIR})
127+
target_link_libraries(${SWIG_TARGET_NAME} PRIVATE ${LUA_LIBRARIES})
126128
elseif(${SWIG_LANG} STREQUAL "python")
127129
find_package(Python3 COMPONENTS Development REQUIRED)
128130
target_link_libraries(${SWIG_TARGET_NAME} PRIVATE Python3::Python)

0 commit comments

Comments
 (0)