usermanager.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include"usermanager.h"
  2. #include"definations.h"
  3. #include<netinet/in.h>
  4. #include<list>
  5. #include<pthread.h>
  6. #include<cstdlib>
  7. #include<cstring>
  8. user_t::user_t(const user_t& src){
  9. (*this)=src;
  10. }
  11. #define simplyCopy(x) x = src.x
  12. #define deepCopy(x) memcpy( x , src.x ,sizeof( x ))
  13. user_t& user_t::operator=(const user_t& src){
  14. simplyCopy(ip.sin_family);
  15. simplyCopy(ip.sin_port);
  16. simplyCopy(ip.sin_addr.s_addr);
  17. deepCopy(sysusrname);
  18. deepCopy(computername);
  19. deepCopy(usrname);
  20. deepCopy(grpname);
  21. deepCopy(macaddr);
  22. deepCopy(space01);
  23. deepCopy(phone);
  24. deepCopy(mail);
  25. deepCopy(printer);
  26. deepCopy(space02);
  27. deepCopy(ipaddr);
  28. deepCopy(space03);
  29. deepCopy(icon);
  30. deepCopy(netstatus);
  31. deepCopy(sign);
  32. simplyCopy(healthy);
  33. return(*this);
  34. }
  35. #undef simplyCopy
  36. #undef deepCopy
  37. static pthread_mutex_t userAddLock;
  38. static pthread_mutex_t userScanLock;
  39. static pthread_mutex_t userCopyLock;
  40. UserManager* UserManager::instance = new UserManager();
  41. UserManager* UserManager::getInstance(){
  42. static bool flgfirst=true;
  43. if(flgfirst){ //2-step init
  44. flgfirst=!flgfirst;
  45. pthread_mutex_init(&userAddLock,NULL);
  46. pthread_mutex_init(&userScanLock,NULL);
  47. pthread_mutex_init(&userCopyLock,NULL);
  48. }
  49. return instance;
  50. }
  51. #define check_replace(x) do{ \
  52. if(strcmp(tar.x , i->x)!=0){ \
  53. fixed=true; \
  54. strcpy(i->x , tar.x); \
  55. }}while(0)
  56. int UserManager::Adduser(user_t& tar){
  57. bool nofound=true;
  58. pthread_mutex_lock(&userAddLock);
  59. auto i=ulist.begin();
  60. auto z=ulist.end();
  61. for(;i!=z;i++){
  62. if(i->ip.sin_addr.s_addr==tar.ip.sin_addr.s_addr){
  63. nofound=false;
  64. break;
  65. }
  66. }
  67. if(nofound){
  68. pthread_mutex_unlock(&userAddLock);
  69. ulist.push_back(tar);
  70. return FUNCSUCCEED;
  71. }else{
  72. bool fixed=false;
  73. check_replace(sysusrname);
  74. check_replace(computername);
  75. check_replace(usrname);
  76. check_replace(grpname);
  77. check_replace(macaddr);
  78. check_replace(space01);
  79. check_replace(phone);
  80. check_replace(mail);
  81. check_replace(printer);
  82. check_replace(space02);
  83. check_replace(ipaddr);
  84. check_replace(space03);
  85. check_replace(icon);
  86. check_replace(netstatus);
  87. check_replace(sign);
  88. i->healthy=true;
  89. pthread_mutex_unlock(&userAddLock);
  90. if(fixed){
  91. return FUNCSUCCEED;
  92. }else{
  93. return FUNCFAILED;
  94. }
  95. }
  96. }
  97. #undef check_replace
  98. int UserManager::Deluser(struct sockaddr_in tar){
  99. pthread_mutex_lock(&userAddLock);
  100. pthread_mutex_lock(&userScanLock);
  101. pthread_mutex_lock(&userCopyLock);
  102. bool nofound=true;
  103. auto i=ulist.begin();
  104. auto z=ulist.end();
  105. for(;i!=z;i++){
  106. if(i->ip.sin_addr.s_addr==tar.sin_addr.s_addr){
  107. nofound=false;
  108. break;
  109. }
  110. }
  111. if(nofound==false){
  112. i=ulist.erase(i);
  113. }
  114. pthread_mutex_unlock(&userAddLock);
  115. pthread_mutex_unlock(&userScanLock);
  116. pthread_mutex_unlock(&userCopyLock);
  117. if(nofound==false){
  118. return FUNCSUCCEED;
  119. }else{
  120. return FUNCFAILED;
  121. }
  122. }
  123. int UserManager::Finduser(user_t &tar,const struct sockaddr_in src){
  124. return Finduser(tar,src.sin_addr.s_addr);
  125. }
  126. int UserManager::Finduser(user_t&tar ,unsigned int src){
  127. pthread_mutex_lock(&userScanLock);
  128. bool nofound=true;
  129. auto i=ulist.begin();
  130. auto z=ulist.end();
  131. for(;i!=z;i++){
  132. if(i->ip.sin_addr.s_addr==src){
  133. nofound=false;
  134. tar=*i;
  135. break;
  136. }
  137. }
  138. pthread_mutex_unlock(&userScanLock);
  139. if(nofound==false){
  140. return FUNCSUCCEED;
  141. }else{
  142. return FUNCFAILED;
  143. }
  144. }
  145. int UserManager::GetUserCopy(list<user_t>&src){
  146. pthread_mutex_lock(&userCopyLock);
  147. std::copy(ulist.begin(),ulist.end(),std::back_inserter(src));
  148. pthread_mutex_unlock(&userCopyLock);
  149. return FUNCSUCCEED;
  150. }
  151. int UserManager::UserIllness(void){
  152. pthread_mutex_lock(&userCopyLock);
  153. auto i=ulist.begin();
  154. auto z=ulist.end();
  155. for(;i!=z;i++){
  156. i->healthy=false;
  157. }
  158. pthread_mutex_unlock(&userCopyLock);
  159. return FUNCSUCCEED;
  160. }