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

Rednet Problems

Started by ReBraLa, 17 July 2015 - 09:43 AM
ReBraLa #1
Posted 17 July 2015 - 11:43 AM
I have a problem with my rednet email system

This is the code from the Server:

-- Config
BaseID = 1 -- Change to the ID of your DataBase
modemSide = "top" -- Change to the Side of your Modem
onClients = 0
-- Main Code
term.clear()
term.setCursorPos(1,1)
print("ReBraLa's Email System V2.7				  Server")
term.setCursorPos(1,3)
print("Clients Online: "..onClients.."#")
term.setCursorPos(1,4)
print("Last Message: None")
rednet.open(modemSide)
while true do
local id,message = rednet.receive()
lastID = id
term.setCursorPos(1,3)
print("Clients Online: "..onClients.."#")
term.setCursorPos(1,4)
term.clearLine()
print("Last Message: '"..message.."'")
print("From: "..id.."#")
if message == "Get Mail" then
  rednet.send(BaseID,"GM")
  rednet.send(BaseID,tonumber(id))
  id,BaseMessage = rednet.receive()
  TotalMessages = tonumber(BaseMessage)
  if id == BaseID then
   rednet.send(lastID,BaseMessage)
  end
elseif message == "online" then
  onClients = onClients + 1
  term.setCursorPos(1,3)
  term.clearLine()
  print("Clients Online: "..onClients.."#")
elseif message == "offline" then
  onClients = onClients - 1
  term.setCursorPos(1,3)
  term.clearLine()
  print("Clients Online: "..onClients.."#") 
elseif message == "Get Mail Names" then
  rednet.send(BaseID,"GN")
  rednet.send(BaseID,tostring(id))
  for i = 0,TotalMessages do
   id,messageBase = rednet.receive()
   rednet.send(lastID,tostring(messageBase))
  end
end
if onClients <= -1 then
  os.reboot()
end
end

This is the from the DataBase:

-- Config --
ServerID = 0 -- Change to ID of your Server
modemSide = "top" -- Change to the Side of your Modem
UMessages = 0   
-- Main Code --
function GetNames()
   id,message = rednet.receive()
   List = tostring(message)
   local FileList = fs.list(List)
   for _, file in ipairs(FileList) do
    rednet.send(ServerID,file)
   end
end
function GetMail()
   id,message = rednet.receive()
   List = tostring(message)
   local FileList = fs.list(List)
   for _, file in ipairs(FileList) do
    UMessages = UMessages + 1
   end
   rednet.send(ServerID,tostring(UMessages))
   UMessages = 0
end
function ReadMail()
   MapName, FileName = rednet.receive()
   term.setCursorPos(1,3)
   term.clearLine()
   print("Last Message: '"..message.."'")  
   print(tostring(MapName).."/"..FileName)
   mail = fs.open(tostring(MapName).."/"..FileName, "r")
   a = mail.readAll()
   print(a)
   rednet.send(ServerID,tostring(a))
   mail.close()
end
term.clear()
term.setCursorPos(1,1)
print("ReBraLa's Email System V2.2			    DataBase")
term.setCursorPos(1,3)
print("Last Message: None")
rednet.open(modemSide)
while true do
id,message = rednet.receive()
term.setCursorPos(1,3)
term.clearLine()
print("Last Message: '"..message.."'")
if id == ServerID then
  if message == "GM" then
   GetMail()
  elseif message == "GN" then
   GetNames()
  end
else
  if message == "Open Mail" then
   ReadMail()
  end
end
end

And this that from the Client:

