package communicator;
import configuration.ClusterConfiguration;
import configuration.Configuration;
import core.CoreManager;
import helpers.ExceptionsHelper;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Communicator {
private CoreManager coreMan = null;
public void setCoreManager(CoreManager coreMan)
{
this.coreMan = coreMan;
}
public CoreManager getCoreManager()
{
return this.coreMan;
}
private boolean continuePingingNodes;
private synchronized boolean getContinuePingingNodes() {
return continuePingingNodes;
}
private synchronized void setContinuePingingNodes(boolean value) {
continuePingingNodes = value;
}
private List<Node> nodes = null;
public List<Node> getNodes() {
return nodes;
}
public void addNode(Node node) {
nodes.add(node);
}
NodeCommunicatorServer server = null;
private Hashtable<String, Node> idsToNodes;
public Hashtable<String, Node> getIdsToNodes() {
return new Hashtable<String, Node>(idsToNodes);
}
public Node getNodeForId(String id) throws Exception {
if (id == null || id.isEmpty()) {
throw new Exception("Node id cannot be null or empty");
}
return idsToNodes.get(id);
}
public void setNodeForId(String id, Node node) throws Exception {
if (id == null || id.isEmpty()) {
throw new Exception("Node id cannot be null or empty");
}
idsToNodes.put(id, node);
}
public String getNodeId() throws Exception {
if (server == null)
{
throw new Exception("Server not initialized.");
}
return server.getNodeId();
}
public RemoteNode getRemoteNode() throws Exception {
if (server == null)
{
throw new Exception("Server not initialized.");
}
return server.getRemoteNode();
}
private Cluster cluster;
public Cluster getCluster() {
return cluster;
}
private static Communicator communicator = null;
public static Communicator getCommunicator() throws Exception {
if (communicator == null) {
Configuration.loadConfiguration();
//get lodead configuration
Configuration config = Configuration.getCurrentConfiguration();
//initializing communicator based on the configuration
initializeCommunicator(config.getClusterConfiguration());
int rmiPort = Configuration.getCurrentConfiguration().getCurrentNodeConfiguration().getRmiRepositoryPort();
Communicator.getCommunicator().initializeCommunicatorServer(rmiPort);
}
return communicator;
}
private ClusterConfiguration clusterConfig;
public ClusterConfiguration getClusterConfiguration() {
return clusterConfig;
}
private Communicator() {
this.idsToNodes = new Hashtable<String, Node>();
this.nodes = new ArrayList<Node>();
}
private static void initializeCommunicator(ClusterConfiguration config) throws Exception {
Communicator comm = new Communicator();
Communicator.communicator = comm;
comm.cluster = new Cluster(config);
}
public void stopCommunicatorServer() {
setContinuePingingNodes(false);
server.stopServer();
server = null;
}
public void initializeCommunicatorServer( int rmiRegistryPort) throws RemoteException, Exception {
if (server == null) {
RemoteNode currentNode = new RemoteNodeImpl();
server =
new NodeCommunicatorServer(currentNode, rmiRegistryPort);
server.initializeServer();
setContinuePingingNodes(true);
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(new Runnable() {
public void run() {
while (getContinuePingingNodes()) {
pingNodes();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(Communicator.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
}
}
private void pingNodes() {
Logger.getLogger(Communicator.class.getName()).log(Level.FINE, "Pinging nodes ");
for (final Node n : nodes) {
(new Thread(new Runnable() {
public void run() {
try {
Logger.getLogger(Communicator.class.getName()).log(Level.FINE, "Pinging Node: " + n.getNodeId());
n.pingRemoteNode();
} catch (Exception ex) {
// ExceptionsHelper.logException(ex);
}
}
})).start();
}
}
public void sendMessageToAllNodes(Message message) {
return ;
}
public void sendMessageToFirstAvaiable(NodeGroup group, Message message) {
return ;
}
ArrayList<MessageListener> listeners = new ArrayList<MessageListener> ();
public void addMessageListener(MessageListener listener)
{
if (listeners.contains(listener))
return;
this.listeners.add(listener);
}
public void pushMessageToListener(Node node, Message message)
{
// Logger.getLogger(Communicator.class.getName()).log(Level.INFO, "Sending message" + message.toString() + "to listeners count" + listeners.size());
if (listeners != null)
{
for (MessageListener lis : listeners)
lis.messageReceived(node, message);
}
}
}