|
|
@ -35,52 +35,62 @@ class TCPTimeCounterServer { |
|
|
|
// Anfrage vom Client empfangen |
|
|
|
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); |
|
|
|
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); |
|
|
|
String input = null; |
|
|
|
|
|
|
|
// Vom Client gesendete Zahl einlesen und auf aktuellen Zählerstand addieren |
|
|
|
long number = new Long(new String(inFromClient.readLine())); |
|
|
|
long number; |
|
|
|
try { |
|
|
|
input = inFromClient.readLine(); |
|
|
|
} catch (IOException ie) { |
|
|
|
// TODO Auto-generated catch block |
|
|
|
ie.printStackTrace(); |
|
|
|
} |
|
|
|
|
|
|
|
// Verbindung zum Redis Cluster (Amazon ElastiCache) |
|
|
|
//JedisPool pool = new JedisPool(new JedisPoolConfig(), redisClusterURL); |
|
|
|
//Jedis jedis = pool.getResource(); |
|
|
|
//jedis.connect(); |
|
|
|
//JedisCluster jedisCluster = new JedisCluster(new HostAndPort(redisClusterURL, 6379)); |
|
|
|
//System.out.println("Connected to redis server"); |
|
|
|
// check whether server is running or not |
|
|
|
//System.out.println("redis server is running: " + jedis.ping()); |
|
|
|
//System.out.println("redis server cluster info: " + jedis.clusterInfo()); |
|
|
|
// Variable für Zählerstand des Servers, dieser Zähler wird durch Anfragen der |
|
|
|
// Clients erhöht |
|
|
|
//jedisCluster.incrBy("vertsys-counter", number); |
|
|
|
//long counter = new Long(jedisCluster.get("vertsys-counter")); |
|
|
|
//jedisCluster.close(); |
|
|
|
//System.out.println("TTL:" + jedisCluster.ttl("vertsys-counter")); |
|
|
|
//System.out.println("Anfrage von Client " + connectionSocket.getInetAddress() + ":" + connectionSocket.getPort() + " Zahl: " + number + " Zählerstand: " + counter); |
|
|
|
if (input != null) { |
|
|
|
number = new Long(input); |
|
|
|
// Verbindung zum Redis Cluster (Amazon ElastiCache) |
|
|
|
//JedisPool pool = new JedisPool(new JedisPoolConfig(), redisClusterURL); |
|
|
|
//Jedis jedis = pool.getResource(); |
|
|
|
//jedis.connect(); |
|
|
|
//JedisCluster jedisCluster = new JedisCluster(new HostAndPort(redisClusterURL, 6379)); |
|
|
|
//System.out.println("Connected to redis server"); |
|
|
|
// check whether server is running or not |
|
|
|
//System.out.println("redis server is running: " + jedis.ping()); |
|
|
|
//System.out.println("redis server cluster info: " + jedis.clusterInfo()); |
|
|
|
// Variable für Zählerstand des Servers, dieser Zähler wird durch Anfragen der |
|
|
|
// Clients erhöht |
|
|
|
//jedisCluster.incrBy("vertsys-counter", number); |
|
|
|
//long counter = new Long(jedisCluster.get("vertsys-counter")); |
|
|
|
//jedisCluster.close(); |
|
|
|
//System.out.println("TTL:" + jedisCluster.ttl("vertsys-counter")); |
|
|
|
//System.out.println("Anfrage von Client " + connectionSocket.getInetAddress() + ":" + connectionSocket.getPort() + " Zahl: " + number + " Zählerstand: " + counter); |
|
|
|
|
|
|
|
//Verbindung zu S3 |
|
|
|
AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient(); |
|
|
|
String region = s3Client.getRegionName(); |
|
|
|
if (!s3Client.doesObjectExist("vertsys-counter", region)) |
|
|
|
{ |
|
|
|
s3Client.putObject(bucketName, region, new Long(0).toString()); |
|
|
|
} |
|
|
|
long counter = new Long(s3Client.getObjectAsString(bucketName, region)); |
|
|
|
counter = counter + number; |
|
|
|
s3Client.putObject(bucketName, region, new Long(counter).toString()); |
|
|
|
//Verbindung zu S3 |
|
|
|
AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient(); |
|
|
|
String region = s3Client.getRegionName(); |
|
|
|
if (!s3Client.doesObjectExist("vertsys-counter", region)) |
|
|
|
{ |
|
|
|
s3Client.putObject(bucketName, region, new Long(0).toString()); |
|
|
|
} |
|
|
|
long counter = new Long(s3Client.getObjectAsString(bucketName, region)); |
|
|
|
counter = counter + number; |
|
|
|
s3Client.putObject(bucketName, region, new Long(counter).toString()); |
|
|
|
|
|
|
|
// Try/Catch hinzugefügt, nachdem bei Einsatz Amazon AWS (Healthcheck des ELB) clientSentence null war |
|
|
|
try { |
|
|
|
// Antwort vom Server senden |
|
|
|
String output = (Long.toString(counter) + " Zeit: " + System.currentTimeMillis() + " " |
|
|
|
+ df.format(new Date(System.currentTimeMillis()))); |
|
|
|
System.out.println("Antwort des Servers: " + output); |
|
|
|
// Zeilenumbruch anfügen, da Client mit readLine auf komplette Zeile wartet |
|
|
|
outToClient.writeBytes(output + '\n'); |
|
|
|
} catch (IOException ioe) { |
|
|
|
ioe.printStackTrace(); |
|
|
|
} |
|
|
|
|
|
|
|
// Try/Catch hinzugefügt, nachdem bei Einsatz Amazon AWS (Healthcheck des ELB) clientSentence null war |
|
|
|
try { |
|
|
|
// Antwort vom Server senden |
|
|
|
String output = (Long.toString(counter) + " Zeit: " + System.currentTimeMillis() + " " |
|
|
|
+ df.format(new Date(System.currentTimeMillis()))); |
|
|
|
System.out.println("Antwort des Servers: " + output); |
|
|
|
// Zeilenumbruch anfügen, da Client mit readLine auf komplette Zeile wartet |
|
|
|
outToClient.writeBytes(output + '\n'); |
|
|
|
} catch (IOException ioe) { |
|
|
|
ioe.printStackTrace(); |
|
|
|
} catch (NullPointerException npe) { |
|
|
|
npe.printStackTrace(); |
|
|
|
} |
|
|
|
connectionSocket.close(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |