Search in sources :

Example 1 with Message

use of ProducerDummy.Messages.Message in project amos2022ss02-audit-chain by amosproj.

the class ConsumerClientBlockchain method start.

/**
 * Start receiving Messages from the RabbitMQ Server.
 * @throws IOException if an I/O error occurs
 * @throws TimeoutException if the timeout expires
 */
public void start() throws IOException, TimeoutException {
    System.out.println("Starting to receive Messages.");
    Connection connection = this.factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages.");
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        AggregateMessage message;
        try {
            message = (AggregateMessage) AggregateConsumerClient.deserializeMessage(delivery.getBody());
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        Vector<Message> messages = message.getMessages();
        Integer[] seq_numbers = new Integer[messages.size()];
        String[] transactions = new String[messages.size()];
        int iterator = 0;
        for (Message m : messages) {
            // if you use instanceOf you could accept both Messages and HmacMessages
            m = (Hmac_Message) m;
            seq_numbers[iterator] = m.getSequence_number();
            transactions[iterator] = m.getMessage();
            iterator++;
        }
        blockchain.addABlock(seq_numbers, transactions);
    // blockchain.printBlockchain();
    };
    channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
    });
}
Also used : DeliverCallback(com.rabbitmq.client.DeliverCallback) Message(ProducerDummy.Messages.Message) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) AggregateConsumerClient(ConsumerDummy.AggregateConsumerClient) Connection(com.rabbitmq.client.Connection) StandardCharsets(java.nio.charset.StandardCharsets) AggregateConsumerClient.deserializeMessage(ConsumerDummy.AggregateConsumerClient.deserializeMessage) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) Vector(java.util.Vector) Gson(com.google.gson.Gson) Map(java.util.Map) AggregateMessage(ProducerDummy.Messages.AggregateMessage) AbstractClient(ProducerDummy.Client.AbstractClient) Blockchain(BlockchainImplementation.Blockchain.Blockchain) Hmac_Message(ProducerDummy.Messages.Hmac_Message) Channel(com.rabbitmq.client.Channel) Message(ProducerDummy.Messages.Message) AggregateConsumerClient.deserializeMessage(ConsumerDummy.AggregateConsumerClient.deserializeMessage) AggregateMessage(ProducerDummy.Messages.AggregateMessage) Hmac_Message(ProducerDummy.Messages.Hmac_Message) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) AggregateMessage(ProducerDummy.Messages.AggregateMessage) DeliverCallback(com.rabbitmq.client.DeliverCallback) Hmac_Message(ProducerDummy.Messages.Hmac_Message) Vector(java.util.Vector)

Example 2 with Message

use of ProducerDummy.Messages.Message in project amos2022ss02-audit-chain by amosproj.

the class AggregateConsumerClient method start.

public void start() throws IOException, TimeoutException {
    System.out.println("Starting to receive Messages.");
    Connection connection = this.factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        AggregateMessage message;
        try {
            message = (AggregateMessage) deserializeMessage(delivery.getBody());
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        Vector<Message> messages = message.getMessages();
        for (int i = 0; i < message.getMessageSize(); i++) {
            Message single_message = messages.get(i);
            this.persistenceStrategy.StoreMessage(new JsonMessage(single_message.getSequence_number(), single_message.getMessage()));
            System.out.println(String.format("Received event %d with the content: %s", single_message.getSequence_number(), single_message.getMessage()));
        }
    };
    channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
    });
}
Also used : AggregateMessageFilePersistence(ProducerDummy.Persistence.AggregateMessageFilePersistence) DeliverCallback(com.rabbitmq.client.DeliverCallback) Message(ProducerDummy.Messages.Message) JsonMessage(ProducerDummy.Messages.JsonMessage) ObjectInputStream(java.io.ObjectInputStream) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Connection(com.rabbitmq.client.Connection) Vector(java.util.Vector) ByteArrayInputStream(java.io.ByteArrayInputStream) Paths(java.nio.file.Paths) AggregateMessage(ProducerDummy.Messages.AggregateMessage) AbstractClient(ProducerDummy.Client.AbstractClient) Channel(com.rabbitmq.client.Channel) Message(ProducerDummy.Messages.Message) JsonMessage(ProducerDummy.Messages.JsonMessage) AggregateMessage(ProducerDummy.Messages.AggregateMessage) JsonMessage(ProducerDummy.Messages.JsonMessage) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) AggregateMessage(ProducerDummy.Messages.AggregateMessage) DeliverCallback(com.rabbitmq.client.DeliverCallback) Vector(java.util.Vector)

Example 3 with Message

use of ProducerDummy.Messages.Message in project amos2022ss02-audit-chain by amosproj.

the class Client method start.

/**
 * Start Sending Messages as JSON to the RabbitMQ Server.
 * TODO sending Messages is still bad and just a minimal example make it better
 * @throws IOException if an I/O error occurs
 * @throws TimeoutException if the timeout expires
 * @throws InterruptedException if the thread is interrupted
 */
public void start() throws IOException, TimeoutException {
    System.out.println("Starting to send Messages.Message to AMQP Host");
    try (Connection connection = this.factory.newConnection();
        Channel channel = connection.createChannel()) {
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        // Recover last Message which was stored in file and maybe was not send
        Message message = this.recoverLastMessage();
        if (message != null) {
            channel.basicPublish("", QUEUE_NAME, null, serialize(message));
            this.sequence_number += 1;
        }
        for (String line = this.dataGenerator.getData(); line != null; line = this.dataGenerator.getData()) {
            System.out.println("The following Messages.Message will be send:\n" + line);
            message = new JsonMessage(this.sequence_number, line);
            this.persistenceStrategy.StoreMessage(message);
            channel.basicPublish("", QUEUE_NAME, null, serialize(message));
            this.sequence_number += 1;
            try {
                TimeUnit.SECONDS.sleep(SECOND_DELAY_BETWEEN_MESSAGES);
            } catch (InterruptedException e) {
                System.out.println("Thread was interrupted");
            }
        }
    }
}
Also used : Message(ProducerDummy.Messages.Message) JsonMessage(ProducerDummy.Messages.JsonMessage) JsonMessage(ProducerDummy.Messages.JsonMessage) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection)

Aggregations

Message (ProducerDummy.Messages.Message)3 Channel (com.rabbitmq.client.Channel)3 Connection (com.rabbitmq.client.Connection)3 AbstractClient (ProducerDummy.Client.AbstractClient)2 AggregateMessage (ProducerDummy.Messages.AggregateMessage)2 JsonMessage (ProducerDummy.Messages.JsonMessage)2 DeliverCallback (com.rabbitmq.client.DeliverCallback)2 IOException (java.io.IOException)2 Vector (java.util.Vector)2 TimeoutException (java.util.concurrent.TimeoutException)2 Blockchain (BlockchainImplementation.Blockchain.Blockchain)1 AggregateConsumerClient (ConsumerDummy.AggregateConsumerClient)1 AggregateConsumerClient.deserializeMessage (ConsumerDummy.AggregateConsumerClient.deserializeMessage)1 Hmac_Message (ProducerDummy.Messages.Hmac_Message)1 AggregateMessageFilePersistence (ProducerDummy.Persistence.AggregateMessageFilePersistence)1 Gson (com.google.gson.Gson)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Paths (java.nio.file.Paths)1