⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jcomm.java

📁 JAMPACK Grid programming
💻 JAVA
字号:
/**
 *
 * Program         : JComm.java
 *
 * Author          : Vijayakrishnan Menon
 *
 * Date            : 10th Jan 2006
 *
 * Organization    : Centre for Excellence in 
 *						Computational Engineering and Networking (CEN),
 *                   		Amrita Viswa Vidyapeetham
 *
 **/

package JAMPack;

/** This class implements the Comm interface and embodies the send and receive 
  * functions. The implementation is nominal, other implementations can happen 
  * in future updates or side versions. Note that this class does not implement 
  * collective communication methods like gather, scatter, bcast etc. These 
  * methods will follow later. 
  **/

public class JComm implements Comm {

   /****************************************************************************
	*			       THE SEND METHODS FROM COMM INTERFACE                    *
	****************************************************************************/

	/** generic send 
	 *
	 *	NOTE :	The Objects passed here cannot have annotated codebases of 
	 *			Dynamic class loading done on them the use simple object 
	 *			streaming for marshalling of argument objects. If the receiver 
	 *			does not find the runtime type ofthis object it might throw a 
	 *			ClassNotFoundException. See generic receive in this class for 
	 *			more detailes. 
	 **/
	public void send(Object buff, int destination, JGrid grid, int tag) throws Exception {
		java.net.Socket so = new java.net.Socket(grid.getIPForRank(destination),
														       JEnvironment.PEER_OBJECT_PORT);
				
		java.io.ObjectOutputStream outObj = new java.io.ObjectOutputStream(so.getOutputStream());
		outObj.writeInt(tag);
		outObj.writeInt(grid.getRank());
		outObj.writeObject(buff);
		
		outObj.close();
		so.close();
	}

    public void send(int buff, int destination, JGrid grid, int tag) throws Exception {
    	java.net.Socket so = new java.net.Socket(grid.getIPForRank(destination),
														       JEnvironment.PEER_DATA_PORT);
		java.io.DataOutputStream out = new java.io.DataOutputStream(so.getOutputStream());

		out.writeBytes(tag+"\n"+grid.getRank()+"\n"+buff);
		out.close();
		so.close();
    }

    public void send(int []buff, int destination, JGrid grid, int tag) throws Exception {
    	java.net.Socket so = new java.net.Socket(grid.getIPForRank(destination),
														       JEnvironment.PEER_DATA_PORT);
		java.io.DataOutputStream out = new java.io.DataOutputStream(so.getOutputStream());

		out.writeBytes(tag+"\n"+grid.getRank()+"\n"+buff.length+"\n");
		for(int i=0;i<buff.length;i++)
			out.writeBytes(buff[i]+"\n");
		out.close();
		so.close();
    }

    public void send(float buff, int destination, JGrid grid, int tag) throws Exception {
    	java.net.Socket so = new java.net.Socket(grid.getIPForRank(destination),
														       JEnvironment.PEER_DATA_PORT);
		java.io.DataOutputStream out = new java.io.DataOutputStream(so.getOutputStream());

		out.writeBytes(tag+"\n"+grid.getRank()+"\n"+buff);
		out.close();
		so.close();
    }

    public void send(float []buff, int destination, JGrid grid, int tag) throws Exception {
		java.net.Socket so = new java.net.Socket(grid.getIPForRank(destination),
														       JEnvironment.PEER_DATA_PORT);
		java.io.DataOutputStream out = new java.io.DataOutputStream(so.getOutputStream());

		out.writeBytes(tag+"\n"+grid.getRank()+"\n"+buff.length+"\n");
		for(int i=0;i<buff.length;i++)
			out.writeBytes(buff[i]+"\n");
		out.close();
		so.close();
    }

    public void send(double buff, int destination, JGrid grid, int tag) throws Exception {
    	java.net.Socket so = new java.net.Socket(grid.getIPForRank(destination),
														       JEnvironment.PEER_DATA_PORT);
		java.io.DataOutputStream out = new java.io.DataOutputStream(so.getOutputStream());

		out.writeBytes(tag+"\n"+grid.getRank()+"\n"+buff);
		out.close();
		so.close();
    }

    public void send(double []buff, int destination, JGrid grid, int tag) throws Exception {
    	java.net.Socket so = new java.net.Socket(grid.getIPForRank(destination),
														       JEnvironment.PEER_DATA_PORT);
		java.io.DataOutputStream out = new java.io.DataOutputStream(so.getOutputStream());

		out.writeBytes(tag+"\n"+grid.getRank()+"\n"+buff.length+"\n");
		for(int i=0;i<buff.length;i++)
			out.writeBytes(buff[i]+"\n");
		out.close();
		so.close();
    }

