3
* $Id: sendhl7command.cpp 4099 2011-08-29 11:27:03Z tovar $
6
* Copyright 2008-10 MetaEmotion S.L. All rights reserved.
7
* http://ginkgo-cadx.com
9
* This file is licensed under LGPL v3 license.
10
* See License.txt for details
13
//#define _GINKGO_TRACE
16
#include <wx/thread.h>
17
#include <endpoint/endpoint.h>
18
#include "sendhl7command.h"
19
#include <main/controllers/controladorlog.h>
20
#include <api/imodelohl7.h>
21
#include <api/iguiasmensajeriasacyl.h>
23
#define IDC_SEND_HL7 117
25
// Singleton de persistencia
28
SendHL7CommandParams::SendHL7CommandParams(const std::list<GIL::HL7::MensajeHL7>& messages)
30
this->messages = messages;
32
m_pSemaphore = new wxSemaphore;
35
SendHL7CommandParams::~SendHL7CommandParams()
40
SendHL7Command::SendHL7Command(SendHL7CommandParams* pParams) : IComando(pParams)
42
m_pSendParams = pParams;
45
EsperaA(IDC_SEND_HL7);
48
void SendHL7Command::Execute()
51
for(std::list<GIL::HL7::MensajeHL7>::iterator it = m_pSendParams->messages.begin(); it!=m_pSendParams->messages.end(); it++)
54
while (!sent && !EstaAbortado())
56
float currentProgress = (float)(current-1)/m_pSendParams->messages.size();
58
wxString strTmp = wxString::Format(_("Sending HL7 message %d of %d"), current, m_pSendParams->messages.size());
59
NotificarProgreso(currentProgress, std::string(strTmp.ToUTF8()));
60
LOG_DEBUG("Core/HL7", "Comienza el procesamiento del mensaje "<<(*it).m_id)
64
switch((*it).m_protocolo) {
65
case GIL::HL7::IControladorHL7::TP_MLLP:
66
EnviarMensajeMLLP((*it).m_mensaje,(*it).m_destino,(*it).m_procesarACK,(*it).m_msgControlId);
67
GIL::HL7::ControladorBBDDHl7::Instance()->ActualizarEstadoMensaje((*it).m_id,GIL::HL7::MensajeHL7::TE_Enviado,"");
68
LOG_DEBUG("Core/HL7", _Std("Message successfully sent"))
72
throw GIL::HL7::HL7Exception(_Std("Unknown protocol"));
74
} catch(GIL::HL7::HL7Exception& ex) {
75
LOG_ERROR("Core/HL7", _Std("Error sending message:") << ex)
76
//ha ocurrido un error
78
//al primer error se para
80
GIL::HL7::ControladorBBDDHl7::Instance()->ActualizarEstadoMensaje((*it).m_id,GIL::HL7::MensajeHL7::TE_Error,ex.GetCause().c_str());
83
GIL::HL7::ControladorBBDDHl7::Instance()->ActualizarEstadoMensaje((*it).m_id,GIL::HL7::MensajeHL7::TE_Warning,ex.GetCause().c_str());
85
NotificarProgreso(currentProgress, _Std("Error sending HL7 message see log for more details"));
88
LOG_DEBUG("Core/HL7", _Std("Error sending message:") << " " <<_Std("Unknown error"));
89
GIL::HL7::ControladorBBDDHl7::Instance()->ActualizarEstadoMensaje((*it).m_id, GIL::HL7::MensajeHL7::TE_Error, _Std("Unknown error"));
93
m_pSendParams->m_pSemaphore->WaitTimeout(60000);
100
void SendHL7Command::Update()
102
if (EstaAbortado()) {
108
void SendHL7Command::OnAbort()
110
if (m_pSendParams->m_idLastSock != -1) {
111
Endpoint::ShutdownSocket(m_pSendParams->m_idLastSock, Endpoint::BOTH);
113
m_pSendParams->m_pSemaphore->Post();
118
/* Envia el mensaje por MLLP */
119
void SendHL7Command::EnviarMensajeMLLP(const std::string& msg, const std::string& url, bool procesarACK, const std::string& msgControlId)
121
const std::string START_BLOCK = "\x0b";
122
const std::string END_BLOCK = "\x1C";
123
const std::string END_MSG = "\x0D";
126
signal(SIGPIPE, SIG_IGN);
128
EndpointAddrlist::g_default_family = AF_INET;
130
Endpoint::Initialize();
132
std::cout << "inicializo..." << std::endl;
133
Endpoint ep(TCP | CLIENT, url);
136
throw GIL::HL7::HL7Exception(_Std("Failed to start connection") + ep.m_error_str, "COMM");
139
m_pSendParams->m_idLastSock = ep.m_sockfd;
141
std::stringstream outputStream;
142
outputStream << START_BLOCK << msg << END_BLOCK << END_MSG;
143
long nbytes = ep.Write(outputStream.str());
144
if (nbytes != (long)outputStream.str().size()) {
145
std::stringstream errmsg;
147
errmsg << _Std("Error sending message:") << ep.m_error_str;
150
errmsg << _Std("Error sending message. It was sent ") << nbytes << _Std(" bytes from a total of ") << outputStream;
152
throw GIL::HL7::HL7Exception(errmsg.str(), "COMM");
159
ep.Shutdown(Endpoint::OUTPUT);
161
//std::cout << "Message Control ID is = " << msgControlId << std::endl;
163
outputStream.str("");
165
//std::cout << "Connected to: " << std::string(ep.m_remote) << std::endl;
167
unsigned int msgNum = 0;
169
std::list<std::string> mStrings;
170
std::stringstream inputStream;
176
bool started = false;
179
while( pending && (bytesReaded = ep.Read(-1024, buff))>0 ) {
182
std::string::size_type sb_loc = buff.find(START_BLOCK, 0);
184
if (sb_loc != std::string::npos) {
188
mStrings.push_back(std::string());
192
std::string::size_type eb_loc = buff.find(END_BLOCK, sb_loc+1);
194
if (eb_loc != std::string::npos) {
195
inputStream << buff.substr(sb_loc + 1, eb_loc - 1 - sb_loc);
197
mStrings.back() = inputStream.str();
202
std::string::size_type em_loc = buff.find(END_MSG, eb_loc+1);
203
if (em_loc != std::string::npos) {
207
inputStream << buff.substr(eb_loc+1);
212
inputStream << buff.substr(sb_loc + 1);
218
std::string::size_type eb_loc = buff.find(END_BLOCK, 0);
220
if (eb_loc != std::string::npos) {
221
inputStream << buff.substr(0, eb_loc - 1);
224
mStrings.back() = inputStream.str();
227
std::string::size_type em_loc = buff.find(END_MSG, eb_loc+1);
228
if (em_loc != std::string::npos) {
232
inputStream << buff.substr(eb_loc+1);
240
bool confirmado = false;
241
std::list<std::string>::iterator it;
243
for ( i = 1, it = mStrings.begin(); it != mStrings.end(); i++, it++) {
244
std::string& s = *it;
246
if (i != msgNum || !pending) {
247
//std::cout << "Mensaje :\n" << s << std::endl;
249
GIL::Sacyl::Messages::ACKInterpreter ai(GIL::HL7::Message::FromER7(s));
250
if (msgControlId != ai.GetReferralMessageControlId()) {
251
throw GIL::HL7::HL7Exception(_Std("Error in the logic of confirmation. The control of the message ID does not match recognized"));
253
if (!ai.IsSuccess()) {
254
std::stringstream errmsg;
255
errmsg << _Std("NACK received:") << ai.ERR.GetErrorCodeDescription();
256
throw GIL::HL7::HL7Exception(errmsg.str());
260
catch (GIL::HL7::HL7Exception& e)
262
throw GIL::HL7::HL7Exception(_Std("Failed delivery confirmation:") + e.GetCause(), "COMM");
268
throw GIL::HL7::HL7Exception(_Std("Error in the confirmation of delivery: Delivery is not recognized by the remote end"), "COMM");
270
//std::cout << "Total: " << ep.GetTotalBytesReaded() << std::endl;*/