made generate source file method static (to allow for remote library compilations)

This commit is contained in:
fros4943 2006-08-30 14:59:35 +00:00
parent b1c7153aa6
commit da561d138b

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: ContikiMoteTypeDialog.java,v 1.5 2006/08/23 14:31:12 fros4943 Exp $ * $Id: ContikiMoteTypeDialog.java,v 1.6 2006/08/30 14:59:35 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote; package se.sics.cooja.contikimote;
@ -850,12 +850,39 @@ public class ContikiMoteTypeDialog extends JDialog {
if (!ContikiMoteType.tempOutputDirectory.exists()) if (!ContikiMoteType.tempOutputDirectory.exists())
ContikiMoteType.tempOutputDirectory.mkdir(); ContikiMoteType.tempOutputDirectory.mkdir();
// Parse selected sensors
Vector<String> sensors = new Vector<String>();
for (Component checkBox : sensorPanel.getComponents()) {
if (((JCheckBox) checkBox).isSelected()) {
sensors.add(((JCheckBox) checkBox).getText());
}
}
// Parse selected core interfaces
Vector<String> coreInterfaces = new Vector<String>();
for (Component checkBox : coreInterfacePanel.getComponents()) {
if (((JCheckBox) checkBox).isSelected()) {
coreInterfaces.add(((JCheckBox) checkBox).getText());
}
}
// Parse selected user processes
Vector<String> userProcesses = new Vector<String>();
for (Component checkBox : processPanel.getComponents()) {
if (((JCheckBox) checkBox).isSelected()) {
userProcesses.add(((JCheckBox) checkBox).getText());
}
}
// Generate main contiki source file // Generate main contiki source file
String errorMessage = generateSourceFile(); String filename = null;
if (errorMessage != null) { try {
filename = generateSourceFile(textID.getText(), sensors, coreInterfaces,
userProcesses);
} catch (Exception e) {
libraryCreatedOK = false; libraryCreatedOK = false;
progressBar.setBackground(Color.ORANGE); progressBar.setBackground(Color.ORANGE);
progressBar.setString("Maximum number of mote types already exist"); progressBar.setString(e.getMessage());
progressBar.setIndeterminate(false); progressBar.setIndeterminate(false);
progressBar.setValue(0); progressBar.setValue(0);
createButton.setEnabled(libraryCreatedOK); createButton.setEnabled(libraryCreatedOK);
@ -966,23 +993,35 @@ public class ContikiMoteTypeDialog extends JDialog {
} }
/** /**
* @return Error message or null if source file generation ok * Generates new source file by reading default source template and replacing
* fields with sensors, core interfaces and processes. Also includes default
* processes from GUI external configuration.
*
* @param id
* Mote type ID (decides name of new source file)
* @param sensors
* Names of sensors
* @param coreInterfaces
* Names of core interfaces
* @param userProcesses
* Names of user processes
* @return New filename
* @throws Exception
* If any error occurs
*/ */
private String generateSourceFile() { public static String generateSourceFile(String id, Vector<String> sensors,
Vector<String> coreInterfaces, Vector<String> userProcesses)
throws Exception {
// SENSORS // SENSORS
String sensorString = ""; String sensorString = "";
String externSensorDefs = ""; String externSensorDefs = "";
for (Component checkBox : sensorPanel.getComponents()) { for (String sensor : sensors) {
if (((JCheckBox) checkBox).isSelected()) { if (!sensorString.equals(""))
if (!sensorString.equals("")) sensorString += ", ";
sensorString = sensorString.concat(", "); sensorString += "&" + sensor;
sensorString = sensorString.concat("&" externSensorDefs += "extern const struct sensors_sensor " + sensor
+ ((JCheckBox) checkBox).getText()); + ";\n";
externSensorDefs = externSensorDefs
.concat("extern const struct sensors_sensor "
+ ((JCheckBox) checkBox).getText() + ";\n");
}
} }
if (!sensorString.equals("")) if (!sensorString.equals(""))
@ -991,17 +1030,13 @@ public class ContikiMoteTypeDialog extends JDialog {
sensorString = "SENSORS(NULL);"; sensorString = "SENSORS(NULL);";
// CORE INTERFACES // CORE INTERFACES
String externInterfaceDefs = "";
String interfaceString = ""; String interfaceString = "";
for (Component checkBox : coreInterfacePanel.getComponents()) { String externInterfaceDefs = "";
if (((JCheckBox) checkBox).isSelected()) { for (String coreInterface : coreInterfaces) {
if (!interfaceString.equals("")) if (!interfaceString.equals(""))
interfaceString = interfaceString.concat(", "); interfaceString += ", ";
interfaceString = interfaceString.concat("&" interfaceString += "&" + coreInterface;
+ ((JCheckBox) checkBox).getText()); externInterfaceDefs += "SIM_INTERFACE_NAME(" + coreInterface + ");\n";
externInterfaceDefs = externInterfaceDefs.concat("SIM_INTERFACE_NAME("
+ ((JCheckBox) checkBox).getText() + ");\n");
}
} }
if (!interfaceString.equals("")) if (!interfaceString.equals(""))
@ -1009,27 +1044,23 @@ public class ContikiMoteTypeDialog extends JDialog {
else else
interfaceString = "SIM_INTERFACES(NULL);"; interfaceString = "SIM_INTERFACES(NULL);";
// PROCESSES // PROCESSES (including any default processes)
String userProcessString = ""; String userProcessString = "";
String externProcessDefs = ""; String externProcessDefs = "";
for (Component checkBox : processPanel.getComponents()) { for (String process : userProcesses) {
if (((JCheckBox) checkBox).isSelected()) { if (!userProcessString.equals(""))
if (!userProcessString.equals("")) userProcessString += ", ";
userProcessString = userProcessString.concat(", "); userProcessString += "&" + process;
userProcessString = userProcessString.concat("&" externProcessDefs += "PROCESS_NAME(" + process + ");\n";
+ ((JCheckBox) checkBox).getText());
externProcessDefs = externProcessDefs.concat("PROCESS_NAME("
+ ((JCheckBox) checkBox).getText() + ");\n");
}
} }
String coreProcessString = ""; String defaultProcessString = "";
String processArray[] = GUI.getExternalToolsSetting( String defaultProcesses[] = GUI.getExternalToolsSetting(
"CONTIKI_STANDARD_PROCESSES").split(";"); "CONTIKI_STANDARD_PROCESSES").split(";");
for (String processString : processArray) { for (String process : defaultProcesses) {
if (!coreProcessString.equals("")) if (!defaultProcessString.equals(""))
coreProcessString = coreProcessString.concat(", "); defaultProcessString += ", ";
coreProcessString = coreProcessString + "&" + processString; defaultProcessString += "&" + process;
} }
if (userProcessString.equals("")) { if (userProcessString.equals("")) {
@ -1038,25 +1069,27 @@ public class ContikiMoteTypeDialog extends JDialog {
} }
String processString; String processString;
if (!coreProcessString.equals("")) if (!defaultProcessString.equals(""))
processString = "PROCINIT(" + coreProcessString + ");"; processString = "PROCINIT(" + defaultProcessString + ");";
else else
processString = "PROCINIT(NULL);"; processString = "PROCINIT(NULL);";
if (!userProcessString.equals("")) if (!userProcessString.equals(""))
processString = processString + "\nAUTOSTART_PROCESSES(" processString += "\nAUTOSTART_PROCESSES(" + userProcessString + ");";
+ userProcessString + ");";
else else
processString = processString.concat("\nAUTOSTART_PROCESSES(NULL);"); processString += "\nAUTOSTART_PROCESSES(NULL);";
// JNI CLASS NAME // CHECK JNI CLASS AVAILABILITY
String libString = CoreComm.getAvailableClassName(); String libString = CoreComm.getAvailableClassName();
if (libString == null) { if (libString == null) {
logger.fatal("No more libraries can be loaded!"); logger.fatal("No more libraries can be loaded!");
return "Maximum number of mote types already exist"; throw new Exception("Maximum number of mote types already exist");
} }
// Create new source file and replace special fields // GENERATE NEW FILE
BufferedWriter destFile = null;
BufferedReader sourceFile = null;
String destFilename = null;
try { try {
Reader reader; Reader reader;
String mainTemplate = GUI String mainTemplate = GUI
@ -1072,10 +1105,13 @@ public class ContikiMoteTypeDialog extends JDialog {
reader = new InputStreamReader(input); reader = new InputStreamReader(input);
} }
BufferedReader sourceFile = new BufferedReader(reader); sourceFile = new BufferedReader(reader);
BufferedWriter destFile = new BufferedWriter(new OutputStreamWriter( destFilename = ContikiMoteType.tempOutputDirectory.getPath()
new FileOutputStream(ContikiMoteType.tempOutputDirectory.getPath() + File.separatorChar + id + ".c";
+ File.separatorChar + textID.getText() + ".c"))); destFile = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(destFilename)));
// Replace special fields in template
String line; String line;
while ((line = sourceFile.readLine()) != null) { while ((line = sourceFile.readLine()) != null) {
line = line line = line
@ -1096,11 +1132,19 @@ public class ContikiMoteTypeDialog extends JDialog {
destFile.close(); destFile.close();
sourceFile.close(); sourceFile.close();
} catch (Exception e) { } catch (Exception e) {
logger.debug("Exception " + e); try {
return "Exception " + e; if (destFile != null)
destFile.close();
if (sourceFile != null)
sourceFile.close();
} catch (Exception e2) {
}
// Forward exception
throw e;
} }
return null; return destFilename;
} }
/** /**