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      /**
46      * Creates a Java file with a main method to allow the controller to be run
47      * in the real RCX. It is this file which is then downloaded to the RCX, and 
48      * it 'pulls' the Controller file with it.
49      */
50      public File createFile(String className)
51      {
52          // create the file
53          File file=new File("real","Real"+className+".java");
54          
55          // try to write the code to the file
56          try
57          {
58              FileWriter fw=new FileWriter(file);
59              
60              fw.write(
61              "\n// This file was generated by Intellego. Do not modify\n\n"+
62              "class Real"+className+"\n{\n\tpublic static void main(String args[])\n"+
63              "\t{\n\t\t"+className+" a=new "+className+"();"+
64              "\n\t\ta.initController(new real.RealRCX(a));\n\t\ta.run();\n\t}\n}");
65              
66              fw.close();
67              
68              Intellego.addToLog("ControllerDL.createFile(): Created file: "+file.getName());
69          }
70          
71          catch(Exception e)
72          {
73              MainInterface.displayMessage("Error: cannot download controller");
74              Intellego.addToLog("ControllerDL.createFile() failedto create file: "+e);
75          }
76          
77          return file;
78      }
79      
80      /**
81      * Compiles the current file
82      */
83      private void compileFile()
84      {
85          if(currentFile!=null)
86          {
87              externalCommand("lejosc -target 1.1 "+currentFile.toString());
88          }
89          else
90          {
91              MainInterface.displayMessage("ControllerDL.compileFile(): cannot compile null file");
92          }
93      }
94      
95      /**
96      * Links and downloads the current file
97      */
98      private void downloadFile()
99      {
100         if(currentFile!=null)
101         {
102             //download
103             
104             // get rid of .class extension
105             String className=currentFile.getName();
106             className=className.substring(0,className.length() - 5);
107             
108             // actually download
109             externalCommand(("lejos "+className)); 
110 
111         }
112         else
113         {
114             MainInterface.displayMessage("ControllerDL.downloadFile(): cannot download null file");
115         }
116     }
117     
118     /**
119     * Processes all external calls, i.e calls to lejos & lejosc
120     */
121     private void externalCommand(String cmd)
122     {
123         int len;
124         byte buffer[] = new byte[1000];
125         
126         Intellego.addToLog("ControllerDL.externalCommand(): External Command attempted: "+cmd);
127         
128         try
129         {
130             Process external=Runtime.getRuntime().exec(cmd);
131             InputStream ees = external.getErrorStream();
132             
133             try 
134             {
135                 // display external messages
136                 ExternalMessager output=MainInterface.createExternalMessagerFrame();
137                 
138                 while ((len = ees.read(buffer)) != -1)
139                 {
140                     String eo = new String (buffer, 0, len);
141                     output.append(eo);            
142                 }
143                 external.waitFor();
144              }
145              catch (Exception e) 
146              {
147                 MainInterface.displayMessage("Error attempting external command");
148                 Intellego.addToLog("ControllerDL.externalCommand(): error: "+e.getMessage());
149              }
150         }
151         catch (Exception e) 
152         {
153             MainInterface.displayMessage("Error attempting external command");
154             Intellego.addToLog("ControllerDL.externalCommand(): error: "+e.getMessage());
155         }
156     }
157 }
158