Mixe for Privacy and Anonymity in the Internet
Public Member Functions | Private Attributes | List of all members
CAAccountingBIInterface Class Reference

This class encapsulates the connection to the JPI. More...

#include <CAAccountingBIInterface.hpp>

Collaboration diagram for CAAccountingBIInterface:

Public Member Functions

 CAAccountingBIInterface ()
 private constructor just initializes fields, server configuration is done separately More...
 
 ~CAAccountingBIInterface ()
 private destructor More...
 
SINT32 setPIServerConfiguration (CAXMLBI *pPiServerConfig)
 
SINT32 initBIConnection ()
 Establishes HTTP(s) connection to the BI (JPI) More...
 
SINT32 terminateBIConnection ()
 Terminate HTTP(s) connection to the BI (JPI) More...
 
CAXMLErrorMessagesettle (CAXMLCostConfirmation &cc)
 Send a cost confirmation to the JPI. More...
 
CAXMLErrorMessage ** settleAll (CAXMLCostConfirmation **CCs, UINT32 nrOfCCs, CAXMLErrorMessage **settleException)
 

Private Attributes

CATLSClientSocketm_pSocket
 
CAHttpClientm_phttpClient
 
CASocketAddrINetm_pPiServerAddress
 
CACertificatem_pPiServerCertificate
 

Detailed Description

This class encapsulates the connection to the JPI.

Author
Bastian Voigt

Definition at line 51 of file CAAccountingBIInterface.hpp.

Constructor & Destructor Documentation

◆ CAAccountingBIInterface()

CAAccountingBIInterface::CAAccountingBIInterface ( )

private constructor just initializes fields, server configuration is done separately

Definition at line 44 of file CAAccountingBIInterface.cpp.

45  {
46  //m_pSocket =NULL;
47  //m_pPiInterfaceMutex = new CAMutex();
51  }
CASocketAddrINet * m_pPiServerAddress
Very simple http client.
This class represents a socket address for Internet (IP) connections.

References m_phttpClient, m_pPiServerAddress, and m_pSocket.

◆ ~CAAccountingBIInterface()

CAAccountingBIInterface::~CAAccountingBIInterface ( )

private destructor

Definition at line 56 of file CAAccountingBIInterface.cpp.

57  {
59  if(m_pSocket!=NULL)
60  {
61  delete m_pSocket;
62  m_pSocket = NULL;
63  }
64  if(m_pPiServerAddress!=NULL)
65  {
66  delete m_pPiServerAddress;
67  m_pPiServerAddress = NULL;
68  }
69  if(m_phttpClient!=NULL)
70  {
71  delete m_phttpClient;
72  m_phttpClient = NULL;
73  }
74  /*if(m_pPiInterfaceMutex!=NULL)
75  {
76  delete m_pPiInterfaceMutex;
77  m_pPiInterfaceMutex = NULL;
78  }*/
79  }
SINT32 terminateBIConnection()
Terminate HTTP(s) connection to the BI (JPI)

References m_phttpClient, m_pPiServerAddress, m_pSocket, and terminateBIConnection().

Here is the call graph for this function:

Member Function Documentation

◆ initBIConnection()

SINT32 CAAccountingBIInterface::initBIConnection ( )

Establishes HTTP(s) connection to the BI (JPI)

Definition at line 131 of file CAAccountingBIInterface.cpp.

132 {
133  SINT32 rc;
134  UINT8 buf[64];
135 
136  memset(buf,0,64);
137 
138  //m_pPiInterfaceMutex->lock();
139  if(m_pPiServerAddress == NULL)
140  {
141  //Should never happen
142  #ifdef DEBUG
143  CAMsg::printMsg(LOG_ERR, "CAAccountingBIInterface: no address initialized while trying to connect. Connect aborted.\n");
144  #endif
145  //m_pPiInterfaceMutex->unlock();
146  return E_UNKNOWN;
147  }
148 
149  if(m_pSocket == NULL)
150  {
151  //Should never happen
152  #ifdef DEBUG
153  CAMsg::printMsg(LOG_ERR,"CAAccountingBIInterface: no socket initialized while trying to connect. Creating new one.\n");
154  #endif
156  }
157 
158  if(m_pPiServerCertificate == NULL)
159  {
160  CAMsg::printMsg(LOG_ERR,"CAAccountingBIInterface: no PI server certificate specified. Connect aborted.\n");
161  //m_pPiInterfaceMutex->unlock();
162  return E_UNKNOWN;
163  }
164  // connect
167  if(rc!=E_SUCCESS)
168  {
171  LOG_ERR,
172  "CAAccountingBIInterface: Could not connect to BI at %s:%i. Reason: %i\n",
173  buf, m_pPiServerAddress->getPort(), rc
174  //pBI->getHostName(), pBI->getPortNumber(), rc
175  );
176  m_pSocket->close();
177  //m_pPiInterfaceMutex->unlock();
178  return E_UNKNOWN;
179  }
181  CAMsg::printMsg(LOG_DEBUG,"CAAccountingBIInterface: BI connection to %s:%i established!\n", buf,m_pPiServerAddress->getPort());
183  //m_pPiInterfaceMutex->unlock();
184  return E_SUCCESS;
185 }
#define PI_CONNECT_TIMEOUT
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
SINT32 setSocket(CASocket *pSocket)
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
Definition: CAMsg.cpp:251
SINT32 getHostName(UINT8 *buff, UINT32 len) const
Returns the hostname for this address.
UINT16 getPort() const
Returns the port value of the address.
SINT32 close()
Shuts down the socket.
SINT32 connect(const CASocketAddr &psa, UINT32 msTimeout)
Establishes the actual TCP/IP connection and performs the TLS handshake.
SINT32 setServerCertificate(CACertificate *pCert)
Sets the Certifcate we accept as server identification.
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3

References CATLSClientSocket::close(), CATLSClientSocket::connect(), E_SUCCESS, E_UNKNOWN, CASocketAddrINet::getHostName(), CASocketAddrINet::getPort(), m_phttpClient, m_pPiServerAddress, m_pPiServerCertificate, m_pSocket, PI_CONNECT_TIMEOUT, CAMsg::printMsg(), CATLSClientSocket::setServerCertificate(), and CAHttpClient::setSocket().

Referenced by CAAccountingInstance::__newSettlementTransaction(), and CAAccountingInstance::settlementTransaction().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setPIServerConfiguration()

SINT32 CAAccountingBIInterface::setPIServerConfiguration ( CAXMLBI *  pPiServerConfig)

Definition at line 82 of file CAAccountingBIInterface.cpp.

83 {
84  UINT8 *pPiName = NULL;
85  UINT16 piPort = 0;
86  CACertificate *pPiCert = NULL;
87 
88  //m_pPiInterfaceMutex->lock();
89 
90  if(pPiServerConfig==NULL)
91  {
92  CAMsg::printMsg(LOG_ERR, "CAAccountingBIInterface: could not configure PI interface: no proper configuration given.\n");
93  //m_pPiInterfaceMutex->unlock();
94  return E_UNKNOWN;
95  }
96 
97  pPiName = pPiServerConfig->getHostName();
98  pPiCert = pPiServerConfig->getCertificate();
99  piPort = (UINT16)pPiServerConfig->getPortNumber();
100 
101  if(pPiName==NULL)
102  {
103  CAMsg::printMsg(LOG_ERR, "CAAccountingBIInterface: could not configure PI interface: no PI name specified.\n");
104  //m_pPiInterfaceMutex->unlock();
105  return E_UNKNOWN;
106  }
107  if(pPiCert==NULL)
108  {
109  CAMsg::printMsg(LOG_ERR, "CAAccountingBIInterface: could not configure PI interface: no PI certificate specified.\n");
110  //m_pPiInterfaceMutex->unlock();
111  return E_UNKNOWN;
112  }
113 
114  if(m_pPiServerAddress == NULL)
115  {
116  //Should never happen
117  #ifdef DEBUG
118  CAMsg::printMsg(LOG_ERR,"CAAccountingBIInterface: no server address initialized while trying to configure PI interface.\n");
119  #endif
121  }
122  m_pPiServerAddress->setAddr(pPiName, piPort);
123  m_pPiServerCertificate = pPiCert;
124  //m_pPiInterfaceMutex->unlock();
125  return E_SUCCESS;
126 }
unsigned short UINT16
Definition: basetypedefs.h:133
SINT32 setAddr(const UINT8 *szIP, UINT16 port)
Sets the address to szIP and port.

References E_SUCCESS, E_UNKNOWN, m_pPiServerAddress, m_pPiServerCertificate, CAMsg::printMsg(), and CASocketAddrINet::setAddr().

Referenced by CAAccountingInstance::CAAccountingInstance().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ settle()

CAXMLErrorMessage * CAAccountingBIInterface::settle ( CAXMLCostConfirmation &  cc)

Send a cost confirmation to the JPI.

Send a cost confirmation to the JPI TODO: Error handling.

