123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- import cherrypy
- import time
- import random
- import string
- import os
- import json
- class SimpleVoice:
- def __init__(self, index_template, servers=[]):
- self.clients = list()
- self.rooms = dict()
- # {"clients": (id1, id2), "offer": {...}, "answer": {...}}
- self.index_template = index_template
- self.servers = servers
-
- def generate_id(self):
- ID = random.choices(string.ascii_lowercase + string.digits, k=16)
- if ID in self.clients:
- return self.generate_id()
- else:
- return "".join(ID)
- @cherrypy.expose
- def index(self, name=""):
- return self.index_template.replace("{{ROOM_NAME}}", f"{name}")
- @cherrypy.tools.json_in()
- @cherrypy.tools.json_out()
- @cherrypy.expose
- def join(self, name):
- if name in self.rooms:
- if self.rooms[name]["clients"][1] != None:
- return {"type":"RoomFull",
- "msg":"The room is full(2 members it has)"}
- else:
- ID = self.generate_id()
- self.clients.append(ID)
- self.rooms[name]["clients"][1] = ID
- self.rooms[name]["ice"][ID] = list()
- return {"ID":ID, "data": self.rooms[name]["offer"], "Servers": self.servers}
- else:
- ID = self.generate_id()
- self.rooms[name] = {}
- self.rooms[name]["clients"] = [ID,None]
- self.rooms[name]["ice"] = dict()
- self.rooms[name]["ice"][ID] = list()
- self.clients.append(ID)
- return {"ID": ID, "Servers": self.servers}
-
- @cherrypy.tools.json_in()
- @cherrypy.tools.json_out()
- @cherrypy.expose
- def offer(self, client_id):
- for room_name, room in self.rooms.items():
- if client_id in room["clients"]:
- self.rooms[room_name]["offer"] = cherrypy.request.json
- return {"msg": "Well done"}
-
- return {"type": "IDNotFound", "msg": "Your ID was not found!"}
-
- @cherrypy.tools.json_in()
- @cherrypy.tools.json_out()
- @cherrypy.expose
- def answer(self, client_id):
- for room_name, room in self.rooms.items():
- if client_id in room["clients"]:
- self.rooms[room_name]["answer"] = cherrypy.request.json
- return room["offer"]
-
- return {"type": "IDNotFound", "msg": "Your ID was not found!"}
-
- @cherrypy.tools.json_in()
- @cherrypy.tools.json_out()
- @cherrypy.expose
- def get_answer(self, client_id):
- start_time = time.time()
- for room_name, room in self.rooms.items():
- if client_id in room["clients"]:
- while (time.time() - start_time) <= 10:
- if "answer" in room:
- return {"data": room["answer"]}
- time.sleep(1)
- return {"again": "again"}
-
- return {"type": "IDNotFound", "msg": "Your ID was not found!"}
-
- @cherrypy.tools.json_in()
- @cherrypy.tools.json_out()
- @cherrypy.expose
- def get_ice(self, client_id, room_name):
- if room_name not in self.rooms:
- return {"type": "NoSuchRoom", "msg": "No such room..."}
- if client_id not in self.rooms[room_name]["clients"]:
- return {"type": "IDNotFound", "msg": "Your ID was not found associated with the given room!"}
-
- client_ids = self.rooms[room_name]["clients"]
- id0, id1 = client_ids
- the_other = id0 if client_id == id1 else id1
- if the_other == None:
- time.sleep(1)
- return {"again": "Again"}
- if self.rooms[room_name]["ice"][the_other] == list():
- time.sleep(1)
- return {"again": "again"}
- return self.rooms[room_name]["ice"][the_other]
- @cherrypy.tools.json_in()
- @cherrypy.tools.json_out()
- @cherrypy.expose
- def ice(self, client_id, room_name):
- if room_name not in self.rooms:
- return {"type": "NoSuchRoom", "msg": "No such room..."}
- if client_id not in self.rooms[room_name]["clients"]:
- return {"type": "IDNotFound", "msg": "Your ID was not found associated with the given room!"}
- self.rooms[room_name]["ice"][client_id] = cherrypy.request.json
-
- @cherrypy.expose
- def leave(self, client_id, room):
- if room in self.rooms and client_id in self.rooms[room]["clients"]:
- self.rooms.pop(room)
- conf = {"global":
- {
- "server.socket_file": "/home/farooqkz/.simplevoice.sock",
- "tools.static.root": "/",
- "tools.staticdir.on": True,
- "tools.staticdir.dir": os.path.abspath(".") + "/static"
- }
- }
- index = ""
- with open("index.html") as fp:
- index = fp.read()
- servers = dict()
- with open("servers.json") as fp:
- servers = json.load(fp)
- cherrypy.quickstart(SimpleVoice(index, servers), "/", conf)
|