multiuser_terminal.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # THIS FILE IS A PART OF VCStudio
  2. # PYTHON 3
  3. ###############################################################################
  4. # Multiuser is a complex system. It's so complex that my brain is melting at
  5. # the moment. Anyways. Users need a way to talk to the server. And I'm going
  6. # to provide it to the user using a multiuser Layer. See:
  7. # studio/studio_multiuserLayer.py.
  8. # Tho there is one problem. The user should be able to read and write messages
  9. # to the server. And you think. Right. There is a network/network_multiuser.py
  10. # for this. Well. for the most stuff yes.
  11. # There are 2 types of protocols multiuser system uses. The TCP and the UDP.
  12. # TCP protocol is used to tranfer sensetive stuff. Like files, scene text. Stuff
  13. # that has to be at full. And where loosing packages is unexceptable.
  14. # UDP will be used for the other stuff. Like log of the server. And messages to
  15. # the server. Such as an Abort message.
  16. # This file handle the UDP side of things.
  17. ###############################################################################
  18. import os
  19. import socket
  20. import datetime
  21. def listen(win):
  22. # Listen function will run in it's own thread and get the data from the
  23. # server. It will be teminal like messages that we show in the multiuser
  24. # window.
  25. UDP_IP = "255.255.255.255"
  26. UDP_PORT = 54545
  27. while True:
  28. try:
  29. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  30. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  31. sock.bind((UDP_IP, UDP_PORT))
  32. #sock.settimeout(0.05)
  33. data, addr = sock.recvfrom(1024)
  34. data = data.decode('utf8')
  35. seconds_format = "%H:%M:%S"
  36. time = datetime.datetime.strftime(datetime.datetime.now(), seconds_format)
  37. if "VCStudio MULTIUSER SERVER TERMINAL" in data:
  38. #print(time+data.replace("VCStudio MULTIUSER SERVER TERMINAL", ""))
  39. win.multiuser["terminal"].append(time+data.replace("VCStudio MULTIUSER SERVER TERMINAL", ""))
  40. # Now i want to use a limit. Because hell I don't want 20000000
  41. # bazillion messages stored in the memory at all times.
  42. win.multiuser["terminal"] = win.multiuser["terminal"][-300:]
  43. # And we want to scroll down on each message so
  44. win.scroll["multiuser_terminal"] = 0 - len(win.multiuser["terminal"])*50
  45. elif "VCStudio ABORT MULTIUSER" in data:
  46. win.multiuser["users"] = {}
  47. sock.close()
  48. except:
  49. pass
  50. def message(message):
  51. # This is a function that will be called by multiple buttons. That want to
  52. # send the server any kind of message. But actually it's gonna send a message
  53. # to 255.255.255.255 port 54545 that the server is listening. At the moment
  54. # of writing this comment the server is programed only for 1 type of command
  55. # like this. It's the "VCStudio ABORT MULTIUSER". That makes the server stop
  56. # working. This command is a bit dangerous at the moment. Because it's running
  57. # on it's own thread in the server. Making it close even in the middle of a
  58. # request. Like it can corrupt a file that's being transferred.
  59. UDP_IP = "255.255.255.255"
  60. UDP_PORT = 54545
  61. cs1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  62. cs1.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  63. cs1.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
  64. cs1.sendto(bytes(message, 'utf-8'), (UDP_IP, UDP_PORT))
  65. cs1.close()