Definition at line 354 of file CAAccountingBIInterface.cpp.

355 {
356  UINT8 * pStrCC=NULL;
357  UINT8* response=NULL;
358  UINT32 contentLen=0, status=0;
359  SINT32 ret = 0;
360  CAXMLErrorMessage *pErrMsg;
361 
362  //m_pPiInterfaceMutex->lock();
363  pStrCC = cc.dumpToMem(&contentLen);
364  if( pStrCC==NULL || m_phttpClient->sendPostRequest((UINT8*)"/settle", pStrCC,contentLen)!= E_SUCCESS)
365  {
366  delete[] pStrCC;
367  pStrCC = NULL;
368  //m_pPiInterfaceMutex->unlock();
369  return NULL;
370  }
371  delete[] pStrCC;
372  pStrCC = NULL;
373  contentLen=0;
374  status=0;
375 
376  m_pSocket->setNonBlocking(true);
377  ret = m_phttpClient->parseHTTPHeader(&contentLen, &status, 20000);
378  if( ret !=E_SUCCESS || (status!=200) || (contentLen==0))
379  {
380  if(ret == E_TIMEDOUT)
381  {
382  CAMsg::printMsg(LOG_ERR, "CAAccountingBIInterface::settle: timeout while receiving response!\n");
383  }
384  else
385  {
386  CAMsg::printMsg(LOG_ERR, "CAAccountingBIInterface::settle: received bad response from PI!\n");
387  }
388  //m_pPiInterfaceMutex->unlock();
389  return NULL;
390  }
391 #ifdef DEBUG
392  CAMsg::printMsg(LOG_DEBUG, "CAAccountingBIInterface::settle: got response header [Status,content-Lenght]=[%i,%i]!\n",status,contentLen);
393 #endif
394  response = new UINT8[contentLen+1];
395 
396  ret = m_pSocket->receiveFullyT(response, contentLen, 3000);
397  if(ret !=E_SUCCESS)
398  {
399  if(ret == E_TIMEDOUT)
400  {
401  CAMsg::printMsg(LOG_ERR, "CAAccountingBIInterface::settle: timeout while receiving settle message!\n");
402  }
403  else
404  {
405  CAMsg::printMsg(LOG_DEBUG, "CAAccountingBIInterface::settle: error\n");
406  }
407  delete[] response;
408  response = NULL;
409  //m_pPiInterfaceMutex->unlock();
410  return NULL;
411  }
412 #ifdef DEBUG
413  CAMsg::printMsg(LOG_DEBUG, "CAAccountingBIInterface::settle: response body received!\n");
414 #endif
415  response[contentLen]='\0';
416  pErrMsg = new CAXMLErrorMessage(response);
417  delete[] response;
418  response = NULL;
419  //m_pPiInterfaceMutex->unlock();
420  return pErrMsg;
421 }
unsigned int UINT32
Definition: basetypedefs.h:131
SINT32 sendPostRequest(const UINT8 *url, const UINT8 *data, const UINT32 dataLen)
Sends a HTTP POST request to the server.
SINT32 parseHTTPHeader(UINT32 *contentLength, UINT32 *statusCode=NULL, UINT32 msTimeOut=3000)
receives the HTTP header and parses the content length
virtual SINT32 receiveFullyT(UINT8 *buff, UINT32 len, UINT32 msTimeOut)
Trys to receive all bytes.
Definition: CASocket.cpp:677
virtual SINT32 setNonBlocking(bool b)
Definition: CASocket.cpp:947
This class encapsulates an error or success message.
#define E_TIMEDOUT
Definition: errorcodes.hpp:10

References E_SUCCESS, E_TIMEDOUT, m_phttpClient, m_pSocket, CAHttpClient::parseHTTPHeader(), CAMsg::printMsg(), CASocket::receiveFullyT(), CAHttpClient::sendPostRequest(), and CASocket::setNonBlocking().

Referenced by CAAccountingInstance::settlementTransaction().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ settleAll()

CAXMLErrorMessage ** CAAccountingBIInterface::settleAll ( CAXMLCostConfirmation **  CCs,
UINT32  nrOfCCs,
CAXMLErrorMessage **  settleException 
)

Definition at line 210 of file CAAccountingBIInterface.cpp.

