/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jgroups.tests;

import org.jgroups.Message;
import org.jgroups.ReceiverAdapter;
import org.jgroups.util.Util;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 *
 * @author dturnbu
 */
public class jGroupsTestReciever extends ReceiverAdapter {
    protected static final String contents;
    protected String current="";

    static {
        try {
            final StringBuilder sb=new StringBuilder();
            Files.lines(Paths.get(JGroupsMessageTest.text)).forEach(sb::append);
            contents=sb.toString();
        }
        catch(IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void receive(Message message) {
        String str=Util.bytesToString(message.getBuffer());
        current+=str;
        System.out.printf("-- received %d bytes from %s, accumulated contents=%d bytes\n", message.getLength(), message.src(), current.length());
        if(current.length() >= contents.length()) {
            if(current.equals(contents)) {
                System.out.printf("received correct test (%d bytes); resetting it\n", current.length());
                current="";
            }
        }
    }
}
