TCPServer.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package server;
  2. import org.json.simple.JSONArray;
  3. import org.json.simple.JSONObject;
  4. import org.json.simple.parser.JSONParser;
  5. import org.json.simple.parser.ParseException;
  6. import java.io.*;
  7. import java.net.InetAddress;
  8. import java.net.ServerSocket;
  9. import java.net.Socket;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.Random;
  13. import java.util.concurrent.ExecutionException;
  14. import java.util.concurrent.ExecutorService;
  15. import java.util.concurrent.Executors;
  16. import java.util.concurrent.Future;
  17. public class TCPServer {
  18. public static void main(String[] args) throws Exception {
  19. TCPServer app = new TCPServer();
  20. app.start();
  21. }
  22. private ServerSocket server;
  23. private final String logPath = "/data/logs/";
  24. private final long startTime = System.currentTimeMillis();
  25. private final long startDataTime = 1351742401;
  26. private final long roundLength = 1800;
  27. private int round = 1;
  28. private File logfile;
  29. private FileWriter logFW;
  30. private BufferedWriter logBW;
  31. private Random rd = new Random();
  32. private static final int delayRange = 0;
  33. private static List<List<String>> lstOfLsts = new ArrayList<>();
  34. private static volatile boolean switchRound = false;
  35. private int lineDone = 0;
  36. public TCPServer() throws Exception {
  37. this.server = new ServerSocket(2345, 1, InetAddress.getLocalHost());
  38. System.out.println("\r\nRunning Server: " + "Host=" + getSocketAddress().getHostAddress() + " Port=" + getPort() + " Hostname=" + getSocketAddress().getHostName());
  39. logfile = new File(logPath + "/slogs.txt");
  40. boolean exists = logfile.exists();
  41. if (!exists) {
  42. logfile.createNewFile();
  43. }
  44. logFW = new FileWriter(logfile, true);
  45. logBW = new BufferedWriter(logFW);
  46. }
  47. void start() throws IOException {
  48. ExecutorService executor = Executors.newCachedThreadPool();
  49. while (true) {
  50. //this will block until a connection is made
  51. Socket client = this.server.accept();
  52. System.out.println("\r\nNew connection from " + client.getInetAddress().getHostAddress());
  53. executor.execute(new ServerThread(client));
  54. }
  55. }
  56. public InetAddress getSocketAddress() {
  57. return this.server.getInetAddress();
  58. }
  59. public int getPort() {
  60. return this.server.getLocalPort();
  61. }
  62. private class ServerThread implements Runnable {
  63. private Socket client;
  64. public ServerThread(Socket client) {
  65. this.client = client;
  66. }
  67. @Override
  68. public void run() {
  69. try {
  70. for(int i = 0; i < delayRange + 2; i++)
  71. lstOfLsts.add(new ArrayList<>());
  72. listen();
  73. this.client.close();
  74. } catch (IOException | ParseException | ExecutionException | InterruptedException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. void listen() throws IOException, ParseException, ExecutionException, InterruptedException {
  79. BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
  80. ExecutorService executor = Executors.newSingleThreadExecutor();
  81. String data = null;
  82. List<Future<?>> tasks = new ArrayList<>();
  83. while((data = in.readLine()) != null) {
  84. if(data.equals("end!")) {
  85. break;
  86. }
  87. //System.out.println("Message from " + client.getInetAddress().getHostAddress() + ": " + data);
  88. JSONObject obj = (JSONObject) new JSONParser().parse(data);
  89. long timestamp = Long.parseLong(String.valueOf(obj.get("timestamp")));
  90. long user_id = Long.parseLong(String.valueOf(obj.get("user_id")));
  91. JSONArray arr = (JSONArray) obj.get("hashtags");
  92. String[] hashtags = new String[arr.size()];
  93. for(int i=0; i<arr.size(); i++)
  94. hashtags[i]= String.valueOf(arr.get(i));
  95. Future<?> task = executor.submit(new Logger(user_id, hashtags, timestamp, client.getInetAddress().getHostAddress()));
  96. tasks.add(task);
  97. if(switchRound) {
  98. for(Future t : tasks)
  99. t.get();
  100. log();
  101. switchRound = false;
  102. }
  103. }
  104. for(List<String> l : lstOfLsts)
  105. for(String s : l) {
  106. logBW.write(s);
  107. logBW.newLine();
  108. incrementCount();
  109. }
  110. logBW.close();
  111. System.out.println("listening done");
  112. System.exit(0);
  113. }
  114. void log() throws IOException {
  115. List<String> logs = lstOfLsts.get(0);
  116. for(int i = 0; i < logs.size(); i++) {
  117. String s = logs.get(i);
  118. logBW.write(s);
  119. logBW.newLine();
  120. incrementCount();
  121. }
  122. lstOfLsts.remove(logs);
  123. lstOfLsts.add(new ArrayList<>());
  124. }
  125. private synchronized void incrementCount() {
  126. lineDone++;
  127. }
  128. }
  129. class Logger implements Runnable {
  130. private String[] hashtags;
  131. private long timestamp;
  132. private String senderAddress;
  133. private long id;
  134. Logger(long user_id, String[] hashtags, long timestamp, String senderAddress) {
  135. this.hashtags = hashtags;
  136. this.senderAddress = senderAddress;
  137. this.id = user_id;
  138. calculateTimeDiff(timestamp);
  139. }
  140. @Override
  141. public void run() {
  142. }
  143. String produceLogString() {
  144. String hashtagsString = "";
  145. for(String s : hashtags)
  146. hashtagsString = hashtagsString + (s + " ");
  147. return timestamp + "\t" + id + "\t" + hashtagsString.trim();
  148. }
  149. private void calculateTimeDiff(long stamp) {
  150. //long currentTime = System.currentTimeMillis();
  151. //long diffCurrentTime = currentTime - startTime;//in milis
  152. //this.timestamp = startDataTime + diffCurrentTime/1000;// in sec
  153. long diffStartTime = stamp - startDataTime;
  154. int inRound = (int)(diffStartTime / roundLength) + 1;
  155. int rdOffset = rd.nextInt(delayRange + 1);
  156. int rdRound = rdOffset + inRound;
  157. this.timestamp = startDataTime + roundLength * rdRound;
  158. //add to q
  159. List<String> targetLst;
  160. if(inRound > round) {
  161. round = inRound;
  162. switchRound = true;
  163. targetLst = lstOfLsts.get(rdOffset + 1);
  164. } else {
  165. targetLst = lstOfLsts.get(rdOffset);
  166. }
  167. targetLst.add(produceLogString());
  168. }
  169. }
  170. }