Package TCM_MPI.SenderClasses.Files

Source Code of TCM_MPI.SenderClasses.Files.AbstractProcessSenderFile

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package TCM_MPI.SenderClasses.Files;

import TCM_MPI.CLS.CONST;
import TCM_MPI.CLS.TcmByteData;
import TCM_MPI.SenderClasses.ItemsProcess;
import java.nio.ByteBuffer;

/**
*
* @author dev
*/
public abstract class AbstractProcessSenderFile implements Runnable{
   
    protected ItemsProcess _PROC;
   
    abstract int NumbersPerPackage();
    abstract int TotalPackages();
    abstract void InsertNumber(ByteBuffer BFF,long Index);
    abstract boolean StopInsert(long Index);
   
    @Override
    public void run() {   
       
        //  CALCULA O NUMERO TOTAL DE PACOTES
        int TOTAL_PACOTES = TotalPackages();
        int NUMEROS_POR_PACOTE = NumbersPerPackage();
        //  NUMERO DE SEQUENCIA
        long SEQUENCE_NUMBER = System.currentTimeMillis() - Thread.currentThread().getId();
        long INDEX = 0;
       
        try{
           
            ByteBuffer HEADER_FILE = ByteBuffer.allocate(CONST.BUFFER_SIZE);
            String TRIM_NAME = this._PROC._FILE.getName().trim();
            byte BYTES_FILE_NAME[] = TRIM_NAME.getBytes();

            HEADER_FILE.putInt(this._PROC._PROC_ID);
            HEADER_FILE.putInt(this._PROC._DEST_ID);
            HEADER_FILE.putInt(this._PROC._TYPE_ID);
            HEADER_FILE.putInt(0);
            HEADER_FILE.putLong(SEQUENCE_NUMBER);
            HEADER_FILE.putInt(TOTAL_PACOTES);
            HEADER_FILE.putInt(0);

            HEADER_FILE.putLong(this._PROC._FILE.length());
            HEADER_FILE.put(BYTES_FILE_NAME);
           
            TcmByteData HEADER_DATA = new TcmByteData();
            int POS = HEADER_FILE.position();
            HEADER_DATA.addRange(HEADER_FILE.array());
            HEADER_DATA.removeChunk(POS, HEADER_DATA.getsize() - POS);
            //  ENVIA A INFORMAÇÃO
            this._PROC._SEND_QUEUE.put(HEADER_DATA);
           
        }catch(Exception ex){           
        }
       
        for(int PACOTE_ATUAL = 1 ; PACOTE_ATUAL <= TOTAL_PACOTES; PACOTE_ATUAL++){
            boolean CONTINUE = false;
            //  CRIA O BUFFER
            ByteBuffer NUMS = ByteBuffer.allocate(CONST.BUFFER_SIZE);
           
            //  MONTA O CABEÇALHO DO PACOTE

            //  ID DO REMETENTE
            NUMS.putInt(this._PROC._PROC_ID);
            //  ID DO DESTINATARIO
            NUMS.putInt(this._PROC._DEST_ID);
            //  TIPO DE DADOS DE ENVIO : ????
            NUMS.putInt(this._PROC._TYPE_ID);
            //  TOTAL DE NUMEROS ENVADOS
            NUMS.putInt(this._PROC._NUMBER_TO_SEND);
            //  NUMERO DA SEQUÊNCA DO PACOTE
            NUMS.putLong(SEQUENCE_NUMBER);
            //  TOTAL DE PACOTES
            NUMS.putInt(TOTAL_PACOTES);
            //  NUMERO DO PACOTE ATUAL
            NUMS.putInt(PACOTE_ATUAL);
            //  POSICAO ATUAL DE LEITURA NO ARQUIVO
            NUMS.putLong(INDEX);
            //  EMQUANTO NÃO ULTRAPASSAR O LIMITE DE ITEMS POR PACOTE
            for(int ELEMENTO = 0 ; ELEMENTO < NUMEROS_POR_PACOTE ; ELEMENTO++){
                //  ADICIONA O NUMERO AO PACOTE
                InsertNumber(NUMS, INDEX);
                //  INCREMENTA O INDICE
                INDEX++;
                //  SE O INDICE ALCANÇOU O TOTAL SAI DOS DOIS LAÇOS
                if(StopInsert(INDEX)){
                    CONTINUE = true;
                    break;
                }
            }

            //  PEGA A POSIÇÃO DO BUFFER
            int BYTEBUFFER_POS = NUMS.position();
            //  OBJETO DE ENVIO
            TcmByteData CKX_BYTE = new TcmByteData();
            // INSERI OS BYTES NO OBJETO DE ENVIO
            CKX_BYTE.addRange(NUMS.array());
            // LIBERA O BUFFER
            NUMS = null;

            //  REMOVE OS ESPAÇOS DESNECESSARIOS DO PACOTE
            CKX_BYTE.removeChunk(BYTEBUFFER_POS, CKX_BYTE.getsize() - BYTEBUFFER_POS);
           
            try {
                //  ENVIA A INFORMAÇÃO
                this._PROC._SEND_QUEUE.put(CKX_BYTE);
            } catch (Exception ex) {
            }
           
            //  LIBERA O OBJETO DE ENVIO
            CKX_BYTE = null;

            //  SAI DO LAÇO
            if(CONTINUE){
                break;
            }
        }
       
        //  LIBERA O OBJETO ITEM
        this._PROC = null;
       
    }
   
}
TOP

Related Classes of TCM_MPI.SenderClasses.Files.AbstractProcessSenderFile

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.