-- Config --
BaseID = 1 -- Change to ID of your DataBase
ServerID = 0 -- Change to ID of your Server
modemSide = "top" -- Change to the Side of your Modem
username = "None"
version = 2.4
Mails = {}
-- Needed Files --
if not fs.exists("fLib") then
shell.run("pastebin get kLRPGier fLib")
end
-- Functions --
function LoadingScreen()
rednet.send(ServerID,"Get Mail")
id,message = rednet.receive()
local Mail = tostring(message)
end
function centert(text,ylvl)
x,y = term.getSize()
term.setCursorPos((x-math.floor(#text))/2,ylvl)
print(text)
end
function Waiting()
term.setCursorPos(5,5)
print("				   ")
term.setCursorPos(5,6)
print("    Waiting For    ")
term.setCursorPos(5,7)
print("	  Mails	    ")
term.setCursorPos(5,8)
print("				   ")
term.setCursorPos(5,9)
print("				   ")
end
function LoadMail()
term.setBackgroundColor(colors.lime)
term.setTextColor(colors.white)
rednet.send(ServerID,"Get Mail")
id,message = rednet.receive()
Mail = tostring(message)
Waiting()
term.setCursorPos(5,5)
print("				   ")
term.setCursorPos(5,6)
print("   Unread Emails   ")
if #Mail == 1 then
  term.setCursorPos(5,7)
  print("	    ("..Mail..")	    ")
elseif #Mail == 2 then
  term.setCursorPos(5,7)
  print("	   ("..Mail..")	    ")
elseif #Mail == 3 then
  term.setCursorPos(5,7)
  print("	   ("..Mail..")	   ")
elseif #Mail == 4 then
  term.setCursorPos(5,7)
  print("	  ("..Mail..")	   ")
elseif #Mail == 5 then
  term.setCursorPos(5,7)
  print("	  ("..Mail..")	  ")
end
term.setCursorPos(5,8)
print("				   ")
term.setCursorPos(5,9)
print("				   ")
end
function NormalLayout()
term.setBackgroundColor(colors.white)
term.setTextColor(colors.gray)
term.clear()
term.setCursorPos(1,1)
print("[Exit]")
centert("ReBraLa's Mail System V"..version,2)
term.setBackgroundColor(colors.red)
term.setCursorPos(42,1)
write(" Settings ")
LoadMail()
end
function Settings()
term.setBackgroundColor(colors.white)
term.setTextColor(colors.gray)
term.clear()
term.setCursorPos(1,1)
print("[Exit]")
term.setCursorPos(4,5)
print("Username: "..username)
term.setCursorPos(42,5)
print("[Edit]")
end
function box(size,color,y)
term.setBackgroundColor(color)
if size == 1 then
  for i = 1,5 do
   centert("							 ",y+i)
  end
elseif size == 2 then
  for i = 1,7 do
   centert("								 ",y+i)
  end
end
end
function Name()
box(2,colors.lime,5)
box(1,colors.gray,6)
term.setTextColor(colors.white)
centert("  Name:					  ",8)
term.setCursorPos(18,8)
end
function showMail()
CurrentState = false
rednet.send(ServerID,"Get Mail")
id,message = rednet.receive()
Mail = tostring(message)
term.setBackgroundColor(colors.white)
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.gray)
print("[Exit]")
centert("Unread Emails: "..Mail.."#",1)
centert("Username: "..username,2)
print("List: ")
rednet.send(ServerID,"Get Mail Names")
centert("Receiving Mails",4)
term.setCursorPos(1,4)
for i = 1,tonumber(Mail) do
  id,message = rednet.receive()
  table.insert(Mails,message)
  if CurrentState == false then
   term.setBackgroundColor(colors.gray)
   term.clearLine()
   term.setTextColor(colors.white)
   print(" "..i..". "..message)
   CurrentState = true
  elseif CurrentState == true then
   term.setBackgroundColor(colors.white)
   term.clearLine()
   term.setTextColor(colors.gray)
   print(" "..i..". "..message)  
   CurrentState = false
  end
end
end
-- Main Code

os.loadAPI("fLib")
rednet.open(modemSide)
LoadingScreen()
rednet.send(ServerID,"online")
NormalLayout()
slc = 0
while true do
event,button,x,y = os.pullEvent("mouse_click")
if slc == 0 then
  if x >= 42 and x <= 51 and y == 1 and button == 1 then
   slc = 1
   Settings()
  elseif x >= 1 and x <= 6 and y == 1 and button == 1 then
   rednet.send(ServerID,"offline")
   term.setBackgroundColor(colors.black)
   term.setTextColor(colors.white)
   term.clear()
   centert("Thanks for using my Mailing system!",1)
   break
  elseif x >= 5 and x <= 24 and y >= 5 and y <= 9 and button == 1 and tonumber(Mail) >= 1 then
   showMail()
   slc = 3
  end
elseif slc == 1 then
  if x >= 1 and x <= 6 and y == 1 and button == 1 then
   slc = 0
   NormalLayout()
  elseif x >= 42 and x <= 48 and y == 5 and button == 1 then
   slc = 2
   Name()
   inp = read()
   term.setBackgroundColor(colors.lime)
   centert("	 [Done]	    [Again]	   ",12)  
  end
elseif slc == 2 then
  if x >= 28 and x <= 34 and y == 12 and button == 1 then
   Name()
   inp = read()
   term.setBackgroundColor(colors.lime)
   centert("	 [Done]	    [Again]	   ",12)
  elseif x >= 14 and x <= 19 and y == 12 and button == 1 then
   fLib.replaceLine("mail",5,'username = "'..inp..'"')
   rednet.send(ServerID,"offline")
   return
   shell.run("mail")
  end
elseif slc == 3 then
  if x >= 1 and x <= 6 and y == 1 and button == 1 then
   slc = 0
   LoadMail()
   NormalLayout()
  elseif x >= 1 and x <= 51 and y == 4 then
   rednet.send(BaseID,"Open Mail")
   sleep(1)
   rednet.send(ServerID,Mails[1])
   id,message = rednet.receive()
   term.clear()
   term.setCursorPos(1,1)
   print(tostring(message))
   break
  end
end
end

And i only have one problem if i want to use fs.open() then it sends me back the name of the file (in the function ReadMail() in the database) please heko
Bomb Bloke #2
Posted 18 July 2015 - 12:44 AM
And i only have one problem if i want to use fs.open() then it sends me back the name of the file (in the function ReadMail() in the database)

   rednet.send(BaseID,"Open Mail")
   sleep(1)
   rednet.send(ServerID,Mails[1])

You'll want to ditch that sleep statement you've got there.

Rednet works by adding an event to the end of the target system's event queue. rednet.receive() is essentially just a fancy method of pulling that event.

When you call sleep(1), a timer is added to the queue a second later, and until the function can find it, all events are pulled from the queue - and discarded!

I also notice this in the server code:

for i = 0,TotalMessages do

If "TotalMessages" is 10, this'll repeat 11 times, as 0 to 10 covers 11 different numbers.