/* * Created on Apr 29, 2005 */ package rmi; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; import java.util.ResourceBundle; import javax.servlet.http.HttpSession; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.PageContext; import com.enterprisedt.net.ftp.FTPClient; import com.enterprisedt.net.ftp.FTPException; /** * @author Ali KIZIL * Starts MSP RMI Client */ public class goOnlineClient{ // RMI goOnline Class public goOnlineRmiInterface goOnlineRmiInterface; // Define 2D double Java array (Send Java array to MATLAB, Get MATLAB array to Java) private double[][] array = null; // Define 1D double Java vector (Send Java array to MATLAB, Get MATLAB array to Java) private double[] vector = null; // Define double Java scalar (Send Java scalar to MATLAB, Get MATLAB scalar to Java) private double scalar; // Define Java string array (Send Java string array to MATLAB, Get MATLAB string array to Java) private String[] chararray = null; // Define the path of Code dir private String Code_Dir; // Define the path of Image dir private String Image_Dir; // Define the web link for images private String link = null; // FTP Client private FTPClient ftp; // HTTP private String HTTP; // Ftp private String FTP; // HTTP Port private String HTTP_Port; // Ftp Port private String FTP_Port; // RMI Port private String RMI_Port; // Ftp Username private String FTP_Username; // Ftp Password private String FTP_Password; // RMI Dir private String RMI_Dir; // Application Root private String APPLICATION; public void goOnlineRMI() { try { // Reads MSPRMI settings from Bundle. ResourceBundle rb = ResourceBundle.getBundle("MSPRMI"); HTTP = rb.getString("HTTP"); FTP = rb.getString("FTP"); HTTP_Port = rb.getString("HTTP_Port"); FTP_Port = rb.getString("FTP_Port"); RMI_Port = rb.getString("RMI_Port"); FTP_Username = rb.getString("FTP_Username"); FTP_Password = rb.getString("FTP_Password"); RMI_Dir = rb.getString("RMI_Dir"); APPLICATION = rb.getString("APPLICATION"); // Connects to the MSP RMI Server goOnlineRmiInterface = (goOnlineRmiInterface)Naming.lookup( "rmi://" + HTTP + ":" + RMI_Port + "/msp"); // Connects to MSP FTP Server ftp = new FTPClient(); ftp.setRemoteHost(FTP); ftp.setControlPort(Integer.parseInt(FTP_Port)); ftp.connect(); Code_Dir = "http://" + HTTP + ":" + HTTP_Port +"/"+ APPLICATION +"/Code/"; Image_Dir = "http://" + HTTP + ":" + HTTP_Port + "/"+ APPLICATION + "/Image/"; System.out.println("MSP RMI Client is started"); } catch (MalformedURLException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (NotBoundException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (NumberFormatException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (IOException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (FTPException e) { System.out.println("MSP Error: "+e.getMessage()); } } public void clean() { // Deletes all MAT files in Code dir. command("delete rm_*.*"); // Directs to Image dir. command("cd " + Image_Dir); // Deletes all JPG and BMP files in Image dir. command("delete rm_*.*"); } public void command(String cmd) { try { // Executes MATLAB Command. goOnlineRmiInterface.command(cmd); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); } } public void debug(String type, JspWriter out) { if (type == "Code") { // MATLAB lasterr dispalys the last occured error. command("err = lasterr;"); } if (type == "Model") { // Always a mspsimdebug MAT file is created, // its coded at evalMDL. This creates error, // if any occured. command("load mspsimdebug"); command("error(s)"); command("err = lasterr;"); } try { // Setting the color and display of MATLAB syntax error. out.println("<b><font color=\"#336699\">"); writeData("err", out); out.println("</font></b>"); } catch (IOException e) { System.out.println("MSP Error: "+e.getMessage()); } } public void engineOpen() { try { // Starts the MSP Engine ftp.login(FTP_Username,FTP_Password); ftp.chdir("Code"); goOnlineRmiInterface.engineOpen(); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (IOException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (FTPException e) { System.out.println("MSP Error: "+e.getMessage()); } } public void engineClose() { try { // Shut downs the engine goOnlineRmiInterface.engineClose(); ftp.quit(); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (IOException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (FTPException e) { System.out.println("MSP Error: "+e.getMessage()); } } public void evalMDL(String modelname, String loadname, String savename) { try { // Sends the Model to the Server ftp.put(RMI_Dir+modelname+".mdl",modelname+".mdl"); if (loadname != "") { ftp.put(RMI_Dir+loadname+".mat",loadname+".mat"); } // Evaluates Model goOnlineRmiInterface.evalMDL(modelname,loadname,savename); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (IOException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (FTPException e) { System.out.println("MSP Error: "+e.getMessage()); } } public void evalMFile(String name) { try { // Sends M-File to the Server ftp.put(RMI_Dir+name+".m",name+".m"); // Evaluates the M-File goOnlineRmiInterface.evalMFile(name); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (IOException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (FTPException e) { System.out.println("MSP Error: "+e.getMessage()); } } public void evalMLSF(String input, String output, JspWriter out) { try { // Reads Input MLSF. FileReader fr = new FileReader(RMI_Dir + input + ".mlsf"); BufferedReader rd = new BufferedReader(fr); String line; while ((line = rd.readLine()) != null) { // Executes every line. command(line); } rd.close(); fr.close(); if (output != null) { // Reads Output MLSF. If null, only Input MLSF is executed. FileReader fr2 = new FileReader(RMI_Dir + output + ".mlsf"); BufferedReader rd2 = new BufferedReader(fr2); String line2; while ((line2 = rd2.readLine()) != null) { // Writes every variable mentioned at Output MLSF. writeData(line2, out); } rd2.close(); fr2.close(); } } catch (FileNotFoundException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (IOException e) { System.out.println("MSP Error: "+e.getMessage()); } } public double[][] getArray() { try { return goOnlineRmiInterface.getArray(); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); return null; } } public void getArray(String name) { try { goOnlineRmiInterface.getArray(name); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); } } public void setArray(double[][] array) { try { goOnlineRmiInterface.setArray(array); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); } } public String[] getChararray() { try { return goOnlineRmiInterface.getChararray(); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); return null; } } public void getCharArray(String name) { try { goOnlineRmiInterface.getCharArray(name); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); } } public void setCharrray(String[] chararray) { try { goOnlineRmiInterface.setChararray(chararray); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); } } public double getScalar() { try { return goOnlineRmiInterface.getScalar(); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); return 0; } } public void getScalar(String name) { try { goOnlineRmiInterface.getScalar(name); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); } } public void setScalar(double scalar) { try { goOnlineRmiInterface.setScalar(scalar); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); } } public double[] getVector() { try { return goOnlineRmiInterface.getVector(); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); return null; } } public void getVector(String name) { try { goOnlineRmiInterface.getVector(name); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); } } public void setVector(double[] vector) { try { goOnlineRmiInterface.setVector(vector); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); } } public void getParam(String filename, String parameter, PageContext pageContext) { PrintWriter textIn; try { textIn = new PrintWriter(new BufferedWriter(new FileWriter(RMI_Dir+filename+".mlsf"))); String input = pageContext.getRequest().getParameter(parameter); textIn.println(input); textIn.close(); } catch (IOException e) { System.out.println("MSP Error: "+e.getMessage()); } } public void multiCommand (String text) { try { goOnlineRmiInterface.multiCommand(text.replaceAll("\n","").trim()); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); } } public String plotData(String cmd, String handle, String filename) { try { goOnlineRmiInterface.plotData(cmd,handle,filename); return ("<img src=\"http://"+HTTP+":"+HTTP_Port+"/"+APPLICATION+"/Image"+"/"+filename+".jpg"+"\">"); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); return null; } } public String plotModel(String simname, String orientation, String filename) { try { goOnlineRmiInterface.plotModel(simname,orientation,filename); return ("<img src=\"http://"+HTTP+":"+HTTP_Port+"/"+APPLICATION+"/Image"+"/"+filename+".bmp"+"\">"); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); return null; } } public String plotSim(String handle, String simresult, String filename) { try { goOnlineRmiInterface.plotSim(handle,simresult,filename); return ("<img src=\"http://"+HTTP+":"+HTTP_Port+"/"+APPLICATION+"/Image"+"/"+filename+".jpg"+"\">"); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); return null; } } public void putArray(String variable) { try { goOnlineRmiInterface.putArray(variable); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); } } public void putVector(String variable) { try { goOnlineRmiInterface.putVector(variable); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); } } public void putScalar(String variable) { try { goOnlineRmiInterface.putScalar(variable); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); } } public void sessionStart(goOnlineRmiInterface goOnlineRmiInterface,String name, PageContext pageContext) { engineOpen(); HttpSession session = pageContext.getSession(); session.setAttribute(name,goOnlineRmiInterface); } public void sessionGet(goOnlineRmiInterface goOnlineRmiInterface,String name, PageContext pageContext) { // Gets session. HttpSession session = pageContext.getSession(); goOnlineRmiInterface = (goOnlineRmiInterface)session.getAttribute(name); // Sets as page attribute. pageContext.setAttribute("goOnlineRmiInterface",goOnlineRmiInterface); } public void sessionEnd(goOnlineRmiInterface goOnlineRmiInterface,String name, PageContext pageContext) { // Gets session HttpSession session = pageContext.getSession(); // Converts object to goOnline. goOnlineRmiInterface = (goOnlineRmiInterface)session.getAttribute(name); engineClose(); // Removes session variable. session.removeAttribute(name); } public String thumbnail(String imagename, String width, String height) { try { return goOnlineRmiInterface.thumbnail(imagename,width,height); } catch (RemoteException e) { System.out.println("MSP Error: "+e.getMessage()); return null; } } public void writeData(String name, JspWriter out) { String type = null; int key2d = 0; try { // These commands defines the MATLAB variable class and size. command("msp_tmp = whos('" + name + "');"); command("msp_class = msp_tmp.class;"); command("msp_size1 = msp_tmp.size(1);"); command("msp_size2 = msp_tmp.size(2);"); getCharArray("msp_class"); String[] msp_class = getChararray(); getScalar("msp_size1"); double msp_size1 = getScalar(); getScalar("msp_size2"); double msp_size2 = getScalar(); if (((msp_class[0]).equals("double"))) { if ((msp_size1 != 1) & (msp_size2 != 1)) type = "1"; if ((msp_size1 != 1) & (msp_size2 == 1)) { type = "2"; key2d = 0; // Transpoze the vector. // JMatLink only reads line vectors. command(name + "=" + name + "'"); } if ((msp_size1 == 1) & (msp_size2 != 1)) { type = "2"; key2d = 1; } if ((msp_size1 == 1) & (msp_size2 == 1)) type = "3"; } if (((msp_class[0]).equals("char"))) type = "4"; if (((msp_class[0]).equals("cell"))) type = "5"; if (((msp_class[0]).equals("struct"))) type = "6"; } catch (NullPointerException e) { try { out.println("<b><font color=\"#558F05\">"); out.println("Undefined variable: " + name); out.println("</font></b>"); } catch (IOException e1) { System.out.println("MSP Error: "+e.getMessage()); } } try { switch (Integer.parseInt(type)) { case 1: getArray(name); array = getArray(); out.println("<table>"); for (int i = 0; i < array.length; i++) { out.println("<tr>"); double[] a; a = array[i]; for (int j = 0; j < a.length; j++) { out.println("<td>"); double b; b = a[j]; out.print(b); out.println("</td>"); } out.println("</tr>"); } out.println("</table>"); break; case 2: getArray(name); double[][] k = getArray(); vector = k[0]; out.println("<table>"); if (key2d == 0) { for (int i = 0; i < vector.length; i++) { out.println("<tr><td>" + vector[i] + "</tr></td>"); } } if (key2d == 1) { out.println("<tr>"); for (int i = 0; i < vector.length; i++) { out.println("<td>" + vector[i] + "</td>"); } out.println("</tr>"); } out.println("</table>"); break; case 3: getScalar(name); scalar = getScalar(); out.print(scalar); break; case 4: getCharArray(name); chararray = getChararray(); out.print(chararray[0]); break; case 5: // Cell and Structure arrays are not a standart // readable class for JMatLink. So its alternatively // written to a temp file and outputed to page. command("for i=1:size(" + name + ",1), for j=1:size(" + name + ",2), str = " + name + "{i,j}; try, str=mat2str(str), catch, end, try, dlmwrite('msptmp.mlsf',str,'delimiter','','-append') ,catch, str='Avoid using structure or cell array in structure or cell array', dlmwrite('msptmp.mlsf',str,'delimiter','','-append'), end, end, end"); ftp.get(RMI_Dir+"msptmp.mlsf","msptmp.mlsf"); command("delete msptmp.mlsf"); FileReader fr = new FileReader(RMI_Dir + "msptmp.mlsf"); BufferedReader rd = new BufferedReader(fr); String line; while ((line = rd.readLine()) != null) { out.println(line); out.println("<br>"); } rd.close(); fr.close(); break; case 6: command("mspdata = struct2cell(" + name + ")"); command("for i=1:size(mspdata,1), for j=1:size(mspdata,2), str = mspdata{i,j}; try, str=mat2str(str), catch, end, try, dlmwrite('msptmp2.mlsf',str,'delimiter','','-append') ,catch, str='Avoid using structure or cell array in structure or cell array', dlmwrite('msptmp2.mlsf',str,'delimiter','','-append'), end, end, end"); ftp.get(RMI_Dir+"msptmp2.mlsf","msptmp2.mlsf"); command("delete msptmp2.mlsf"); FileReader fr2 = new FileReader(RMI_Dir + "msptmp2.mlsf"); BufferedReader rd2 = new BufferedReader(fr2); String line2; while ((line2 = rd2.readLine()) != null) { out.println(line2); out.println("<br>"); } rd2.close(); fr2.close(); break; } } catch (NumberFormatException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (IOException e) { System.out.println("MSP Error: "+e.getMessage()); } catch (FTPException e) { System.out.println("MSP Error: "+e.getMessage()); } } }