Advertisement
Aidocxe

Server client 2.0

Nov 26th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. local liste_client = {}
  2.  
  3. local idChannelLogin = 130
  4. local idChannelAdmin = 131
  5. local idChannelAdminRep = 13101
  6. local idChannelMsgRec = 132
  7. local idChannelMsgEnv = 133
  8.  
  9. local keygen = "azerty"
  10.  
  11. function attente_client()
  12. -- attente de connexion/déconnexion des clients
  13. print("lancement attente_client OK.")
  14. while true do
  15. modem.open(idChannelLogin)
  16. local event, modemSide, senderChannel, replyChannel, text, senderDistance = os.pullEvent("modem_message")
  17. if senderChannel == idChannelLogin then
  18. local msg = textutils.unserialize(text)
  19. if msg.key == keygen then
  20. if msg.action == "login" then
  21. liste_client[msg.id] = msg.nickname
  22. print("Nouveau client : "..msg.id.." "..liste_client[msg.id])
  23. elseif msg.action == "leave" then
  24. table.remove(liste_client, msg.id)
  25. print("Exit du client : "..msg.id)
  26. end
  27. end
  28. end
  29. modem.close(idChannelLogin)
  30. end
  31. end
  32.  
  33. function administration()
  34. -- envoi régulier d'infos aux clients
  35. print("lancement admin OK.")
  36. modem.open(idChannelAdmin)
  37. while true do
  38.  
  39. local event, modemSide, senderChannel, replyChannel, text, senderDistance = os.pullEvent("modem_message")
  40. if text == "majliste" then
  41. local msg = textutils.serialize(liste_client)
  42. modem.transmit(idChannelAdminRep,idChannelAdmin,msg)
  43. print("maj liste envoyee")
  44. end
  45.  
  46. end
  47. modem.close(idChannelAdmin)
  48. end
  49.  
  50. function echange_msg()
  51. -- reçoit et distribue les messages
  52. print("lancement echange_msg OK.")
  53. while true do
  54. sleep(5)
  55. end
  56. end
  57.  
  58. modem = peripheral.wrap("back")
  59.  
  60. term.clear()
  61. term.setCursorPos(1,1)
  62. print("ID du serveur : "..os.getComputerID())
  63.  
  64. parallel.waitForAny(attente_client, administration, echange_msg)
  65.  
  66. print("Fin du programme")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement