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 ClientRecuperoFileAppend {
public static void main(String[] args) throws ClientProtocolException,
IOException, InterruptedException {
TimeAndSize2[] timeAndSizes = readTimesFromFile("/Users/giuseppeaversano/Desktop/File_di_Test/XmlAnalyzerKv/times3.txt");
for(int i=0; i< 3000 ;i++){
Thread.sleep(1000);
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://10.1.1.203:1337/raw/DatoStore/2");
HttpResponse response = client.execute(get);
InputStream stream = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
String line = br.readLine();
ScriviFileAppend("/Users/giuseppeaversano/Desktop/File_di_Test/XmlAnalyzerKvTempi3_2_casa/Result_Xml100_"+i+".txt",line);
}
}
private static TimeAndSize2[] readTimesFromFile(String fileName) throws IOException {
Vector<TimeAndSize2> timeAndSizes = new Vector<TimeAndSize2>();
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 TimeAndSize2(time, size));
line = br.readLine();
}
TimeAndSize2[] timeAndSizeArray = new TimeAndSize2[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);
}
}
public static void ScriviFileAppend(String fileName, String Val){
try{
FileWriter w=new FileWriter(fileName,true);
BufferedWriter b =new BufferedWriter (w);
PrintWriter printout = new PrintWriter(b);
printout.println(Val);
printout.flush();
}
catch (Exception e) {
System.out.println("ERRORE: "+e);
}
}
}
class TimeAndSize2{
private double time;
private int size;
public TimeAndSize2(double time, int size) {
this.time = time;
this.size = size;
}
public int getSize() {
return size;
}
public double getTime() {
return time;
}
}