PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/queue2.js

https://gitlab.com/Kamisama666/taxiserver
JavaScript | 383 lines | 204 code | 97 blank | 82 comment | 24 complexity | cb868c23b2709e094374fc3194372c9b MD5 | raw file
  1. "use strict";
  2. var gu = require('geoutils');
  3. var _ = require('underscore');
  4. var zmq = require('zmq');
  5. var datalayer;
  6. var nconf;
  7. var queueUser = require('./queueuser');
  8. var msgid=1;
  9. var requester = zmq.socket('req');
  10. /**
  11. * Creates a queue for users. The queue is stored in memory and in a database.
  12. * @param {object} dblayer MariaDB connexion object
  13. */
  14. function QueueMain(dblayer,config) {
  15. datalayer=dblayer;
  16. nconf=config;
  17. requester.connect(nconf.get("queue:ipcpath"));
  18. process.on('SIGINT', function() {
  19. responder.close();
  20. });
  21. //In case of an error
  22. process.on('uncaughtException', function (err) {
  23. responder.close();
  24. });
  25. return exports;
  26. }
  27. module.exports = QueueMain;
  28. /**
  29. * The object for the user inside the queue
  30. * @param {string} userid The user id
  31. * @param {object} location The location of the user (geoutil)
  32. function queueUser(userid,location) {
  33. this.userid=userid;
  34. this.location=location;
  35. this.lastUpdate=new Date();
  36. }*/
  37. /**
  38. * Makes a query to the queue
  39. * @param {string} queryName Name of the function to query
  40. * @param {array} parameters Array containing the parameters for the function in the exact order
  41. * @param {Function} cb Callback function
  42. */
  43. function queryQueue(queryName,parameters,cb) {
  44. var myMsgID=msgid;
  45. msgid++;
  46. requester.on("message", function(reply) {
  47. var result={};
  48. var response=JSON.parse(reply.toString());
  49. if (response.ID!==myMsgID)
  50. return;
  51. result.State=response.State;
  52. if (result.State==="True") {
  53. result.Content=response.Content;
  54. }
  55. else {
  56. result.Error=response.Error;
  57. }
  58. cb(true,result);
  59. });
  60. var request=JSON.stringify({Function:queryName,Arguments:parameters,ID:myMsgID});
  61. requester.send(request);
  62. }
  63. /**
  64. * Checks if the user is on the queue
  65. * @param {string} userid The user id
  66. * @param {Function} cb Callback function
  67. */
  68. function isUserOnQueue(userid,cb) {
  69. queryQueue("isUserOnQueue",[userid],cb);
  70. }
  71. /**
  72. * Checks if the queue is empty
  73. * @param {Function} cb Callback function
  74. */
  75. function isQueueEmpty(cb) {
  76. queryQueue("isQueueEmpty",[],cb)
  77. }
  78. /**
  79. * Gets the position of an user on queue
  80. * @param {string} userid The user id
  81. * @param {Function} cb Callback function
  82. */
  83. function getPositionOfUser(userid,cb) {
  84. queryQueue("getPositionOfUser",[userid],cb)
  85. }
  86. /**
  87. * Gets the position of an user on queue
  88. * @param {string} userid The user id
  89. * @param {Function} cb The callback function (if the user is found, the position starting on 1. If not, it returns -1)
  90. */
  91. exports.getPositionOfUser=getPositionOfUser;
  92. /**
  93. * Adds an user to the queue
  94. * @param {string} userid The user id
  95. * @param {double} lat The latitude coordinate of the user
  96. * @param {double} long The longitude coordinate of the user
  97. * @param {Function} cb The callback function
  98. */
  99. exports.addToQueue = function(userid,lat,long,cb) {
  100. var newUser;
  101. queryQueue("isUserOnQueue",[userid],isUserOnQueueQuery)
  102. function isUserOnQueueQuery(result,content) {
  103. if (content.Content) {
  104. return cb(false,"The user "+userid+" is already on queue");
  105. }
  106. datalayer.getUserById(userid,getUserDB);
  107. }
  108. function getUserDB(result,content) {
  109. if (!result)
  110. return cb(false,content);
  111. var userlocation=new gu.LatLon(lat,long);
  112. newUser=new queueUser(userid,userlocation,content.extension);
  113. datalayer.addUserToQueue(newUser,addToQueueDB);
  114. }
  115. function addToQueueDB(result,content) {
  116. if (!result)
  117. return cb(false,content);
  118. queryQueue("addToQueue",[userid,lat,long,newUser.extension],addToQueueQuery);
  119. }
  120. function addToQueueQuery(result,content) {
  121. if (!content.State==="True")
  122. return cb(false,content.Error);
  123. return cb(true,"The user "+userid+" has beed added to the queue");
  124. }
  125. }
  126. /**
  127. * Takes an user from the queue
  128. * @param {string} userid The user id
  129. * @param {Function} cb The callback function
  130. */
  131. exports.takeUserFromQueue = function(userid,cb) {
  132. queryQueue('getPositionOfUser',[userid],getPositionOfUserQuery)
  133. function getPositionOfUserQuery(result,content) {
  134. var position=content.Content;
  135. if (position===-1)
  136. return cb(false,"The user "+userid+" is not on queue");
  137. datalayer.takeUserFromQueue(userid,takeUserFromQueueDB);
  138. }
  139. function takeUserFromQueueDB(result,content) {
  140. if (!result)
  141. return cb(false,content);
  142. queryQueue('takeUserFromQueue',[userid],takeUserFromQueueQuery)
  143. }
  144. function takeUserFromQueueQuery(result,content) {
  145. if (content.State!=="True");
  146. return cb(false,content.Error);
  147. return cb(true,"The user "+userid+" has been removed from queue");
  148. }
  149. }
  150. /**
  151. * Takes the first user from the queue and sends it back
  152. * @param {Function} cb The callback function
  153. */
  154. exports.takeNextUserFromQueue = function(cb) {
  155. var nextUser;
  156. var response={};
  157. queryQueue('isQueueEmpty',[],isQueueEmptyQuery);
  158. function isQueueEmptyQuery(result,content) {
  159. if (content.Content)
  160. return cb(false,"There is no users on queue");
  161. queryQueue('takeNextUserFromQueue',[],takeNextUserFromQueueQuery);
  162. }
  163. function takeNextUserFromQueueQuery(result,content) {
  164. if (content.State!=='True')
  165. return cb(false,content.Error);
  166. nextUser=content.Content;
  167. datalayer.takeUserFromQueue(nextUser.userid,takeNextUserFromQueueDB);
  168. }
  169. function takeNextUserFromQueueDB(result,content) {
  170. if (result) {
  171. response.userid=nextUser.userid;
  172. response.lat=nextUser.lat;
  173. response.long=nextUser.lon;
  174. response.extension=nextUser.extension;
  175. return cb(true,response);
  176. }
  177. else {
  178. //Put it at the end
  179. queryQueue(
  180. "addToQueue",
  181. [
  182. nextUser.userid,
  183. nextUser.lat,
  184. nextUser.lon
  185. ],
  186. addToQueueQuery);
  187. }
  188. }
  189. function addToQueueQuery(result,content) {
  190. if (!content.State==="True")
  191. return cb(false,content.Error);
  192. return cb(false,"The user "+userid+" couldn't be taken from the queue. It has been added again");
  193. }
  194. };
  195. /**
  196. * Gets the content of the queue
  197. * @param {Function} cb The callback function
  198. */
  199. exports.getQueueContent = function(cb) {
  200. queryQueue('isQueueEmpty',[],isQueueEmptyQuery);
  201. function isQueueEmptyQuery(result,content) {
  202. if (content.Content)
  203. return cb(false,"There is no users on queue");
  204. queryQueue('getQueueContent',[],getQueueContentQuery)
  205. }
  206. function getQueueContentQuery(result,content) {
  207. if (content.State!=='True')
  208. return cb(false,content.Error);
  209. cb(true,content.Content);
  210. }
  211. }
  212. /**
  213. * Gets the location on an user
  214. * @param {string} userid The user id
  215. * @param {Function} cb The callback function
  216. */
  217. exports.getUserLocation = function(userid,cb) {
  218. queryQueue('isUserOnQueue',[userid],isUserOnQueueQuery);
  219. function isUserOnQueueQuery(result,content) {
  220. if (!content.Content)
  221. return cb(false,'The user is not on queue');
  222. queryQueue('getUserLocation',[userid],getUserLocationQuery);
  223. }
  224. function getUserLocationQuery(result,content) {
  225. if (content.State!=='True')
  226. return cb(false,content.Error);
  227. return cb(true, content.Content);
  228. }
  229. }
  230. /**
  231. * Updates the user location
  232. * @param {string} userid The user id
  233. * @param {string} lat The new latitude coordenates
  234. * @param {string} long The new longitude coordenates
  235. * @param {Function} cb The callback function
  236. */
  237. exports.updateDriverLocation = function(userid,lat,long,cb) {
  238. var userUpdatedPosition;
  239. var userUpdated;
  240. queryQueue('isUserOnQueue',[userid],isUserOnQueueQuery);
  241. function isUserOnQueueQuery(result,content) {
  242. if (!content.Content)
  243. return cb(false,'There user is not on queue');
  244. queryQueue('updateDriverLocation',[userid,lat,long],updateDriverLocationQuery);
  245. }
  246. function updateDriverLocationQuery(result,content) {
  247. if (content.State!=='True')
  248. return cb(false,content.Error);
  249. userUpdatedPosition=new gu.LatLon(lat,long);
  250. userUpdated=new queueUser(userid,userUpdatedPosition);
  251. userUpdated.lastUpdate=new Date(content.Content.lastUpdate);
  252. datalayer.updateUserFromQueue(userUpdated,updateDriverLocationDB);
  253. }
  254. function updateDriverLocationDB(result,content) {
  255. if (!result)
  256. return cb(false,content);
  257. return cb(true,'The user '+userid+' location has been updated');
  258. }
  259. }
  260. /**
  261. * Gets the last time that the user was updated
  262. * @param {string} userid The user id
  263. * @param {Function} cb The callback function
  264. */
  265. exports.getUserLastUpdate = function(userid,cb) {
  266. queryQueue('getUserLastUpdate',[userid],getUserLastUpdateQuery);
  267. function getUserLastUpdateQuery(result,content) {
  268. if (content.State!=='True')
  269. return cb(false,content.Error);
  270. return cb(true,new Date(content.Content));
  271. }
  272. }
  273. /**
  274. * Gets all the update times from the users on the queue
  275. * @param {Function} cb The callback function
  276. */
  277. exports.getAllLastUpdates = function(cb) {
  278. queryQueue('getAllLastUpdates',[],getAllLastUpdatesQuery);
  279. function getAllLastUpdatesQuery(result,content) {
  280. for (var i=0;i<content.Content.length;i++) {
  281. content.Content[i].lastUpdate=new Date(content.Content[i].lastUpdate);
  282. }
  283. cb(true,content.Content)
  284. }
  285. }