# Path: baroque/entities/event.py
# class Event:
#     """An event that can be published.
# 
#     Args:
#         eventtype (:obj:`baroque.entities.eventtype.EventType` instance or `type` object): the type of the event
#         payload (dict, optional): the content of this event
#         description (str, optional): the description of this event
#         owner (str, optional): the owner of this event
# 
#     """
#     def __init__(self, eventtype, payload=None, description=None, owner=None):
#         assert eventtype is not None
#         if type(eventtype) == type:
#             eventtype = eventtype()
#         assert isinstance(eventtype, EventType)
#         if payload is not None:
#             assert isinstance(payload, dict)
#         self.id = str(uuid.uuid4())
#         self.type = eventtype
#         self.owner = owner
#         self.status = EventStatus.UNPUBLISHED
#         self.description = description
#         self.payload = payload
#         self.tags = set()
#         self.timestamp = None
#         self.touch()
# 
#     def set_published(self):
#         """Sets the status of this event to published."""
#         self.status = EventStatus.PUBLISHED
# 
#     def set_unpublished(self):
#         """Sets the status of this event to unpublished."""
#         self.status = EventStatus.UNPUBLISHED
# 
#     def touch(self):
#         """Sets the current time as timestamp of this event"""
#         self.timestamp = ts.utc_now()
# 
#     def json(self):
#         """Dumps this object to a JSON string.
# 
#         Returns:
#             str
# 
#         """
#         data = dict(id=self.id, type=self.type.json(),
#                     owner=self.owner, status=self.status,
#                     description=self.description, payload=self.payload,
#                     tags=list(self.tags), timestamp=ts.stringify(self.timestamp))
#         return dumps(data)
# 
#     def md5(self):
#         """Returns the MD5 hash of this object.
# 
#         Returns:
#             str
# 
#         """
#         m = hashlib.md5()
#         m.update(self.json().encode('utf-8'))
#         return m.hexdigest()
# 
#     def __repr__(self):
#         return '<{}.{} - type: {} - id: {}>'.format(
#             __name__,
#             self.__class__.__name__,
#             self.type.__class__.__name__,
#             self.id
#         )
# 
# Path: samples/chatroom/eventtypes.py
# class PeerJoinedEventType(EventType):
#     def __init__(self, owner=None):
#         EventType.__init__(
#             self,
#             '''{
#               "$schema": "http://json-schema.org/draft-04/schema#",
#               "type": "object",
#               "properties": {
#                 "payload": {
#                   "type": "object",
#                   "properties": {
#                     "peer": {
#                       "type": ["string", "number"]
#                     }
#                   },
#                   "required": [
#                     "peer"
#                   ]
#                 }
#               },
#               "required": [
#                 "payload"
#               ]
#             }''',
#             description='Peer joining chatroom events',
#             owner=owner)
# 
# class PeerLeftEventType(EventType):
#     def __init__(self, owner=None):
#         EventType.__init__(
#             self,
#             '''{
#               "$schema": "http://json-schema.org/draft-04/schema#",
#               "type": "object",
#               "properties": {
#                 "payload": {
#                   "type": "object",
#                   "properties": {
#                     "peer": {
#                       "type": ["string", "number"]
#                     }
#                   },
#                   "required": [
#                     "peer"
#                   ]
#                 }
#               },
#               "required": [
#                 "payload"
#               ]
#             }''',
#             description='Peer leaving chatroom events',
#             owner=owner)


from baroque import Event
from .eventtypes import PeerJoinedEventType, PeerLeftEventType



