Skip to content

Instantly share code, notes, and snippets.

@jsinai
Created July 25, 2017 15:17
Show Gist options
  • Save jsinai/b56413c60b74543885a65f1a8682d053 to your computer and use it in GitHub Desktop.
Save jsinai/b56413c60b74543885a65f1a8682d053 to your computer and use it in GitHub Desktop.
Shows how to get network interface information using an http endpoint.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Shows how to get network interface information using an http endpoint.
' This is especially useful from javascript.
' The http endpoint is http://localhost:8080/get-network-info.
' As written, this gets the wired (ethernet) interface info. You can
' get the wifi interface info by passing a 1 instead of 0 to
' GetNicInfo on line 66:
' m.mVar.GetNicInfo(1, retAA.info)
Sub Main()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
m.SetupFunctionHandlers = SetupFunctionHandlers
SetupFunctionHandlers()
m.CreateBsObjects()
m.AddHttpHandlers()
m.EventLoop()
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub SetupFunctionHandlers()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
m.AddHttpHandlers = AddHttpHandlers
m.CreateBsObjects = CreateBsObjects
m.EventLoop = EventLoop
m.GetNetworkInfo = GetNetworkInfo
m.GetNicInfo = GetNicInfo
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub CreateBsObjects()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
m.msgPort = CreateObject("roMessagePort")
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub EventLoop()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
while true
event = wait(0, m.msgPort)
func = event.GetUserData()
If type(func) = "roAssociativeArray" and type(func.__invoke) = "roFunction" Then
func(event)
Endif
End while
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub AddHttpHandlers()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
m.localServer = CreateObject("roHttpServer", { port: 8080 })
m.localServer.SetPort(m.msgPort)
getNetworkInfoAA = { __invoke: m.GetNetworkInfo, mVar: m }
m.localServer.AddGetFromEvent({ url_path: "/get-network-info", user_data: getNetworkInfoAA, passwords: invalid })
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub SendHttpResponse(e as Object, retAaOrStr, statusCode as Integer)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If type(retAaOrStr) = "roAssociativeArray" Then
ret$ = FormatJson(retAaOrStr)
Else
ret$ = retAaOrStr
End If
e.SetResponseBodyString(ret$)
e.SendResponse(statusCode)
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub GetNetworkInfo(e as Object)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
retAA = {}
retAA.info = {}
m.mVar.GetNicInfo(0, retAA.info)
SendHttpResponse(e, retAA, 200)
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub GetNicInfo(wiredOrWifi% as Integer, args as Object)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
nc = CreateObject("roNetworkConfiguration", wiredOrWifi%)
cc = invalid
If type(nc) = "roNetworkConfiguration" Then
cc = nc.GetCurrentConfig()
End If
If cc <> invalid Then
args.priority = cc.metric
args.hostname = cc.hostname
args.ipAddress = cc.ip4_address
args.subnetMask = cc.ip4_netmask
args.defaultGateway = cc.ip4_gateway
args.macAddress = cc.ethernet_mac
args.domain = cc.domain
args.dhcp = cc.dhcp
args.dnsServers = cc.dns_servers
args.timeServer = cc.time_server
If wiredOrWifi% = 1 Then
args.ssid = cc.wifi_essid
End If
Else
args.priority = -1
args.hostname = ""
args.ipAddress = ""
args.subnetMask = ""
args.defaultGateway = ""
args.macAddress = ""
args.domain = ""
args.dhcp = true
args.dnsServers = ""
args.timeServer = ""
If wiredOrWifi% = 1 Then
args.ssid = ""
End If
End If
nc = invalid
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment