dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Remote invocation - unknown error

  1. #1
    Join Date
    Dec 2013
    Posts
    3

    Remote invocation - unknown error

    I am unable to find the error... using Netbeans 8.2

    Node
    Code:
    package assignment1;
    
    import java.rmi.AlreadyBoundException;
    import java.rmi.ConnectException;
    import java.rmi.NotBoundException;
    import java.rmi.RemoteException;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;
    import java.rmi.server.UnicastRemoteObject;
    import java.util.Timer;
    import java.util.TimerTask;
    
    @SuppressWarnings("serial")
    public class Node extends UnicastRemoteObject implements ElectionNode {
    
    
    	private static final int min = 1;
    	private static final int max = 5;
    
    	private static final int delay = 5000;
    	private final int silencePeriod = 
    			(min + (int)(Math.random() * ((max - min) + 1))) * 1000;
    	private int messagePeriod = 0;
    
    	private String host;
    
    	private String name;
    	private String leaderName = "";
    
    	private int ignoreElection = 0;
    	private boolean heardFromLeader = false;
    	private boolean noLeaderFound = true;
    
    	@SuppressWarnings("unused")
    	private Node() throws RemoteException {super();}
    
    	public Node(String nameIn, String hostIn) throws RemoteException {
    		super();
    		
    		this.name = nameIn;
    		this.host = hostIn;
    
    		// Make sure that messages are getting sent more frequently then
    		// the node checks for silence
    		while (messagePeriod < silencePeriod) {
    			messagePeriod = (min + (int) (Math.random() * ((max - min) + 1))) * 1000;
    		}
    		
    		Timer timer = new Timer();
    		timer.scheduleAtFixedRate(new TimerTask() {
    			@Override
    			public void run() {
    				if (!name.equals(leaderName) && 
    						!heardFromLeader) {
    					try {
    						System.out.println("Calling election...");
    						Registry reg = LocateRegistry.getRegistry(host);
    						for (String nodeName : reg.list()) {
    							try {
    								if (!nodeName.equals(name) && nodeName.compareTo(name) > 0) {
    									ElectionNode otherNode = (ElectionNode) reg.lookup(nodeName);
    									String response = otherNode.startElection(name);
    									
    									if (response.length() > 0) {
    										noLeaderFound = false;
    										break;
    									}
    								}
    							} catch (DeadNodeException e) {
    								System.out.println(e.toString());
    							} catch (NotBoundException e) {
    								try {
    									System.out.println("Node Error: " + nodeName + " unbound.");
    									reg.unbind(nodeName);
    								} catch (NotBoundException er) {
    									// Shouldn't happen
    								}
    							} catch (ConnectException e) {
    								try {
    									System.out.println("Node Error: " + nodeName + " unbound.");
    									reg.unbind(nodeName);
    								} catch (NotBoundException er) {
    									// Shouldn't happen
    								}
    							}
    						}
    						
    						if (noLeaderFound) {
    							try {
    								System.out.println("No leader found, electing myself.");
    								startElection(name);
    								noLeaderFound = false;
    							} catch (DeadNodeException e) {
    								System.out.println("Node Error: " + e.toString());
    							}
    						}
    					} catch (RemoteException e) {
    						System.out.println("Node Error: " + e.toString());
    						e.printStackTrace();
    					}
    				} else if (heardFromLeader)
    					heardFromLeader = false;
    			}
    		}, delay, silencePeriod);
    
    		timer.scheduleAtFixedRate(new TimerTask() {
    			@Override
    			public void run() {
    				if (leaderName != null && !name.equals(leaderName)) {
    					sendLeaderMsg("Message from " + name);
    				}
    			}
    		}, delay, messagePeriod);
    		
    		System.out.println(name + " ready.");
    	}
    
    	private void sendLeaderMsg(String msg) {
    		try {
    			Registry reg = LocateRegistry.getRegistry(host);
    			try {
    				ElectionNode leaderNode = (ElectionNode) reg.lookup(leaderName);
    
    				String response = leaderNode.recvMsg(name, msg);
    				System.out.println(leaderName + ": " + response);
    
    				if (!heardFromLeader)
    					heardFromLeader = true;
    			} catch (NotBoundException e) {
    				try {
    					System.out.println("Node Error: " + leaderName + " unbound.");
    					reg.unbind(leaderName);
    				} catch (NotBoundException er) {
    					// Shouldn't happen
    				}
    			} catch (ConnectException e) {
    				try {
    					System.out.println("Node Error: " + leaderName + " unbound.");
    					reg.unbind(leaderName);
    				} catch (NotBoundException er) {
    					// Shouldn't happen
    				}
    			}
    		} catch (RemoteException e) {
    			System.out.println("Node Error: " + e.toString());
    			e.printStackTrace();
    		}
    	}
    
    	
    	@Override
    	public String startElection(String senderName) throws DeadNodeException {
    		String ret = "";
    		
    		if (ignoreElection > 0) {
    			ignoreElection--;
    			System.out.println(ignoreElection + " more elections being ignored.");
    			throw new DeadNodeException(name + " is dead.");
    		} else {
    			System.out.println("Election started.");
    
    			try {
    				Registry reg = LocateRegistry.getRegistry(host);
    				
    				ret = "Leader accepted.";
    				System.out.println(ret);
    				leaderName = name;
    				for (String nodeName : reg.list()) {
    					if (!nodeName.equals(name)) {
    						try {
    							ElectionNode node = (ElectionNode) reg.lookup(nodeName);
    							node.newLeader(name);
    						} catch (NotBoundException e) {
    							try {
    								System.out.println("Node Error: " + nodeName + " unbound.");
    								reg.unbind(nodeName);
    							} catch (NotBoundException er) {
    								// Shouldn't happen
    							}
    						} catch (ConnectException e) {
    							try {
    								System.out.println("Node Error: " + nodeName + " unbound.");
    								reg.unbind(nodeName);
    							} catch (NotBoundException er) {
    								// Shouldn't happen
    							}
    						}
    					}
    				}
    			} catch (RemoteException e) {
    				System.out.println("Node Error: " + e.toString());
    				e.printStackTrace();
    			}
    		}
    		
    		return ret;
    	}
    	
    	@Override
    	public void newLeader(String newLeaderName) {
    		leaderName = newLeaderName;
    		System.out.println(newLeaderName + " is the new leader.");
    	}
    
    	
    	@Override
    	public String recvMsg(String senderName, String msg) {
    		String ret = "Not the leader.";
    
    		if (leaderName.equals(name)) {
    			System.out.println(senderName + ": " + msg);
    			ret = "Message received.";
    		}
    
    		return ret;
    	}
    
    	
    	@Override
    	public void makeChaos(String newName, int ignore) {
    		if (!name.equals(leaderName)) {
    			try {
    				Registry reg = LocateRegistry.getRegistry(host);
    	
    				String oldName = name;
    				name = newName;
    				ignoreElection = ignore;
    	
    				reg.unbind(oldName);
    				reg.bind(name, this);
    				
    				System.out.println("New node name: " + newName);
    				System.out.println("Ignoring " + ignore + " elections...");
    			} catch(RemoteException e) {
    				System.out.println("Node Error: " + e.toString());
    				e.printStackTrace();
    			} catch (NotBoundException e) {
    				System.out.println("Node Error: " + e.toString());
    				e.printStackTrace();
    			} catch (AlreadyBoundException e) {
    				System.out.println("Node Error: " + e.toString());
    				e.printStackTrace();
    			}
    		}
    	}
    
    	public static void main(String[] args) {
    		String name = (args.length < 1 || args[0].equals("!")) ? 
    				"Node-" + System.currentTimeMillis() : args[0];
    				String host = (args.length < 2) ? null : args[1];
    				try {
    					Node node = new Node(name, host);
    
    					// Bind stub to the registry
    					Registry reg = LocateRegistry.getRegistry(host);
    					reg.bind(name, node);
    				} catch (Exception e) {
    					System.out.println("Node Error: " + e.toString());
    					e.printStackTrace();
    				}
    	}
    
    }
    ElectionNode
    Code:
    package assignment1;
    
    import java.rmi.Remote;
    import java.rmi.RemoteException;
    
    public interface ElectionNode extends Remote {
    	
    	// Node methods
    	public String startElection(String senderName) throws RemoteException, DeadNodeException;
    	public void newLeader(String newLeaderName) throws RemoteException;
    	public String recvMsg(String senderName, String msg) throws RemoteException;
    	
    	// Election Driver methods
    	public void makeChaos(String newName, int ignore) throws RemoteException;
    
    }
    ElectionDriver
    Code:
    package assignment1;
    
    import java.rmi.NotBoundException;
    import java.rmi.RemoteException;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class ElectionDriver {
    
    	int delay = 5000;
    	
    	private final int min = 10;
    	private final int max = 15;
    
    	private String host;
    	
    	public ElectionDriver(String hostIn) {
    		host = hostIn;
    		int period = min + (int) (Math.random() * ((max - min) + 1)) * 1000;
    		
    		Timer t = new Timer();
    		t.scheduleAtFixedRate(new TimerTask() {
    			@Override
    			public void run() {
    				try {
    					Registry reg = LocateRegistry.getRegistry(host);
    					for (String nodeName : reg.list()) {
    						try {
    							int silence = (int) (Math.random() * 5);
    							
    							ElectionNode node = (ElectionNode) reg.lookup(nodeName);
    							node.makeChaos("Node-" + System.currentTimeMillis(), silence);
    						} catch (NotBoundException e) {
    							System.out.println("Election Driver Error: " + e.toString());
    							e.printStackTrace();
    						}
    					}
    				} catch (RemoteException e) {
    					System.out.println("Election Driver Error: " + e.toString());
    					e.printStackTrace();
    				}
    			}
    		}, delay, period);
    	}
    
    	public static void main(String[] args) {
    		String host = (args.length < 1) ? null : args[0];
                ElectionDriver electionDriver = new ElectionDriver(host);
    	}
    
    }
    DeadNodeException
    Code:
    package assignment1;
    
    public class DeadNodeException extends Exception {
    	
    	public DeadNodeException() {
    		super();
    	}
    	
    	public DeadNodeException(String message) {
    		super(message);
    	}
    
    }

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,789

    Re: Remote invocation - unknown error

    Is there an error message that gives the name of the exception and where it happened?
    Normally that is shown in a stack trace.
    Norm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)