Posted 15 December 2013 - 10:20 AM
Hello..
Im still new to the whole "modem API" and therefore i wanted to make some test with modem.transmit, modem.open etc..
I want to make a pinging program ( 1 client ( can be as many as i want ) and 1 server ( which handles everything ) )
but i'm having trouble calculating how long the response time was from when the ping was sent, to a pong was received..
CLIENT
SERVER
If you have time, i would also like to hear more about how the os.startTimer works so i can improve my knowledge :)/>
Thanks in Advance
Im still new to the whole "modem API" and therefore i wanted to make some test with modem.transmit, modem.open etc..
I want to make a pinging program ( 1 client ( can be as many as i want ) and 1 server ( which handles everything ) )
but i'm having trouble calculating how long the response time was from when the ping was sent, to a pong was received..
CLIENT
Spoiler
-- ID
local modem = peripheral.wrap("right")
local data={
["channel"]={
_server=24510,
_client=24511
},
["message"]={
_check={"PING";"PONG"};
},
}
local colorW=function(...)
local curColor
for i=1, #arg do -- arg is ...
if type(arg[i]) == 'number' then
curColor = arg[i]
else
if curColor then
term.setTextColor(curColor)
end
term.write(arg[i])
end
end
print()
end;
modem.open(data["channel"]._server) modem.open(data["channel"]._client)
colorW(colors.blue,"PINGING "..data["channel"]._server)
local timeToRespond = os.startTimer(0)
modem.transmit(data["channel"]._client, data["channel"]._server, data["message"]._check[1])
while true do
local event, p1,p2,p3,p4,p5 = os.pullEvent()
--local event, modemSide, senderChannel, replyChannel, message, senderDistance
if event == "timer" then
local str=tostring(p1)
if p2 then
str=str.." - "..tostring(p2)
if p3 then
str=str.." - "..tostring(p3)
if p4 then
str=str.." - "..tostring(p4)
if p5 then
str=str.." - "..tostring(p5)
end
end
end
end
print("TIMER\n"..str)
elseif event == "modem_message" then
if senderChannel==data["channel"]._server then
if message==data["message"]._check[1] then
colorW(colors.blue,"Received PING from "..senderChannel)
colorW(colors.blue,"Reply PONG to "..data["channel"]._client)
modem.transmit(data["channel"]._client, data["channel"]._server, data["message"]._check[2])
elseif message==data["message"]._check[2] then
colorW(colors.blue,"Received PONG from "..senderChannel)
colorW(colors.green,"Server respond in "..timeToRespond)
else
print("The message was: "..message)
end
end
end
end
Spoiler
-- ID
local modem = peripheral.wrap("right")
local data={
["channel"]={
_server=24510,
_client=24511
},
["message"]={
_check={"PING";"PONG"};
},
}
local colorW=function(...)
local curColor
for i=1, #arg do -- arg is ...
if type(arg[i]) == 'number' then
curColor = arg[i]
else
if curColor then
term.setTextColor(curColor)
end
term.write(arg[i])
end
end
print()
end;
modem.open(data["channel"]._server) modem.open(data["channel"]._client)
while true do
local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
if senderChannel==data["channel"]._client then
if message==data["message"]._check[1] then
colorW(colors.lime,"CLIENT ",colors.yellow,"> ",colors.blue,"Ping from "..senderChannel)
modem.transmit(data["channel"]._server, data["channel"]._client, data["message"]._check[2])
elseif message==data["message"]._check[2] then
colorW(colors.lime,"CLIENT ",colors.yellow,"> ",colors.blue,"Reply from "..senderChannel.." PONG")
else
print("The message was: "..message)
end
end
end
If you have time, i would also like to hear more about how the os.startTimer works so i can improve my knowledge :)/>
Thanks in Advance