def handle_publisher(self, connection, message):
        self.publishers[str(datetime.now())] = connection

        new_topic = Topic(
            ' '.join(self.separate_str_by_space(message)[1:]).rstrip('\n'),
            connection)
        self.topics[str(datetime.now())] = new_topic

        reply = "Server: topic \'" + new_topic.get_name(
        ) + "\' has been published."
        print(reply)

        try:
            connection.sendall(str.encode(reply + '\n'))

            self.receive_topic_messages_and_respond(connection)
        except:
            print('Connection to ' +
                  ':'.join(map(str, connection.getpeername())) + ' lost.\n')

            for topic in self.topics.values():
                if topic.get_publisher() == connection:
                    topic.set_is_active(False)

                    self.send_inactive_topic_message_to_subs(topic)

            self.remove_client_from_clients_list(connection)
            self.remove_client_from_publishers_list(connection)

        return
示例#2
0
 def create_topic(self, name):
     t = Topic(name)
     self.topics[t.get_name()] = t
     return t