ClientManager.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <algorithm>
  2. #include "ClientManager.h"
  3. ClientManager g_ClientManager;
  4. ClientManager::ClientManager()
  5. {
  6. }
  7. ClientManager::~ClientManager()
  8. {
  9. }
  10. bool ClientManager::RegistClient(UINT64 Addr)
  11. {
  12. std::cout << "ClientManager::RegistClient addr: " << Addr << endl;
  13. Thread_Lock();
  14. m_ClientMap[Addr] = (LogicClient*)Addr;
  15. Thread_UnLock();
  16. return true;
  17. }
  18. bool ClientManager::RegistClient(unsigned int proc, unsigned int addr, const char* pszKey, LogicClient* client)
  19. {
  20. //std::cout << "ClientManager::RegistClient proc" << proc << " org addr " << addr << " key " << pszKey << " LogicClient addr: " << (UINT64)client << endl;
  21. Thread_Lock();
  22. /*
  23. char szKeys[256];
  24. strcpy(szKeys, pszKey);
  25. char* pt = szKeys;
  26. while (*pt != 0)
  27. {
  28. if (*pt == '/' || *pt == '{' || *pt == '}')
  29. *pt = '_';
  30. pt++;
  31. }
  32. ccos_bus_client clt(proc, addr, szKeys);*/
  33. ccos_bus_client clt(proc, addr, pszKey);
  34. m_ClientOpenMap[clt] = client;
  35. //m_ClientMap[proc<<32 | addr] = client;
  36. Thread_UnLock();
  37. return true;
  38. }
  39. void ClientManager::UnRegistAll()
  40. {
  41. //框架需要做2件事情
  42. //作为Client需要发送Close命令给设备
  43. // 没必要,在CDI传递->Local失败的情况,有对应.
  44. //作为Device,需要发送Close通知给Client
  45. // 没必要,本地驱动卸载的时候,自动发送Close给Client.
  46. // 非本地本进程的Client,直接清除即可.
  47. //问题:Bus线程和DrvTree线程中监视ROOT驱动的Client端也会被干掉
  48. //方案:需要清除的Client应该是非本地本进程的Client.
  49. // 而本地本进程的Client将会在驱动卸载的时候收到CloseNotify.
  50. //所以,简单方案是,本地驱动全部卸载后,进行UnRegistAll就可以了.
  51. Thread_Lock();
  52. //NOT FINISHED YET
  53. //need to send close notify to current clients
  54. m_ClientMap.clear();
  55. Thread_UnLock();
  56. }
  57. bool ClientManager::UnRegistClient(UINT64 Addr)
  58. {
  59. bool ret = false;
  60. Thread_Lock();
  61. map<UINT64, LogicClient*>::iterator proc_iter = m_ClientMap.find(Addr);
  62. if (proc_iter != m_ClientMap.end())
  63. {
  64. m_ClientMap.erase(proc_iter);
  65. Thread_UnLock();
  66. ret = true;
  67. }
  68. else
  69. {
  70. Thread_UnLock();
  71. //printf("Driver Thread:%d,UnRegistClient Empty\n", GetCurrentThreadId());
  72. }
  73. return ret;
  74. }
  75. bool ClientManager::UnRegistClient(unsigned int proc, unsigned int addr)
  76. {
  77. bool ret = false;
  78. Thread_Lock();
  79. map<UINT64, LogicClient*>::iterator proc_iter = m_ClientMap.find(proc<<32|addr);
  80. if (proc_iter != m_ClientMap.end())
  81. {
  82. m_ClientMap.erase(proc_iter);
  83. Thread_UnLock();
  84. ret = true;
  85. }
  86. else
  87. {
  88. Thread_UnLock();
  89. //printf("Driver Thread:%d,UnRegistClient Empty\n", GetCurrentThreadId());
  90. }
  91. return ret;
  92. }
  93. bool ClientManager::FindClient(UINT64 Addr)
  94. {
  95. bool ret = false;
  96. std::cout << "try find client use addr " << Addr << endl;
  97. Thread_Lock();
  98. map<UINT64, LogicClient*>::iterator proc_iter = m_ClientMap.find(Addr);
  99. if (proc_iter != m_ClientMap.end())
  100. {
  101. ret = true;
  102. }
  103. Thread_UnLock();
  104. std::cout << "Find result : " << ret <<endl;
  105. return ret;
  106. }
  107. LogicClient* ClientManager::GetClient(unsigned int proc, unsigned int addr , const char* pszKey)
  108. {
  109. LogicClient *ret = nullptr;
  110. std::cout << "ClientManager::GetClient proc " << proc << " org addr " << addr << " key " << pszKey << endl;
  111. /*
  112. char szKeys[256];
  113. strcpy(szKeys, pszKey);
  114. char* pt = szKeys;
  115. while (*pt != 0)
  116. {
  117. if (*pt == '/' || *pt == '{' || *pt == '}')
  118. *pt = '_';
  119. pt++;
  120. }
  121. ccos_bus_client clt(proc, addr, szKeys);*/
  122. ccos_bus_client clt(proc, addr, pszKey);
  123. Thread_Lock();
  124. auto proc_iter = m_ClientOpenMap.find(clt);
  125. if (proc_iter != m_ClientOpenMap.end())
  126. {
  127. ret = proc_iter->second;
  128. }
  129. Thread_UnLock();
  130. std::cout << " Find Result LogicClient " << (UINT64)ret << endl;
  131. return ret;
  132. }