You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.7 KiB

  1. package verteiltesysteme.aws;
  2. import java.io.BufferedReader;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.InetAddress;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;
  9. import java.text.DateFormat;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Date;
  12. import com.amazonaws.services.s3.AmazonS3;
  13. import com.amazonaws.services.s3.AmazonS3ClientBuilder;
  14. class TCPTimeCounterServer {
  15. public static void main(String args[]) throws Exception {
  16. int tcpPort = 36038;
  17. String bucketName = "vertsys-counter";
  18. String bucketRegion = "eu-central-1";
  19. //String redisClusterURL = "vertsys-ec1-0001-002.71rxr9.0001.euc1.cache.amazonaws.com";
  20. DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
  21. // Socket erzeugen
  22. // Warnung, dass Socket nicht geschlossen wird unterdr�cken ;)
  23. @SuppressWarnings("resource")
  24. ServerSocket serverSocket = new ServerSocket(tcpPort);
  25. System.out.println("TCP Time Counter Server ready... waiting for packets...");
  26. while (true) {
  27. // Auf Anfrage vom Client warten
  28. Socket connectionSocket = serverSocket.accept();
  29. // Anfrage vom Client empfangen
  30. BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
  31. DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
  32. String input = null;
  33. // Vom Client gesendete Zahl einlesen und auf aktuellen Z�hlerstand addieren
  34. long number;
  35. try {
  36. input = inFromClient.readLine();
  37. } catch (IOException ie) {
  38. // TODO Auto-generated catch block
  39. ie.printStackTrace();
  40. }
  41. if (input != null) {
  42. number = new Long(input);
  43. // Verbindung zum Redis Cluster (Amazon ElastiCache)
  44. //JedisPool pool = new JedisPool(new JedisPoolConfig(), redisClusterURL);
  45. //Jedis jedis = pool.getResource();
  46. //jedis.connect();
  47. //JedisCluster jedisCluster = new JedisCluster(new HostAndPort(redisClusterURL, 6379));
  48. //System.out.println("Connected to redis server");
  49. // check whether server is running or not
  50. //System.out.println("redis server is running: " + jedis.ping());
  51. //System.out.println("redis server cluster info: " + jedis.clusterInfo());
  52. // Variable f�r Z�hlerstand des Servers, dieser Z�hler wird durch Anfragen der
  53. // Clients erh�ht
  54. //jedisCluster.incrBy("vertsys-counter", number);
  55. //long counter = new Long(jedisCluster.get("vertsys-counter"));
  56. //jedisCluster.close();
  57. //System.out.println("TTL:" + jedisCluster.ttl("vertsys-counter"));
  58. //Verbindung zu S3
  59. AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
  60. if (!s3Client.doesObjectExist("vertsys-counter", bucketRegion))
  61. {
  62. s3Client.putObject(bucketName, bucketRegion, new Long(0).toString());
  63. }
  64. long counter = new Long(s3Client.getObjectAsString(bucketName, bucketRegion));
  65. counter = counter + number;
  66. s3Client.putObject(bucketName, bucketRegion, new Long(counter).toString());
  67. System.out.println("Request from Client " + connectionSocket.getInetAddress() + ":" + connectionSocket.getPort() + " Zahl: " + number + " Z�hlerstand: " + counter);
  68. // Try/Catch hinzugef�gt, nachdem bei Einsatz Amazon AWS (Healthcheck des ELB) clientSentence null war
  69. try {
  70. // Antwort vom Server senden
  71. String output = (Long.toString(counter) + " Zeit: " + System.currentTimeMillis() + " "
  72. + df.format(new Date(System.currentTimeMillis())) + " " + InetAddress.getLocalHost());
  73. System.out.println("Reply from Server: " + output);
  74. // Zeilenumbruch anf�gen, da Client mit readLine auf komplette Zeile wartet
  75. outToClient.writeBytes(output + '\n');
  76. } catch (IOException ioe) {
  77. ioe.printStackTrace();
  78. }
  79. }
  80. connectionSocket.close();
  81. }
  82. }
  83. }