ClientLogParser.java 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package analyzer;
  2. import analyzer.models.Client;
  3. import analyzer.models.Round;
  4. import java.io.BufferedReader;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.util.*;
  8. /**
  9. * This class reads the client logs, creates objects and organizes them into data structures.
  10. * An instance of this class must run cooperatively with an instance of class ServerLogParser
  11. */
  12. public class ClientLogParser{
  13. private static long startRoundTime = 1351742401;
  14. public static volatile int clientRoundNo = 0;
  15. public static volatile boolean DONE = false;
  16. public static volatile int clientLineCounter = 0;
  17. public static volatile Map<String, Client> clients = new Hashtable<>(Analyzer.clientNo, 1);
  18. public static void run() {
  19. try {
  20. parse();
  21. } catch (IOException | InterruptedException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. private static void parse() throws IOException, InterruptedException {
  26. FileReader fr = new FileReader(Analyzer.clientLogPath);
  27. BufferedReader br= new BufferedReader(fr);
  28. String line;
  29. int roundLinesCount = 0;
  30. while((line = br.readLine()) != null) {
  31. clientLineCounter++;
  32. roundLinesCount++;
  33. String[] elements = line.trim().split("\t");//client-id destination timestamp
  34. long timestamp = Long.parseLong(elements[2]);
  35. String client_id = elements[0];
  36. //get log start time
  37. if(clientLineCounter == 1){
  38. clientRoundNo = 1;
  39. }
  40. //parse behind ServerLogParser
  41. while (ClientLogParser.clientRoundNo >= ServerLogParser.serverRoundNo - 1 && !ServerLogParser.DONE)
  42. Thread.onSpinWait();
  43. //next round
  44. if(timestamp >= startRoundTime + Analyzer.roundLength) {
  45. //System.out.println(clientRoundNo + " " + roundLinesCount);
  46. startRoundTime += Analyzer.roundLength;
  47. clientRoundNo++;
  48. roundLinesCount = 1;
  49. }
  50. addUser(client_id, clientRoundNo);
  51. //if(ClientLogParser.clientLineCounter == ServerLogParser.serverLineCounter && ServerLogParser.DONE)
  52. // break;
  53. }
  54. //executor.shutdown();
  55. System.out.println("No of Rounds " + clientRoundNo);
  56. System.out.println(clients.size() + " clients found");
  57. //printOut();;
  58. System.out.println();
  59. br.close();
  60. DONE = true;
  61. }
  62. private static void addUser(String id, int round) {
  63. Client c = clients.get(id);
  64. if (c == null) {
  65. c = new Client(id);
  66. clients.put(id, c);
  67. }
  68. Round r = ServerLogParser.rounds.get(round);
  69. c.addRound(r);
  70. }
  71. }