-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathforman.lua
More file actions
75 lines (66 loc) · 1.69 KB
/
forman.lua
File metadata and controls
75 lines (66 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
local tArgs = { ... }
if #tArgs ~= 1 then
print("Usage: forman <plan>|<filename> ...")
print(" [<sendChannel> ...")
print(" [<recieveChannel>]]")
return
end
os.loadAPI("apis/act")
local modem
for _, side in ipairs(rs.getSides()) do
if peripheral.getType(side) == "modem" then
modem = peripheral.wrap(side)
end
end
if not modem then
print("Not a wireless modem or computer")
return
end
local plan = tArgs[1]
if fs.exists(plan) then
print("using file "..plan)
local file = fs.open(plan, "r")
plan = file.readAll()
file.close()
end
local sendChannel = 284
if tArgs[2] then
sendChannel = tonumber(tArgs[2]) or sendChannel
end
local recieveChannel = 220
if tArgs[2] then
recieveChannel = tonumber(tArgs[2]) or recieveChannel
end
if not modem.isOpen(recieveChannel) then
modem.open(recieveChannel)
end
local ast = act.parse(plan)
if not ast then
print("act script cannot be parsed")
return
end
function ping(worker, nTimeout)
local message = worker.."@"..worker..":If"
nTimeout = nTimeout or 1
modem.transmit(sendChannel, recieveChannel, message)
local timer = os.startTimer(nTimeout)
while true do
local event, modemSide, senderChannel, replyChannel, returnMessage, senderDistance = os.pullEvent()
if event == "modem_message" then
return message
elseif event == "timer" and modemSide == timer then
return nil
end
end
end
local workers = act.getWorkers(ast)
for worker, _ in pairs(workers) do
if ping(worker) then
print(worker.." online")
else
print(worker.." offline")
return
end
end
act.interpret(ast, {executing=true, modem=modem, channel=sendChannel, replyChannel=recieveChannel})
modem.closeAll()