ioListener.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * TraCINg-Server - Gathering and visualizing cyber incidents on the world
  3. *
  4. * Copyright 2013 Matthias Gazzari, Annemarie Mattmann, André Wolski
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /*
  19. * This module provides a real time connection service by socket.io
  20. */
  21. var socketio = require("socket.io");
  22. // custom modules
  23. //var db = require("./db/db.js");
  24. var db = require("./db/database.js");
  25. var fields = require("./fields.js");
  26. function listen(server) {
  27. io = socketio.listen(server);
  28. io.sockets.on("connection", function (socket) {
  29. console.log("got a connection");
  30. socket.emit("setFields", fields.fields);
  31. db.getSensorTypes(function(err, sensors){
  32. console.log(arguments);
  33. if (err) {
  34. socket.emit("error", err);
  35. console.log(err);
  36. return;
  37. }
  38. socket.emit("setSensors", sensors);
  39. });
  40. socket.on("requestAttacks", function (filter) {
  41. console.log("requestAttacks:", filter);
  42. try{
  43. db.requestAttacks(filter, function(err, data){
  44. if (err) {
  45. socket.emit("error", err);
  46. return;
  47. }
  48. // TODO use socket callback instead of the socket event
  49. socket.emit("incidents", data);
  50. }, {type: "string"});
  51. }
  52. catch(e){
  53. console.log("Internal error. " + e);
  54. console.log(e.stack);
  55. socket.emit("error", {code: "internal error"});
  56. }
  57. });
  58. socket.on("requestAttacksForTable", function(filter){
  59. console.log("requestAttacksTable:", filter);
  60. try{
  61. db.requestAttacks(filter, function(err, data){
  62. if (err) {
  63. socket.emit("error", err);
  64. return;
  65. }
  66. socket.emit("incidentsTable", data);
  67. });
  68. }
  69. catch(e){
  70. console.log("Internal error. " + e);
  71. console.log(e.stack);
  72. socket.emit("error", {code: "internal error"});
  73. }
  74. });
  75. socket.on("getLog", function (data, socketCallback) {
  76. console.log("getLog");
  77. console.log(data);
  78. try{
  79. db.getLog(data, function(err, data){
  80. if (err) {
  81. socket.emit("error", err);
  82. }
  83. // socket callback also on error
  84. socketCallback(data);
  85. });
  86. }
  87. catch(e){
  88. console.log("Internal error. " + e);
  89. console.log(e.stack);
  90. socket.emit("error", {code: "internal error"});
  91. }
  92. });
  93. socket.on("getStatistics", function (options, socketCallback) {
  94. console.log("getStatistics", options);
  95. try{
  96. db.getStatistics(options, function(err, data){
  97. if(err){
  98. socket.emit("error", err);
  99. return;
  100. }
  101. socketCallback(data);
  102. });
  103. }
  104. catch(e){
  105. console.log("Internal error. " + e);
  106. console.log(e.stack);
  107. socket.emit("error", {code: "internal error"});
  108. }
  109. });
  110. });
  111. return io;
  112. }
  113. exports.listen = listen;