package org.jgroups.tests;

import org.jgroups.*;
import org.jgroups.util.Util;

import java.io.Serializable;

/**
 * @author Bela Ban
 * @since x.y
 */

public class bla2 {
    protected JChannel ch;
    protected static final int SIZE=45000;

    protected void start(String name) throws Exception {
        ch=new JChannel("/Users/bela/asym-encrypt.xml").name(name);
        ch.setReceiver(new Receiver() {
            public void receive(Message msg) {
                System.out.printf("-- received %d bytes from %s: %s\n", msg.getLength(), msg.src(), msg.getObject());
            }

            public void viewAccepted(View new_view) {
                System.out.printf("-- view: %s\n", new_view);
            }
        });
        ch.connect("demo");

        while(true) {
            Util.keyPress("enter to send:");
            Message msg=new ObjectMessage(null, new Person("Bela Ban", 55, new byte[SIZE]));
            ch.send(msg);
        }
    }

    public static void main (String[]args) throws Exception {
        new bla2().start(args[0]);

    }

    public static class Person implements Serializable {
        protected String name;
        protected int age;
        protected byte[] payload;

        public Person(String name, int age, byte[] payload) {
            this.name=name;
            this.age=age;
            this.payload=payload;
        }


        public String toString() {
            return String.format("%s %d %d bytes", name, age, payload.length);
        }
    }

}