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

Weird Text Utilities + script error

Started by Himself12794, 11 February 2014 - 08:47 PM
Himself12794 #1
Posted 11 February 2014 - 09:47 PM
So I'm working on a network programwith routers, servers, subserves, clients, etc., and I came across a weird error I can't solve. This program was working just fine, and then randomly started giving me the textutils:177 error. It seems every time I reboot it, itis receiving a blank message, but no other computer is sending out messages. It was working fine, I made no changes, then it just started doing this.
Here's the code:
Spoiler

rednet.open('top')
print('This is computer #',os.getComputerID())
print('This is the password server')
local function numbersOnly(pre,limit)-- Works like read(), except only accepts numbers. Writes a desired prefix and can accept a max number length
	term.setCursorBlink(true)
	local x,y=term.getCursorPos()
	local num=''
	while true do
		if pre then write(pre) end
		write(num)
		local event,key=os.pullEvent()
		if event=='char' and tonumber(key) then
			if #num<limit then
				num=num..key
			end
		elseif event=='key' then
			if key==14 then
				num=string.sub(num, 1, -2)
			elseif key==28 then
				print()
				term.setCursorBlink(false)
				return tonumber(num)
			end
		end
		term.setCursorPos(x,y)
		term.clearLine()
	end
end

local function checkId(id)--Checks for id in file id, returns true if present, false if not
	if fs.exists("ids") then
		local f = fs.open("ids", "r")
		for line in f.readLine do
			if line:find(id) then
				return true
			end
		end
		return false
	end
end

local function saveId(id)-- Saves an id to file id
	if not checkId(id) then
		local f=fs.open('ids','a')
		f.writeLine(id..'')
		f.close()
		return true
	else
		return false
	end
end

local function receiveMessage()-- Same as rednet.receive(), but returns true if sending was successful, false if not
	local id,msg,d=rednet.receive()
	rednet.send(id,true)
	return id,msg,d
end

local function sendMessage(id,msg)-- Same as rednet.send(), but sends confirmation of received message
	rednet.send(id,msg,5)
	local tid,tmsg,td=rednet.receive(5)
	if tid==id and tmsg then
		return true
	else
		return false
	end
end

local function createFile(name)--Creates a file with filename 'name'
	if not fs.exists(name) then
		f=fs.open(name,'w')
		f.writeLine('{}')
		f.close()
	end
end

local function checkId(id)
	if fs.exists("ids") then
		local f = fs.open("ids", "r")
		for line in f.readLine do
			if line:find(id) then
				return true
			end
		end
		return false
	end
end

local function saveId(id)
	if not checkId(id) then
		local f=fs.open('ids','a')
		f.writeLine(id..'')
		f.close()
		return true
	else
		return false
	end
end

local function loadFromFile(variable,file)
	local file_path=fs.open(file,'r')
	local values=textutils.unserialize(file_path.readLine())
	for i,v in pairs(values) do
		if i==variable then
			file_path.close()
			return v
		end
	end
	file_path.close()
	return nil
end

local function saveToFile(variable,value,file)
	local file=file or 'config'
	createFile(file)
	local file_path=fs.open(file,'r')
	local values=textutils.unserialize(file_path.readLine())
	values[variable]=value
	file_path.close()
	
	local file_path=fs.open(file,'w')
	file_path.writeLine(textutils.serialize(values))
	file_path.close()
end

local function getParentId()
	parent_id=loadFromFile('parent_id','config')
	if not parent_id then
		while true do
			local parent_id=numbersOnly('Please give the id of the parent router: ',3)
			local msg=textutils.serialize({action='registerSubServer',id=os.getComputerID(),value='password_server'})
			if sendMessage(parent_id,msg) then
				saveToFile('parent_id',parent_id,'config')
				break
			else
				print('The requested router could not be found. Please check its availability, or try another id.')
			end
			sleep(0.01)
		end
	end
	return parent_id
end

local function manager(msg)
	print('da heck?')
	local values=textutils.unserialize(msg)
	print('unserialized')
	print('Value is ',values['value'])
	-- for i,v in pairs(values) do
		-- print(i,' ',v)
	-- end
	local verify=false
	if values['value']=='create' then
		saveToFile(values['username'],values['password'],'passwords')
		print('Created account for ',values['username'])
	elseif values['value']=='verify' then
		if loadFromFile(values['username'],'passwords')==values['password'] then
			verify=true
		end
		local msg=textutils.serialize({action='return',id=values['id'],value=verify})
		sendMessage(parent_id,msg)
	end
end
		

createFile('config')
createFile('passwords')
local parent_id=getParentId()

while true do
	local id,msg=receiveMessage
	print('got message')
	manager(msg)
end

It makes it up to print statement 'da heck?' before I get that error. Not sure what couldbe causing it.

Edit:
Woops, nevermind, I found it. Somehow the parenthesis at the end of 'receiveMessage()' were somehow deleted.
Edited on 11 February 2014 - 08:54 PM
awsmazinggenius #2
Posted 11 February 2014 - 09:54 PM
You can't send booleans unless in a table if you are not using 1.6pr0, I think (don't quote me).
Himself12794 #3
Posted 11 February 2014 - 10:01 PM
You can't send booleans unless in a table if you are not using 1.6pr0, I think (don't quote me).
Thanks, I found the error though, a simple syntax error.