Math2mat

/home/ythoma/docs/math2mat/svn/wp1/framework/m2mGUI/src/m2m/backend/verifpga/M2MClient.java

Go to the documentation of this file.
00001 
00022 package m2m.backend.verifpga;
00023 
00024 import java.io.BufferedInputStream;
00025 import java.io.BufferedReader;
00026 import java.io.DataOutputStream;
00027 import java.io.File;
00028 import java.io.FileInputStream;
00029 import java.io.FileOutputStream;
00030 import java.io.IOException;
00031 import java.io.InputStreamReader;
00032 import java.net.*;
00033 import java.util.*;
00034 
00035 import m2m.backend.processing.Errors;
00036 import m2m.backend.processing.ProcessWatcher;
00037 import m2m.backend.project.ExternalToolsProperties;
00038 import m2m.backend.project.M2MProject;
00039 
00040 public class M2MClient {
00041 
00042         public final static int M2M_TEST_PORT = 55444;
00043         
00044         public final static int COMMAND_DEBUG = 2;
00045         public final static int COMMAND_NBTESTS = 3;
00046         public final static int COMMAND_INPUTDATA = 4;
00047         public final static int COMMAND_STARTM2M = 5;
00048         public final static int COMMAND_XSVF = 6;
00049         public final static int COMMAND_NBINPUTS = 7;
00050         public final static int COMMAND_NBOUTPUTS = 8;
00051         
00052         
00053         //public final static String server_name = "localhost";
00054 //      public final static String server_name = "10.192.51.40";
00055         
00056         private M2MData m2mData;
00057         private String isePath;
00058                 
00059         private M2MProject project;
00060         
00061 
00065         public M2MClient() {
00066         }
00067 
00076         public boolean testOnUbidule(int nbrInput, int nbrOutput, int nbrSamples, M2MProject project) {
00077                 m2mData = new M2MData(nbrSamples,nbrInput,nbrOutput, project.getIOFilesPath()+"/");
00078                 
00079                 m2mData.readInputData();
00080                 this.isePath = project.getProperties().getProjectPath() + "/"+ M2MProject.getUbiduleDirName();
00081                 this.project = project;
00082 
00083 //              System.out.println("Generating XSVF file");
00084 //              generateXSVF();
00085 //              System.out.println("XSVF is generated...");
00086                 Socket sock;
00087                 try {
00088                         sock = new Socket(ExternalToolsProperties.getReference().getUbiduleIP(), M2M_TEST_PORT);
00089 
00090                 }catch (IOException e) {
00091                                 System.err.println(e);
00092                                 Errors.setLastError(Errors.ErrorNum.UBIDULECONNECTIONERROR);
00093                                 return false;
00094                 }
00095                 
00096                 BinaryOut os = new BinaryOut(sock);
00097                 BinaryIn is = new BinaryIn(sock);
00098 
00099                 sendXSVF(os);
00100                         
00101                 sendNbInputs(os);
00102                 sendNbOutputs(os);
00103                 sendNbTests(os);
00104                 sendInputData(os);
00105 
00106                         
00107                 sendM2MStart(os);
00108                 
00109                 os.flush();
00110                 
00111                          
00112                 receiveM2MData(is);
00113 
00114                         
00115                 compareOctaveFPGA();
00116                         
00117                 try {
00118                         sock.close();
00119                 }
00120                 catch (IOException e) {
00121                         
00122                 }
00123                 
00124                 return true;
00125         }
00126 
00127 
00128         
00134         private void receiveM2MData(BinaryIn is) {
00135                 float data;
00136                 LinkedList<Float> dataList = new LinkedList<Float>();
00137 
00138                 System.out.println("Waiting for the reception of M2MData...");
00139                 int sizeBlock = is.readInt();
00140                 //os.writeInt(sizeBlock/4-1);
00141 //              System.out.println("Blocksize: " + (sizeBlock/4-1));
00142                 
00143                 // there is a *one* in the files produced by Octave...
00144                 //os.writeInt(1);
00145                 
00146                 /*int command = */is.readInt();
00147 //              System.out.println("Command: " + command);
00148                 
00149                 int nbData=is.readInt();
00150                 for (int i=0; i<nbData; i++) {
00151                         data = is.readFloat();
00152                         //os.writeFloat(data);
00153                         dataList.add(data);
00154 //                      System.out.println("From server " + i + " " + data);
00155                 }
00156                 int time=is.readInt();
00157                 System.out.println("Time of treatement: " + time);
00158                 
00159 //              int latency=is.readInt();
00160                 is.readInt();
00161 //              System.out.println("Latency: " + latency);
00162                 
00163                 
00164                 // create files with results of M2M treatment
00165                 DataOutputStream[] os = new DataOutputStream[m2mData.getNbOutputs()];
00166                 try {
00167                         //fileoutputstream = new FileOutputStream("file.dat");
00168 
00169                         for (int i=0; i<m2mData.getNbOutputs(); i++) {
00170                                 os[i] = new DataOutputStream(new FileOutputStream(project.getIOFilesPath() + "/file_output" + (i+1) + "_FPGA.dat"));
00171                                 os[i].writeInt((sizeBlock/4-1)/m2mData.getNbOutputs());
00172                                 os[i].writeInt(1);
00173                         }
00174                         
00175                         for (int i=0; i<dataList.size(); i++) {
00176                                 os[0].writeFloat(dataList.get(i));
00177                         }
00178                         
00179                 
00180             } catch (IOException e) {
00181                         e.printStackTrace();
00182                         System.err.println("There is a problem into M2MClient.receiveM2MData...");
00183                 }
00184         }
00185 
00191         private boolean compareOctaveFPGA() {
00192                 int cnt = 0;
00193                 
00194                 BinaryIn[] isFiles = new BinaryIn[m2mData.getNbOutputs()];
00195                 BinaryIn[] isFilesFPGA = new BinaryIn[m2mData.getNbOutputs()];
00196                 
00197                 for (int i=0; i<m2mData.getNbOutputs(); i++) {
00198                         isFiles[i] = new BinaryIn(project.getIOFilesPath() + "/file_output"+(i+1)+".dat"); // enumeration of input data files starts at one
00199                         isFilesFPGA[i] = new BinaryIn(project.getIOFilesPath() + "/file_output"+(i+1)+"_FPGA.dat"); // enumeration of input data files starts at one
00200                 }
00201                 
00202                 int nbTests;
00203                 
00204                 System.out.println("file_output" +(1)+ ".dat, nbsamples: " + isFiles[0].readInt());
00205                 System.out.println("file_output" +(1)+ ".dat, size: " + isFiles[0].readInt());
00206                 nbTests = isFilesFPGA[0].readInt();
00207                 System.out.println("file_output" +(1)+ "_FPGA.dat, nbsamples: " + nbTests);
00208                 System.out.println("file_output" +(1)+ "_FPGA.dat, size: " + isFilesFPGA[0].readInt());
00209 
00210 //              System.out.println("file_output" +(2)+ ".dat, nbsamples: " + isFiles[1].readInt());
00211 //              System.out.println("file_output" +(2)+ ".dat, size: " + isFiles[1].readInt());
00212 //
00213 //              System.out.println("file_output" +(2)+ "_FPGA.dat, nbsamples: " + isFilesFPGA[1].readInt());
00214 //              System.out.println("file_output" +(2)+ "_FPGA.dat, size: " + isFilesFPGA[1].readInt());         
00215 
00216 //              if (nbTests != m2mData.getNbTests()) {
00217 //                      System.err.println("Error, number of samples is wrong...");
00218 //                      return false;
00219 //              }
00220                 
00221                 for (int currentNbOut=0; currentNbOut<m2mData.getNbOutputs(); currentNbOut++) {
00222                         for (int currentNbSample=0; currentNbSample<m2mData.getNbTests()-1; currentNbSample++) {
00223                                 float dataOctave;
00224                                 float dataFPGA;
00225                                 float absVal;
00226                                 dataOctave  = isFiles[currentNbOut].readFloat();
00227                                 dataFPGA        = isFilesFPGA[currentNbOut].readFloat();
00228                                 absVal = dataOctave - dataFPGA;
00229                                 
00230                                 if (absVal < 0 )
00231                                         absVal = -absVal;
00232                                 
00233                                 if (absVal > 0.1) {
00234                                         System.err.println("Files nbr " + currentNbOut + " ,sample nbr " + currentNbSample + " " + dataOctave + " vs " + dataFPGA);
00235                                         ++cnt;
00236                                 }
00237                                 
00238                         }
00239                         
00240                         
00241                 }
00242                 
00243                 System.err.println("There are " + cnt + " errors in " + m2mData.getNbOutputs()*m2mData.getNbTests() + " samples");
00244 
00245 
00246                 return true;
00247         }
00248 
00254         private void sendNbTests(BinaryOut os) {
00255                 System.out.println("sendNbTest...");
00256                 os.write(COMMAND_NBTESTS);
00257                 os.write(4);
00258                 os.write(m2mData.getNbTests());
00259                 os.flush();
00260         }
00261 
00267         private void sendNbInputs(BinaryOut os) {
00268                 System.out.println("sendNbInputs...");
00269                 os.write(COMMAND_NBINPUTS);
00270                 os.write(4);
00271                 os.write(m2mData.getNbInputs());
00272                 os.flush();
00273         }
00274         
00280         private void sendNbOutputs(BinaryOut os) {
00281                 System.out.println("sendNbOutputs...");
00282                 os.write(COMMAND_NBOUTPUTS);
00283                 os.write(4);
00284                 os.write(m2mData.getNbOutputs());
00285                 os.flush();
00286         }
00287         
00293         private void sendInputData(BinaryOut os) {
00294                 System.out.println("sendInputData...");
00295                 float entry;
00296                 
00297                 int inputSize = m2mData.getNbTests()*m2mData.getNbInputs()*4;
00298                 
00299                 os.write(COMMAND_INPUTDATA);    
00300                 os.write(inputSize);
00301 
00302                 for (int currentTest=0; currentTest<m2mData.getNbTests(); currentTest++) {
00303                         for (int currentIn=0; currentIn<m2mData.getNbInputs(); currentIn++) {
00304                                 entry = (Float) m2mData.getInputData()[currentIn].get(currentTest);
00305                                 os.write(entry);
00306                         }
00307                 }
00308                 
00309                 os.flush();
00310         }
00311 
00312         
00318         private void sendM2MStart(BinaryOut os) {
00319                 System.out.println("sendM2MStart...");
00320                 os.write(COMMAND_STARTM2M);
00321                 os.write(0);
00322                 os.flush();
00323         }
00324 
00330         private void sendXSVF(BinaryOut os) {
00331                 System.out.println("sendXSVF...");
00332                 os.write(COMMAND_XSVF);
00333                 
00334                 // FIXME if the file doesn't exist ...
00335                 File myFile = new File (isePath + "/M2M_XSVF_generated.xsvf");
00336                 //File myFile = new File ("data.dat");
00337                 byte [] mybytearray  = new byte [(int)myFile.length()];
00338                 System.out.println("File length: " + myFile.length());
00339                 
00340                 // block size
00341                 os.write((int)myFile.length());
00342           
00343                 try
00344                 {
00345                         FileInputStream fis = new FileInputStream(myFile);
00346                         BufferedInputStream bis = new BufferedInputStream(fis);
00347                         bis.read(mybytearray,0,mybytearray.length);
00348   
00349                 } catch (IOException e)
00350                 {
00351                         e.printStackTrace();
00352                 }
00353           
00354                 System.out.println("Sending xsvf...");
00355         
00356                 // write xsvf file to socket
00357                 for (int i=0; i<mybytearray.length; i++) {
00358                         os.write(mybytearray[i]);
00359                 }
00360 
00361                 os.flush();
00362                 
00363         }
00364         
00365         
00366         public static boolean generateXSVF(M2MProject project) {
00367                 String isePath = project.getProperties().getProjectPath() + M2MProject.getUbiduleDirName();
00368 
00369 
00370                 String line = "";
00371                 BufferedReader brOctave;
00372                 BufferedReader brErrOctave;
00373                 
00374                 Errors.clearError();
00375                 try
00376                 {         
00377 
00378                         // change write access of script to execute, otherwise an error
00379                         File scriptGenXSVF = new File(isePath  + "/script_gen_xsvf");
00380                         scriptGenXSVF.setExecutable(true);
00381                                                  
00382                     Process process = new ProcessBuilder(isePath  + "/script_gen_xsvf").start();
00383                     
00384                     ProcessWatcher.getInstance().addProcess(process);
00385                     
00386                         //create streams to communicate with Octave application
00387         //              BufferedWriter bwOctave;
00388         //              bwOctave  = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
00389                         brOctave = new BufferedReader(new InputStreamReader(process.getInputStream()));
00390                         brErrOctave = new BufferedReader(new InputStreamReader(process.getErrorStream()));
00391                 
00392 
00393                         if (brOctave == null || brErrOctave == null)
00394                                 return false;
00395 
00396                         while (true) {
00397                                 try {
00398                                         if ((line = brOctave.readLine()) != null)
00399                                                 System.out.println(line);
00400                                         else
00401                                                 break;
00402                                 } catch (IOException e1) {
00403                                         // TODO Auto-generated catch block
00404                                         e1.printStackTrace();
00405                                 }
00406                         }
00407                         while (true) {
00408                                 try {
00409                                         if ((line = brErrOctave.readLine()) != null)
00410                                                 System.err.println(line);
00411                                         else {
00412                                                 System.out.println("XSVF generating finished...");
00413                                                 break;
00414                                         }
00415                                 } catch (IOException e1) {
00416                                         e1.printStackTrace();
00417                                 }
00418                         }
00419                         
00420                         process.waitFor();
00421                         System.out.println("Process exit code: "+process.exitValue()+"\n");
00422                         if (process.exitValue()==127)
00423                                 Errors.setLastError(Errors.ErrorNum.ISENOTFOUND);
00424                         
00425                     ProcessWatcher.getInstance().removeProcess(process);
00426   
00427                 } catch (Throwable t)
00428                   {
00429                     t.printStackTrace();
00430                   }
00431                 
00432                 
00433                 return true;
00434         }
00435         
00442         public void generateXSVF() {
00443                 generateXSVF(this.project);
00444         }
00445         
00446 
00447 }
 All Classes Namespaces Files Functions Variables Enumerations