/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package TCM_MPIv2.CLS;
import TCM_MPI.CLS.CONST;
import TCM_MPI.Interfaces.TCM_MPI_Interface;
import TCM_MPI_UTILS.Message.Interfaces.InterfaceTcmMpiBasicPackage;
import TCM_MPI_UTILS.Message.TcmMpiBasicPackage;
import TCM_MPI_UTILS.Socket.Interfaces.ISocketTcm;
import TCM_MPI_UTILS.Socket.SocketTcm;
import TCM_MPI_UTILS.TcmMpiTypeUtil;
import TCM_MPIv2.Buffer.BufferInfov2;
import TCM_MPIv2.CheckClasses.CheckDatav2_JvmTcp;
import TCM_MPIv2.SenderClasses.SendDatav2_JvmTcp;
import java.util.concurrent.ArrayBlockingQueue;
/**
*
* @author dev
*/
public class TCM_MPI_TCPv2 implements TCM_MPI_Interface{
private int _PROC_ID;
private ISocketTcm ISOCKET;
private Thread _THREAD_CHECK;
private Thread _THREAD_SEND;
private ArrayBlockingQueue<InterfaceTcmMpiBasicPackage> _SEND_QUEUE;
private BufferInfov2 TCM_BUFFER;
public TCM_MPI_TCPv2(int ID,String SERVER_IP,int POR_SERVER)throws Exception{
this.ISOCKET = new SocketTcm();
while(this.ISOCKET.Connect(SERVER_IP, 12345) != true){
System.out.println("NÃO CONECTADO ...");
Thread.sleep(1000);
}
CheckDatav2_JvmTcp CHECK = new CheckDatav2_JvmTcp(this.ISOCKET,this.TCM_BUFFER);
this._THREAD_CHECK = new Thread(CHECK);
this._THREAD_CHECK.start();
SendDatav2_JvmTcp SENDER = new SendDatav2_JvmTcp(this.ISOCKET, this._SEND_QUEUE);
this._THREAD_SEND = new Thread(SENDER);
this._THREAD_SEND.start();
}
@Override
@SuppressWarnings("SleepWhileInLoop")
public byte[] TCM_RECV_BYTE(int FROM, int N) throws Exception {
while(!this.TCM_BUFFER.IsExistData(FROM,TcmMpiTypeUtil.ID_TYPE_BYTE)){
Thread.sleep(TcmMpiTypeUtil.SLEEP_SEND_TIME);
}
TcmMpiBasicPackage pkg = this.TCM_BUFFER.GetData(FROM,TcmMpiTypeUtil.ID_TYPE_BYTE);
byte[] B = new byte[N];
System.arraycopy(pkg.getBYTES(), 0, B, 0, N);
return B;
}
@Override
public char[] TCM_RECV_CHAR(int FROM, int N) throws Exception {
while(!this.TCM_BUFFER.IsExistData(FROM,TcmMpiTypeUtil.ID_TYPE_CHAR)){
Thread.sleep(TcmMpiTypeUtil.SLEEP_SEND_TIME);
}
TcmMpiBasicPackage pkg = this.TCM_BUFFER.GetData(FROM,TcmMpiTypeUtil.ID_TYPE_CHAR);
char[] B = new char[N];
System.arraycopy(pkg.getCHAR(), 0, B, 0, N);
return B;
}
@Override
public int[] TCM_RECV_INT(int FROM, int N) throws Exception {
while(!this.TCM_BUFFER.IsExistData(FROM,TcmMpiTypeUtil.ID_TYPE_INTEGER)){
Thread.sleep(TcmMpiTypeUtil.SLEEP_SEND_TIME);
}
TcmMpiBasicPackage pkg = this.TCM_BUFFER.GetData(FROM,TcmMpiTypeUtil.ID_TYPE_INTEGER);
int[] B = new int[N];
System.arraycopy(pkg.getINTEGER(), 0, B, 0, N);
return B;
}
@Override
public double[] TCM_RECV_DOUBLE(int FROM, int N) throws Exception {
while(!this.TCM_BUFFER.IsExistData(FROM,TcmMpiTypeUtil.ID_TYPE_DOUBLE)){
Thread.sleep(TcmMpiTypeUtil.SLEEP_SEND_TIME);
}
TcmMpiBasicPackage pkg = this.TCM_BUFFER.GetData(FROM,TcmMpiTypeUtil.ID_TYPE_DOUBLE);
double[] B = new double[N];
System.arraycopy(pkg.getDOUBLE(), 0, B, 0, N);
return B;
}
@Override
public void TCM_SEND_BYTE(byte[] BY, int DEST, int N) throws Exception {
if(BY == null){
throw new NullPointerException("referência de Array nulo");
}
if(N <= 0 || N > BY.length || BY.length == 0){
throw new ArrayIndexOutOfBoundsException("você quer acesar um indice não existente");
}
if(N > CONST.MAX_ARRAY_LEN){
throw new ArrayIndexOutOfBoundsException("você ultrapassou o limite maximo de um array");
}
InterfaceTcmMpiBasicPackage pkg = new TcmMpiBasicPackage();
pkg.setPROC_ID(this._PROC_ID);
pkg.setDEST_ID(DEST);
pkg.setTYPE_ID(TcmMpiTypeUtil.ID_TYPE_BYTE);
pkg.setNUMBER_OF_PACKS(1);
pkg.setNUMBER_TO_SEND(N);
pkg.setPACKAGE_NUMBER(System.currentTimeMillis());
byte[] BS = new byte[N];
System.arraycopy(BY, 0, BS, 0, N);
pkg.setBYTES(BS);
this._SEND_QUEUE.put(pkg);
}
@Override
public void TCM_SEND_CHAR(char[] CHR, int DEST, int N) throws Exception {
if(CHR == null){
throw new NullPointerException("referência de Array nulo");
}
if(N <= 0 || N > CHR.length || CHR.length == 0){
throw new ArrayIndexOutOfBoundsException("você quer acesar um indice não existente");
}
if(N > CONST.MAX_ARRAY_LEN){
throw new ArrayIndexOutOfBoundsException("você ultrapassou o limite maximo de um array");
}
InterfaceTcmMpiBasicPackage pkg = new TcmMpiBasicPackage();
pkg.setPROC_ID(this._PROC_ID);
pkg.setDEST_ID(DEST);
pkg.setTYPE_ID(TcmMpiTypeUtil.ID_TYPE_CHAR);
pkg.setNUMBER_OF_PACKS(1);
pkg.setNUMBER_TO_SEND(N);
pkg.setPACKAGE_NUMBER(System.currentTimeMillis());
char[] C = new char[N];
System.arraycopy(CHR, 0, C, 0, N);
pkg.setCHAR(C);
this._SEND_QUEUE.put(pkg);
}
@Override
public void TCM_SEND_INT(int[] INTS, int DEST, int N) throws Exception {
if(INTS == null){
throw new NullPointerException("referência de Array nulo");
}
if(N <= 0 || N > INTS.length || INTS.length == 0){
throw new ArrayIndexOutOfBoundsException("você quer acesar um indice não existente");
}
if(N > CONST.MAX_ARRAY_LEN){
throw new ArrayIndexOutOfBoundsException("você ultrapassou o limite maximo de um array");
}
InterfaceTcmMpiBasicPackage pkg = new TcmMpiBasicPackage();
pkg.setPROC_ID(this._PROC_ID);
pkg.setDEST_ID(DEST);
pkg.setTYPE_ID(TcmMpiTypeUtil.ID_TYPE_BYTE);
pkg.setNUMBER_OF_PACKS(1);
pkg.setNUMBER_TO_SEND(N);
pkg.setPACKAGE_NUMBER(System.currentTimeMillis());
int[] I = new int[N];
System.arraycopy(INTS, 0, I, 0, N);
pkg.setINTEGER(I);
this._SEND_QUEUE.put(pkg);
}
@Override
public void TCM_SEND_DOUBLE(double[] DBL, int DEST, int N) throws Exception {
if(DBL == null){
throw new NullPointerException("referência de Array nulo");
}
if(N <= 0 || N > DBL.length || DBL.length == 0){
throw new ArrayIndexOutOfBoundsException("você quer acesar um indice não existente");
}
if(N > CONST.MAX_ARRAY_LEN){
throw new ArrayIndexOutOfBoundsException("você ultrapassou o limite maximo de um array");
}
InterfaceTcmMpiBasicPackage pkg = new TcmMpiBasicPackage();
pkg.setPROC_ID(this._PROC_ID);
pkg.setDEST_ID(DEST);
pkg.setTYPE_ID(TcmMpiTypeUtil.ID_TYPE_BYTE);
pkg.setNUMBER_OF_PACKS(1);
pkg.setNUMBER_TO_SEND(N);
pkg.setPACKAGE_NUMBER(System.currentTimeMillis());
double[] D = new double[N];
System.arraycopy(DBL,0, D, 0, N);
pkg.setDOUBLE(DBL);
this._SEND_QUEUE.put(pkg);
}
}