📄 jgrid.java
字号:
/**
*
* Program : JGrid.java
*
* Author : Vijayakrishnan Menon
*
* Date : 15th Dec 2005
*
*Organization : Centre for Excellence in
* Computational Engineering and Networking (CEN),
* Amrita Viswa Vidyapeetham
**/
package JAMPack;
/** This is an Exception thrown when an unmaped rank id is passed to resolve an
* ip. The Exception prevents the code from accessing resources that is out
* side this enviroment. These type of connections can cause more serious
* Exceptions to thrown by the RMI run time. This can all be avoided if we make
* sure that the connetions that are made, strictly is kept inside the
* enviroment.
**/
class UnknownRankException extends Exception {
public UnknownRankException(String msg) {
super(msg);
}
}
/** The Environment information has to stored on each of the local machine.
* This enables each group to create sub-groups. A Grid environment with global
* scope is initialised when the system is initiated. This object engulfs all
* the hosts in a HostList.
**/
public final class JGrid implements java.io.Serializable {
private String []_hosts;
transient private int _rank = -2;
transient boolean _valid = false;
/** The constructor for a Grid object */
public JGrid(String []hosts) {
this._hosts = hosts;
try { this._rank = getRankForIP( (java.net.InetAddress.getLocalHost()).getHostAddress() );}
catch (java.net.UnknownHostException e) { e.printStackTrace(); }
this._valid = true;
}
/** This overload allows constructions of sub Grids */
public JGrid (JGrid grid, int []hosts) throws UnknownRankException {
this._hosts = new String[hosts.length];
for(int i=0;i<hosts.length;i++)
this._hosts[i] = grid.getIPForRank(hosts[i]);
try {this._rank = getRankForIP( (java.net.InetAddress.getLocalHost()).toString() ); }
catch (java.net.UnknownHostException e) { e.printStackTrace(); }
this._valid = true;
}
/** Returns IP String for a member of this Grid, when the rank id is passed.
* Throws an UnknownRankException if the rank is not valid in this grid
* objects environment. */
public String getIPForRank(int rank) throws UnknownRankException {
if (rank < this._hosts.length)
return this._hosts[rank];
else
throw new UnknownRankException("The rank id could not be resolved");
}
/** Returns the Rank id for a member if the ip is passed. Returns -1 if the
* ip is not a member of this grid */
public int getRankForIP(String ip) {
for(int i=0;i<this._hosts.length;i++)
{
if (_hosts[i].compareTo(ip) == 0) return i;
}
return -1;
}
/** A utility function that lets us test the state of the member. Returns
* true if the member is online else false. If the rank id is not valid
* then it throws an UnknownRankException
**/
public boolean checkHost(int rank) throws UnknownRankException {
String ip= getIPForRank(rank);
boolean result = false;
try { result = (java.net.InetAddress.getByName(ip)).isReachable(5000);}
catch (java.net.UnknownHostException e) { e.printStackTrace(); }
catch (java.io.IOException e ) { e.printStackTrace(); }
return result;
}
/** Retuns the rank of the current machine */
public int getRank() {
if(!this._valid) {
int rank = -1;
try { rank = ( getRankForIP( (java.net.InetAddress.getLocalHost()).getHostAddress()) ); }
catch (java.net.UnknownHostException e) { e.printStackTrace();}
this._valid = true;
this._rank = rank;
}
return this._rank;
}
/** Returns the size, in terms of member count of this grid object. */
public int getSize() {
return _hosts.length;
}
/** This methods returns one of the free threads of a given processor. If no
* threads are free then it returns null */
public RemoteThread getValidThreadForRank(int rank) {
String ip = this._hosts[rank];
RemoteThread remote = null;
for(int j = 0 ; j<JEnvironment.MAX_NO_OF_REMOTE_PROCESSES;j++)
try {
remote = (RemoteThread)java.rmi.Naming.lookup("rmi://"+ip+":"
+JEnvironment.REGISTRY_PORT+"/RemoteThread"+(j+1));
if(!remote.getValidity()) return remote;
}catch(java.rmi.NotBoundException e) { e.printStackTrace();}
catch(java.rmi.RemoteException e) { e.printStackTrace(); }
catch(java.net.MalformedURLException e) { e.printStackTrace();}
return null;
}
public String[] getHostList() {
return this._hosts;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -