1   package real;
2   
3   import intellego.Intellego;
4   import interfaces.*;
5   import main.*;
6   
7   import java.io.*;
8   import java.lang.*;
9   
10  /**
11  * This class provides the functionality necessary to download an Intellego
12  * Controller to the real RCX robot.
13  *
14  * @author Graham Ritchie
15  */
16  public class ControllerDL
17  {
18      // the current file
19      private File currentFile;
20      
21      //the current file's local name
22      private String currentFileName;
23      
24      // the current file's directory
25      private File currentDir;
26      
27      /**
28      * Constructor: attempts to download the controller file passed to it.
29      *
30      * @param sourceFile the Java source file of the controller to be downloaded
31      * @param dir the directory where this file resides.
32      */
33      public ControllerDL(File sourceFile, File dir)
34      {
35              currentFile=sourceFile;
36              currentFileName=currentFile.getName();
37              currentDir=dir;
38  
39              currentFile=createFile(currentFileName.substring(0,currentFileName.length() - 5));
40  
41              compileFile();
42              downloadFile();
43      }
44      
45      /** Creates a Java file with a main method to allow the controller to be run
46           * in the real RCX. It is this file which is then downloaded to the RCX, and
47           * it 'pulls' the Controller file with it.
48           * @param className the class name
49           * @return the newly created file
50           */
51      public File createFile(String className)
52      {
53              // create the file
54              File file=new File("real","Real"+className+".java");
55  
56  
57              // try to write the code to the file
58              try
59              {
60                  FileWriter fw=new FileWriter(file);
61                  //"\n import controllers."+className+";"+
62                  fw.write(
63                  "\n// This file was generated by Intellego. Do not modify\n\n"+
64                  "class Real"+className+"\n{\n\tpublic static void main(String args[])\n"+
65                  "\t{\n\t\t"+className+" a=new "+className+"();"+
66                  "\n\t\ta.initController(new real.RealRCX(a));\n\t\ta.run();\n\t}\n}");
67  
68                  fw.close();
69  
70                  Intellego.addToLog("ControllerDL.createFile(): Created file: "+file.getName());
71              }
72  
73              catch(Exception e)
74              {
75                  MainInterface.displayMessage("Error: cannot download controller");
76                  Intellego.addToLog("ControllerDL.createFile() failedto create file: "+e);
77              }
78  
79              return file;
80      }
81      
82      /**
83      * Compiles the current file
84      */
85      private void compileFile()
86      {    
87              if(currentFile!=null)
88              {
89                  //externalCommand("lejosc -target 1.1 "+currentFile.toString());
90                  externalCommand("lejosc "+currentFile.toString(),0);
91              }
92              else
93              {
94                  MainInterface.displayMessage("ControllerDL.compileFile(): cannot compile null file");
95              }
96      }
97      
98      /**
99      * Links and downloads the current file
100     */
101     private void downloadFile()
102     {
103             if(currentFile!=null)
104             {
105                 //download
106 
107                 // get rid of .class extension
108                 String className=currentFile.getName();
109                 className=className.substring(0,className.length() - 5);
110 
111                 // actually download
112                 externalCommand(("lejos "+className),1); 
113 
114             }
115             else
116             {
117                 MainInterface.displayMessage("ControllerDL.downloadFile(): cannot download null file");
118             }
119     }
120     
121     /** Processes all external calls, i.e calls to lejos & lejosc
122          * @param cmd the command
123          * @param num the type of external messenger
124          */
125     private void externalCommand(String cmd, int num)
126     {
127             int len;
128             byte buffer[] = new byte[1000];
129 
130             Intellego.addToLog("ControllerDL.externalCommand(): External Command attempted: "+cmd);
131 
132             try
133             {
134                 Process external=Runtime.getRuntime().exec(cmd);
135                 InputStream ees = external.getErrorStream();
136 
137                 try 
138                 {
139                     // display external messages
140                     ExternalMessager output=MainInterface.createExternalMessagerFrame(num);
141                     
142                     while ((len = ees.read(buffer)) != -1)
143                     {
144                         String eo = new String (buffer, 0, len);
145                         output.append(eo);            
146                     }
147                     external.waitFor();
148                     
149                      if(num==0){
150                         if(output.successfullCompile()){
151                 
152                             output.append("\n"+"\n"+"   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
153                             output.append("\n"+"\n"+"     |  COMPILING SUCCESSFUL: NO ERRORS FOUND  |");
154                             output.append("\n"+"\n"+"   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
155                             }
156 
157                         else{
158 
159                             output.append("\n"+"\n"+"   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
160                             output.append("\n"+"\n"+"     |  COMPILING FAILED: ERRORS FOUND   |");
161                             output.append("\n"+"\n"+"   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
162 
163                         }
164                      }
165                      
166                     /*
167                      else{
168                          if(output.successfullCompile()){
169                 
170                             output.append("\n"+"\n"+"   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
171                             output.append("\n"+"\n"+"     |  UPLOADING SUCCESSFUL: NO ERRORS FOUND  |");
172                             output.append("\n"+"\n"+"   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
173                             }
174 
175                         else{
176 
177                             output.append("\n"+"\n"+"   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
178                             output.append("\n"+"\n"+"     |  UPLOADING FAILED: ERRORS FOUND   |");
179                             output.append("\n"+"\n"+"   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
180 
181                         }
182                          
183                      }
184                       **/
185                     
186                  }
187                  catch (Exception e) 
188                  {
189                     MainInterface.displayMessage("Error attempting external command");
190                     Intellego.addToLog("ControllerDL.externalCommand(): error: "+e.getMessage());
191                  }
192             }
193             catch (Exception e) 
194             {
195                 MainInterface.displayMessage("Error attempting external command");
196                 Intellego.addToLog("ControllerDL.externalCommand(): error: "+e.getMessage());
197             }
198     }
199 }
200