package eu.mosaic_cloud.sunutilities;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
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 SendMessageOnAQueue_Dev10 {
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");
String FileName="/Users/giuseppeaversano/Desktop/APP/APP_Queue_Step/Risultati_12.txt";
int[] numMsgs = {1,10,100};
int NITERS = 6;
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
JsonObjectfromString obj = gson.fromJson(new BufferedReader(
new InputStreamReader(new FileInputStream("/Users/giuseppeaversano/workspace/mosaic-applications-sun/BenchmarkQueueStep/TestQueue_Xml/Start_3.txt"))),
JsonObjectfromString.class);
for (int numMsg : numMsgs) {
for (int i = 0; i < NITERS; i++) {
postFile("/Users/giuseppeaversano/workspace/mosaic-applications-sun/BenchmarkQueueBlock/TestQueueBlock_Xml/Ready.txt");
//modifica l'oggetto come ti pare
obj.getParameters()[0] = "" + numMsg;
// System.out.println(gson.toJson(obj));
waitStatus("Ready");
postString(gson.toJson(obj));
waitStatus("Stopped");
}
}
postFile("/Users/giuseppeaversano/workspace/mosaic-applications-sun/BenchmarkQueueBlock/TestQueueBlock_Xml/Plot.txt");
Thread.sleep(5000);
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://10.1.1.200:1337/raw/default/2");
HttpResponse response = client.execute(get);
InputStream stream = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
String line = br.readLine();
System.out.println("Plot result is: " + line);
ScriviFile(FileName,line);
}
public static void postFile(String fileName)
throws ClientProtocolException, IOException {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://10.1.1.200: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.200: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(1000);
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://10.1.1.200: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,true);
BufferedWriter b =new BufferedWriter (w);
PrintWriter printout = new PrintWriter(b);
printout.println(Val);
printout.flush();
}
catch (Exception e) {
System.out.println("ERRORE: "+e);
}
}
}