/*
 * 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.JChannel;
import org.jgroups.Message;
import org.jgroups.util.Util;

import java.io.FileInputStream;

/**
 *
 * @author dturnbu
 */
public class JGroupsMessageTest {
    public static final String dir="/home/bela/turnbull";
    public static final String cfg=dir + "/config.xml";
    public static final String text=dir + "/TestData.txt";
    static JChannel channel;

    
    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        System.setProperty("java.net.preferIPv4Stack" , "true");
        channel = new JChannel(cfg);
        channel.connect("TestCluster");
        channel.setReceiver(new jGroupsTestReciever());

        while(true) {
            Util.keyPress("enter to send file");
            SendFile();
        }
    }
    static void SendFile() throws Exception {
        try(FileInputStream file=new FileInputStream(text)) {
            int bytesRead=0;
            int bytes_sent=0;
            for(; ; ) {
                byte[] fileByte=new byte[128];
                bytesRead=file.read(fileByte);
                bytes_sent+=bytesRead;
                if(bytesRead == -1)
                    break;
                System.out.printf("-- sending %d bytes\n", bytesRead);
                channel.send(new Message(null, fileByte, 0, bytesRead));
            }
            System.out.printf("done, sent a total of %d bytes\n", bytes_sent);
        }
    }
}
