Package client.jg

Source Code of client.jg.JGClient

package client.jg;

import org.apache.log4j.Logger;
import org.jgroups.JChannel;
import java.net.URL;
import org.jgroups.log.Trace;
import org.jgroups.ChannelListener;
import org.jgroups.Channel;
import org.jgroups.Address;
import org.jgroups.Message;
import org.jgroups.View;
import org.jgroups.GetStateEvent;
import org.jgroups.SetStateEvent;
import org.jgroups.util.Util;

/**
* An interactive JG client. Run it with util.tester.Tester.
*
* @author Ovidiu Feodorov <ovidiu@feodorov.com>
* @version $Revision: 1.3 $ $Date: 2004/03/04 04:43:59 $
**/
public class JGClient {

    private static final Logger log = Logger.getLogger(JGClient.class);

    private static final String CHANNEL_CONFIG_FILE = "sljms.xml";

    private JChannel channel;
    private volatile boolean reading = true;
    private Thread puller;
    private byte[] state;

    public JGClient() throws Exception {

        Trace.setIdentifier("org.jgroups");

        URL config = getClass().getClassLoader().getResource(CHANNEL_CONFIG_FILE);
       
        if (config == null) {
            String msg =
                "No channel configuration file found (" + CHANNEL_CONFIG_FILE + "). Make sure "+
                "it is in the classpath";
            throw new Exception(msg);
        }
        channel = new JChannel(config);
        state = new byte[1];
        channel.setOpt(Channel.GET_STATE_EVENTS, Boolean.TRUE);
        channel.setChannelListener(new ChannelListener() {

                public void channelClosed(Channel channel) {
                    log.info("ChannelListener: CHANNEL CLOSED");
                }
          
                public void channelConnected(Channel channel) {
                    log.info("ChannelListener: CHANNEL CONNECTED");
                   
                }
          
                public void channelDisconnected(Channel channel) {
                    log.info("ChannelListener: CHANNEL DISCONNECTED");
                   
                }
          
                public void channelReconnected(Address addr) {
                    log.info("ChannelListener: CHANNEL RECONNECTED");
                   
                }
          
                public void channelShunned() {
                    log.info("ChannelListener: CHANNEL SHUNNED");
                   
                }
            });

        // the puller thread
        puller = new Thread(new Runnable() {
                public void run() {
                    while(reading) {
                        try {
                            Object o = channel.receive(0);
                            log.info("RECEIVED "+o.getClass().getName()+": "+o);
                            if (o instanceof GetStateEvent) {
                                channel.returnState(state);
                            }
                            else if (o instanceof SetStateEvent) {
                                log.info("Setting state to "+
                                         Util.objectFromByteBuffer(((SetStateEvent)o).getArg()));
                            }
                        }
                        catch(Exception e) {
                            log.warn("receive() failed", e);
                        }
                    }
                }
            }, "Puller Thread");

    }


    public void connect(String groupName) throws Exception {
        channel.connect(groupName);
        reading = true;
        puller.start();
    }

    public void send(String msg) throws Exception {
        channel.send(new Message(null, null, msg));
    }


    public View getView() throws Exception {
        return channel.getView();
    }

    public void options() {
        System.out.println("Channel.GET_STATE_EVENTS: "+channel.getOpt(Channel.GET_STATE_EVENTS));
        System.out.println("Channel.VIEW:             "+channel.getOpt(Channel.VIEW));
        System.out.println("Channel.SUSPECT:          "+channel.getOpt(Channel.SUSPECT));
        System.out.println("Channel.LOCAL:            "+channel.getOpt(Channel.LOCAL));
        System.out.println("Channel.BLOCK:            "+channel.getOpt(Channel.BLOCK));
        System.out.println("Channel.AUTO_RECONNECT:   "+channel.getOpt(Channel.AUTO_RECONNECT));
        System.out.println("Channel.AUTO_GETSTATE:    "+channel.getOpt(Channel.AUTO_GETSTATE));

    }


    //
    // STATE METHODS
    //

    public boolean getState() throws Exception {
        return channel.getState(null, 0);
    }

    public void setLocalState(String s) throws Exception {
        state = Util.objectToByteBuffer(s);
    }

    public String getLocalState() throws Exception {
        return (String)Util.objectFromByteBuffer(state);
    }


    //
    // END OF STATE METHODS
    //


    public void exit() {

        reading = false;
        if (channel != null) {
            try {
                channel.close();
            }
            catch(Exception e) {
                log.warn("Cannot close the channel", e);
            }
        }
        System.exit(0);
    }

//     private void test() {
//         System.out.println(JChannel.TEST_VARIABLE);
//     }
   

}


TOP

Related Classes of client.jg.JGClient

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.