linux版本中间件

Session.cpp 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "Session.h"
  2. #include <algorithm>
  3. #include "Log.h"
  4. Session::Session(const std::string & strId) :m_strId(strId), m_transSentenceIndex(0)
  5. {
  6. }
  7. Session::~Session()
  8. {
  9. m_lstChan.clear();
  10. }
  11. void Session::addChan(const std::shared_ptr<Chan>&spChan)
  12. {
  13. auto it = std::find_if(m_lstChan.begin(), m_lstChan.end(), [=](const std::shared_ptr<Chan>&pChan) {return pChan->ChanID().compare(spChan->ChanID()) == 0; });
  14. if (it == m_lstChan.end()) {
  15. m_lstChan.emplace_back(spChan);
  16. auto bk = m_lstChan.back();
  17. LOG_DEBUG_S(boost::str(Format("添加通道信息\n src[%s] \n dst[%s]")% spChan->to_string() % bk->to_string()));
  18. }
  19. else {
  20. LOG_WARN_S(boost::str(Format("会话[%s]已存在该通道[%s],查到通道[%s]") % spChan->CallID() % spChan->ChanID() % (*it)->to_string()));
  21. }
  22. LOG_DEBUG_S(boost::str(Format("会话[%s]添加通道[%s][%s]当前数量[%ld]") % m_strId % spChan->CallID() %spChan->ChanID() % m_lstChan.size()));
  23. }
  24. void Session::delChan(const std::shared_ptr<Chan>&spChan)
  25. {
  26. auto it = std::find_if(m_lstChan.begin(), m_lstChan.end(), [=](const std::shared_ptr<Chan>&pChan) {return pChan->ChanID().compare(spChan->ChanID())==0; });
  27. if (it != m_lstChan.end()) {
  28. m_lstChan.erase(it);
  29. }
  30. LOG_DEBUG_S(boost::str(Format("会话[%s]删除通道[%s]当前数量[%ld]") % m_strId %spChan->ChanID() % m_lstChan.size()));
  31. }
  32. const std::shared_ptr<Chan> Session::getChan(const std::string&strChanID)
  33. {
  34. std::shared_ptr<Chan> pChan = nullptr;
  35. auto it = std::find_if(m_lstChan.begin(), m_lstChan.end(), [=](const std::shared_ptr<Chan>&pChan) {return pChan->ChanID().compare(strChanID) == 0; });
  36. if (it != m_lstChan.end()) {
  37. pChan = *it;
  38. }
  39. return pChan;
  40. }
  41. const std::shared_ptr<Chan> Session::getFirstChan()
  42. {
  43. if (!m_lstChan.empty()) {
  44. return m_lstChan.front();
  45. }
  46. return nullptr;
  47. }
  48. const std::shared_ptr<Chan> Session::getAssoChan(const std::shared_ptr<Chan>& spChan)
  49. {
  50. auto it = std::find_if(m_lstChan.begin(), m_lstChan.end(), [=](const std::shared_ptr<Chan>&pChan) {return pChan->ChanID().compare(spChan->ChanID())!=0; });
  51. if (it != m_lstChan.end()) {
  52. return *it;
  53. }
  54. return nullptr;
  55. }
  56. const std::shared_ptr<Chan> Session::getAssoChan(const std::string & strChanID)
  57. {
  58. auto it = std::find_if(m_lstChan.begin(), m_lstChan.end(), [=](const std::shared_ptr<Chan>&pChan) {return pChan->ChanID().compare(strChanID)!=0; });
  59. if (it != m_lstChan.end()) {
  60. return *it;
  61. }
  62. return nullptr;
  63. }
  64. void Session::printfChanList()
  65. {
  66. for (auto it = m_lstChan.begin(); it != m_lstChan.end(); ++it) {
  67. auto pChan = *it;
  68. LOG_DEBUG_S(boost::str(Format("会话[%s]存在通道[%s][%s]") % m_strId %pChan->CallID() % pChan->ChanID()));
  69. }
  70. }