    public void send(char buff, int destination, JGrid grid, int tag) throws Exception {
    	java.net.Socket so = new java.net.Socket(grid.getIPForRank(destination),
														       JEnvironment.PEER_DATA_PORT);
		java.io.DataOutputStream out = new java.io.DataOutputStream(so.getOutputStream());

		out.writeBytes(tag+"\n"+grid.getRank()+"\n"+buff);
		out.close();
		so.close();
    }

    public void send(char []buff, int destination, JGrid grid, int tag) throws Exception {
    	java.net.Socket so = new java.net.Socket(grid.getIPForRank(destination),
														       JEnvironment.PEER_DATA_PORT);
		java.io.DataOutputStream out = new java.io.DataOutputStream(so.getOutputStream());

		out.writeBytes(tag+"\n"+grid.getRank()+"\n"+buff.length+"\n");
		for(int i=0;i<buff.length;i++)
			out.writeBytes(buff[i]+"\n");
		out.close();
		so.close();
    }

    public void send(boolean buff, int destination, JGrid grid, int tag) throws Exception {
    	java.net.Socket so = new java.net.Socket(grid.getIPForRank(destination),
														       JEnvironment.PEER_DATA_PORT);
		java.io.DataOutputStream out = new java.io.DataOutputStream(so.getOutputStream());

		out.writeBytes(tag+"\n"+grid.getRank()+"\n"+buff);
		out.close();
		so.close();
    }

    public void send(boolean []buff, int destination, JGrid grid, int tag) throws Exception {
    	java.net.Socket so = new java.net.Socket(grid.getIPForRank(destination),
														       JEnvironment.PEER_DATA_PORT);
		java.io.DataOutputStream out = new java.io.DataOutputStream(so.getOutputStream());

		out.writeBytes(tag+"\n"+grid.getRank()+"\n"+buff.length+"\n");
		for(int i=0;i<buff.length;i++)
			out.writeBytes(buff[i]+"\n");
		out.close();
		so.close();
    }

    public void send(long buff, int destination, JGrid grid, int tag) throws Exception {
    	java.net.Socket so = new java.net.Socket(grid.getIPForRank(destination),
														       JEnvironment.PEER_DATA_PORT);
		java.io.DataOutputStream out = new java.io.DataOutputStream(so.getOutputStream());

		out.writeBytes(tag+"\n"+grid.getRank()+"\n"+buff);
		out.close();
		so.close();
    }

    public void send(long []buff, int destination, JGrid grid, int tag) throws Exception {
    	java.net.Socket so = new java.net.Socket(grid.getIPForRank(destination),
														       JEnvironment.PEER_DATA_PORT);
		java.io.DataOutputStream out = new java.io.DataOutputStream(so.getOutputStream());

		out.writeBytes(tag+"\n"+grid.getRank()+"\n"+buff.length+"\n");
		for(int i=0;i<buff.length;i++)
			out.writeBytes(buff[i]+"\n");
		out.close();
		so.close();
    }

    public void send(short buff, int destination, JGrid grid, int tag) throws Exception {
		java.net.Socket so = new java.net.Socket(grid.getIPForRank(destination),
														       JEnvironment.PEER_DATA_PORT);
		java.io.DataOutputStream out = new java.io.DataOutputStream(so.getOutputStream());

		out.writeBytes(tag+"\n"+grid.getRank()+"\n"+buff);
		out.close();
		so.close();
    }

    public void send(short []buff, int destination, JGrid grid, int tag) throws Exception {
    	java.net.Socket so = new java.net.Socket(grid.getIPForRank(destination),
														       JEnvironment.PEER_DATA_PORT);
		java.io.DataOutputStream out = new java.io.DataOutputStream(so.getOutputStream());

		out.writeBytes(tag+"\n"+grid.getRank()+"\n"+buff.length+"\n");
		for(int i=0;i<buff.length;i++)
			out.writeBytes(buff[i]+"\n");
		out.close();
		so.close();
    }

   /****************************************************************************
	*				THE RECEIVE METHODS FROM COMM INTERFACE                    *
	****************************************************************************/
	
	/** The generic receive
	 *
	 *	NOTE :  The objects streamed using these methods should be popular with 
	 *			the	class files available every where. If the object's runtime 
	 *			class is not found, a ClassNotFoundException will be thrown and 
	 *			program terminated. The unknown classes cannot be Dynamically 
	 *			loaded for the remote receiver like the RMI based posting 
	 *			mechanism, So be on the look out for such inconsistencies. 
	 **/
	public Object receive(Object buff, int source, JGrid grid, int tag) throws Exception {
		JPeerAgent peer = JPeerServer.receiveCall(tag,source);
		buff = peer.getObject();
		peer.close();
		return buff;
	}