class Room:
    def __init__(self, name, event_broker, events_topic):
        self.name = name
        self.peers = list()
        self.broker = event_broker
        self.topic = events_topic
        
    def join_peer(self, peer):
        if peer not in self.peers:
            self.peers.append(peer)
            
    def join_peers(self, peers_list):
        for p in peers_list:
            self.peers.append(p)
            self.broker.publish_on_topic(
                Event(PeerJoinedEventType,
                      payload=dict(peer=p.name),
                      owner=self.name),
                self.topic)
            
    def remove_peer(self, peer):
        if peer in self.peers:
            self.peers.remove(peer)
            self.broker.publish_on_topic(

# Path: baroque/entities/event.py
# class Event:
#     """An event that can be published.
# 
#     Args:
#         eventtype (:obj:`baroque.entities.eventtype.EventType` instance or `type` object): the type of the event
#         payload (dict, optional): the content of this event
#         description (str, optional): the description of this event
#         owner (str, optional): the owner of this event
# 
#     """
#     def __init__(self, eventtype, payload=None, description=None, owner=None):
#         assert eventtype is not None
#         if type(eventtype) == type:
#             eventtype = eventtype()
#         assert isinstance(eventtype, EventType)
#         if payload is not None:
#             assert isinstance(payload, dict)
#         self.id = str(uuid.uuid4())
#         self.type = eventtype
#         self.owner = owner
#         self.status = EventStatus.UNPUBLISHED
#         self.description = description
#         self.payload = payload
#         self.tags = set()
#         self.timestamp = None
#         self.touch()
# 
#     def set_published(self):
#         """Sets the status of this event to published."""
#         self.status = EventStatus.PUBLISHED
# 
#     def set_unpublished(self):
#         """Sets the status of this event to unpublished."""
#         self.status = EventStatus.UNPUBLISHED
# 
#     def touch(self):
#         """Sets the current time as timestamp of this event"""
#         self.timestamp = ts.utc_now()
# 
#     def json(self):
#         """Dumps this object to a JSON string.
# 
#         Returns:
#             str
# 
#         """
#         data = dict(id=self.id, type=self.type.json(),
#                     owner=self.owner, status=self.status,
#                     description=self.description, payload=self.payload,
#                     tags=list(self.tags), timestamp=ts.stringify(self.timestamp))
#         return dumps(data)
# 
#     def md5(self):
#         """Returns the MD5 hash of this object.
# 
#         Returns:
#             str
# 
#         """
#         m = hashlib.md5()
#         m.update(self.json().encode('utf-8'))
#         return m.hexdigest()
# 
#     def __repr__(self):
#         return '<{}.{} - type: {} - id: {}>'.format(
#             __name__,
#             self.__class__.__name__,
#             self.type.__class__.__name__,
#             self.id
#         )
# 
# Path: samples/chatroom/eventtypes.py
# class PeerJoinedEventType(EventType):
#     def __init__(self, owner=None):
#         EventType.__init__(
#             self,
#             '''{
#               "$schema": "http://json-schema.org/draft-04/schema#",
#               "type": "object",
#               "properties": {
#                 "payload": {
#                   "type": "object",
#                   "properties": {
#                     "peer": {
#                       "type": ["string", "number"]
#                     }
#                   },
#                   "required": [
#                     "peer"
#                   ]
#                 }
#               },
#               "required": [
#                 "payload"
#               ]
#             }''',
#             description='Peer joining chatroom events',
#             owner=owner)
# 
# class PeerLeftEventType(EventType):
#     def __init__(self, owner=None):
#         EventType.__init__(
#             self,
#             '''{
#               "$schema": "http://json-schema.org/draft-04/schema#",
#               "type": "object",
#               "properties": {
#                 "payload": {
#                   "type": "object",
#                   "properties": {
#                     "peer": {
#                       "type": ["string", "number"]
#                     }
#                   },
#                   "required": [
#                     "peer"
#                   ]
#                 }
#               },
#               "required": [
#                 "payload"
#               ]
#             }''',
#             description='Peer leaving chatroom events',
#             owner=owner)


# Path: samples/chatroom/room.py
from baroque import Event
from .eventtypes import PeerJoinedEventType, PeerLeftEventType



class Room:
    def __init__(self, name, event_broker, events_topic):
        self.name = name
        self.peers = list()
        self.broker = event_broker
        self.topic = events_topic
        
    def join_peer(self, peer):
        if peer not in self.peers:
            self.peers.append(peer)
            
    def join_peers(self, peers_list):
        for p in peers_list:
            self.peers.append(p)
            self.broker.publish_on_topic(
                Event(PeerJoinedEventType,
                      payload=dict(peer=p.name),
                      owner=self.name),
                self.topic)
            
    def remove_peer(self, peer):
        if peer in self.peers:
            self.peers.remove(peer)
            self.broker.publish_on_topic(

                Event(PeerLeftEventType,
