Advertisement
Guest User

Untitled

a guest
Aug 11th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.60 KB | None | 0 0
  1. from asyncio import coroutine
  2. from autobahn.wamp.types import SubscribeOptions
  3. from autobahn.asyncio.wamp import ApplicationSession, ApplicationRunner
  4.  
  5. userarr = []
  6. channelarr = []
  7.  
  8.  
  9. def findChannel(name):
  10.     for channel in channelarr:
  11.         if (channel.name == name):
  12.             return channel
  13.     return -1
  14.  
  15.  
  16. def findUser(name):
  17.     for user in userarr:
  18.         if (user.name == name):
  19.             return user
  20.     return -1
  21.  
  22.  
  23. def removeUser(user):
  24.     if(findUser(user.name) == -1):
  25.         return -1
  26.     userarr.remove(user)
  27.  
  28.  
  29. def removeChannel(channel):
  30.     if(findChannel(channel.name) == -1):
  31.         return -1
  32.     channelarr.remove(channel)
  33.  
  34.  
  35. def removeUserFromName(name):
  36.     obj = findUser(name)
  37.     if(obj != -1):
  38.         userarr.remove(obj)
  39.     else:
  40.         return -1
  41.  
  42.  
  43. def removeChannelFromName(name):
  44.     obj = findChannel(name)
  45.     if (obj != -1):
  46.         channelarr.remove(obj)
  47.     else:
  48.         return -1
  49.  
  50.  
  51. class Channel:
  52.     def __init__(self, name):
  53.         self.name = name
  54.         self.users = []
  55.  
  56.     def findUser(self,name):
  57.         for username in self.users:
  58.             if(username == name):
  59.                 return username
  60.         return -1
  61.  
  62.     def addUser(self,name):
  63.         self.users.append(name)
  64.  
  65.     def removeUser(self,name):
  66.         rv = findUser(name)
  67.         if(rv != -1):
  68.             self.users.remove(name)
  69.         else:
  70.             return -1
  71.  
  72.     def pushToChannelFromUser(self,name,message):
  73.         obj = self.findUser(name)
  74.         if ((obj != -1) and (message != '')):
  75.             for username in self.users:
  76.                 if (username != obj):
  77.                     user = findUser(username)
  78.                     if(user != -1):
  79.                         user.publish(user.ctlchan,[':','MESSAGE',self.name,message])
  80.                
  81.  
  82.  
  83. class User:
  84.     def __init__(self, name, ctlchan, audiochan, session):
  85.         self.name = name
  86.         self.ctlchan = ctlchan
  87.         self.audiochan = audiochan
  88.         self.session = session
  89.         self.channel = ""
  90.         self.role = "user"
  91.         print("Making user with name " + self.name)
  92.  
  93.     def publish(self, channel, arguments):
  94.         yield self.session.publish(channel, arguments)
  95.  
  96.     def ctlCallback(self, *command):
  97.         if (command[0] == "JOINCHANNEL"):
  98.             if (findChannel(command[1]) != -1):
  99.                 self.channel = command[1]
  100.                 self.publish(self.ctlchan, [':', 'JOINCHANNEL', command[1]])
  101.             else:
  102.                 self.publish(self.ctlchan, [':', '404', 'CHANNOTFOUND'])
  103.         if (command[0] == "MKCHANNEL") and (findChannel(command[1]) != -1 and command[1] != ""):
  104.             print('Creating channel with name ' + command[1])
  105.             channelarr.append(Channel(command[1]))
  106.         if (command[0] == "CHANNAMES"):
  107.             for channel in channelarr:
  108.                 self.publish(self.ctlchan, [':', 'CHANNAME', channel.name])
  109.  
  110.  
  111. class Server(ApplicationSession):
  112.     def onMainCtlEvent(self, *command):
  113.         print("test")
  114.         print(command[0])
  115.         if (command[0] == "NICK" and (findUser(command[1])) == -1):
  116.             print("test")
  117.             print(command[1])
  118.             user = User(command[1], 'com.audioctl.' + command[1], 'com.audiodata.' + command[1], self)
  119.             userarr.append(user)
  120.             yield from self.subscribe(user.ctlCallback, user.ctlchan)
  121.  
  122.     def onJoin(self, details):
  123.         print("hello")
  124.         yield from self.subscribe(self.onMainCtlEvent, u"com.audioctl.main")
  125.  
  126.  
  127. runner = ApplicationRunner(u"ws://127.0.0.1:8080/ws", u"realm1")
  128. runner.run(Server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement