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

eMail Sistem

Started by TheDegree0, 27 August 2012 - 11:25 AM
TheDegree0 #1
Posted 27 August 2012 - 01:25 PM
Hello,
Im making an email sistem that uses a server and I have a little problem with it.
If you see the code you will understand better the problem.

Client Code:

--[[ Config File ]]--
IDserver = 11  --[[ The server that this computer is gona use for send messages
					  IMPORTANT: 2 Computers cant use the same server! ]]--
IDcomputer = "7" --[[ For check the ID of this computer, start the mail program
					  and check it at top right of the screen. ]]--
modem = "back"   --[[ Where the modem is at?
					  (front, back, top, bottom, right, left) ]]--

--[[ End of Config ]]--

--[[ Start of program ]]--
generalloop = true
while (generalloop == true) do
sleep(0.1)
rednet.open(modem)
term.clear()
term.setCursorPos(45,1)
Start = "ID: "
ID = os.computerID()
End = "  \n"
Start = Start..ID
write(Start..End)
term.setCursorPos(1,1)
print "Mail Menu"
print "-------------------------"
print "1. Send Message"
print "2. Look for new messages"
print "3. Disconnect"
print "-------------------------"
write "Please select an option: "
input = read()
if (input == "1") then
SMloop = true
while (SMloop == true) do
term.clear()
term.setCursorPos(1,1)
print "Send Message"
print "---------------"
write "To: "
to = read()
write "Message: "
message = read()
print "---------------"
print ""
print "Sending Message..."
sleep(2)
if (to == "") or (message == "") then
term.clear()
term.setCursorPos(1,1)
print "Message Failed"
print "-----------------------------"
print "Please fill all the fields"
sleep(2)
else
SMloop = false
term.clear()
term.setCursorPos(1,1)
rednet.send(IDserver, message)
rednet.send(IDserver, to)
rednet.send(IDserver, IDcomputer)
server, msg = rednet.receive()
end
end
if (msg == "Message Recived") then
print "Message Sent"
print "---------------------------------------"
print("To: " .. to)
print("Message: " .. message)
print "---------------------------------------"
print "The message has been sent to the server"
print "As soon as the computer"
print "that you sended refreshes"
print "the new messages, he will recive your"
print "email."
print ""
print "NOTE: If you did not enter any computer"
print "	  ID, the message it will never be"
print "	  recived by anyone!"
print "---------------------------------------"
print "(Press B for go back)"
backloop = true
while (backloop == true) do
local sBack, back = os.pullEvent("key")
if (sBack == "key") then
if (back == 48) then
break
end
end
end
elseif (msg == "Error") then
term.clear()
term.setCursorPos(1,1)
print "Error: Message not sent"
print "-------------------------"
print "Something went wrong!"
print "Please try it again."
print "Remember to put a valid"
print "destination!"
print ""
print "Ex:"
print "To: 6"
print "-------------------------"
print "(Press B for go back)"
backloop = true
while (backloop == true) do
local sBack, back = os.pullEvent("key")
if (sBack == "key") then
if (back == 48) then
break
end
end
end
end

elseif (input == "2") then
term.clear()
term.setCursorPos(1,1)
print "Looking for new messages"
print "-------------------------------------"
print "Asking the server for new messages..."
print ""
print "This can take a while, please wait."
from, senderMsg, distance = rednet.receive(30)
from, sendby, distance = rednet.receive(2)
if (senderMsg ~= nil) then
if (sendby ~= nil) then
rednet.send(from, "Recived")
term.clear()
term.setCursorPos(1,1)
print "Message Recived"
print "------------------------------"
print("From: " ..sendby)
print ""
print("Message: " ..senderMsg)
print "------------------------------"
print "(Press B for go back)"
backloop = true
while (backloop == true) do
local sBack, back = os.pullEvent("key")
if (sBack == "key") then
if (back == 48) then
break
end
end
end
else
term.clear()
term.setCursorPos(1,1)
print "No new messages recived"
print ""
print "(Press B for go back)"
backloop = true
while (backloop == true) do
local sBack, back = os.pullEvent("key")
if (sBack == "key") then
if (back == 48) then
break
end
end
end
end
end
elseif (input == "3") then
generalloop = false
term.clear()
term.setCursorPos(1,1)
print "Disconecting..."
print "------------------"
print "Disconnected!"
print ""
print "(Press Q for quit)"
rednet.close(modem)
quitloop = true
while (quitloop == true) do
local sQuit, quit = os.pullEvent("key")
if (sQuit == "key") then
if (quit == 16) then
break
end
end
end
sleep(0.1)
term.clear()
term.setCursorPos(1,1)
else
print ""
print "Unknown option!"
print "Please try again."
sleep(3)
end
end
--[[ End of program ]]--

Server Code:

--[[ Config File ]]--
modem = "back"   --[[ Where the modem is at?
								  (front, back, top, bottom, right, left) ]]--

--[[ End of Config ]]--