    public int receive(int buff, int source, JGrid grid, int tag) throws Exception {
		JPeerAgent peer = JPeerServer.receiveCall(tag,source);
		buff = Integer.parseInt(peer.getData());
		peer.close();
		return buff;
    }

    public int[] receive(int []buff, int source, JGrid grid, int tag) throws Exception {
		JPeerAgent peer = JPeerServer.receiveCall(tag,source);
		int s = Integer.parseInt(peer.getData());
		int []buffs = new int[s];
		for(int i=0;i<s;i++)
			buffs[i] = Integer.parseInt(peer.getData());
		peer.close();
		return buffs;
    }

    public float receive(float buff, int source, JGrid grid, int tag) throws Exception {
		JPeerAgent peer = JPeerServer.receiveCall(tag,source);
		buff = Float.parseFloat(peer.getData());
		peer.close();
		return buff;
    }

    public float[] receive(float []buff, int source, JGrid grid, int tag) throws Exception {
		JPeerAgent peer = JPeerServer.receiveCall(tag,source);
		int s = Integer.parseInt(peer.getData());
		float []buffs = new float[s];
		for(int i=0;i<s;i++)
			buffs[i] = Float.parseFloat(peer.getData());
		peer.close();
		return buffs;
    }

    public double receive(double buff, int source, JGrid grid, int tag) throws Exception {
		JPeerAgent peer = JPeerServer.receiveCall(tag,source);
		buff = Double.parseDouble(peer.getData());
		peer.close();
		return buff;
    }

    public double[] receive(double []buff, int source, JGrid grid, int tag) throws Exception {
		JPeerAgent peer = JPeerServer.receiveCall(tag,source);
		int s = Integer.parseInt(peer.getData());
		double []buffs = new double[s];
		for(int i=0;i<s;i++)
			buffs[i] = Double.parseDouble(peer.getData());
		peer.close();
		return buffs;
    }

    public char receive(char buff, int source, JGrid grid, int tag) throws Exception {
		JPeerAgent peer = JPeerServer.receiveCall(tag,source);
		buff = (peer.getData()).charAt(0);
		peer.close();
		return buff;
    }

    public char[] receive(char []buff, int source, JGrid grid, int tag) throws Exception {
		JPeerAgent peer = JPeerServer.receiveCall(tag,source);
		int s = Integer.parseInt(peer.getData());
		char []buffs = new char[s];
		for(int i=0;i<s;i++)
			buffs[i] = (peer.getData()).charAt(0);
		peer.close();
		return buffs;
    }

    public boolean receive(boolean buff, int source, JGrid grid, int tag) throws Exception {
    	JPeerAgent peer = JPeerServer.receiveCall(tag,source);
		buff = Boolean.parseBoolean(peer.getData());
		peer.close();
		return buff;
    }

    public boolean[] receive(boolean []buff, int source, JGrid grid, int tag) throws Exception {
		JPeerAgent peer = JPeerServer.receiveCall(tag,source);
		int s = Integer.parseInt(peer.getData());
		boolean []buffs = new boolean[s];
		for(int i=0;i<s;i++)
			buffs[i] = Boolean.parseBoolean(peer.getData());
		peer.close();
		return buffs;
    }

    public long receive(long buff, int source, JGrid grid, int tag) throws Exception {
    	JPeerAgent peer = JPeerServer.receiveCall(tag,source);
		buff = Long.parseLong(peer.getData());
		peer.close();
		return buff;
    }

    public long[] receive(long []buff, int source, JGrid grid, int tag) throws Exception {
		JPeerAgent peer = JPeerServer.receiveCall(tag,source);
		int s = Integer.parseInt(peer.getData());
		long []buffs = new long[s];
		for(int i=0;i<s;i++)
			buffs[i] = Long.parseLong(peer.getData());
		peer.close();
		return buffs;
    }

    public short receive(short buff, int source, JGrid grid, int tag) throws Exception {
    	JPeerAgent peer = JPeerServer.receiveCall(tag,source);
		buff = Short.parseShort(peer.getData());
		peer.close();
		return buff;
    }

    public short[] receive(short []buff, int source, JGrid grid, int tag) throws Exception {
		JPeerAgent peer = JPeerServer.receiveCall(tag,source);
		int s = Integer.parseInt(peer.getData());
		short []buffs = new short[s];
		for(int i=0;i<s;i++)
			buffs[i] = Short.parseShort(peer.getData());
		peer.close();
		return buffs;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -