Package communicator

Source Code of communicator.Node

package communicator;

import configuration.NodeConfiguration;
import core.CoreManager;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class Node {

    private NodeStatus status = NodeStatus.UNKNOWN;
    private RemoteNode remoteNode = null;
    private String id = null;

    public NodeStatus getNodeStatus() {
        return status;
    }

    public void setNodeStatus(NodeStatus status) {
        this.status = status;
    }

    public String getNodeId() {
        return id;
    }
    private NodeConfiguration config;
    private NodeGroup nodeGroup;

    public NodeGroup getNodeGroup() {
        return nodeGroup;
    }

    public NodeConfiguration getConfig() {
        return config;
    }

    /**
     * Creates new node using given configuration.
     * @param config
     */
    public Node(NodeConfiguration config, NodeGroup nodeGroup) throws Exception {
        this.config = config;
        this.nodeGroup = nodeGroup;
        Communicator.getCommunicator().addNode(this);
    }

    public void sendMessage(Message message) throws MessageSendException, Exception {

        if (this.status != NodeStatus.REACHABLE)
        {
            // do nothing
            return;
             // throw new MessageSendException("Message cannot be send - node UNREACHABLE");
        }
        try {
            validateInitialized();

            remoteNode.receiveMesage(Communicator.getCommunicator().getNodeId(), message);

        } catch (Exception ex) {
            setUnreachable();
            throw new MessageSendException("Message cannot be send.", ex);
        }
    }

    public void pingRemoteNode() throws MessageSendException, Exception {
         //LoggerManager.getLogger().log(Level.FINE, "pinging node "+this.config.getRmiRegistryAddress());

        try {
            validateInitialized();

            String myId = Communicator.getCommunicator().getNodeId();
            remoteNode.ping(myId, Communicator.getCommunicator().getRemoteNode());

        } catch (Exception ex) {

            setUnreachable();
            throw new MessageSendException("Message cannot be send.", ex);
        }
    }

    private void setUnreachable() throws Exception {
        NodeStatus s = this.status;
        this.status = NodeStatus.UNREACHABLE;
        remoteNode = null;
        String id_prev = id;
        id = null;

         CoreManager co = Communicator.getCommunicator().getCoreManager();
         if (co != null && s == NodeStatus.REACHABLE)
                co.nodeDisabled(id_prev);
    }

    @Override
    public String toString() {
        return this.config.getRmiRegistryAddress();
    }

    private void validateInitialized() throws RemoteException, NotBoundException, Exception {

        if (remoteNode == null) {
            remoteNode = (RemoteNode) Naming.lookup(
                    this.config.getRmiRegistryAddress() + "/" + RemoteNode.class.getName());
        }

        if (remoteNode == null)
        {
            throw new Exception("Cannot get Remotenode from"+
                    this.config.getRmiRegistryAddress() + "/" + RemoteNode.class.getName());
        }

        if (id == null) {
            String remoteid = remoteNode.getId();
            if (remoteid != null && !remoteid.isEmpty()) {
                this.id = remoteid;
                Communicator.getCommunicator().setNodeForId(remoteid, this);
            } else {
                // wtf ?!
                throw new Exception("Node id cannot be null or empty.");
            }
        }
    }
}
TOP

Related Classes of communicator.Node

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.