中间件底层,websocket

AgentGroup.cpp 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include "StdAfx.h"
  2. #include "AgentGroup.h"
  3. #include "Agent.h"
  4. #include "NetworkAcd.h"
  5. #include "StrategyAD.h"
  6. #include <algorithm>
  7. CAgentGroup::CAgentGroup( void )
  8. {
  9. }
  10. CAgentGroup::~CAgentGroup( void )
  11. {
  12. }
  13. /*****************************************************************
  14. **【函数名称】 add
  15. **【函数功能】 添加座席
  16. **【参数】
  17. **【返回值】
  18. ****************************************************************/
  19. void CAgentGroup::add( CAgent* a_pAgent )
  20. {
  21. ASSERT(a_pAgent != NULL);
  22. Add(a_pAgent);
  23. }
  24. /*****************************************************************
  25. **【函数名称】 remove
  26. **【函数功能】 删除座席
  27. **【参数】
  28. **【返回值】
  29. ****************************************************************/
  30. bool CAgentGroup::remove( CAgent* a_pAgent )
  31. {
  32. ASSERT(a_pAgent != NULL);
  33. for(int i = 0; i < m_nSize; ++i)
  34. {
  35. if(m_pData[i] == a_pAgent)
  36. {
  37. RemoveAt(i, 1);
  38. TRACE(_T("Succeed @CAgentGroup::remove\r\n"));
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. /*****************************************************************
  45. **【函数名称】 clear
  46. **【函数功能】 释放所有座席
  47. **【参数】
  48. **【返回值】
  49. ****************************************************************/
  50. void CAgentGroup::clear( void )
  51. {
  52. RemoveAll();
  53. }
  54. /*****************************************************************
  55. **【函数名称】 isNoFreeAgent
  56. **【函数功能】 是否座席全忙
  57. **【参数】
  58. **【返回值】
  59. ****************************************************************/
  60. bool CAgentGroup::isNoFreeAgent( void )
  61. {
  62. for(int i = 0; i < m_nSize; ++i) // 保证遍历一轮
  63. {
  64. if(m_pData[i]->isFree())
  65. return false;
  66. }
  67. return true;
  68. }
  69. int CAgentGroup::talkIngAgentCount()
  70. {
  71. CAgent *pAgent = NULL;
  72. int count = 0;
  73. for (int idx = m_nSize; idx > 0; idx--) {
  74. pAgent = m_pData[0];
  75. if (pAgent->state()== AGENT_STATE_BUSY)
  76. {
  77. count += 1;
  78. }
  79. }
  80. return count;
  81. }
  82. //2018.6.14 for 严格循环
  83. CAgent* CAgentGroup::GetFreeAgentForLoop()
  84. {
  85. CAgent *pAgent = NULL;
  86. CString str;
  87. for (int idx = 0; idx < m_nSize; idx++) {
  88. str.Format("%s|%d|%s|%d", str, m_pData[idx]->id(), m_pData[idx]->group(), m_pData[idx]->isFree());
  89. }
  90. ILogger::getInstance().log(LOG_CLASS_SOCKET, LOG_LEVEL_NORMAL, "轮训排队信息[%s]", str);
  91. for (int idx = 0; idx < m_nSize; idx++) {
  92. pAgent = m_pData[idx];
  93. if (pAgent->isFree()) {
  94. RemoveAt(idx, 1);
  95. Add(pAgent);
  96. return pAgent;
  97. }
  98. }
  99. //for (int idx = m_nSize; idx > 0; idx-- ) {
  100. // pAgent = m_pData[0];
  101. // RemoveAt(0, 1);
  102. // Add(pAgent);
  103. // if (pAgent->isFree()) {
  104. // return pAgent; // ych
  105. // }
  106. //}
  107. return NULL; // ych
  108. }
  109. CAgent * CAgentGroup::GetFreeAgentForLoopByAgentId()
  110. {
  111. static UINT agentId = 0; // 2020-01-13记录本次分配的座席id
  112. CAgent *pAgent = NULL;
  113. bool isExist = false;
  114. std::map<UINT, CAgent*> mAgents;
  115. for (size_t i = 0; i < m_nSize; i++)
  116. {
  117. pAgent = m_pData[i];
  118. mAgents.insert(std::pair<UINT, CAgent*>(pAgent->id(), pAgent));
  119. }
  120. auto it = mAgents.begin();
  121. while (it != mAgents.end())
  122. {
  123. if (it->first > agentId && it->second->isFree())
  124. {
  125. agentId = it->first;
  126. isExist = true;
  127. break;
  128. }
  129. ++it;
  130. }
  131. if (isExist = false)
  132. {
  133. it = mAgents.begin();
  134. while (it != mAgents.end())
  135. {
  136. if (it->first <= agentId && it->second->isFree())
  137. {
  138. agentId = it->first;
  139. isExist = true;
  140. break;
  141. }
  142. if (it->first > agentId)
  143. {
  144. break;
  145. }
  146. ++it;
  147. }
  148. }
  149. if (isExist = true)
  150. {
  151. for (size_t i = 0; i < m_nSize; i++)
  152. {
  153. pAgent = m_pData[i];
  154. if (m_pData[i]->id() == agentId)
  155. return m_pData[i];
  156. }
  157. }
  158. return NULL;
  159. // ----------------------
  160. CArray<CAgent*,CAgent*> *agent = this;
  161. std::sort(this->GetData(), this->GetData() + this->GetSize(), [&](CAgent *a, CAgent *b) mutable ->bool { return a->id() < b->id(); });
  162. for (int i = 0; i < m_nSize; i++)
  163. {
  164. pAgent = m_pData[i];
  165. if (pAgent->isFree()&&pAgent->id()> agentId)
  166. {
  167. agentId = pAgent->id();
  168. return pAgent;
  169. }
  170. }
  171. for (int i = 0; i < m_nSize; i++)
  172. {
  173. pAgent = m_pData[i];
  174. if (pAgent->isFree() && pAgent->id() <= agentId)
  175. {
  176. agentId = pAgent->id();
  177. return pAgent;
  178. }
  179. }
  180. return nullptr;
  181. }
  182. std::string CAgentGroup::GetCurAllAgent()
  183. {
  184. CString str;
  185. std::stringstream ss;
  186. for (int idx = 0; idx < m_nSize; idx++) {
  187. //str.Format("%s#%d|%d|%s|%d", str, m_pData[idx]->id(), m_pData[idx]->assoExten(), m_pData[idx]->group(), m_pData[idx]->type());
  188. ss << m_pData[idx]->id()<<"|";
  189. ss << m_pData[idx]->assoExten() << "|";
  190. ss << m_pData[idx]->group() << "|";
  191. ss << m_pData[idx]->type() << "#";
  192. }
  193. return ss.str();
  194. }
  195. /*****************************************************************
  196. **【函数名称】 distributeAgent
  197. **【函数功能】 分派座席
  198. **【参数】
  199. **【返回值】
  200. ****************************************************************/
  201. QUEUE_AGENT_RESULT CAgentGroup::distributeAgent( CStrategyAD* pStrategy, const CString& InfoEx, UINT& Agent, UINT& Exten )
  202. {
  203. if(m_nSize == 0)
  204. return QUEUE_AGENT_FAILED_NO_AGENT;
  205. // 如果座席全忙
  206. if(isNoFreeAgent())
  207. return QUEUE_AGENT_FAILED_NO_FREE;
  208. CAgent* pAgent = NULL;
  209. QUEUE_AGENT_RESULT Result = pStrategy->distributeAgent(*this, InfoEx, pAgent);
  210. if(pAgent != NULL && pAgent->lock()) // 对于无法锁定的座席,默认分配失败
  211. {
  212. Agent = pAgent->id();
  213. Exten = pAgent->assoExten();
  214. return Result;
  215. }
  216. else
  217. {
  218. return QUEUE_AGETN_FAILED_NO_SPEC;
  219. }
  220. }