SANClient.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "SANClient.h"
  2. SANClient::SANClient()
  3. {
  4. iCount = 0;
  5. iTotDelay = 0;
  6. }
  7. SANClient::~SANClient()
  8. {
  9. fclose(iFp);
  10. close(iClientSockFd);
  11. }
  12. bool SANClient::OpenFile(std::string aFileName)
  13. {
  14. const char *name = aFileName.c_str();
  15. iFp = fopen(name, "r");
  16. if(iFp != NULL)
  17. return true;
  18. else
  19. return false;
  20. }
  21. void SANClient::SetDestnPort(int aPort)
  22. {
  23. iPort = aPort;
  24. }
  25. void SANClient::SetDestnIp(std::string aIp)
  26. {
  27. iIp = aIp;
  28. }
  29. void SANClient::SetDelay(double aDelay)
  30. {
  31. //iDelay.tv_sec = aDelay;
  32. //iDelay.tv_nsec = aDelay * 1000000000;
  33. //qDebug("idelay int %d", iDelay.tv_sec);
  34. }
  35. bool SANClient::SetupConnection()
  36. {
  37. const char *ip = iIp.c_str();
  38. iClientSockFd = socket(AF_INET, SOCK_STREAM, 0);
  39. iServAddr.sin_addr.s_addr = inet_addr(ip);
  40. iServAddr.sin_port = htons(iPort);
  41. iServAddr.sin_family = AF_INET;
  42. if(iClientSockFd > 0)
  43. return true;
  44. else
  45. return false;
  46. }
  47. bool SANClient::ConnectToServer()
  48. {
  49. if(::connect(iClientSockFd, (struct sockaddr*)&iServAddr, sizeof(iServAddr) ) < 0)
  50. return false;
  51. else
  52. return true;
  53. }
  54. void SANClient::SetDistribution(int aDistribution)
  55. {
  56. iDistribution = aDistribution;
  57. }
  58. int SANClient::GetDistribution()
  59. {
  60. return iDistribution;
  61. }
  62. void SANClient::SetSendRate(int aSendRate)
  63. {
  64. iSendRate = aSendRate;
  65. }
  66. int SANClient::GetSendRate()
  67. {
  68. return iSendRate;
  69. }
  70. long double SANClient::GetTotDelay()
  71. {
  72. return iTotDelay;
  73. }
  74. void SANClient::SendData()
  75. {
  76. int len = 0;
  77. char *buf = (char*)calloc(DATA_LEN, sizeof(char));
  78. double d, delay;
  79. while(!feof(iFp))
  80. {
  81. if(iDistribution == KPoisson)
  82. {
  83. d = -1 * log(drand48());
  84. delay = (d * DATA_LEN )/(iSendRate * 1000);
  85. qDebug("delay %f", delay);
  86. iTotDelay += delay;
  87. qDebug("totaldelay %f", iTotDelay);
  88. //in micro secs
  89. iUSleep = delay * 1000000;
  90. }
  91. len = fread(buf, sizeof(char), DATA_LEN, iFp);
  92. //introduce sleep here if needed to control send rate
  93. usleep(iUSleep);
  94. send(iClientSockFd, buf, len, 0);
  95. iCount++;
  96. //to show sent data in UI
  97. //emit EmitDataSent(buf);
  98. }
  99. }
  100. int SANClient::GetCount()
  101. {
  102. return iCount;
  103. }
  104. void SANClient::run(void)
  105. {
  106. SendData();
  107. }
  108. SANReceive::SANReceive()
  109. {
  110. iBuf = (char*)calloc(DATA_LEN, sizeof(char));
  111. iLen = 0;
  112. iCount = 0;
  113. }
  114. SANReceive::~SANReceive()
  115. {
  116. if(iBuf)
  117. free(iBuf);
  118. close(iServSockFd);
  119. close(iBlankSockFd);
  120. }
  121. void SANReceive::SetPort(int aPort)
  122. {
  123. iPort = aPort;
  124. }
  125. int SANReceive::GetPort()
  126. {
  127. return iPort;
  128. }
  129. bool SANReceive::SetupConnection()
  130. {
  131. iServSockFd = socket(AF_INET, SOCK_STREAM, 0);
  132. iClientAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  133. iClientAddr.sin_family = AF_INET;
  134. iClientAddr.sin_port = htons(iPort);
  135. if(iServSockFd > 0)
  136. return true;
  137. else
  138. return false;
  139. }
  140. bool SANReceive::StartServer()
  141. {
  142. if(bind(iServSockFd, (struct sockaddr*)&iClientAddr, sizeof(iClientAddr)) < 0)
  143. return false;
  144. if(listen(iServSockFd, 1) < 0)
  145. return false;
  146. return true;
  147. }
  148. int SANReceive::AcceptConnections()
  149. {
  150. int blankfd = accept(iServSockFd, 0, 0);
  151. if(blankfd < 0)
  152. return -1;
  153. else
  154. return blankfd;
  155. }
  156. void SANReceive::run(void)
  157. {
  158. bool status = true;
  159. if(SetupConnection() == false)
  160. {
  161. status = false;
  162. emit EmitServerStart(false);
  163. }
  164. status = StartServer();
  165. if(status == false)
  166. {
  167. emit EmitServerStart(false);
  168. }
  169. else
  170. {
  171. emit EmitServerStart(true);
  172. iBlankSockFd = AcceptConnections();
  173. if(iBlankSockFd > 0)
  174. {
  175. emit EmitClientAccepted(true);
  176. RecvData();
  177. }
  178. else
  179. emit EmitClientAccepted(false);
  180. }
  181. }
  182. int SANReceive::GetCount()
  183. {
  184. return iCount;
  185. }
  186. void SANReceive::RecvData()
  187. {
  188. while(true)
  189. {
  190. bzero(iBuf, DATA_LEN);
  191. iLen = 0;
  192. iLen = recv(iBlankSockFd, (char*)iBuf, DATA_LEN, 0);
  193. if(iLen > 0)
  194. {
  195. iCount++;
  196. //to display sorted and received data in UI
  197. //QString temp(iBuf);
  198. //emit EmitDisplaySortedData(temp);
  199. }
  200. }
  201. }