212 {
213  XERCES_CPP_NAMESPACE::DOMDocument *allCCsEnvelopeDoc = createDOMDocument();
214  DOMElement *allCCsEnvelope = createDOMElement(allCCsEnvelopeDoc, XML_ELEMENT_CCS);
215  CAXMLErrorMessage **resultMessages = NULL;
216  DOMNodeList *errorMsgList = NULL;
217  UINT8 *allCCsAsString = NULL, *response = NULL;
218  UINT32 contentLength = 0, nrOfErrorMessages = 0, i = 0;
219  UINT32 status = 0;
220  SINT32 transmitStatus = 0, domStatus;
221  bool settleSuccessful = false;
222 
223 
224  if(nrOfCCs <= 0)
225  {
226  return NULL;
227  }
228 
229  allCCsEnvelopeDoc->appendChild(allCCsEnvelope);
230 
231  for (i = 0; i < nrOfCCs; i++)
232  {
233  allCCsEnvelope->appendChild(allCCsEnvelopeDoc->importNode(
234  ccs[i]->getXMLDocument()->getDocumentElement(), true));
235  }
236 
237  allCCsAsString = DOM_Output::dumpToMem(allCCsEnvelopeDoc, &contentLength);
238  transmitStatus = (allCCsAsString != NULL) ?
240  allCCsAsString, contentLength) : E_UNKNOWN;
241 
242  allCCsEnvelopeDoc->release();
243  allCCsEnvelopeDoc = NULL;
244  allCCsEnvelope = NULL;
245 
246  delete [] allCCsAsString;
247  allCCsAsString = NULL;
248 
249  if( transmitStatus != E_SUCCESS )
250  {
251  return NULL;
252  }
253 
254  m_pSocket->setNonBlocking(true);
255  transmitStatus = m_phttpClient->parseHTTPHeader(&contentLength, &status, 20000);
256  if( (transmitStatus != E_SUCCESS) || (status != 200) || (contentLength == 0))
257  {
258  if(transmitStatus == E_TIMEDOUT)
259  {
260  CAMsg::printMsg(LOG_ERR, "CAAccountingBIInterface::settle: timeout while receiving response!\n");
261  }
262  else
263  {
264  CAMsg::printMsg(LOG_ERR, "CAAccountingBIInterface::settle: received bad response from PI: %u!\n", status);
265  }
266  return NULL;
267  }
268 #ifdef DEBUG
269  CAMsg::printMsg(LOG_DEBUG, "CAAccountingBIInterface::settle: got response header [Status,content-Length]=[%i,%i]!\n",
270  status, contentLength);
271 #endif
272  response = new UINT8[contentLength+1];
273 
274  transmitStatus = m_pSocket->receiveFullyT(response, contentLength, 3000);
275  if(transmitStatus !=E_SUCCESS)
276  {
277  if(transmitStatus == E_TIMEDOUT)
278  {
279  CAMsg::printMsg(LOG_ERR, "CAAccountingBIInterface::settle: timeout while receiving settle message!\n");
280  }
281  else
282  {
283  CAMsg::printMsg(LOG_DEBUG, "CAAccountingBIInterface::settle: error\n");
284  }
285  delete [] response;
286  response = NULL;
287  return NULL;
288  }
289 
290 
291 
292  response[contentLength]='\0';
293 #ifdef DEBUG
294  CAMsg::printMsg(LOG_DEBUG, "CAAccountingBIInterface::settle: response body received: %s\n", response);
295 #endif
296 
297  allCCsEnvelopeDoc = parseDOMDocument(response, contentLength);
298  if(allCCsEnvelopeDoc != NULL)
299  {
300  errorMsgList = getElementsByTagName(allCCsEnvelopeDoc->getDocumentElement(),
302  domStatus =
303  getDOMElementAttribute(allCCsEnvelopeDoc->getDocumentElement(), XML_ATTR_SETTLE_SUCCESSFUL, settleSuccessful);
304  if(domStatus != E_SUCCESS)
305  {
306  settleSuccessful = false;
307  }
308 
309  nrOfErrorMessages = errorMsgList->getLength();
310  if(settleSuccessful)
311  {
312  if(nrOfErrorMessages == nrOfCCs)
313  {
314  resultMessages = new CAXMLErrorMessage *[nrOfCCs];
315  for (i = 0; i < nrOfErrorMessages; i++)
316  {
317  resultMessages[i] = new CAXMLErrorMessage((DOMElement *) errorMsgList->item(i));
318  }
319  }
320  else
321  {
322  CAMsg::printMsg(LOG_WARNING, "CAAccountingBIInterface::settle: BUG: number of messages != number of cost confirmations!\n");
323  //This would be a bug.
324  *settleException =
326 
327 
328  }
329  }
330  else
331  {
332  CAMsg::printMsg(LOG_WARNING, "CAAccountingBIInterface::settle: PI exception raised.\n");
333  *settleException = (nrOfErrorMessages > 0) ?
334  new CAXMLErrorMessage((DOMElement *) errorMsgList->item(0)) :
336  }
337  }
338 
339  delete [] response;
340  response = NULL;
341 
342  if(allCCsEnvelopeDoc != NULL)
343  {
344  allCCsEnvelopeDoc->release();
345  allCCsEnvelopeDoc = NULL;
346  }
347 
348  return (settleException != NULL) ? resultMessages : NULL;
349 }
#define XML_ELEMENT_CCS
#define XML_ATTR_SETTLE_SUCCESSFUL
#define POST_CMD_SETTLEALL
XERCES_CPP_NAMESPACE::DOMDocument * parseDOMDocument(const UINT8 *const buff, UINT32 len)
Parses a buffer containing an XML document and returns this document.
Definition: CAUtil.cpp:663
DOMNodeList * getElementsByTagName(DOMElement *pElem, const char *const name)
Definition: CAUtil.cpp:1711
XERCES_CPP_NAMESPACE::DOMDocument * createDOMDocument()
Parses a timestamp in JDBC timestamp escape format (as it comes from the BI) and outputs the value in...
Definition: CAUtil.cpp:1568
DOMElement * createDOMElement(XERCES_CPP_NAMESPACE::DOMDocument *pOwnerDoc, const char *const name)
Creates a new DOMElement with the given name which belongs to the DOMDocument owernDoc.
Definition: CAUtil.cpp:814
SINT32 getDOMElementAttribute(const DOMNode *const elem, const char *attrName, UINT8 *value, UINT32 *len)
Definition: CAUtil.cpp:780
#define XML_ELEMENT_ERROR_MSG
static const UINT32 ERR_INTERNAL_SERVER_ERROR
static SINT32 dumpToMem(const DOMNode *node, UINT8 *buff, UINT32 *size)
Dumps the node and all childs into buff.
Definition: DOM_Output.hpp:161

