serviceWindowGenerationFromText.cpp 4.35 KB
#include "serviceWindowGenerationFromText.h"

ServiceWindowGenerationFromText::ServiceWindowGenerationFromText(char* pathFile, int sublanguage, int transp, char* id, char* client) {
	this->path_input = pathFile;
	this->transparency = transp;
    this->sub_language = sublanguage;
	this->user_id = id;
	client_type = client;
	running = true;
	finish = false;
	numero_legendas = INT_MAX;
    legendas_enviadas = 0;
	extrator_factory = new ExtratorFactory();
    try{
        setPathContents();
    }catch(RuntimeException ex){
        throw ServiceException(ex.getMessage());
    }
    running = true;
    finish = false;
	DPRINTF("Done!\n");
}

ServiceWindowGenerationFromText::~ServiceWindowGenerationFromText() {
	if (tradutor) delete tradutor;
    if (renderer) delete renderer;
	if (extratorTXT)delete extratorTXT;
	if (extrator_factory) delete extrator_factory;
    DDDPRINTF("Service Text finalized!\n");
}

void ServiceWindowGenerationFromText::setPathContents() {
   if(strcmp(client_type,DEVELOPER) == 0){
        this->path_contents = PATH_DEVEL_CONTENTS;
        //this->path_uploads = PATH_DEVEL_UPLOADS;
    }else if(strcmp(client_type, PRODUCTION) == 0){
        ifstream conf_file(PATH_CONF_FILE, ifstream::binary);
        parsingSuccessful = reader.parse(conf_file, root);
        if(!parsingSuccessful){
            throw new RuntimeException("Fail to parsing param.json");
        }
        string attr = "vlibras_user/";
        attr += root.get("storage", PATH_VBOX_UPLOADS).asString();
        this->path_contents = new char[MAX_SIZE_PATH];
        strcpy(this->path_contents, attr.c_str());
        //this->path_uploads = PATH_VBOX_UPLOADS;
    }else{
        throw ServiceException("Invalid client!");
    }
}

void ServiceWindowGenerationFromText::setPathLibras() {
    string final_path = "";
    path_libras = new char[MAX_SIZE_PATH];
    final_path.append(this->path_contents).append("/").
    append(this->user_id).append(".mp4");
    strcpy(this->path_libras, final_path.c_str());
}

void ServiceWindowGenerationFromText::setSizeOfSubtitles(int sub_size) {
    numero_legendas = sub_size;
    if (legendas_enviadas >= numero_legendas){
        try{
            renderer->finalize();
        }catch(lavidlib::RuntimeException &ex){
            throw ServiceException(ex.getMessage().c_str());
        }
    }
}

void ServiceWindowGenerationFromText::notifyTranslator(unsigned char* text) {
    tradutor->traduz(text);   
}

void ServiceWindowGenerationFromText::notifyLine(unsigned char* line){
    if (sub_language == 1)
        notifyTranslator(line);
    else{
        string glosa(reinterpret_cast<char*>(line));
        notifyRenderer(glosa);
    }
}

void ServiceWindowGenerationFromText::notifyTranslation(char* glosa) {
    string sGlosa(glosa);
    notifyRenderer(sGlosa);
}

void ServiceWindowGenerationFromText::notifyRenderer(string glosa) {
    while(renderer->isSending())
        usleep(10000);
    try{
        renderer->receiveGlosa(glosa, (int64_t) -1);
        legendas_enviadas++;
    }catch(lavidlib::RuntimeException &ex){
        throw ServiceException(ex.getMessage().c_str());
    }
}

void ServiceWindowGenerationFromText::notifyEndOfRenderization() {
    this->running = false;
}

void ServiceWindowGenerationFromText::notifyEnd(int line_size) {
    DPRINTF("Service Text recebeu: %d linhas.\n", line_size);
    setSizeOfSubtitles(line_size);
}

bool ServiceWindowGenerationFromText::isRunning() {
	return this->running;
}

bool ServiceWindowGenerationFromText::isFinished() {
	return this->finish;
}

void ServiceWindowGenerationFromText::initialize() {
	DPRINTF("Service Text Initialize.\n");
    setPathLibras();
    extratorTXT = (ExtratorTXT*) extrator_factory->getExtrator(Extrator::TXT);	    
    extratorTXT->addListener(this);
    extratorTXT->setFilePath(path_input);

    tradutor = new TradutorPortGlosa();
    if (this->sub_language == 1) {
        tradutor->addListener(this);
    }

    renderer = new Renderer(this->path_libras, this->user_id);
    renderer->addListener(this);
    
    try{
        renderer->Start();
  	    extratorTXT->initialize();            
    }catch(ExtratorException ex){
        throw ServiceException(ex.getMessage().c_str());
    }catch(RuntimeException &ex){
        throw ServiceException(ex.getMessage().c_str());
    }
    this->Start();
}

void ServiceWindowGenerationFromText::Run(){
	while(isRunning()){
		usleep(200000);
	}
	finish = true;
}