serviceWindowGenerationFromText.cpp
5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "serviceWindowGenerationFromText.h"
ServiceWindowGenerationFromText::ServiceWindowGenerationFromText(char* pathFile, int sublanguage, int transp, char* id, int mode) {
this->path_input = pathFile;
this->transparency = transp;
this->sub_language = sublanguage;
this->user_id = id;
this->exec_mode = mode;
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;
PRINTL(util::_DEBUG, "Service Text Done!\n");
}
ServiceWindowGenerationFromText::~ServiceWindowGenerationFromText() {
if (tradutor) delete tradutor;
if (renderer) delete renderer;
if (extratorTXT)delete extratorTXT;
if (extrator_factory) delete extrator_factory;
PRINTL(util::_DEBUG, "Service Text finalized!\n");
}
void ServiceWindowGenerationFromText::setPathContents() {
switch(exec_mode) {
case DEVELOPER:
{
char* vStorage;
vStorage = getenv("VLSTORAGE");
if(vStorage != NULL){
this->path_contents = vStorage;
PRINTL(util::_DEBUG, "Path definido pelo desenvolvedor:\n%s\n", path_contents);
}else{
this->path_contents = (char*) PATH_DEVEL_CONTENTS;
}
}break;
case PRODUCTION:
{
ifstream conf_file(PATH_CONF_FILE, ifstream::binary);
parsingSuccessful = reader.parse(conf_file, root);
if(parsingSuccessful) {
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());
}else{
conf_file.close();
Logging::instance()->writeLog("Erro com a leitura do arquivo params.json");
throw new RuntimeException("Fail to parsing params.json");
}
conf_file.close();
}break;
case TESTER:
{
this->path_contents = (char*) PATH_TESTER_CONTENTS;
}break;
default:
throw ServiceException("Invalid execution mode!");
}
}
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->initialize();
}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 text(reinterpret_cast<char*>(line));
locale loc;
string glosa = "";
for (string::size_type i=0; i< text.length(); ++i) {
glosa += std::toupper(text[i], loc);
}
notifyRenderer(glosa);
}
}
void ServiceWindowGenerationFromText::notifyTranslation(char* glosa) {
string sGlosa(glosa);
notifyRenderer(sGlosa);
}
void ServiceWindowGenerationFromText::notifyRenderer(string glosa) {
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) {
PRINTL(util::_DEBUG, "Service Text recebeu: %d linhas.\n", line_size);
try{
setSizeOfSubtitles(line_size);
}catch(ServiceException &ex) {
finish = true;
}
}
bool ServiceWindowGenerationFromText::isRunning() {
return this->running;
}
bool ServiceWindowGenerationFromText::isFinished() {
return this->finish;
}
void ServiceWindowGenerationFromText::initialize() {
PRINTL(util::_DEBUG, "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{
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;
}