Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from asyncio import coroutine
- from autobahn.wamp.types import SubscribeOptions
- from autobahn.asyncio.wamp import ApplicationSession, ApplicationRunner
- userarr = []
- channelarr = []
- def findChannel(name):
- for channel in channelarr:
- if (channel.name == name):
- return channel
- return -1
- def findUser(name):
- for user in userarr:
- if (user.name == name):
- return user
- return -1
- def removeUser(user):
- if(findUser(user.name) == -1):
- return -1
- userarr.remove(user)
- def removeChannel(channel):
- if(findChannel(channel.name) == -1):
- return -1
- channelarr.remove(channel)
- def removeUserFromName(name):
- obj = findUser(name)
- if(obj != -1):
- userarr.remove(obj)
- else:
- return -1
- def removeChannelFromName(name):
- obj = findChannel(name)
- if (obj != -1):
- channelarr.remove(obj)
- else:
- return -1
- class Channel:
- def __init__(self, name):
- self.name = name
- self.users = []
- def findUser(self,name):
- for username in self.users:
- if(username == name):
- return username
- return -1
- def addUser(self,name):
- self.users.append(name)
- def removeUser(self,name):
- rv = findUser(name)
- if(rv != -1):
- self.users.remove(name)
- else:
- return -1
- def pushToChannelFromUser(self,name,message):
- obj = self.findUser(name)
- if ((obj != -1) and (message != '')):
- for username in self.users:
- if (username != obj):
- user = findUser(username)
- if(user != -1):
- user.publish(user.ctlchan,[':','MESSAGE',self.name,message])
- class User:
- def __init__(self, name, ctlchan, audiochan, session):
- self.name = name
- self.ctlchan = ctlchan
- self.audiochan = audiochan
- self.session = session
- self.channel = ""
- self.role = "user"
- print("Making user with name " + self.name)
- def publish(self, channel, arguments):
- yield self.session.publish(channel, arguments)
- def ctlCallback(self, *command):
- if (command[0] == "JOINCHANNEL"):
- if (findChannel(command[1]) != -1):
- self.channel = command[1]
- self.publish(self.ctlchan, [':', 'JOINCHANNEL', command[1]])
- else:
- self.publish(self.ctlchan, [':', '404', 'CHANNOTFOUND'])
- if (command[0] == "MKCHANNEL") and (findChannel(command[1]) != -1 and command[1] != ""):
- print('Creating channel with name ' + command[1])
- channelarr.append(Channel(command[1]))
- if (command[0] == "CHANNAMES"):
- for channel in channelarr:
- self.publish(self.ctlchan, [':', 'CHANNAME', channel.name])
- class Server(ApplicationSession):
- def onMainCtlEvent(self, *command):
- print("test")
- print(command[0])
- if (command[0] == "NICK" and (findUser(command[1])) == -1):
- print("test")
- print(command[1])
- user = User(command[1], 'com.audioctl.' + command[1], 'com.audiodata.' + command[1], self)
- userarr.append(user)
- yield from self.subscribe(user.ctlCallback, user.ctlchan)
- def onJoin(self, details):
- print("hello")
- yield from self.subscribe(self.onMainCtlEvent, u"com.audioctl.main")
- runner = ApplicationRunner(u"ws://127.0.0.1:8080/ws", u"realm1")
- runner.run(Server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement