Control.java 4.18 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
package network.server;

import network.Requisition;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;

public class Control extends Thread {

private Socket connection;
private static int count=0;
ObjectOutputStream objoutstream;
ObjectInputStream objinstream;


public Control(Socket s) {
connection = s;

}

public void run() {

try {

Requisition req_received,req_answer;

// objetos que permitem controlar fluxo de comunicação

this.objoutstream = new ObjectOutputStream(connection.getOutputStream() );
this.objinstream = new ObjectInputStream(connection.getInputStream() );


System.out.println("Receiving proof obligation...");

req_received = (Requisition) this.objinstream.readObject();


System.out.println("The prover is working");


req_answer = this.callprover(req_received);



this.objoutstream.writeObject(req_answer);

System.out.println("Answer sended!");
this.connection.close();


}


catch (Exception e) {


e.printStackTrace();
}


}

protected Requisition callprover(Requisition req) {
Requisition req_answer = new Requisition();
StringBuffer output = new StringBuffer();
String path_po_file = new String(java.lang.System.getProperty("java.io.tmpdir") + File.separator+"proof"+(count++)+".smt");


// write the proof obligation to file
try{

FileWriter fstream = new FileWriter(path_po_file);
BufferedWriter out = new BufferedWriter(fstream);
String pO = req.getProofobligation();
String novapO = "";
for (int i=0; i< pO.length() ;i++) {
if (pO.charAt(i) == 13) {
novapO = novapO + ' ';
}
else {
novapO = novapO + pO.charAt(i);
}
}
out.write(novapO);
out.close();
} catch (IOException e) {
System.err.println("Error writing the file from proof obligation.");
}

// call the prover

try {
String[] tmp = req.getProvername().split(" ");
String [] commands = new String[ tmp.length + 1 ];
for(int i=0;i<tmp.length;i++)
commands[i]=tmp[i];
commands[tmp.length]=path_po_file;
String arquivo = commands[commands.length -1];
String parametros = "";
//String[] commands = new String[]{req.getProvername().split(" "),path_po_file };
//System.out.println("Comando executado"+req.getProvername());
for (int i=0; i < commands.length -1; i++) {
parametros = parametros + commands[i] + " ";
}
System.out.println(parametros + arquivo);
Process child = Runtime.getRuntime().exec(parametros + arquivo);

// Get the input stream and read from it
System.out.println("Waiting answer!");

InputStream in = child.getInputStream();
InputStream erro = child.getErrorStream();
int c;

while ((c = in.read()) != -1) {
output.append((char)c);
}
in.close();
String error = "";
int k;
if (error!= null) {
while ((k = erro.read()) != -1) {
error = error + ((char)k);
}
erro.close();
}
if (error != "")
System.err.println("Error Message in Prover: " + error);


} catch (IOException e) {
e.printStackTrace();
System.err.println("Error calling the prover!");
}
System.out.println("OUT " + output.toString());
String[] lines = returnLines(output.toString());

String respostaHarvey = "";
if (lines.length > 0) {
respostaHarvey = lines[lines.length - 1];
}
req_answer.setResult( new String( respostaHarvey) );
return req_answer;

}
private static String[] returnLines(String str)
{
String lineSep = System.getProperty("line.separator");
StringTokenizer st = new StringTokenizer(str,lineSep);
List<String> list = new Vector<String>();
while (st.hasMoreTokens()) {
list.add(st.nextToken());
}
String[] retorno = list.toArray(new String[0]);
return retorno;
}

}