def publish_message(user): recipient = random.choice(usernames) message = Message(user.username, recipient) tags_quantity = random.randint(0, len(tags) // 2) msg_tags = '' if tags_quantity == 0 else ','.join(set(random.choice(tags) for _ in range(tags_quantity))) message.set_data('{0} -> {1}: {2}'.format(message.sender, message.recipients, message.msg_id), msg_tags) print('User %s sending new message to %s' % (message.sender, message.recipients)) ChatController.create_message(message) ChatController.publish_message(message)
def send_messages(count_users: int): user_prefix = "user_id" client_controller = Client() message_controller = Message() for idx in range(count_users): users.append(user_prefix + str(idx)) client_controller.register(users[idx]) client_controller.login(users[idx]) i = 0 while True: from_username = user_prefix + str(randrange(len(users))) to_username = from_username while to_username != from_username: to_username = user_prefix + str(randrange(len(users))) message = "test message, id:" + str(i) i += 1 message_controller.send_message(message, from_username, to_username) sleep(2)
def send_message(user): while True: recipient_username = input('Recipient: ----> ') if not user.load(recipient_username): pm.print_warning_message( 'The user {0} does not exist! Try again.'.format( recipient_username)) continue content = input(f'{Colors.WHITE}Content: ---> ') message = Message(user.username, recipient_username) message.set_data(content) ChatController.create_message(message) confirm = input(f'{Colors.WHITE}Confirm[y/n]: ----> ') if confirm == 'y': ChatController.publish_message(message) break else: continue
from confluent_kafka import KafkaError from services.message import Message from services.kafka import Kafka message = Message() producer = Kafka() kafka = Kafka() kafka.group = 'aplicacao' consumer = kafka.connect_consumer('tdc-entrada','<<< APLICAÇÃO >>>') try: while True: msg = consumer.poll(0.1) if msg is None: continue elif not msg.error(): print('Received message: {0}'.format(msg.value())) msgAux = message.handle_message(msg.value()) producer.send_message('tdc-saida',msgAux) elif msg.error().code() == KafkaError._PARTITION_EOF: print('End of partition reached {0}/{1}'.format(msg.topic(), msg.partition())) else: print('Error occured: {0}'.format(msg.error().str())) except KeyboardInterrupt: pass finally: consumer.close()
def __init__(self, username): self.message = Message() self.username = username