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
16 public class ControllerDL
17 {
18 private File currentFile;
20
21 private String currentFileName;
23
24 private File currentDir;
26
27
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
50 public File createFile(String className)
51 {
52 File file=new File("real","Real"+className+".java");
54
55 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
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
98 private void downloadFile()
99 {
100 if(currentFile!=null)
101 {
102
104 String className=currentFile.getName();
106 className=className.substring(0,className.length() - 5);
107
108 externalCommand(("lejos "+className));
110
111 }
112 else
113 {
114 MainInterface.displayMessage("ControllerDL.downloadFile(): cannot download null file");
115 }
116 }
117
118
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 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