QxTransaction.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /****************************************************************************
  2. **
  3. ** https://www.qxorm.com/
  4. ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
  5. **
  6. ** This file is part of the QxOrm library
  7. **
  8. ** This software is provided 'as-is', without any express or implied
  9. ** warranty. In no event will the authors be held liable for any
  10. ** damages arising from the use of this software
  11. **
  12. ** Commercial Usage
  13. ** Licensees holding valid commercial QxOrm licenses may use this file in
  14. ** accordance with the commercial license agreement provided with the
  15. ** Software or, alternatively, in accordance with the terms contained in
  16. ** a written agreement between you and Lionel Marty
  17. **
  18. ** GNU General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU
  20. ** General Public License version 3.0 as published by the Free Software
  21. ** Foundation and appearing in the file 'license.gpl3.txt' included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU General Public License version 3.0 requirements will be
  24. ** met : http://www.gnu.org/copyleft/gpl.html
  25. **
  26. ** If you are unsure which license is appropriate for your use, or
  27. ** if you have questions regarding the use of this file, please contact :
  28. ** contact@qxorm.com
  29. **
  30. ****************************************************************************/
  31. #ifdef Q_MOC_RUN
  32. #include <QxCommon/QxConfig.h> // Need to include this file for the 'moc' process
  33. #endif // Q_MOC_RUN
  34. #ifdef _QX_ENABLE_QT_NETWORK
  35. #ifndef _QX_SERVICE_TRANSACTION_H_
  36. #define _QX_SERVICE_TRANSACTION_H_
  37. #ifdef _MSC_VER
  38. #pragma once
  39. #endif
  40. /*!
  41. * \file QxTransaction.h
  42. * \author Lionel Marty
  43. * \ingroup QxService
  44. * \brief Transaction of QxService module (contains request from client and reply from server)
  45. */
  46. #ifdef _QX_NO_PRECOMPILED_HEADER
  47. #ifndef Q_MOC_RUN
  48. #include <QxPrecompiled.h> // Need to include precompiled header for the generated moc file
  49. #endif // Q_MOC_RUN
  50. #endif // _QX_NO_PRECOMPILED_HEADER
  51. #include <QtCore/qdatastream.h>
  52. #ifndef _QX_NO_JSON
  53. #include <QtCore/qjsonvalue.h>
  54. #endif // _QX_NO_JSON
  55. #include <QtNetwork/qtcpsocket.h>
  56. #ifndef QT_NO_SSL
  57. #include <QtNetwork/qsslsocket.h>
  58. #include <QtNetwork/qsslconfiguration.h>
  59. #include <QtNetwork/qsslcertificate.h>
  60. #include <QtNetwork/qsslerror.h>
  61. #include <QtNetwork/qsslkey.h>
  62. #endif // QT_NO_SSL
  63. #ifndef Q_MOC_RUN
  64. #include <QxCommon/QxBool.h>
  65. #include <QxRegister/QxRegisterInternalHelper.h>
  66. #include <QxService/IxService.h>
  67. #include <QxService/IxParameter.h>
  68. #include <QxConvert/QxConvert.h>
  69. #endif // Q_MOC_RUN
  70. namespace qx {
  71. namespace service {
  72. class QxTransaction;
  73. } // namespace service
  74. } // namespace qx
  75. QX_DLL_EXPORT QDataStream & operator<< (QDataStream & stream, const qx::service::QxTransaction & t) QX_USED;
  76. QX_DLL_EXPORT QDataStream & operator>> (QDataStream & stream, qx::service::QxTransaction & t) QX_USED;
  77. #ifndef _QX_NO_JSON
  78. namespace qx {
  79. namespace cvt {
  80. namespace detail {
  81. template <> struct QxConvert_ToJson< qx::service::QxTransaction >;
  82. template <> struct QxConvert_FromJson< qx::service::QxTransaction >;
  83. QX_DLL_EXPORT QJsonValue QxConvert_ToJson_Helper(const qx::service::QxTransaction & t, const QString & format) QX_USED;
  84. QX_DLL_EXPORT qx_bool QxConvert_FromJson_Helper(const QJsonValue & j, qx::service::QxTransaction & t, const QString & format) QX_USED;
  85. } // namespace detail
  86. } // namespace cvt
  87. } // namespace qx
  88. #endif // _QX_NO_JSON
  89. namespace qx {
  90. namespace service {
  91. /*!
  92. * \ingroup QxService
  93. * \brief qx::service::QxTransaction : transaction of QxService module (contains request from client and reply from server)
  94. *
  95. * <a href="https://www.qxorm.com/qxorm_en/tutorial_2.html" target="_blank">Click here to access to a tutorial to explain how to work with QxService module.</a>
  96. */
  97. class QX_DLL_EXPORT QxTransaction : public QObject
  98. {
  99. Q_OBJECT
  100. QX_REGISTER_FRIEND_CLASS(qx::service::QxTransaction)
  101. friend QX_DLL_EXPORT QDataStream & ::operator<< (QDataStream & stream, const qx::service::QxTransaction & t);
  102. friend QX_DLL_EXPORT QDataStream & ::operator>> (QDataStream & stream, qx::service::QxTransaction & t);
  103. #ifndef _QX_NO_JSON
  104. friend struct qx::cvt::detail::QxConvert_ToJson< qx::service::QxTransaction >;
  105. friend struct qx::cvt::detail::QxConvert_FromJson< qx::service::QxTransaction >;
  106. friend QX_DLL_EXPORT QJsonValue qx::cvt::detail::QxConvert_ToJson_Helper(const qx::service::QxTransaction & t, const QString & format);
  107. friend QX_DLL_EXPORT qx_bool qx::cvt::detail::QxConvert_FromJson_Helper(const QJsonValue & j, qx::service::QxTransaction & t, const QString & format);
  108. #endif // _QX_NO_JSON
  109. public:
  110. enum connection_status { conn_none, conn_keep_alive, conn_close };
  111. protected:
  112. QString m_sTransactionId; //!< Transaction id (GUID)
  113. quint32 m_uiInputTransactionSize; //!< Input transaction size
  114. quint32 m_uiOutputTransactionSize; //!< Output transaction size
  115. QDateTime m_dtTransactionBegin; //!< Date-time transaction begin
  116. QDateTime m_dtTransactionRequestSent; //!< Date-time transaction request sent
  117. QDateTime m_dtTransactionRequestReceived; //!< Date-time transaction request received
  118. QDateTime m_dtTransactionReplySent; //!< Date-time transaction reply sent
  119. QDateTime m_dtTransactionReplyReceived; //!< Date-time transaction reply received
  120. QDateTime m_dtTransactionEnd; //!< Date-time transaction end
  121. QString m_sIpSource; //!< Ip address source (request)
  122. QString m_sIpTarget; //!< Ip address target (reply)
  123. long m_lPortSource; //!< Port number source (request)
  124. long m_lPortTarget; //!< Port number target (reply)
  125. QString m_sServiceName; //!< Service name to create using 'QxFactory'
  126. QString m_sServiceMethod; //!< Service method to call to execute transaction
  127. qx_bool m_bMessageReturn; //!< Message return to indicate if an error occured
  128. IxParameter_ptr m_pInputParameter; //!< List of input parameters (request)
  129. IxParameter_ptr m_pOutputParameter; //!< List of output parameters (reply)
  130. IxService_ptr m_pServiceInstance; //!< Service instance created by 'm_sServiceName' property
  131. connection_status m_eForceConnectionStatus; //!< Sometimes we have to force connection status
  132. public:
  133. QxTransaction() : QObject(), m_uiInputTransactionSize(0), m_uiOutputTransactionSize(0), m_lPortSource(0), m_lPortTarget(0), m_eForceConnectionStatus(conn_none) { ; }
  134. virtual ~QxTransaction() { ; }
  135. virtual void clear();
  136. QString getTransactionId() const { return m_sTransactionId; }
  137. quint32 getInputTransactionSize() const { return m_uiInputTransactionSize; }
  138. quint32 getOutputTransactionSize() const { return m_uiOutputTransactionSize; }
  139. QDateTime getTransactionBegin() const { return m_dtTransactionBegin; }
  140. QDateTime getTransactionRequestSent() const { return m_dtTransactionRequestSent; }
  141. QDateTime getTransactionRequestReceived() const { return m_dtTransactionRequestReceived; }
  142. QDateTime getTransactionReplySent() const { return m_dtTransactionReplySent; }
  143. QDateTime getTransactionReplyReceived() const { return m_dtTransactionReplyReceived; }
  144. QDateTime getTransactionEnd() const { return m_dtTransactionEnd; }
  145. QString getIpSource() const { return m_sIpSource; }
  146. QString getIpTarget() const { return m_sIpTarget; }
  147. long getPortSource() const { return m_lPortSource; }
  148. long getPortTarget() const { return m_lPortTarget; }
  149. QString getServiceName() const { return m_sServiceName; }
  150. QString getServiceMethod() const { return m_sServiceMethod; }
  151. qx_bool getMessageReturn() const { return m_bMessageReturn; }
  152. IxParameter_ptr getInputParameter() const { return m_pInputParameter; }
  153. IxParameter_ptr getOutputParameter() const { return m_pOutputParameter; }
  154. connection_status getForceConnectionStatus() const { return m_eForceConnectionStatus; }
  155. void setTransactionId(const QString & s) { m_sTransactionId = s; }
  156. void setInputTransactionSize(quint32 ui) { m_uiInputTransactionSize = ui; }
  157. void setOutputTransactionSize(quint32 ui) { m_uiOutputTransactionSize = ui; }
  158. void setTransactionBegin(const QDateTime & dt) { m_dtTransactionBegin = dt; }
  159. void setTransactionRequestSent(const QDateTime & dt) { m_dtTransactionRequestSent = dt; }
  160. void setTransactionRequestReceived(const QDateTime & dt) { m_dtTransactionRequestReceived = dt; }
  161. void setTransactionReplySent(const QDateTime & dt) { m_dtTransactionReplySent = dt; }
  162. void setTransactionReplyReceived(const QDateTime & dt) { m_dtTransactionReplyReceived = dt; }
  163. void setTransactionEnd(const QDateTime & dt) { m_dtTransactionEnd = dt; }
  164. void setIpSource(const QString & s) { m_sIpSource = s; }
  165. void setIpTarget(const QString & s) { m_sIpTarget = s; }
  166. void setPortSource(long l) { m_lPortSource = l; }
  167. void setPortTarget(long l) { m_lPortTarget = l; }
  168. void setServiceName(const QString & s) { m_sServiceName = s; }
  169. void setServiceMethod(const QString & s) { m_sServiceMethod = s; }
  170. void setMessageReturn(const qx_bool & b) { m_bMessageReturn = b; }
  171. void setInputParameter(IxParameter_ptr p) { m_pInputParameter = p; }
  172. void setOutputParameter(IxParameter_ptr p) { m_pOutputParameter = p; }
  173. void setForceConnectionStatus(connection_status e) { m_eForceConnectionStatus = e; }
  174. virtual void executeServer();
  175. virtual qx_bool writeSocketServer(QTcpSocket & socket);
  176. virtual qx_bool readSocketServer(QTcpSocket & socket);
  177. virtual void executeClient(IxService * pService, const QString & sMethod);
  178. virtual qx_bool writeSocketClient(QTcpSocket & socket);
  179. virtual qx_bool readSocketClient(QTcpSocket & socket);
  180. QString getInfos() const;
  181. protected:
  182. #ifndef QT_NO_SSL
  183. QSslSocket * initSocketSSL();
  184. bool checkSocketSSLEncrypted(QTcpSocket * socket);
  185. #endif // QT_NO_SSL
  186. Q_SIGNALS:
  187. void onCustomRequestHandler();
  188. };
  189. typedef std::shared_ptr<QxTransaction> QxTransaction_ptr;
  190. QX_DLL_EXPORT void execute_client(IxService * pService, const QString & sMethod);
  191. } // namespace service
  192. } // namespace qx
  193. QX_REGISTER_INTERNAL_HELPER_HPP(QX_DLL_EXPORT, qx::service::QxTransaction, 0)
  194. #endif // _QX_SERVICE_TRANSACTION_H_
  195. #endif // _QX_ENABLE_QT_NETWORK