Server code:
Spoiler
rednet.open("top")
local connect1=false
local connect2=false
local user1id
local user2id
function handshakes()
local senderID, toConnect=rednet.receive("cProt")
user1id=senderID
user2id=tonumber(toConnect)
rednet.send(senderID,"handshake","handshake")
print("sent user # "..senderID.." a handshake")
rednet.send(tonumber(toConnect),"handshake","handshake")
print("sent user # "..toConnect.." a handshake")
local l, userOneHandshake,u6 = rednet.receive("handshakeConfirm")
if(userOneHandshake == "confirm") then
connect1=true
print("user "..l.." handshake confirmed")
end
local p, userTwoHandshake,u1 = rednet.receive("handshakeConfirm")
if(userTwoHandshake=="confirm") then
print("user "..p.." handshake confirmed")
end
rednet.send(82,user1id,"dbGet")
print("requesting nickname data from database")
local u2,nick1=rednet.receive("dbNickSend")
print("recieved data")
rednet.send(user2id,nick1,"nickProt")
print("sent user: "..user2id.." default username: "..nick1)
rednet.send(82,tostring(user2id),"dbGet")
print("requesting nickname data from database")
local u3, nick2=rednet.receive("dbNickSend")
print("recieved data")
rednet.send(user1id,nick2,"nickProt")
print("sent user: "..user1id.." default username: "..nick2)
connect2=true
end
function messageTransfer()
local id,msg,u4=rednet.receive("messageProtocol")
print("message recieved. relaying message")
if id==tonumber(user1id) then
rednet.send (user2id,msg,"MPincoming")
print("message sent to user 2")
elseif id==tonumber(user2id) then
rednet.send(user1id, msg,"MPincoming")
print("message sent to user 1")
end
end
function Nicknames()
local IDc, cmd, u5=rednet.receive("commandProtocol")
local find, u6 =string.find(cmd,"/nick",1,true)
local cmdlength=string.len(cmd)
if find==1 then
local nickname=string.sub(cmd,7)
print("user "..IDc.." changed nickname to "..nickname)
if IDc==user1id then
rednet.send(user2id,nickname,"nickProt")
else if IDc==user2id then
rednet.send(user1id,nickname,"nickProt")
end
end
end
end
function relayConnections()
local senderID, toConnect=rednet.receive("cProt")
if connect1==true and connect2==true and senderID ~=0 and toConnect~=0 then
rednet.send(82,"fillerTextLol","GetNextServer")
local db,nextServer=rednet.receive("NextServer")
rednet.send(senderID,nextServer,"relay")
rednet.send(tonumber(toConnect),nextServer,"relay")
end
end
while connect1==false and connect2==false do
handshakes()
end
while connect1==true and connect2==true do
parallel.waitForAny(messageTransfer,Nicknames,relayConnections)
end
Client code:
Spoiler
rednet.open("back")
local server=61
local isTransfered=false
local isTransferedStandby=false
function standby()
print("standing by to recieve messages from server" )
local sID,hs,u1=rednet.receive("handshake")
if(hs=="handshake") then
rednet.send(sID,"confirm","handshakeConfirm")
server=sID
end
end
function connect()
print("enter the id of the user you wish to connect to")
local requestedConnection=read(requestedConnection)
rednet.send(server,requestedConnection,"cProt")
end
function sendMessage()
local sent=read(sent)
local st, u2=string.find(sent,"/",1,true)
if st==1 then
rednet.send(server,sent,"commandProtocol")
else
rednet.send(server,sent,"messageProtocol")
end
end
--local NicknameRec="default"
function nickname()
local id, nick, u3=rednet.receive("nickProt")
NicknameRec=nick
end
function receiveMessage()
local sID, message, u4=rednet.receive("MPincoming")
print(NicknameRec..": "..message)
end
function handshakeConfirm()
local u5, hs ,u6=rednet.receive("handshake")
if(hs == "handshake") then
rednet.send(server,"confirm","handshakeConfirm")
end
local id,nick=rednet.receive("nickProt")
NicknameRec=nick
end
function transferConnections()
local sID, msg=rednet.receive("relay", 1)
server=msg
connect()
isTransfered=true
end
function transConStndby()
local sID,msg=rednet.receive("relay")
server=msg
isTransferedStandby=true
end
print("Do you want to connect to someone? y/n")
local answer=read(answer)
if(answer=="y") then
connect()
parallel.waitForAny(transferConnections,handshakeConfirm)
if isTransfered==true then
handshakeConfirm()
end
while 1==1 do
parallel.waitForAny(receiveMessage,sendMessage,nickname)
end
elseif(answer=="n") then
parallel.waitForAny(transConStndby,standby)
if isTransferedStandby==true then
standby()
end
while 1==1 do
parallel.waitForAny(receiveMessage,sendMessage,nickname)
end
end
Database code:
Spoiler
rednet.open("top")
local IDdb={53,57,71,72,96,97}
local NickDB={"pieguy1","pieguy2","stevethedude1","stevethedude2","user1","user2"}
local ServerDB={61,78,63,64,65,66}
function Transfer()
local sID=rednet.receive("GetNextServer")
for l=1,#ServerDB do
if ServerDB[l]==sID then
rednet.send(sID,ServerDB[l+1],"NextServer")
end
end
end
function NickHandler()
local nickToSend1
local sID,getId=rednet.receive("dbGet")
getId=tonumber(getId)
print("Recieved request")
for i=1,#IDdb do
if getId==IDdb[i] then
nickToSend1=tostring(NickDB[i])
rednet.send(sID,nickToSend1,"dbNickSend")
print("connection served. username sent: "..nickToSend1)
end
end
local nickToSend2
local sID2,getId2=rednet.receive("dbGet")
getId2=tonumber(getId2)
print("Recieved request")
for j=1,#IDdb do
if getId2==IDdb[j] then
nickToSend2=tostring(NickDB[j])
rednet.send(sID2,nickToSend2,"dbNickSend")
print("connection served. username sent: "..nickToSend2)
end
end
end
while 1==1 do
parallel.waitForAny(NickHandler,Transfer)
end
Yes I know this is probably confusing, poorly formatted, and likely inefficient. Just bear with me, this is my first big project ;_;