Skip to content

Commit 25c9a45

Browse files
Finalized v1.0
1 parent bd03e99 commit 25c9a45

3 files changed

Lines changed: 78 additions & 2 deletions

File tree

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# luaping
2-
The missing ping command for Lua - implemented in C for Windows.
2+
The missing ping command for Lua - currently implemented in C for Windows platforms.
33

44
## Build for OneLuaPro
55

@@ -26,3 +26,40 @@ local result, errmsg = p.settimeout(50) -- sets timeout to 50ms
2626
-- If result == false, errmsg provides error info, else errmsg = nil
2727
```
2828

29+
## Testbench
30+
31+
```cmd
32+
C:\misc\luaping\test>lua tb.lua
33+
Version info: luaping 1.0
34+
35+
Testing malformed IP address, must return false + errmsg:
36+
Testing xxx.xxx.xxx.xxx : false Argument is not an IP address.
37+
Testing 12345678 : false Call to IcmpSendEcho failed.
38+
Testing localhost : false Argument is not an IP address.
39+
40+
Testing 127.0.0.1, must return true + nil:
41+
Testing 127.0.0.1 : true nil
42+
43+
Testing valid but unreachable IP address, must return false + errmsg:
44+
Note: runtime measurement below 1000ms is not vey exact.
45+
Timeout set to 50 ms.
46+
Testing 192.168.99.99 : false Call to IcmpSendEcho failed.
47+
Runtime : 63 ms.
48+
Timeout set to 100 ms.
49+
Testing 192.168.99.99 : false Call to IcmpSendEcho failed.
50+
Runtime : 499 ms.
51+
Timeout set to 1000 ms.
52+
Testing 192.168.99.99 : false Call to IcmpSendEcho failed.
53+
Runtime : 999 ms.
54+
Timeout set to 5000 ms.
55+
Testing 192.168.99.99 : false Call to IcmpSendEcho failed.
56+
Runtime : 5000 ms.
57+
Timeout set to 10000 ms.
58+
Testing 192.168.99.99 : false Call to IcmpSendEcho failed.
59+
Runtime : 10000 ms.
60+
61+
End of testbench.
62+
63+
C:\misc\luaping\test>
64+
```
65+

src/luaping.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ Code heavily insprired by lsleep library (https://github.com/andrewstarks/lsleep
4141

4242
#define TRUE 1
4343
#define FALSE 0
44+
#define LUAPING_VERSION "luaping 1.0"
4445

4546
// Globals
47+
// https://stackoverflow.com/questions/75701
4648
DWORD timeout = 1000; // in milliseconds
4749

4850
#ifdef _WINDLL
@@ -204,7 +206,7 @@ static int luaping_ping(lua_State *L) {
204206
return 2;
205207
}
206208
#else
207-
// FIXME - non-Win not yet implemented
209+
// FIXME - non-_WINDLL not yet implemented
208210
#endif
209211

210212
static const struct luaL_Reg luaping_metamethods [] = {
@@ -225,5 +227,7 @@ DLL int luaopen_luaping(lua_State *L){
225227
luaL_newlib(L, luaping_funcs);
226228
luaL_newlib(L, luaping_metamethods);
227229
lua_setmetatable(L, -2);
230+
lua_pushliteral(L,LUAPING_VERSION);
231+
lua_setfield(L,-2,"_VERSION");
228232
return 1;
229233
}

test/tb.lua

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
-- luaping test bench
2+
local lp = require "luaping"
3+
local socket = require "socket"
4+
5+
print("Version info: ",lp._VERSION)
6+
print()
7+
8+
print("Testing malformed IP address, must return false + errmsg:")
9+
local stuff0 = {"xxx.xxx.xxx.xxx",12345678,"localhost"}
10+
for i,v in ipairs(stuff0) do
11+
print("Testing ",v,":", lp.ping(v))
12+
end
13+
print()
14+
15+
print("Testing 127.0.0.1, must return true + nil:")
16+
local lh="127.0.0.1"
17+
print("Testing ",lh,":", lp.ping(lh))
18+
print()
19+
20+
print("Testing valid but unreachable IP address, must return false + errmsg:")
21+
print("Note: runtime measurement below 1000ms is not vey exact.")
22+
local ur = "192.168.99.99" -- may vary
23+
local timeouts = {50, 100, 1000, 5000, 10000} -- in milliseconds
24+
for _,v in ipairs(timeouts) do
25+
lp.settimeout(v)
26+
print("Timeout set to",lp.timeout(),"ms.")
27+
local startTime = socket.gettime()*1000 -- milliseconds
28+
print(" Testing ",ur,":", lp.ping(ur))
29+
local stopTime = socket.gettime()*1000 -- milliseconds
30+
print(" Runtime :",math.floor(stopTime-startTime),"ms.")
31+
end
32+
print()
33+
34+
print("End of testbench.")
35+
os.exit(0)

0 commit comments

Comments
 (0)