Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local network = peripheral.wrap("back");
- local s_netChannel = os.getComputerID();
- --[[
- Network Functions
- ]]--
- function netTcpReceiveResponse(target)
- local timeout = os.startTimer(5);
- while true do
- local e, v1, sChannel, rChannel, resp = os.pullEvent();
- if( e == "modem_message" ) then
- if( resp == "tcpreceived:"..target ) then
- return true;
- end --if
- elseif( e == "timer" and v1 == timeout ) then
- return false;
- end --if
- end --while
- end --netTcpReceiveResponse
- function netSend(target, message, tcp)
- network.transmit(target, s_netChannel, message);
- if(tcp ~= nil and tcp == true) then
- netTcpReceiveResponse(target);
- end --if
- end --netSend
- function netPackage(act, dt)
- local pack = { action = act, data = dt }
- return textutils.serialise(pack);
- end --netPackage
- function netReceive(target, act)
- local timer = os.startTimer(5);
- while true do
- local e, v1, sChannel, rChannel, resp = os.pullEvent();
- if( e == "modem_message" ) then
- local data = textutils.unserialise(resp);
- if( data.action == act ) then
- return data.data;
- end
- end --if
- end --while
- end --netReceive
- function netTcpSendResponse(target)
- network.transmit(target, s_netChannel, "tcpreceived:"..os.getComputerID());
- print("tcp response sent");
- end --netTcpSendResponse
- --[[
- Main
- ]]--
- function main()
- network.open(os.getComputerID());
- while true do
- local _, _, sChannel, rChannel, resp = os.pullEvent("modem_message");
- print("modem message received");
- if( resp ~= nil ) then
- local data = textutils.unserialise(resp);
- if( data.action == "requestName" ) then
- netTcpSendResponse(rChannel);
- local pack = netPackage("requestName", { name = os.getComputerLabel() });
- netSend(rChannel, pack, false);
- end --if
- end --if
- end --while
- end --main
- main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement