PhmsPushSession.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include "PhmsPushSession.h"
  2. #include "PhmsLogger.h"
  3. #include "PhmsRequestHead.h"
  4. #include "PhmsRequestBody.h"
  5. #include "Util.h"
  6. #include "ErrorCode.h"
  7. #include "Poco/Exception.h"
  8. #include "Poco/AutoPtr.h"
  9. #include "Poco/Util/IniFileConfiguration.h"
  10. #include "Poco/DOM/Document.h"
  11. #include "Poco/DOM/Element.h"
  12. #include "Poco/DOM/Text.h"
  13. #include "Poco/DOM/ProcessingInstruction.h"
  14. #include "Poco/DOM/DOMWriter.h"
  15. #include "Poco/XML/XMLWriter.h"
  16. #include "Poco/TextEncoding.h"
  17. #include "Poco/GBKEncoding.h"
  18. #include "Poco/KOI8REncoding.h"
  19. #include "Poco/Base64Encoder.h"
  20. #include <sstream>
  21. #include <fstream>
  22. #include "AllObjectManagerUtil.h"
  23. using Poco::Exception;
  24. using Poco::FileNotFoundException;
  25. using Poco::AutoPtr;
  26. using Poco::Util::IniFileConfiguration;
  27. using Poco::XML::Document;
  28. using Poco::XML::Element;
  29. using Poco::XML::Text;
  30. using Poco::XML::ProcessingInstruction;
  31. using Poco::XML::DOMWriter;
  32. using Poco::XML::XMLWriter;
  33. using Poco::Base64Encoder;
  34. using Poco::GBKEncoding;
  35. using Poco::KOI8REncoding;
  36. using Poco::TextEncoding;
  37. CPhmsPushSession::CPhmsPushSession(void):m_pHttpPushSession(NULL), m_bSsl(false)
  38. {
  39. //获得ssl环境
  40. {
  41. string stringWorkingDir = CUtil::GetCurrentAppDir();
  42. CNetConfig netConfig;
  43. this->GetNetConfig(stringWorkingDir+"PhmsConfig.ini", netConfig);
  44. m_bSsl = netConfig.GetSsl();
  45. }
  46. m_pHttpPushSession = new CHttpPushSession(m_bSsl);
  47. }
  48. CPhmsPushSession::~CPhmsPushSession(void)
  49. {
  50. delete m_pHttpPushSession;
  51. m_pHttpPushSession = NULL;
  52. }
  53. void CPhmsPushSession::abort()
  54. {
  55. Mutex::ScopedLock lock(m_mutexAbortAndSslSwitch);
  56. m_pHttpPushSession->abort();
  57. }
  58. string CPhmsPushSession::GetSessionId()
  59. {
  60. return m_stringSessionId;
  61. }
  62. void CPhmsPushSession::SetSessionId(string stringSessionId)
  63. {
  64. m_stringSessionId = stringSessionId;
  65. }
  66. void CPhmsPushSession::SetPushCallback(pHttpPushCallback pCallback)
  67. {
  68. m_pCallback = pCallback;
  69. }
  70. int CPhmsPushSession::PushMsg(string stringSenderId, string stringCaseId, string stringCaseType, string stringMsgType, string stringMsgDirection, string stringVersion)
  71. {
  72. AutoPtr<Document> pDoc = new Document;
  73. //AutoPtr<ProcessingInstruction> pi = pDoc->createProcessingInstruction("xml", "version='1.0' encoding='GBK'");
  74. AutoPtr<Element> pRoot = pDoc->createElement("request");
  75. AutoPtr<Element> pChildSenderId = pDoc->createElement("senderid");
  76. AutoPtr<Element> pChildCaseId = pDoc->createElement("caseid");
  77. AutoPtr<Element> pChildCaseType = pDoc->createElement("casetype");
  78. AutoPtr<Element> pChildMsgType = pDoc->createElement("msgtype");
  79. AutoPtr<Element> pChildMsgDirection = pDoc->createElement("msgdirection");
  80. AutoPtr<Element> pChildVersion = pDoc->createElement("version");
  81. AutoPtr<Text> pTextSenderId = pDoc->createTextNode(stringSenderId);
  82. AutoPtr<Text> pTextCaseId = pDoc->createTextNode(stringCaseId);
  83. AutoPtr<Text> pTextCaseType = pDoc->createTextNode(stringCaseType);
  84. AutoPtr<Text> pTextMsgType = pDoc->createTextNode(stringMsgType);
  85. AutoPtr<Text> pTextMsgDirection = pDoc->createTextNode(stringMsgDirection);
  86. AutoPtr<Text> pTextVersion = pDoc->createTextNode(stringVersion);
  87. pChildSenderId->appendChild(pTextSenderId);
  88. pChildCaseId->appendChild(pTextCaseId);
  89. pChildCaseType->appendChild(pTextCaseType);
  90. pChildMsgType->appendChild(pTextMsgType);
  91. pChildMsgDirection->appendChild(pTextMsgDirection);
  92. pChildVersion->appendChild(pTextVersion);
  93. pRoot->appendChild(pChildSenderId);
  94. pRoot->appendChild(pChildCaseId);
  95. pRoot->appendChild(pChildCaseType);
  96. pRoot->appendChild(pChildMsgType);
  97. pRoot->appendChild(pChildMsgDirection);
  98. pRoot->appendChild(pChildVersion);
  99. //pDoc->appendChild(pi);
  100. pDoc->appendChild(pRoot);
  101. string stringXmlContent;
  102. int nRet = PHMS_SUCCESSFUL_RESULT;
  103. nRet = this->GetRequestXml(pDoc, stringXmlContent);
  104. if(nRet != PHMS_SUCCESSFUL_RESULT)
  105. {
  106. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  107. return nRet;
  108. }
  109. CPhmsRequestHead phmsRequestHead("10", "1031", m_stringSessionId, "11");
  110. CPhmsRequestBody phmsRequestBody(stringXmlContent);
  111. CPhmsRequest phmsRequest(phmsRequestHead, phmsRequestBody, "");
  112. phmsRequest.GenerateAndSetSign();
  113. CPhmsResponse phmsResponse;
  114. nRet = ExecutePhmsPushSession(phmsRequest);
  115. if(nRet != PHMS_SUCCESSFUL_RESULT)
  116. {
  117. //写日志
  118. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  119. return nRet;
  120. }
  121. return PHMS_SUCCESSFUL_RESULT;
  122. }
  123. int CPhmsPushSession::ExecutePhmsPushSession(CPhmsRequest& phmsRequest)
  124. {
  125. CNetConfig netConfig;
  126. int nRet = PHMS_SUCCESSFUL_RESULT;
  127. //设置网络环境
  128. try
  129. {
  130. string stringWorkingDir = CUtil::GetCurrentAppDir();
  131. nRet = this->GetNetConfig(stringWorkingDir+"PhmsConfig.ini", netConfig);
  132. if(nRet != PHMS_SUCCESSFUL_RESULT)
  133. {
  134. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  135. return nRet;
  136. }
  137. Mutex::ScopedLock lock(m_mutexAbortAndSslSwitch);
  138. if(m_bSsl != netConfig.GetSsl())
  139. {
  140. delete m_pHttpPushSession;
  141. m_pHttpPushSession = new CHttpPushSession(netConfig.GetSsl());
  142. m_bSsl = netConfig.GetSsl();
  143. }
  144. }
  145. catch(FileNotFoundException& e)
  146. {
  147. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  148. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_NO_NET_CONFIG_FILE), __FUNCTION__, __FILE__, __LINE__);
  149. return COMMON_NO_NET_CONFIG_FILE;
  150. }
  151. if(!phmsRequest.ValidataPhmsRequest())
  152. {
  153. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_PHMS_REQUEST_VALIDATE_FAIL), __FUNCTION__, __FILE__, __LINE__);
  154. return COMMON_PHMS_REQUEST_VALIDATE_FAIL;
  155. }
  156. nRet = m_pHttpPushSession->ExecuteHttpPushSession(netConfig, phmsRequest, m_pCallback);
  157. if(nRet != PHMS_SUCCESSFUL_RESULT)
  158. {
  159. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(nRet), __FUNCTION__, __FILE__, __LINE__);
  160. return nRet;
  161. }
  162. return PHMS_SUCCESSFUL_RESULT;
  163. }
  164. int CPhmsPushSession::GetNetConfig(string stringFilePath, CNetConfig& netConfig)
  165. {
  166. try
  167. {
  168. AutoPtr<IniFileConfiguration> iniFile = new IniFileConfiguration(stringFilePath, "gbk");
  169. string stringContent;
  170. unsigned short nContent;
  171. istringstream iStringStream;
  172. string stringPriorIp = CAllObjectManagerUtil::GetPriorIp();
  173. if(stringPriorIp.size() != 0)
  174. {
  175. stringContent = stringPriorIp;
  176. }
  177. else
  178. {
  179. if(iniFile->has("NET.IP"))
  180. {
  181. stringContent = iniFile->getString("NET.IP", "");
  182. if(stringContent.size() == 0)
  183. {
  184. stringContent = iniFile->getString("NET.SERVER_NAME", "data2.contec365.com");
  185. if(stringContent.size() == 0)
  186. {
  187. stringContent = "data2.contec365.com";
  188. }
  189. }
  190. }
  191. else
  192. {
  193. stringContent = iniFile->getString("NET.SERVER_NAME", "data2.contec365.com");
  194. if(stringContent.size() == 0)
  195. {
  196. stringContent = "data2.contec365.com";
  197. }
  198. }
  199. }
  200. netConfig.SetAddr(stringContent);
  201. stringContent = iniFile->getString("NET.SERVER_NAME", "data2.contec365.com");
  202. if(stringContent.size() == 0)
  203. {
  204. stringContent = "data2.contec365.com";
  205. }
  206. netConfig.SetHost(stringContent);
  207. stringContent = iniFile->getString("NET.PORT", "80");
  208. iStringStream.str(stringContent);
  209. iStringStream>>nContent;
  210. netConfig.SetPort(nContent);
  211. stringContent = iniFile->getString("NET.SSL", "0");
  212. if(stringContent == "1")
  213. {
  214. netConfig.SetSsl(true);
  215. }
  216. else
  217. {
  218. netConfig.SetSsl(false);
  219. }
  220. //推送暂时只有一个地址
  221. stringContent = iniFile->getString("NET.HTTP_PUSH", "/submsg");
  222. netConfig.SetUrl(stringContent);
  223. stringContent = iniFile->getString("GENERAL.NETWORK", "0");
  224. iStringStream.clear();
  225. iStringStream.str(stringContent);
  226. iStringStream>>nContent;
  227. switch (nContent)
  228. {
  229. case 0:
  230. netConfig.SetProxyType(NO_PROXY);
  231. break;
  232. case 1:
  233. netConfig.SetProxyType(HTTP_PROXY);
  234. break;
  235. case 2:
  236. netConfig.SetProxyType(SOCK_PROXY);
  237. break;
  238. default:
  239. netConfig.SetProxyType(NO_PROXY);
  240. break;
  241. }
  242. stringContent = iniFile->getString("PROXY.IP", "");
  243. netConfig.SetProxyAddr(stringContent);
  244. stringContent = iniFile->getString("PROXY.PORT", "");
  245. iStringStream.clear();
  246. iStringStream.str(stringContent);
  247. iStringStream>>nContent;
  248. netConfig.SetProxyPort(nContent);
  249. stringContent = iniFile->getString("PROXY.USERNAME", "");
  250. netConfig.SetProxyUsername(stringContent);
  251. stringContent = iniFile->getString("PROXY.PASSWORD", "");
  252. netConfig.SetProxyPassword(stringContent);
  253. }
  254. catch(FileNotFoundException& e)
  255. {
  256. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  257. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_NO_NET_CONFIG_FILE), __FUNCTION__, __FILE__, __LINE__);
  258. throw;
  259. }
  260. return PHMS_SUCCESSFUL_RESULT;
  261. }
  262. int CPhmsPushSession::GetRequestXml(AutoPtr<Document> pDoc, string& stringXmlContent)
  263. {
  264. string stringWorkingDir = CUtil::GetCurrentAppDir();
  265. string stringIniFilePath = stringWorkingDir+"PhmsConfig.ini";
  266. AutoPtr<IniFileConfiguration> iniFile = new IniFileConfiguration(stringIniFilePath, "gbk");
  267. string stringEncoding = iniFile->getString("OTHER.Encoding", "GBK");
  268. DOMWriter domWriter;
  269. //ProcessingInstruction和XMLWriter::WRITE_XML_DECLARATION 只用一个就好,都用会写两个头
  270. domWriter.setOptions(XMLWriter::WRITE_XML_DECLARATION | XMLWriter::PRETTY_PRINT);
  271. domWriter.setNewLine(XMLWriter::NEWLINE_CRLF);
  272. domWriter.setIndent(" ");
  273. TextEncoding* pEnvEncoding = TextEncoding::find(stringEncoding);
  274. TextEncoding* pUtf8Encoding = TextEncoding::find("UTF-8");
  275. //domWriter.setEncoding(stringEncoding, *pEnvEncoding);
  276. domWriter.setEncoding("UTF-8", *pUtf8Encoding);
  277. domWriter.setInputEncoding(*pEnvEncoding);
  278. std::ostringstream oStringStream;
  279. Base64Encoder encoderBase64(oStringStream);
  280. encoderBase64.rdbuf()->setLineLength(0);
  281. try
  282. {
  283. domWriter.writeNode(encoderBase64,pDoc);
  284. ostringstream oStringStreamXmlContent;
  285. domWriter.writeNode(oStringStreamXmlContent, pDoc);
  286. //domWriter.writeNode(cout, pDoc);
  287. //cout<<endl<<endl;
  288. }
  289. catch(Exception& e)
  290. {
  291. //写日志
  292. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  293. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_WRITE_XML_ERROR), __FUNCTION__, __FILE__, __LINE__);
  294. return COMMON_WRITE_XML_ERROR;
  295. }
  296. encoderBase64.close();
  297. stringXmlContent = oStringStream.str();
  298. return PHMS_SUCCESSFUL_RESULT;
  299. }