References createDOMDocument(), createDOMElement(), DOM_Output::dumpToMem(), E_SUCCESS, E_TIMEDOUT, E_UNKNOWN, CAXMLErrorMessage::ERR_INTERNAL_SERVER_ERROR, getDOMElementAttribute(), getElementsByTagName(), m_phttpClient, m_pSocket, parseDOMDocument(), CAHttpClient::parseHTTPHeader(), POST_CMD_SETTLEALL, CAMsg::printMsg(), CASocket::receiveFullyT(), CAHttpClient::sendPostRequest(), CASocket::setNonBlocking(), XML_ATTR_SETTLE_SUCCESSFUL, XML_ELEMENT_CCS, and XML_ELEMENT_ERROR_MSG.

Referenced by CAAccountingInstance::__newSettlementTransaction().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ terminateBIConnection()

SINT32 CAAccountingBIInterface::terminateBIConnection ( )

Terminate HTTP(s) connection to the BI (JPI)

Definition at line 192 of file CAAccountingBIInterface.cpp.

193 {
194  //m_pPiInterfaceMutex->lock();
195  if(m_pSocket == NULL)
196  {
197  //Should never happen
198  #ifdef DEBUG
199  CAMsg::printMsg(LOG_ERR,"CAAccountingBIInterface: no socket initialized while trying to close connection.\n");
200  #endif
201  //m_pPiInterfaceMutex->unlock();
202  return E_UNKNOWN;
203  }
204  m_pSocket->close();
205  //m_pPiInterfaceMutex->unlock();
206  return E_SUCCESS;
207 }

References CATLSClientSocket::close(), E_SUCCESS, E_UNKNOWN, m_pSocket, and CAMsg::printMsg().

Referenced by CAAccountingInstance::__newSettlementTransaction(), CAAccountingInstance::settlementTransaction(), and ~CAAccountingBIInterface().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_phttpClient

CAHttpClient* CAAccountingBIInterface::m_phttpClient
private

◆ m_pPiServerAddress

CASocketAddrINet* CAAccountingBIInterface::m_pPiServerAddress
private

◆ m_pPiServerCertificate

CACertificate* CAAccountingBIInterface::m_pPiServerCertificate
private

Definition at line 92 of file CAAccountingBIInterface.hpp.

Referenced by initBIConnection(), and setPIServerConfiguration().

◆ m_pSocket

CATLSClientSocket* CAAccountingBIInterface::m_pSocket
private

The documentation for this class was generated from the following files: