中间件底层,websocket

RcfEcho.cpp 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #include "RcfEcho.h"
  2. #include <iostream>
  3. #include <sstream>
  4. #include <fstream>
  5. #include <boost\date_time\posix_time\posix_time.hpp>
  6. #include <boost\filesystem.hpp>
  7. #include <boost/algorithm/string.hpp>
  8. #include "CRedis.h"
  9. #include "GlobalVar.h"
  10. std::string GetCurTime()
  11. {
  12. std::string curTime = boost::posix_time::to_iso_extended_string(boost::posix_time::microsec_clock::local_time());
  13. int pos = curTime.find('T');
  14. curTime.replace(pos, 1, std::string(" "));
  15. curTime += " ";
  16. return curTime;
  17. }
  18. static std::string GetCurDate()
  19. {
  20. std::string curTime = boost::posix_time::to_iso_extended_string(boost::posix_time::microsec_clock::local_time());
  21. int pos = curTime.find('T');
  22. curTime = curTime.substr(0, pos);
  23. return curTime;
  24. }
  25. static std::string logPath = "./data/rcf/";
  26. void Log(const RCF::ByteBuffer & byte)
  27. {
  28. std::cout << byte.string() << std::endl;
  29. if (!boost::filesystem::exists(logPath))
  30. boost::filesystem::create_directories(logPath);
  31. static std::string lastFile = logPath + GetCurDate() + "_rcf.txt";
  32. std::string curFile = logPath + GetCurDate() + "_rcf.txt";
  33. static std::ofstream file(lastFile, std::ios::app);
  34. if (curFile != lastFile) // 第二天重新写文件
  35. {
  36. if (file.is_open())
  37. {
  38. file.close();
  39. }
  40. lastFile = curFile;
  41. file.open(lastFile, std::ios::app);
  42. }
  43. if (file.is_open())
  44. {
  45. file << byte.string() << std::endl;
  46. file.flush();
  47. }
  48. }
  49. void print(std::string str)
  50. {
  51. RCF::ByteBuffer byte_buff(str);
  52. auto log = RCF::LogToFunc(std::bind(Log, std::placeholders::_1));
  53. log.write(byte_buff);
  54. }
  55. CRcfEcho::CRcfEcho():m_ctiRunState(false)
  56. {
  57. RCF::enableLogging();
  58. }
  59. bool CRcfEcho::pushAgentList(const int agentId, const std::string & agentList)
  60. {
  61. std::unique_lock<std::mutex>lock(m_agentListLock);
  62. m_agentList[agentId] = agentList;
  63. lock.unlock();
  64. //std::cout << GetCurTime() << "保存:" << __FUNCTION__ << " " << agentId << ": " << agentList << std::endl;
  65. std::stringstream ss;
  66. ss << GetCurTime() << " " << __FUNCTION__ << " " << agentId << ": \n" << agentList << std::endl;
  67. print(ss.str());
  68. return true;
  69. }
  70. bool CRcfEcho::getAgentList(const int agentId, std::string & agentList)
  71. {
  72. std::unique_lock<std::mutex>lock(m_agentListLock);
  73. auto it = m_agentList.find(agentId);
  74. if (it == m_agentList.end())
  75. return false;
  76. agentList = it->second;
  77. m_agentList.erase(it);
  78. lock.unlock();
  79. //std::cout << GetCurTime() << "获取:" << __FUNCTION__ << " " << agentId << ": " << agentList << std::endl;
  80. std::stringstream ss;
  81. ss << GetCurTime() << " " << __FUNCTION__ << " " << agentId << ": \n" << agentList << std::endl;
  82. print(ss.str());
  83. return true;
  84. }
  85. bool CRcfEcho::pushRedis(std::vector<std::string>& v)
  86. {
  87. for (auto val : v)
  88. {
  89. std::vector<std::string> vecs;
  90. boost::split(vecs, val, boost::algorithm::is_any_of("|"));
  91. if (vecs.size()>=2)
  92. {
  93. GlobalVar::GetInstance()->updateGroupTalkingNum(vecs.at(0), vecs.at(1)); // 2022-03-16 保存到全局变量中,供http接口访问
  94. bool ret = CRedis::GetInstance()->set(vecs.at(0).c_str(),vecs.at(1).c_str());
  95. std::stringstream ss;
  96. ss << GetCurTime() << " " << __FUNCTION__ << " " << ret << " \n" << vecs.at(0)<< " :"<<vecs.at(1) << std::endl;
  97. print(ss.str());
  98. }
  99. }
  100. return true;
  101. }
  102. bool CRcfEcho::pushCallIndex(const std::string& callIndex)
  103. {
  104. std::stringstream ss;
  105. ss << GetCurTime() << " " << __FUNCTION__ << " " << " \n" << callIndex << std::endl;
  106. print(ss.str());
  107. std::vector<std::string> vecs;
  108. boost::split(vecs, callIndex, boost::algorithm::is_any_of("#"));
  109. for (auto val : vecs)
  110. {
  111. std::vector<std::string> ivr;
  112. boost::split(ivr, val, boost::algorithm::is_any_of("|"));
  113. if (vecs.size() >= 2)
  114. {
  115. m_callIndexs[ivr.at(0)] = ivr.at(1);
  116. }
  117. }
  118. return true;
  119. }
  120. bool CRcfEcho::getCallIndex(const int index, std::string & callIndex)
  121. {
  122. std::string strIndex = std::to_string(index);
  123. auto it = m_callIndexs.find(strIndex);
  124. if (it == m_callIndexs.end())
  125. {
  126. std::stringstream ss1;
  127. ss1 << GetCurTime() << " " << __FUNCTION__ << "\n"<<" ivr索引" << index << " 失败" << std::endl;
  128. print(ss1.str());
  129. return false;
  130. }
  131. callIndex = it->second;
  132. std::stringstream ss1;
  133. ss1 << GetCurTime() << " " << __FUNCTION__ << " " << " \n" << index << " :" << callIndex << std::endl;
  134. print(ss1.str());
  135. return true;
  136. }
  137. bool CRcfEcho::pushSubmitData(const int nEventType, const std::string& state)
  138. {
  139. std::unique_lock<std::mutex>lock(m_submitDataLock);
  140. m_submitDatas[nEventType] = state;
  141. m_submitData = state;
  142. lock.unlock();
  143. std::stringstream ss;
  144. ss << GetCurTime() << " " << __FUNCTION__ << " " << nEventType << ": \n" << state << std::endl;
  145. print(ss.str());
  146. return true;
  147. }
  148. bool CRcfEcho::getSubmitData(const int nEventType, std::string & state)
  149. {
  150. std::unique_lock<std::mutex>lock(m_submitDataLock);
  151. auto it = m_submitDatas.find(nEventType);
  152. if (it == m_submitDatas.end())
  153. {
  154. std::stringstream ss;
  155. ss << GetCurTime() << " " << __FUNCTION__ << " " << nEventType << ": \n" << "失败" << std::endl;
  156. print(ss.str());
  157. return false;
  158. }
  159. state = it->second;
  160. //m_submitDatas.erase(it);
  161. //state = m_submitData;
  162. lock.unlock();
  163. std::stringstream ss;
  164. ss << GetCurTime() << " " << __FUNCTION__ << " " << nEventType << ": \n" << state << std::endl;
  165. print(ss.str());
  166. return true;
  167. }
  168. bool CRcfEcho::pushZmdExtenStateList(const std::string & extList)
  169. {
  170. m_extZmdList = extList;
  171. std::stringstream ss;
  172. ss << GetCurTime() << " " << __FUNCTION__ << " " << m_extZmdList << std::endl;
  173. print(ss.str());
  174. return true;
  175. }
  176. bool CRcfEcho::getZmdExtenStateList(std::string & extList)
  177. {
  178. extList = m_extZmdList;
  179. std::stringstream ss;
  180. ss << GetCurTime() << " " << __FUNCTION__ << " " << m_extZmdList << std::endl;
  181. print(ss.str());
  182. return true;
  183. }
  184. bool CRcfEcho::pushOnlineAgentGroupDetail(const std::string & groupNo, const std::string & agentList)
  185. {
  186. m_agentGroup[groupNo] = agentList;
  187. std::stringstream ss;
  188. ss << GetCurTime() << " " << __FUNCTION__ << " " << groupNo << ": " << agentList << std::endl;
  189. print(ss.str());
  190. return true;
  191. }
  192. bool CRcfEcho::getOnlineAgentGroupDetail(std::vector<std::string>& agentList)
  193. {
  194. agentList.clear();
  195. auto it = m_agentGroup.begin();
  196. while (it != m_agentGroup.end())
  197. {
  198. agentList.emplace_back(it->second);
  199. // 日志打印
  200. std::stringstream ss;
  201. ss << GetCurTime() << " " << __FUNCTION__ << " " << it->first << ": " << it->second << std::endl;
  202. print(ss.str());
  203. //
  204. ++it;
  205. }
  206. return true;
  207. }
  208. bool CRcfEcho::delOnlineAgentGroupDetail(const int agentID, const int extenNo)
  209. {
  210. std::vector<std::string> vecs; // 坐席集合
  211. std::vector<std::string> detailVec; // 坐席信息 下标0坐席号,1分机号
  212. std::string str; // 重新拼接每个组对应的坐席
  213. auto it = m_agentGroup.begin();
  214. while (it != m_agentGroup.end()) {
  215. vecs.clear();
  216. str = "";
  217. boost::split(vecs, it->second, boost::algorithm::is_any_of("#"));
  218. // 遍历每个坐席信息,用于判断是否是要删除的
  219. auto vecIt = vecs.begin();
  220. while (vecIt != vecs.end()) {
  221. detailVec.clear();
  222. boost::split(detailVec, *vecIt, boost::algorithm::is_any_of("|"));
  223. if (detailVec.size() > 2) {
  224. if (detailVec[0] == std::to_string(agentID) && detailVec[1] == std::to_string(extenNo)) {
  225. // 该坐席删除
  226. }
  227. else {
  228. str += *vecIt;
  229. str += "#";
  230. }
  231. }
  232. ++vecIt;
  233. }
  234. // 重新赋值
  235. it->second = str;
  236. // 日志打印
  237. std::stringstream ss;
  238. ss << GetCurTime() << " " << __FUNCTION__ << " " << it->first << ": "<< it->second << std::endl;
  239. print(ss.str());
  240. //
  241. ++it;
  242. }
  243. return true;
  244. }
  245. bool CRcfEcho::clearOnlineAgentGroupDetail()
  246. {
  247. m_agentGroup.clear();
  248. // 日志打印
  249. std::stringstream ss;
  250. ss << GetCurTime() << " " << __FUNCTION__ << std::endl;
  251. print(ss.str());
  252. return false;
  253. }
  254. bool CRcfEcho::pushCtiState(const bool isRun)
  255. {
  256. m_ctiRunState.store(isRun);
  257. // 日志打印
  258. std::stringstream ss;
  259. ss << GetCurTime() << " " << __FUNCTION__ << ":" << isRun << std::endl;
  260. print(ss.str());
  261. return m_ctiRunState.load();
  262. }
  263. bool CRcfEcho::getCtiState()
  264. {
  265. std::stringstream ss;
  266. ss << GetCurTime() << " " << __FUNCTION__ << ":" << m_ctiRunState.load() << std::endl;
  267. print(ss.str());
  268. return m_ctiRunState.load();
  269. }