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 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 netTcpResponse(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 --netTcpResponse
- function netSend(target, message, tcp = true)
- modem.transmit(target, s_netChannel, textutils.serialise(message));
- if(tcp) then
- netTcpResponse(target);
- end --if
- end --netSend
- function netPackage(act, dt = nil)
- 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 getComputers()
- local names = network.getNamesRemote();
- for i = 1, #names do
- local target = peripheral.wrap(names[i])
- if(target.isOn()) then
- local pack = netPackage("requestName")
- netSend(target.getID(), pack);
- netReceive(target.getID(), "requestName")
- end --if
- end --for
- end --getComputers
- function main()
- network.open(1);
- getComputers();
- end --main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement