PageRenderTime 23ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/CoMapScaleServer/clientconnection.cpp

https://gitlab.com/lewkoo/ComMapScaleServer
C++ | 421 lines | 329 code | 77 blank | 15 comment | 64 complexity | 4ea7421a18ec573723a5ce93455bad29 MD5 | raw file
  1. #include <iostream>
  2. #include <QStringList>
  3. #include "clientconnection.h"
  4. #include "clientstate.h"
  5. #include "tcpserver.h"
  6. #include "datalogger.h"
  7. using namespace std;
  8. ClientConnection::ClientConnection(QTcpSocket* socket, QString id, QObject *parent) :
  9. QObject(parent), blockSize(0), clientSocket(socket)
  10. {
  11. connect(socket, SIGNAL(readyRead()), this, SLOT(inDataReady()));
  12. connect (socket, SIGNAL(disconnected()), this, SLOT(disconnectClient()));
  13. this->clientId = id;
  14. logger = new DataLogger(id);
  15. }
  16. ClientConnection::~ClientConnection()
  17. {
  18. if (clientSocket != NULL)
  19. {
  20. delete clientSocket;
  21. }
  22. if (clientState != NULL)
  23. {
  24. delete clientState;
  25. }
  26. if (logger != NULL)
  27. {
  28. logger->closeFiles();
  29. delete logger;
  30. }
  31. }
  32. void ClientConnection::disconnectClient()
  33. {
  34. ((TcpServer*)parent())->removeClient(this);
  35. clientSocket->deleteLater();
  36. clientSocket = NULL;
  37. }
  38. void ClientConnection::inDataReady()
  39. {
  40. QDataStream in(clientSocket);
  41. in.setVersion(QDataStream::Qt_4_0);
  42. int bytes;
  43. bytes = clientSocket->bytesAvailable();
  44. while (bytes > 0)
  45. {
  46. bytes = clientSocket->bytesAvailable();
  47. if (bytes >= (int)sizeof(quint16))
  48. {
  49. if (blockSize == 0)
  50. {
  51. in >> blockSize;
  52. }
  53. bytes = clientSocket->bytesAvailable();
  54. if (bytes >= blockSize)
  55. {
  56. QString text;
  57. in >> text;
  58. parseMessage(text);
  59. blockSize = 0;
  60. }
  61. else
  62. {
  63. qDebug() << "Too many bytes available";
  64. blockSize = 0;
  65. }
  66. }
  67. else
  68. {
  69. qDebug() << "Insufficient bytes available";
  70. }
  71. bytes = clientSocket->bytesAvailable();
  72. }
  73. }
  74. void ClientConnection::parseMessage(QString message)
  75. {
  76. QStringList elements = message.split(',');
  77. bool parseOK = true;
  78. int id = 0;
  79. float lat = 0;
  80. float lon = 0;
  81. float topLeftLat = 0;
  82. float topLeftLon = 0;
  83. float botRightLat = 0;
  84. float botRightLon = 0;
  85. qreal scale = 0;
  86. QString clickInfo = NULL;
  87. for (int i = 0; i < elements.size(); i++)
  88. {
  89. QString pair = elements.at(i);
  90. // if (pair.startsWith("id"))
  91. // {
  92. // QString idString = pair.remove(0, 3);
  93. // id = idString.toInt(&parseOK);
  94. // if (!parseOK)
  95. // {
  96. // qDebug() << "Problem parsing id";
  97. // }
  98. // }
  99. // else
  100. if (pair.startsWith("lat")) //Current position latitude
  101. {
  102. QString latString = pair.remove(0, 4);
  103. lat = latString.toFloat(&parseOK);
  104. if (!parseOK)
  105. {
  106. qDebug() << "Problem parsing lat";
  107. }
  108. }
  109. else if (pair.startsWith("lon")) //Position longitude
  110. {
  111. QString lonString = pair.remove(0, 4);
  112. lon = lonString.toFloat(&parseOK);
  113. if (!parseOK)
  114. {
  115. qDebug() << "Problem parsing lon";
  116. }
  117. }
  118. else if (pair.startsWith("tllat")) //Top left latitude
  119. {
  120. QString latString = pair.remove(0, 6);
  121. topLeftLat = latString.toFloat(&parseOK);
  122. if (!parseOK)
  123. {
  124. qDebug() << "Problem parsing lon";
  125. }
  126. }
  127. else if (pair.startsWith("tllon")) //Top left longitude
  128. {
  129. QString lonString = pair.remove(0, 6);
  130. topLeftLon = lonString.toFloat(&parseOK);
  131. if (!parseOK)
  132. {
  133. qDebug() << "Problem parsing lon";
  134. }
  135. }
  136. else if (pair.startsWith("brlat")) //Bottom right latitude
  137. {
  138. QString latString = pair.remove(0, 6);
  139. botRightLat = latString.toFloat(&parseOK);
  140. if (!parseOK)
  141. {
  142. qDebug() << "Problem parsing lon";
  143. }
  144. }
  145. else if (pair.startsWith("brlon")) //Bottom right longitude
  146. {
  147. QString lonString = pair.remove(0, 6);
  148. botRightLon = lonString.toFloat(&parseOK);
  149. if (!parseOK)
  150. {
  151. qDebug() << "Problem parsing brlon";
  152. }
  153. }
  154. else if (pair.startsWith("scale")) //zoom level
  155. {
  156. QString scaleString = pair.remove(0, 6);
  157. scale = scaleString.toFloat(&parseOK);
  158. if (!parseOK)
  159. {
  160. qDebug() << "Problem parsing scale";
  161. }
  162. }else if (pair.startsWith("click"))//icon click
  163. {
  164. clickInfo = pair.remove(0,6);
  165. }
  166. else
  167. qDebug() << "Unrecognized message element: " << pair;
  168. }
  169. if (parseOK)
  170. {
  171. QGeoCoordinate location(lat, lon);
  172. QGeoCoordinate topLeft(topLeftLat, topLeftLon);
  173. QGeoCoordinate bottomRight(botRightLat, botRightLon);
  174. QGeoBoundingBox box(topLeft, bottomRight);
  175. //locHist.push_back(location);
  176. QTime time = QDateTime::currentDateTime().time();
  177. clientState->setLastPosition(location, box, scale, time);
  178. clientState->setLastBox(box);
  179. clientState->setZoomLevel(scale);
  180. //Repaint if location or scale update has been sent.
  181. if (lat != 0 || lon != 0 || scale != 0)
  182. {
  183. emit requestRepaint();
  184. emit clientStateChanged(this->clientId);
  185. logger->writePos(time.toString("hh:mm:ss.zzz"), QString::number(lat), QString::number(lon), QString::number(topLeftLat), QString::number(topLeftLon),
  186. QString::number(botRightLat), QString::number(botRightLon), QString::number(scale));
  187. }
  188. //Write if any click info received
  189. if(clickInfo != NULL && (lat != 0 || lon != 0 || scale != 0)){
  190. logger->writeClick(time.toString("hh:mm:ss.zzz"), QString::number(lat), QString::number(lon), QString::number(topLeftLat), QString::number(topLeftLon),
  191. QString::number(botRightLat), QString::number(botRightLon), QString::number(scale),clickInfo);
  192. displayClick(clickInfo);
  193. }
  194. //qDebug() << "Received update: " << message;
  195. }
  196. else
  197. {
  198. qDebug() << "Failed to parse incoming message: " << message;
  199. }
  200. }
  201. void ClientConnection::sendMessage(QString text)
  202. {
  203. QByteArray block;
  204. QDataStream out(&block, QIODevice::WriteOnly);
  205. out.setVersion(QDataStream::Qt_4_0);
  206. out << (quint16)0;
  207. out << text;
  208. out.device()->seek(0);
  209. out << (quint16)(block.size() - sizeof(quint16));
  210. //qDebug() << "Sending message: " << text;
  211. clientSocket->write(block);
  212. }
  213. void ClientConnection::logVw(QGeoCoordinate position, QGeoBoundingBox viewportBounds, qreal scale)
  214. {
  215. QTime time = QDateTime::currentDateTime().time();
  216. logger->writeVw(time.toString("hh:mm:ss.zzz"), QString::number(position.latitude()), QString::number(position.longitude()),
  217. QString::number(viewportBounds.topLeft().latitude()), QString::number(viewportBounds.topLeft().longitude()),
  218. QString::number(viewportBounds.bottomRight().latitude()), QString::number(viewportBounds.bottomRight().longitude()),
  219. QString::number(scale));
  220. }
  221. void ClientConnection::sendMapObjects(QList<QGeoMapObject *> mapObjects)
  222. {
  223. foreach (QGeoMapObject* object, mapObjects)
  224. {
  225. MapMarker* mapMarker = dynamic_cast<MapMarker*>(object);
  226. if (mapMarker != NULL)
  227. {
  228. MapMarker::MarkerType objectType = mapMarker->markerType();
  229. if (objectType != MapMarker::PeerType &&
  230. objectType != MapMarker::PeerOrangeType &&
  231. objectType != MapMarker::PeerPurpleType &&
  232. objectType != MapMarker::PeerRedType &&
  233. objectType != MapMarker::PeerBlueType)
  234. {
  235. QString text;
  236. switch (objectType)
  237. {
  238. case MapMarker::AnchorRedType:
  239. text = tr("id:0,") +
  240. tr("vwLat:%1,").arg(mapMarker->coordinate().latitude()) +
  241. tr("vwLon:%1").arg(mapMarker->coordinate().longitude());
  242. break;
  243. case MapMarker::AnchorBlueType:
  244. text = tr("id:1,") +
  245. tr("vwLat:%1,").arg(mapMarker->coordinate().latitude()) +
  246. tr("vwLon:%1").arg(mapMarker->coordinate().longitude());
  247. break;
  248. case MapMarker::RestaurantType:
  249. text = tr("res,") +
  250. tr("lat:%1,").arg(mapMarker->coordinate().latitude()) +
  251. tr("lon:%1,").arg(mapMarker->coordinate().longitude()) +
  252. tr("txt:%1").arg(mapMarker->getText());
  253. break;
  254. case MapMarker::HotelType:
  255. text = tr("hot,") +
  256. tr("lat:%1,").arg(mapMarker->coordinate().latitude()) +
  257. tr("lon:%1,").arg(mapMarker->coordinate().longitude()) +
  258. tr("txt:%1").arg(mapMarker->getText());
  259. break;
  260. }
  261. sendMessage(text);
  262. }
  263. }
  264. }
  265. }
  266. void ClientConnection::sendWedgeStatus(bool isEnabled, bool objWedgeEnabled)
  267. {
  268. QString text;
  269. if (isEnabled)
  270. {
  271. text = tr("wedge,");
  272. if (objWedgeEnabled)
  273. {
  274. text += tr("objw");
  275. }
  276. else
  277. {
  278. text += tr("noobjw");
  279. }
  280. }
  281. else
  282. {
  283. text = tr("nowedge");
  284. }
  285. sendMessage(text);
  286. }
  287. void ClientConnection::sendGlobalButtonStatus(bool isEnabled){
  288. QString text;
  289. if(isEnabled){
  290. text = tr("global");
  291. }
  292. else
  293. {
  294. text = tr("noglobal");
  295. }
  296. sendMessage(text);
  297. }
  298. void ClientConnection::sendWedgeIconsStatus(bool isEnabled){
  299. QString text;
  300. if(isEnabled){
  301. text = tr("iconswedge");
  302. }
  303. else
  304. {
  305. text = tr("noiconswedge");
  306. }
  307. sendMessage(text);
  308. }
  309. void ClientConnection::sendWedgeIconsPresses(bool isEnabled){
  310. QString text;
  311. if(isEnabled){
  312. text = tr("presseswedge");
  313. }
  314. else
  315. {
  316. text = tr("nopresseswedge");
  317. }
  318. sendMessage(text);
  319. }
  320. void ClientConnection::sendStatusSlider(bool isEnabled){
  321. QString text;
  322. if(isEnabled){
  323. text = tr("statusslider");
  324. }
  325. else
  326. {
  327. text = tr("nostatusslider");
  328. }
  329. sendMessage(text);
  330. }
  331. void ClientConnection::sendStatusSliderInteractivity(bool isEnabled){
  332. QString text;
  333. if(isEnabled){
  334. text = tr("interactivitystatusslider");
  335. }
  336. else
  337. {
  338. text = tr("interactivitynostatusslider");
  339. }
  340. sendMessage(text);
  341. }
  342. void ClientConnection::displayClick(QString toDisplay){
  343. emit clientClick(toDisplay);
  344. }