This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
App1eGenius93's profile picture

Parallel API troubles...

Started by App1eGenius93, 14 June 2012 - 06:49 PM
App1eGenius93 #1
Posted 14 June 2012 - 08:49 PM
Hi,

I'm trying to implement the parallel api and make use of it but I'm having a very diffiuclt time with it. No errors occur I'm just not able to get it to run both functions. I'm guessing it's I just don't understand enough of how the API works to properly modify my code. If anyone could help me make this work, I would be very grateful!

This program basically will listen for a rednet message, split the message, compare some values from a global table variable and then send a reply back. However, I wanted to be able to have a prompt so I can change values while the program is running to prevent downtime. The code I have below probably has a few testing bits in it well I was trying to make this work but I think I removed most of them. I eventually will be releasing this entire program once I finish the other half of it.

If you would like any more information just let me know please. Thank you in advanced!

<code>

function boot()
os.pullEvent = os.pullEventRaw
term.clear()
term.setCursorPos(1,1)
print("Computer ID: "..os.computerID())
print("--------------------------")
end
function buildauth()
authrules = {}
local authfile = fs.open("/data/authrules", "r")
local file = authfile.readLine()
while true do
  line = authfile.readLine()
  if not line then
   break
  else
   for doorid, useraccess in string.gmatch(line, "(%d+),(%d+)") do
    authrules[doorid] = useraccess
   end
  end
end
authfile.close()
end
function main()
  rednet.open("back")
  local d, msg, dist = rednet.receive()
  for doorid, username, useraccess, securitycode in string.gmatch(msg, "(%d+),(%w+),(%d+),(%d+)") do
   if authrules[doorid] <= useraccess then
    status = "GRANTED"
    rednet.broadcast(doorid..",GRANTED,"..securitycode)
   else
    status = "DENIED"
   end
   local logfile = fs.open("/data/log", "a") -- Write to log
   logfile.writeLine(username.." attempted to open door: "..doorid.." with access level "..useraccess.." - "..status)
   logfile.close()
  end
end
function prompt(msg)
  term.clear()
  term.setCursorPos(1,1)
  print(msg)
  print("What would you like to do?")
  print("")
  print("1. Register door")
  print("2. Remove door")
  print("3. Change access level")
  print("")
  local input = io.read()
  input = tonumber(input)
  print("")
  if input == 1 then
   term.write("Enter door ID: ")
   print("")
   local dooruid = io.read()
   term.write("Enter access level: ")
   print("")
   local al = io.read()
   if authrules[dooruid] then
    print("Door exists!")
    prompt()
   else
    local aauthfile = fs.open("/data/authrules", "a")
    aauthfile.write(dooruid..","..al)
    aauthfile.close()
    authrules[dooruid] = al
   end
   prompt()
  elseif input == 2 then
  
  elseif input == 3 then
  
  else
   prompt()
  end
end
-- Start
boot()
buildauth()
while true do
parallel.waitForAll(prompt(), function() shell.run("shell") end)
--parallel.waitForAny(prompt(), main())
end
MysticT #2
Posted 14 June 2012 - 09:06 PM
You need to give the functions as arguments, you don't have to call them:

paralle.waitForAny(prompt, function() shell.run("shell") end)
App1eGenius93 #3
Posted 14 June 2012 - 09:11 PM
You need to give the functions as arguments, you don't have to call them:

paralle.waitForAny(prompt, function() shell.run("shell") end)

Ah! Thank you! That seems to have fixed it. I'm not 100% sure (need to build the client part still) but the rednet turned on so that means it's working believe.

Basically based on what you said, I don't need to do prompt(), i do just prompt (Without the ()'s)

Thank you so much! :(/>/> Been trying to figure that out for a few hours lol.