simplevoice_server.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import cherrypy
  2. import time
  3. import random
  4. import string
  5. import os
  6. import json
  7. class SimpleVoice:
  8. def __init__(self, index_template, servers=[]):
  9. self.clients = list()
  10. self.rooms = dict()
  11. # {"clients": (id1, id2), "offer": {...}, "answer": {...}}
  12. self.index_template = index_template
  13. self.servers = servers
  14. def generate_id(self):
  15. ID = random.choices(string.ascii_lowercase + string.digits, k=16)
  16. if ID in self.clients:
  17. return self.generate_id()
  18. else:
  19. return "".join(ID)
  20. @cherrypy.expose
  21. def index(self, name=""):
  22. return self.index_template.replace("{{ROOM_NAME}}", f"{name}")
  23. @cherrypy.tools.json_in()
  24. @cherrypy.tools.json_out()
  25. @cherrypy.expose
  26. def join(self, name):
  27. if name in self.rooms:
  28. if self.rooms[name]["clients"][1] != None:
  29. return {"type":"RoomFull",
  30. "msg":"The room is full(2 members it has)"}
  31. else:
  32. ID = self.generate_id()
  33. self.clients.append(ID)
  34. self.rooms[name]["clients"][1] = ID
  35. self.rooms[name]["ice"][ID] = list()
  36. return {"ID":ID, "data": self.rooms[name]["offer"], "Servers": self.servers}
  37. else:
  38. ID = self.generate_id()
  39. self.rooms[name] = {}
  40. self.rooms[name]["clients"] = [ID,None]
  41. self.rooms[name]["ice"] = dict()
  42. self.rooms[name]["ice"][ID] = list()
  43. self.clients.append(ID)
  44. return {"ID": ID, "Servers": self.servers}
  45. @cherrypy.tools.json_in()
  46. @cherrypy.tools.json_out()
  47. @cherrypy.expose
  48. def offer(self, client_id):
  49. for room_name, room in self.rooms.items():
  50. if client_id in room["clients"]:
  51. self.rooms[room_name]["offer"] = cherrypy.request.json
  52. return {"msg": "Well done"}
  53. return {"type": "IDNotFound", "msg": "Your ID was not found!"}
  54. @cherrypy.tools.json_in()
  55. @cherrypy.tools.json_out()
  56. @cherrypy.expose
  57. def answer(self, client_id):
  58. for room_name, room in self.rooms.items():
  59. if client_id in room["clients"]:
  60. self.rooms[room_name]["answer"] = cherrypy.request.json
  61. return room["offer"]
  62. return {"type": "IDNotFound", "msg": "Your ID was not found!"}
  63. @cherrypy.tools.json_in()
  64. @cherrypy.tools.json_out()
  65. @cherrypy.expose
  66. def get_answer(self, client_id):
  67. start_time = time.time()
  68. for room_name, room in self.rooms.items():
  69. if client_id in room["clients"]:
  70. while (time.time() - start_time) <= 10:
  71. if "answer" in room:
  72. return {"data": room["answer"]}
  73. time.sleep(1)
  74. return {"again": "again"}
  75. return {"type": "IDNotFound", "msg": "Your ID was not found!"}
  76. @cherrypy.tools.json_in()
  77. @cherrypy.tools.json_out()
  78. @cherrypy.expose
  79. def get_ice(self, client_id, room_name):
  80. if room_name not in self.rooms:
  81. return {"type": "NoSuchRoom", "msg": "No such room..."}
  82. if client_id not in self.rooms[room_name]["clients"]:
  83. return {"type": "IDNotFound", "msg": "Your ID was not found associated with the given room!"}
  84. client_ids = self.rooms[room_name]["clients"]
  85. id0, id1 = client_ids
  86. the_other = id0 if client_id == id1 else id1
  87. if the_other == None:
  88. time.sleep(1)
  89. return {"again": "Again"}
  90. if self.rooms[room_name]["ice"][the_other] == list():
  91. time.sleep(1)
  92. return {"again": "again"}
  93. return self.rooms[room_name]["ice"][the_other]
  94. @cherrypy.tools.json_in()
  95. @cherrypy.tools.json_out()
  96. @cherrypy.expose
  97. def ice(self, client_id, room_name):
  98. if room_name not in self.rooms:
  99. return {"type": "NoSuchRoom", "msg": "No such room..."}
  100. if client_id not in self.rooms[room_name]["clients"]:
  101. return {"type": "IDNotFound", "msg": "Your ID was not found associated with the given room!"}
  102. self.rooms[room_name]["ice"][client_id] = cherrypy.request.json
  103. @cherrypy.expose
  104. def leave(self, client_id, room):
  105. if room in self.rooms and client_id in self.rooms[room]["clients"]:
  106. self.rooms.pop(room)
  107. conf = {"global":
  108. {
  109. "server.socket_file": "/home/farooqkz/.simplevoice.sock",
  110. "tools.static.root": "/",
  111. "tools.staticdir.on": True,
  112. "tools.staticdir.dir": os.path.abspath(".") + "/static"
  113. }
  114. }
  115. index = ""
  116. with open("index.html") as fp:
  117. index = fp.read()
  118. servers = dict()
  119. with open("servers.json") as fp:
  120. servers = json.load(fp)
  121. cherrypy.quickstart(SimpleVoice(index, servers), "/", conf)