Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function setHost()
- modemState = rednet.isOpen(checkForModem_modem)
- if not modemState then
- setHost_fail = true
- printMessage("-", tostring(setHost_fail))
- else
- rednet.host(server1_protocol, server1_host)
- setHost_fail = false
- printMessage(nil, "Hosted "..server1_protocol..":"..server1_host)
- end
- end
- function userCheck() --Logs in an user, then returns the username to be used as the hostname
- local isRegistered = fs.exists("/user/localuser")
- if not isRegistered then
- fs.makeDir("/user/")
- local userData = fs.open("/user/localuser", "w")
- if not userData then userCheck_fail = true
- else
- printMessage(nil, "Input Username")
- local newUsername = read()
- userData.write(newUsername)
- userData.close()
- return newUsername
- end
- elseif isRegistered then
- local userData = fs.open("/user/localuser", "r")
- local username = userData.readLine()
- userData.close()
- return username
- end
- end
- function logIn() --Fancy security measures
- local passwordExists = fs.exists("/user/localpassword")
- if passwordExists then
- local passwordRead = fs.open("/user/localpassword", "r")
- local password = passwordRead.readLine()
- passwordRead.close()
- printMessage(nil, "Input password")
- inputPassword = read('*')
- if inputPassword == password then
- return true
- else
- logIn_retry()
- end
- else
- local passwordWrite = fs.open("/user/localpassword", "w")
- printMessage(nil, "Input a new password")
- newPassword = read()
- passwordWrite.write(newPassword)
- printMessage(nil, "Password saved. Rebooting...")
- os.sleep(1)
- os.reboot()
- end
- end
- function logIn_retry()
- if logIn_attempts ~= 3 then
- logIn_attempts = logIn_attempts + 1
- printMessage(2, "Login failed, retry. "..logIn_attempts.."/3")
- logIn()
- else
- if retryTime >= 51 then retryTime = 50 end
- printMessage(2, "Login failed, tried too much. try again in "..retryTime.."s")
- printMessage(2, "Waiting "..retryTime.." seconds to retry.")
- local bX, bY = term.getCursorPos()
- retryTime_count = retryTime
- while retryTime_count ~= 0 do
- retryTime_count = retryTime_count - 1
- os.sleep(1)
- term.setCursorPos(bX, bY - 1)
- printMessage(2, "Waiting "..retryTime_count.." seconds to retry. ")
- end
- retryTime = retryTime + retryTime
- loggedIn = logIn()
- end
- end
- function checkForModem()
- checkForModem_fail = false
- checkForModem_modem = modemPos
- end
- function printMessage(mode, message)
- if aY >= 15 then
- term.clear()
- term.setCursorPos(1, 1)
- end
- if aX == termXsize then
- term.setCursorPos(1, aY+1)
- end
- if not mode then mode = "S:"
- elseif mode == 1 then mode = "E:"
- elseif mode == 2 then mode = "Sec:"
- elseif mode == 3 then mode = "MS:"
- elseif mode == 0 then
- term.clear()
- term.setCursorPos(1, 1)
- mode = "S:"
- message = "------------------------------------"
- elseif mode == "-" then mode = ""
- else mode = "nil:"
- end
- aX, aY = term.getCursorPos()
- term.write(tostring(mode)..tostring(message))
- term.setCursorPos(1, aY+1)
- end
- function errorHandle()
- if setHost_fail then
- printMessage(1, "setHost() failed.")
- errorCount = errorCount + 1
- end
- if checkForModem_fail then
- printMessage(1, "checkForModem() failed.")
- errorCount = errorCount + 1
- end
- if userCheck_fail then
- printMessage(1, "userCheck() failed.")
- errorCount = errorCount + 1
- end
- if houseServer_fail then
- printMessage(1, "Cannot locate Main Server.")
- errorCount = errorCount + 1
- end
- if not receivedCommands_success then
- printMessage(1, "Nothing received, Server busy or closed.")
- errorCount = errorCount + 1
- end
- if requestCommands_retry >= 4 then
- printMessage(1, "Cannot fetch commands. No more attempts.")
- errorCount = errorCount + 1
- end
- if not loggedIn then
- printMessage(1, "Not logged in.")
- errorCount = errorCount + 1
- end
- if stayOn_fail then
- printMessage(1, "Stay On failed.")
- errorCount = errorCount + 1
- end
- if identify_fail then
- printMessage(1, "Identification failed.")
- errorCount = errorCount + 1
- end
- end
- --[[Action Functions]]--
- function requestCommands()
- rednet.send(houseServer, request_commands, "Request")
- local id, message, protocol = rednet.receive(5)
- if id then
- return message, true
- else
- return "No commands available", false
- end
- end
- function issueCommand(command)
- rednet.send(houseServer, command, "Command")
- local id, message, protocol = rednet.receive(20)
- if id and message == "stayOn" then
- stayOn(id)
- return message, true
- elseif id then
- return message, true
- else
- return message, false
- end
- end
- function inputCommand()
- printMessage(nil, "Input a command")
- local command = read()
- if command == shutdown then
- printMessage(nil, "Command: 'shutdown' received.")
- printMessage(nil, "Shutting down...")
- os.sleep(2)
- os.shutdown()
- else
- printMessage(nil,"'"..command.."' issued.")
- return command
- end
- end
- function act()
- local command = inputCommand()
- local reply, success = issueCommand(command)
- if success then
- printMessage(3, message)
- else
- printMessage(1, "Unsuccesful")
- end
- end
- function stayOn(serverID)
- rednet.send(serverID, true, "Success")
- stayOn_act = true
- while stayOn_act and not stayOn_fail do
- printMessage(nil, "Waiting 15 seconds for reply")
- id, message, protocol = rednet.receive(15)
- if message == "actionEnd" then
- printMessage(nil, "actionEnd received. stopping.")
- stayOn_act = false
- elseif message == "actionREAD" then
- printMessage(nil, "actionREAD received. reading input")
- stayOn_inputCommand = read()
- printMessage(nil, stayOn_inputCommand.." issued to "..serverID)
- rednet.send(serverID, stayOn_inputCommand, "Success")
- elseif id then
- printMessage(3, message)
- elseif not id then
- printMessage(1, "No message received, stopping...")
- stayOn_fail = true
- end
- end
- end
- --[[Action Functions end]]--
- function fetchCommands()
- printMessage(nil, "Fetching commands...")
- receivedCommands, receivedCommands_success = requestCommands()
- if receivedCommands_success then
- return true
- else
- return false
- end
- end
- if not setHost_fail or checkForModem_fail then
- startup_fail = true
- end
- function identify()
- printMessage(nil, "Registering in Server")
- rednet.send(houseServer, "identity", "Command")
- printMessage(nil, "Waiting for reply")
- local id, message, protocol = rednet.receive(10)
- if id then
- printMessage(nil, "Sending identity data")
- rednet.send(houseServer, username, "Success")
- else
- identify_retry()
- end
- end
- function identify_retry()
- printMessage(1, "Did not receive reply in time")
- identify_retryCount = identify_retryCount + 1
- if identify_retryCount ~= 4 then
- printMessage(nil, "Trying again...")
- identify()
- else
- printMessage(1, "Tried too many times, gave up.")
- identify_fail = true
- end
- end
- function startup()
- -- protocol hierarchy list MUST BE TOP ROW IN EVERY startup() FUNCTION
- protocolLv1 = "alpha"
- protocolLv2 = "beta"
- protocolLv3 = "gamma"
- protocolLv4 = "delta"
- -- end
- -- local host settings MUST BE SECOND ROW IN EVERY startup() FUNCTION
- server1_protocol = protocolLv2
- server1_host = tostring(userCheck())
- -- end
- -- global variables MUST BE THIRD ROW IN EVERY startup() FUNCTION
- errorCount = 0
- checkForCommand_attempts = 0
- logIn_attempts = 0
- retryTime = 2
- modemPos = "back"
- requestCommands_retry = 0
- receivedCommands = nil
- termXsize, termYsize = term.getSize()
- aX = 1
- aY = 1
- identify_retryCount = 0
- RCD_VERSION = "A.1.1.7"
- request_commands = "request_commands"
- -- end
- --peripherals MUST BE FOURTH ROW IN EVER startup() FUNCTION
- term.clear()
- term.setCursorPos(1,1)
- checkForModem()
- rednet.open(modemPos)
- -- end
- setHost()
- printMessage(nil, "Running RCD v.'"..RCD_VERSION.."'")
- printMessage(nil, "with '".._VERSION.."'")
- loggedIn = logIn()
- -- static hosts settings MUST BE AFTER setHost() CALL IN EVERY startup() FUNCTION
- printMessage(nil, "Looking for Main Server...")
- houseServer = rednet.lookup(protocolLv1, "houseServer")
- if not houseServer then
- printMessage(1, "Could not find Main Server")
- houseServer_fail = true
- return true
- else
- printMessage(nil, "Main Server's ID: "..tostring(houseServer))
- end
- -- end
- -- Identify MUST BE RIGHT BEFORE fetchCommands() CALL
- identify()
- -- commands list MUST BE RIGHT BEFORE errorHandle() CALL
- fetchCommands()
- while not receivedCommands_success and requestCommands_retry ~= 3 do
- requestCommands_retry = requestCommands_retry + 1
- printMessage(1, "Failed. Retrying. "..requestCommands_retry.. "/3")
- fetchCommands()
- end
- -- end
- errorHandle()
- if errorCount == 1 then
- printMessage(nil, "Started with "..errorCount.." error.")
- elseif errorCount == 0 then
- printMessage(nil, "Started with no errors")
- elseif errorCount >= 2 then
- printMessage(nil, "Started with "..errorCount.." errors.")
- end
- printMessage(nil, "Available commands are: ")
- printMessage("-", receivedCommands)
- end
- startup_fail = startup()
- if startup_fail then
- printMessage(1, "Startup unsuccesful")
- os.sleep(2)
- os.shutdown()
- end
- while true do
- act()
- errorHandle()
- end
- --DiegoG
- --Base RCD OS A.1.1.7
- --23/10/2016
Advertisement
Add Comment
Please, Sign In to add comment