--[[ Start of program ]]--
rednet.open(modem)
os.pullEvent = os.pullEventRaw
term.clear()
term.setCursorPos(1,1)
serverID = os.computerID()
print "You can't use this computer because its a server!"
print ""
print("Server ID: " ..serverID)
print "-------------"
print "Logs:"
print ""
while true do
info, msg, distance = rednet.receive()
info, to, distance2 = rednet.receive()
info, from, distance3 = rednet.receive()
print("Message recived from: " ..from)
print("Sending message to: " ..to)
if (to == "10") then
rednet.send(info, "Message Recived")
loop10 = true
while (loop10 == true) do
rednet.send(10, msg)
rednet.send(10, from)
event, id, text = os.pullEvent()
if (event == "rednet_message") then
if (text == "Recived") then
loop10 = false
print ""
print "Message Sent"
print "---------------"
end
end
end
elseif (to == "9") then
rednet.send(info, "Message Recived")
loop9 = true
while (loop9 == true) do
rednet.send(9, msg)
rednet.send(9, from)
event, id, text = os.pullEvent()
if (event == "rednet_message") then
if (text == "Recived") then
loop9 = false
print ""
print "Message Sent"
print "---------------"
end
end
end
elseif (to == "8") then
rednet.send(info, "Message Recived")
loop8 = true
while (loop8 == true) do
rednet.send(8, msg)
rednet.send(8, from)
event, id, text = os.pullEvent()
if (event == "rednet_message") then
if (text == "Recived") then
loop8 = false
print ""
print "Message Sent"
print "---------------"
end
end
end

elseif (to == "7") then
rednet.send(info, "Message Recived")
loop7 = true
while (loop7 == true) do
rednet.send(7, msg)
rednet.send(7, from)
event, id, text = os.pullEvent()
if (event == "rednet_message") then
if (text == "Recived") then
loop7 = false
print ""
print "Message Sent"
print "---------------"
end
end
end
elseif (to == "6") then
rednet.send(info, "Message Recived")
loop6 = true
while (loop6 == true) do
rednet.send(6, msg)
rednet.send(6, from)
event, id, text = os.pullEvent()
if (event == "rednet_message") then
if (text == "Recived") then
loop6 = false
print ""
print "Message Sent"
print "---------------"
end
end
end

elseif (to == "5") then
rednet.send(info, "Message Recived")
loop5 = true
while (loop8 == true) do
rednet.send(5, msg)
rednet.send(5, from)
event, id, text = os.pullEvent()
if (event == "rednet_message") then
if (text == "Recived") then
loop5 = false
print ""
print "Message Sent"
print "---------------"
end
end
end

elseif (to == "4") then
rednet.send(info, "Message Recived")
loop4 = true
while (loop8 == true) do
rednet.send(4, msg)
rednet.send(4, from)
event, id, text = os.pullEvent()
if (event == "rednet_message") then
if (text == "Recived") then
loop4 = false
print ""
print "Message Sent"
print "---------------"
end
end
end

elseif (to == "3") then
rednet.send(info, "Message Recived")
loop3 = true
while (loop8 == true) do
rednet.send(3, msg)
rednet.send(3, from)
event, id, text = os.pullEvent()
if (event == "rednet_message") then
if (text == "Recived") then
loop3 = false
print ""
print "Message Sent"
print "---------------"
end
end
end

elseif (to == "2") then
rednet.send(info, "Message Recived")
loop2 = true
while (loop8 == true) do
rednet.send(2, msg)
rednet.send(2, from)
event, id, text = os.pullEvent()
if (event == "rednet_message") then
if (text == "Recived") then
loop2 = false
print ""
print "Message Sent"
print "---------------"
end
end
end

elseif (to == "1") then
rednet.send(info, "Message Recived")
loop1 = true
while (loop8 == true) do
rednet.send(1, msg)
rednet.send(1, from)
event, id, text = os.pullEvent()
if (event == "rednet_message") then
if (text == "Recived") then
loop1 = false
print ""
print "Message Sent"
print "---------------"
end
end
end
else
rednet.send(info, "Error")
print ""
print "Error"
print "---------------"
end
end

The problem is when the client checks if its reciving any message from the server for some reason it says that it havent recive anything. But when the server is sending the message and the computer was alredy looking for new messages it works.

Im trying to fix this problem for like 4h and I cant find it.
Thanks in advance!
Kingdaro #2
Posted 27 August 2012 - 02:00 PM
it usually helps if you tab your file, so that if / for / while blocks are easier to read and understand.


stuff()
while true do
  stuff()
  if condition then
	stuff()
	for i=1, 2 do
	  stuff()
	end
  end
end
ardera #3
Posted 27 August 2012 - 02:04 PM
Turn the modem on oO
TheDegree0 #4
Posted 27 August 2012 - 02:04 PM
Yeah but I did made it in game, thats why.
TheDegree0 #5
Posted 27 August 2012 - 02:05 PM
@ardera: Its alredy on, for the client and the server.
TheDegree0 #6
Posted 27 August 2012 - 02:13 PM
Found why! :D/>/> finally!

The backloop was repeated everywhere so that was interfering somehow with the error message.