61
49
void GIL::HL7::ControladorEnvioHl7::Arrancar()
63
wxCriticalSectionLocker locker(m_criticalSection);
64
if (m_pInstance == NULL) {
65
m_pInstance = new GIL::HL7::ControladorEnvioHl7();
66
m_pInstance->Create();
67
GNC::GCS::Threading::SetThreadName( m_pInstance->GetId(), "EnvioHL7");
68
wxThreadError error = m_pInstance->Run();
69
if(error != wxTHREAD_NO_ERROR) {
70
LOG_ERROR("Core/HL7", "Error al arrancar el controlador de envío HL7 error: " << error )
72
LOG_INFO("Core/HL7", "Arrancando controlador de envío Hl7")
75
if(!m_pInstance->IsRunning()) {
76
m_pInstance->Abortar();
52
GNC::GCS::ConfigurationController::Instance()->readBoolGeneral("/GinkgoCore/HCE", "HL7MonitorEnabled", boolValue, true);
55
wxCriticalSectionLocker locker(m_criticalSection);
56
if (m_pInstance == NULL) {
79
57
m_pInstance = new GIL::HL7::ControladorEnvioHl7();
80
58
m_pInstance->Create();
59
GNC::GCS::Threading::SetThreadName( m_pInstance->GetId(), "EnvioHL7");
81
60
wxThreadError error = m_pInstance->Run();
82
61
if(error != wxTHREAD_NO_ERROR) {
83
62
LOG_ERROR("Core/HL7", "Error al arrancar el controlador de envío HL7 error: " << error )
85
64
LOG_INFO("Core/HL7", "Arrancando controlador de envío Hl7")
67
if(!m_pInstance->IsRunning()) {
68
m_pInstance->Abortar();
71
m_pInstance = new GIL::HL7::ControladorEnvioHl7();
72
m_pInstance->Create();
73
wxThreadError error = m_pInstance->Run();
74
if(error != wxTHREAD_NO_ERROR) {
75
LOG_ERROR("Core/HL7", "Error al arrancar el controlador de envío HL7 error: " << error )
77
LOG_INFO("Core/HL7", "Arrancando controlador de envío Hl7")
124
122
void* GIL::HL7::ControladorEnvioHl7::Entry()
127
124
m_Abortado = false;
128
//esperamos a que arranque todo correctamente
135
while(!TestDestroy() && !m_Abortado && !error) {
126
while(!TestDestroy() && !m_Abortado) {
141
127
if(TestDestroy() || m_Abortado){
145
error = !ProcesarMensajes();
131
//esperamos a que arranque todo correctamente
132
m_semaphore.WaitTimeout(5000);
133
TListaMensajes listaMensajes;
135
GIL::HL7::ControladorBBDDHl7::Instance()->GetMensajesHL7(listaMensajes,true);
137
if (listaMensajes.size() > 0) {
139
GADAPI::SendHL7CommandParams* pParams = new GADAPI::SendHL7CommandParams(listaMensajes);
140
GADAPI::SendHL7Command* pCmd = new GADAPI::SendHL7Command(pParams);
141
GNC::GCS::ControladorComandos::Instance()->ProcessAsync("Send HL7", pCmd, this);
147
145
catch (HL7Exception ex)
149
147
std::ostringstream ostr;
150
148
LOG_ERROR("Core/HL7", "Excepción al procesar mensaje:" << ex)
155
//la instancia ha finalizado, no hace falta borrarla
156
LOG_WARN("Core/HL7", "El Controlador de envío Hl7 ha finalizado debido a un error")
158
LOG_DEBUG("Core/HL7", "El Controlador de envío Hl7 ha finalizado correctamente")
163
bool GIL::HL7::ControladorEnvioHl7::ProcesarMensajes() {
164
TListaMensajes listaMensajes;
166
GIL::HL7::ControladorBBDDHl7::Instance()->GetMensajesHL7(listaMensajes,true);
167
for(TListaMensajes::iterator it = listaMensajes.begin(); it!=listaMensajes.end(); it++) {
169
std::ostringstream ostr;
170
LOG_DEBUG("Core/HL7", "Comienza el procesamiento del mensaje "<<(*it).m_id)
174
switch((*it).m_protocolo) {
175
case GIL::HL7::IControladorHL7::TP_MLLP:
176
EnviarMensajeMLLP((*it).m_mensaje,(*it).m_destino,(*it).m_procesarACK,(*it).m_msgControlId);
177
GIL::HL7::ControladorBBDDHl7::Instance()->ActualizarEstadoMensaje((*it).m_id,GIL::HL7::MensajeHL7::TE_Enviado,"");
178
LOG_DEBUG("Core/HL7", "Mensaje enviado correctamente")
181
throw HL7Exception(_Std("Unknown protocol"));
183
} catch(HL7Exception& ex) {
184
LOG_ERROR("Core/HL7", "Error al enviar el mensaje:" << ex)
185
//ha ocurrido un error
187
//al primer error se para
189
GIL::HL7::ControladorBBDDHl7::Instance()->ActualizarEstadoMensaje((*it).m_id,GIL::HL7::MensajeHL7::TE_Error,ex.GetCause().c_str());
193
GIL::HL7::ControladorBBDDHl7::Instance()->ActualizarEstadoMensaje((*it).m_id,GIL::HL7::MensajeHL7::TE_Warning,ex.GetCause().c_str());
198
LOG_DEBUG("Core/HL7", "Error al enviar el mensaje: Error interno");
199
GIL::HL7::ControladorBBDDHl7::Instance()->ActualizarEstadoMensaje((*it).m_id, GIL::HL7::MensajeHL7::TE_Error, "Error interno");
208
/* Envia el mensaje por MLLP */
209
void GIL::HL7::ControladorEnvioHl7::EnviarMensajeMLLP(const std::string& msg, const std::string& url, bool procesarACK, const std::string& msgControlId) const
154
void GIL::HL7::ControladorEnvioHl7::ProcesarEvento(GNC::GCS::Eventos::IEvento *evt)
211
const std::string START_BLOCK = "\x0b";
212
const std::string END_BLOCK = "\x1C";
213
const std::string END_MSG = "\x0D";
216
signal(SIGPIPE, SIG_IGN);
218
EndpointAddrlist::g_default_family = AF_INET;
220
Endpoint::Initialize();
222
Endpoint ep(TCP | CLIENT, url);
225
throw HL7Exception(_Std("Failed to start connection") + ep.m_error_str, "COMM");
228
std::stringstream outputStream;
229
outputStream << START_BLOCK << msg << END_BLOCK << END_MSG;
231
long nbytes = ep.Write(outputStream.str());
232
if (nbytes != (long)outputStream.str().size()) {
233
std::stringstream errmsg;
235
errmsg << _Std("Error sending message:") << ep.m_error_str;
238
errmsg << _Std("Error sending message. It was sent ") << nbytes << _Std(" bytes from a total of ") << outputStream;
240
throw HL7Exception(errmsg.str(), "COMM");
156
GNC::GCS::Eventos::EventoProgresoComando* pEvt = dynamic_cast<GNC::GCS::Eventos::EventoProgresoComando*> (evt);
157
GNC::GCS::IComando* pCmd = pEvt->GetComando();
158
if (pEvt == NULL || pEvt->GetComando() == NULL) {
247
ep.Shutdown(Endpoint::OUTPUT);
249
//std::cout << "Message Control ID is = " << msgControlId << std::endl;
251
outputStream.str("");
253
//std::cout << "Connected to: " << std::string(ep.m_remote) << std::endl;
255
unsigned int msgNum = 0;
257
std::list<std::string> mStrings;
258
std::stringstream inputStream;
264
bool started = false;
267
while( pending && (bytesReaded = ep.Read(-1024, buff))>0 ) {
270
std::string::size_type sb_loc = buff.find(START_BLOCK, 0);
272
if (sb_loc != std::string::npos) {
276
mStrings.push_back(std::string());
280
std::string::size_type eb_loc = buff.find(END_BLOCK, sb_loc+1);
282
if (eb_loc != std::string::npos) {
283
inputStream << buff.substr(sb_loc + 1, eb_loc - 1 - sb_loc);
285
mStrings.back() = inputStream.str();
290
std::string::size_type em_loc = buff.find(END_MSG, eb_loc+1);
291
if (em_loc != std::string::npos) {
295
inputStream << buff.substr(eb_loc+1);
300
inputStream << buff.substr(sb_loc + 1);
306
std::string::size_type eb_loc = buff.find(END_BLOCK, 0);
308
if (eb_loc != std::string::npos) {
309
inputStream << buff.substr(0, eb_loc - 1);
312
mStrings.back() = inputStream.str();
315
std::string::size_type em_loc = buff.find(END_MSG, eb_loc+1);
316
if (em_loc != std::string::npos) {
320
inputStream << buff.substr(eb_loc+1);
328
bool confirmado = false;
329
std::list<std::string>::iterator it;
331
for ( i = 1, it = mStrings.begin(); it != mStrings.end(); i++, it++) {
332
std::string& s = *it;
334
if (i != msgNum || !pending) {
335
//std::cout << "Mensaje :\n" << s << std::endl;
337
GIL::Sacyl::Messages::ACKInterpreter ai(GIL::HL7::Message::FromER7(s));
338
if (msgControlId != ai.GetReferralMessageControlId()) {
339
throw HL7Exception(_Std("Error in the logic of confirmation. The control of the message ID does not match recognized"));
341
if (!ai.IsSuccess()) {
342
std::stringstream errmsg;
343
errmsg << _Std("NACK received:") << ai.ERR.GetErrorCodeDescription();
344
throw HL7Exception(errmsg.str());
348
catch (HL7Exception& e)
350
throw HL7Exception(_Std("Failed delivery confirmation:") + e.GetCause(), "COMM");
356
throw HL7Exception(_Std("Error in the confirmation of delivery: Delivery is not recognized by the remote end"), "COMM");
358
//std::cout << "Total: " << ep.GetTotalBytesReaded() << std::endl;*/
161
if (pEvt->GetTipo() == GNC::GCS::Eventos::EventoProgresoComando::TEP_Finalizado)
163
GADAPI::SendHL7Command* pCmdSend = dynamic_cast< GADAPI::SendHL7Command * > (pCmd);
164
if (pCmdSend != NULL)