Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem = peripheral.wrap("top");
- local network = peripheral.wrap("back");
- --[[
- Networking Variables
- ]]--
- local s_netChannel = 1;
- local s_computers = {};
- local responseChannels = {};
- --[[
- Generic Functions
- ]]--
- function in_table(tbl, val)
- for i,v in pairs(tbl) do
- if(v == val) then
- return true;
- end --if
- end --for
- return false;
- end --in_table
- --[[
- Network Functions
- ]]--
- function generateResponseChannel()
- while true do
- local channel = math.random(10000,12000);
- if(in_table(reponseChannels, channel) == false) then
- table.insert(responseChannels, channel);
- return channel;
- end --if
- end --while
- end --generateResponseChannel
- 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 netTcpSendResponse(target)
- network.transmit(target, s_netChannel, "tcpreceived:"..os.getComputerID());
- end --netTcpSendResponse
- function netSend(target, message, tcp)
- network.transmit(target, s_netChannel, message);
- print("Request Sent");
- if( tcp ~= nil and tcp == true ) then
- for i = 1, 3 do
- if( netTcpReceiveResponse(target) ) then
- return true;
- end --if
- end --for
- return false;
- end --if
- return true;
- end --netSend
- function netPackage(act, dt)
- local pack = { action = act, data = dt }
- return textutils.serialise(pack);
- end --netPackage
- function netReceive(target, act)
- local timeout = 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 --if
- elseif( e == "timer" and v1 == timeout ) then
- break;
- end --if
- end --while
- end --netReceive
- function getComputers()
- local names = network.getNamesRemote();
- for i = 1, #names do
- local target = peripheral.wrap(names[i])
- if(target.isOn()) then
- local pack = netPackage("requestName")
- if( netSend(target.getID(), pack, true) ) then
- local data = netReceive(target.getID(), "requestName");
- s_computers[target.getID()] = { name = data.name, channel = target.getID() };
- end --if
- end --if
- end --for
- end --getComputers
- function main()
- network.open(1);
- getComputers();
- term.clear();
- term.setCursorPos(1,1);
- for k,v in pairs(s_computers) do
- print(v);
- end --if
- end --main
- main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement