SANServer.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "SANServer.h"
  2. pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
  3. SANServer::SANServer()
  4. {
  5. iBuf = (char*)calloc(DATA_LEN, sizeof(char));
  6. iLen = 0;
  7. iCount = 0;
  8. iDroppedCount = 0;
  9. fp = fopen("size.txt","w");
  10. }
  11. SANServer::~SANServer()
  12. {
  13. fclose(fp);
  14. delete iBuf;
  15. close(iServSockFd);
  16. close(iBlankSockFd);
  17. }
  18. void SANServer::SetMaxClients(int aMaxClients)
  19. {
  20. iMaxClients = aMaxClients;
  21. }
  22. void SANServer::SetServerPort(int aPort)
  23. {
  24. iPort = aPort;
  25. }
  26. bool SANServer::SetupConnection()
  27. {
  28. iServSockFd = socket(AF_INET, SOCK_STREAM, 0);
  29. iServAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  30. iServAddr.sin_family = AF_INET;
  31. iServAddr.sin_port = htons(iPort);
  32. if(iServSockFd < 0)
  33. return false;
  34. else
  35. return true;
  36. }
  37. int SANServer::StartServer()
  38. {
  39. if(bind(iServSockFd, (struct sockaddr*)&iServAddr, sizeof(iServAddr)) < 0)
  40. return KBindFail;
  41. if(listen(iServSockFd, iMaxClients) < 0)
  42. return KListenFail;
  43. return KSuccess;
  44. }
  45. int SANServer::AcceptClients()
  46. {
  47. int blankfd = accept(iServSockFd, 0, 0);
  48. if(blankfd < 0)
  49. return KAcceptFail;
  50. else
  51. return blankfd;
  52. }
  53. void SANServer::RecvData()
  54. {
  55. QString temp;
  56. while(true)
  57. {
  58. bzero(iBuf, DATA_LEN);
  59. iLen = recv(iBlankSockFd, (char*)iBuf, DATA_LEN, 0);
  60. temp.clear();
  61. temp.append(iBuf);
  62. //to display recd data in UI
  63. //emit EmitDataReceived(temp);
  64. if(iLen > 0)
  65. {
  66. //rite the current q length to the file
  67. bzero(iBuf, DATA_LEN);
  68. sprintf(iBuf, "%d", iServerQueue.size());
  69. fwrite(iBuf, sizeof(char), DATA_LEN, fp);
  70. bzero(iBuf, DATA_LEN);
  71. strcpy(iBuf, "\n");
  72. fwrite(iBuf, sizeof(char), DATA_LEN, fp);
  73. iCount++;
  74. if(iServerQueue.size() > SERVER_QUEUE_SIZE)
  75. {
  76. iDroppedCount++;
  77. }
  78. else
  79. {
  80. pthread_mutex_lock(&mutex1);
  81. iServerQueue.append(temp);
  82. pthread_mutex_unlock(&mutex1);
  83. //emit EmitSortData();
  84. }
  85. }
  86. }
  87. }
  88. void SANServer::run(void)
  89. {
  90. int status = true;
  91. //setup the server first
  92. if(SetupConnection() == false)
  93. status = false;
  94. //do bind and listen
  95. int status_1 = StartServer();
  96. if(status_1 == 0 && status == true)
  97. emit EmitServerConnected(true);
  98. else
  99. emit EmitServerConnected(false);
  100. //perform accept here
  101. iBlankSockFd = AcceptClients();
  102. if(iBlankSockFd > 0)
  103. {
  104. emit EmitClientAccepted(true);
  105. RecvData();
  106. }
  107. else
  108. {
  109. emit EmitClientAccepted(false);
  110. }
  111. }
  112. char* SANServer::GetBuffer()
  113. {
  114. return iBuf;
  115. }
  116. long long int SANServer::GetCount()
  117. {
  118. return iCount;
  119. }
  120. long long int SANServer::GetDroppedCount()
  121. {
  122. return iDroppedCount;
  123. }
  124. void SANServer::DecrementCount()
  125. {
  126. iCount--;
  127. }
  128. SANSorting::SANSorting(SANServer *aServer)
  129. {
  130. iServer = aServer;
  131. iBuf = (char*)calloc(DATA_LEN, sizeof(char));
  132. iCount = 0;
  133. }
  134. SANSorting::~SANSorting()
  135. {
  136. if(iBuf)
  137. free(iBuf);
  138. close(iSockFd);
  139. }
  140. void SANSorting::ConnectSortingThread()
  141. {
  142. if(SetupConnection() == false)
  143. {
  144. emit EmitServerToClient(false);
  145. }
  146. else
  147. {
  148. if(ConnectToClient() == false)
  149. {
  150. emit EmitServerToClient(false);
  151. }
  152. else
  153. {
  154. emit EmitServerToClient(true);
  155. }
  156. }
  157. }
  158. void SANSorting::run(void)
  159. {
  160. QByteArray e;
  161. while(true)
  162. {
  163. pthread_mutex_lock(&mutex1);
  164. if(iServer->iServerQueue.isEmpty() == false)
  165. e = iServer->iServerQueue.takeFirst().toUtf8();
  166. pthread_mutex_unlock(&mutex1);
  167. if(e != NULL)
  168. {
  169. char *buf = e.data();
  170. char temp;
  171. for(int j = 0; j < DATA_LEN; j ++)
  172. {
  173. for(int i = 0; i < DATA_LEN-1; i++)
  174. {
  175. if(buf[i] > buf[i+1])
  176. {
  177. temp = buf[i];
  178. buf[i] = buf[i+1];
  179. buf[i+1] = temp;
  180. }
  181. }
  182. }
  183. send(iSockFd, buf, DATA_LEN, 0);
  184. iCount++;
  185. }
  186. //to display sorted data in UI
  187. //emit EmitDisplaySortedData(buf);
  188. }
  189. }
  190. void SANSorting::SetIp(std::string aIp)
  191. {
  192. iIp = aIp;
  193. }
  194. void SANSorting::SetPort(int aPort)
  195. {
  196. iPort = aPort;
  197. }
  198. int SANSorting::GetPort()
  199. {
  200. return iPort;
  201. }
  202. std::string SANSorting::GetIp()
  203. {
  204. return iIp;
  205. }
  206. bool SANSorting::SetupConnection()
  207. {
  208. const char *ip = iIp.c_str();
  209. iSockFd = socket(AF_INET, SOCK_STREAM, 0);
  210. iClientAddr.sin_addr.s_addr = inet_addr(ip);
  211. iClientAddr.sin_port = htons(iPort);
  212. iClientAddr.sin_family = AF_INET;
  213. if(iSockFd > 0)
  214. {
  215. return true;
  216. }
  217. else
  218. {
  219. return false;
  220. }
  221. }
  222. bool SANSorting::ConnectToClient()
  223. {
  224. if(::connect(iSockFd, (struct sockaddr*)&iClientAddr, sizeof(iClientAddr)) < 0)
  225. return false;
  226. else
  227. return true;
  228. }
  229. long long int SANSorting::GetCount()
  230. {
  231. return iCount;
  232. }