package eu.mosaic_cloud.sunutilities;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.Vector;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class ClientTime {
public static void main(String[] args) throws ClientProtocolException,
IOException, InterruptedException {
//File f1 = new File("/Users/giuseppeaversano/Desktop/File_di_Test/Queue/Ready.txt");
//File f2 = new File("/Users/giuseppeaversano/Desktop/File_di_Test/Queue/Start_1.txt");
//File f3 = new File("/Users/giuseppeaversano/Desktop/File_di_Test/Queue/Plot.txt");
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
JsonObjectfromString obj4 = gson.fromJson(new BufferedReader(
new InputStreamReader(new FileInputStream("/Users/giuseppeaversano/Desktop/File_di_Test/XmlAnalyzerKv/Start4.txt"))),
JsonObjectfromString.class);
JsonObjectfromString obj8 = gson.fromJson(new BufferedReader(
new InputStreamReader(new FileInputStream("/Users/giuseppeaversano/Desktop/File_di_Test/XmlAnalyzerKv/Start8.txt"))),
JsonObjectfromString.class);
JsonObjectfromString obj12 = gson.fromJson(new BufferedReader(
new InputStreamReader(new FileInputStream("/Users/giuseppeaversano/Desktop/File_di_Test/XmlAnalyzerKv/Start12.txt"))),
JsonObjectfromString.class);
String string4 = gson.toJson(obj4);
String string8 = gson.toJson(obj8);
String string12 = gson.toJson(obj12);
TimeAndSize[] timeAndSizes = readTimesFromFile("/Users/giuseppeaversano/Desktop/File_di_Test/XmlAnalyzerKv/times1.txt");
for(TimeAndSize timeAndSize: timeAndSizes){
double nextWaitTime = timeAndSize.getTime();
int nextMsgSize = timeAndSize.getSize();
long millisWaitTime = (long) (nextWaitTime * 1000);
System.out.println("Sleeping " + millisWaitTime + "ms");
Thread.sleep(millisWaitTime);
System.out.println("Sending msg of size " + nextMsgSize);
switch (nextMsgSize) {
case 4: postString(gson.toJson(obj4));
break;
case 8: postString(gson.toJson(obj8));
break;
case 12: postString(gson.toJson(obj12));
break; }
}
}
private static TimeAndSize[] readTimesFromFile(String fileName) throws IOException {
Vector<TimeAndSize> timeAndSizes = new Vector<TimeAndSize>();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName))));
String line = br.readLine();
while(line !=null){
StringTokenizer st = new StringTokenizer(line, "*");
double time = Double.parseDouble(st.nextToken());
int size = Integer.parseInt(st.nextToken());
System.out.println("Read "+ time + " and " + size);
timeAndSizes.add(new TimeAndSize(time, size));
line = br.readLine();
}
TimeAndSize[] timeAndSizeArray = new TimeAndSize[timeAndSizes.size()];
timeAndSizes.toArray(timeAndSizeArray);
br.close();
return timeAndSizeArray;
}
public static void postFile(String fileName)
throws ClientProtocolException, IOException {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://10.1.1.203:1337");
MultipartEntity entity = new MultipartEntity();
entity.addPart("file", new FileBody(new File(fileName)));
post.setEntity(entity);
HttpResponse response = client.execute(post);
}
public static void postString(String string)
throws ClientProtocolException, IOException {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://10.1.1.203:1337");
MultipartEntity entity = new MultipartEntity();
entity.addPart("file", new StringBody(string));
post.setEntity(entity);
HttpResponse response = client.execute(post);
}
public static void waitStatus(String status) throws IllegalStateException,
IOException, InterruptedException {
String line;
do {
Thread.sleep(300);
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://10.1.1.203:1337/raw/storeStato/2");
HttpResponse response = client.execute(get);
InputStream stream = response.getEntity().getContent();
BufferedReader br = new BufferedReader(
new InputStreamReader(stream));
line = br.readLine();
System.out.println("Status is " + line);
} while (line.equalsIgnoreCase(status) == false);
}
public static void ScriviFile(String fileName, String Val){
try{
FileWriter w=new FileWriter(fileName);
BufferedWriter b =new BufferedWriter (w);
PrintWriter printout = new PrintWriter(b);
printout.println(Val);
printout.flush();
}
catch (Exception e) {
System.out.println("ERRORE: "+e);
}
}
}
class TimeAndSize{
private double time;
private int size;
public TimeAndSize(double time, int size) {
this.time = time;
this.size = size;
}
public int getSize() {
return size;
}
public double getTime() {
return time;
}
}