SpinningAround
January 23rd, 2010, 01:28 AM
There is something wrong in my code but I can't figure out what it's, first error or logical error is when I try to read a stream from a bash script (script work) for some unknown reason will it end up in the System.out.println(ERROR:...). Second problem is that setVar won't work in the active() method, yet when I try it in main(...) does it work. setVar(boolean,double,String)
Stream Error:
private static long runScript(String scriptPath, boolean destroy, boolean stream){
long resultat=-1;
try{
Process process = Runtime.getRuntime().exec(scriptPath);
process.waitFor();
if(stream){
BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader errStreamReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
StringBuffer output = new StringBuffer();
StringBuffer error = new StringBuffer();
for(String line;(line=inputStreamReader.readLine())!=null;)
{
output.append(line);
}
for(String line;(line=errStreamReader.readLine())!=null;)
{
error.append(line);
}
if( error.toString()!="" ){
System.out.println("ERROR:|" + error.toString() + "|"); //edit
process.destroy();
return -1;
}
resultat = Long.parseLong( output.toString() ); //catch error
}
else{
resultat = process.exitValue();
}
if(destroy){
process.destroy();
}
return resultat;
}
catch (InterruptedException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
return -1;
}
setvar() in active() don't work
long todayTraffic=0;
double procent;
try{
Thread.currentThread().sleep(1000);
todayTraffic=runScript(TRAFFICCHECK,true,true);
if(todayTraffic!=-1){
if( (limitTraffic>0) && (todayTraffic>=limitTraffic) ){
closeOperation();
}
}
else{
closeOperation();
}
procent = ( (double)todayTraffic/(double)limitTraffic )*100;
System.out.println("" + currentStatus +" "+ procent +" "+ reportTraffic(todayTraffic) ); //looks fine
monitor.setVar( currentStatus , procent , reportTraffic(todayTraffic) );
}
closeOperation();
}
varSet(): in main(..) Works
window = new SettingsGraphic();
monitor = new ActiveGraphic();
double a=0;
long c = 1000;
boolean b=true;
while(true){
for(int i=0;i<90;i++){
a=a+0.01;
c=c+108431258;
monitor.setVar(b,a,reportTraffic(c));
if(b){
b=false;
}
else{
b=true;
}
Thread.currentThread().sleep(1000);
}
a=0;
}
EDIT:
setVar();
private JLabel infoTraffic,status;
private boolean active;
private double procent;
public void setVar(boolean active, double procent,String traffic){
this.active=active;
this.procent=procent;
infoTraffic.setText("Traffic : " + traffic);
repaint();
}
I have added these lines to active(), but with no difference
NumberFormat form = new DecimalFormat("#.##");
form = NumberFormat.getInstance(Locale.US);
procent = Double.parseDouble( form.format( ( (double)todayTraffic/(double)limitTraffic )*100 ) );
Stream Error:
private static long runScript(String scriptPath, boolean destroy, boolean stream){
long resultat=-1;
try{
Process process = Runtime.getRuntime().exec(scriptPath);
process.waitFor();
if(stream){
BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader errStreamReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
StringBuffer output = new StringBuffer();
StringBuffer error = new StringBuffer();
for(String line;(line=inputStreamReader.readLine())!=null;)
{
output.append(line);
}
for(String line;(line=errStreamReader.readLine())!=null;)
{
error.append(line);
}
if( error.toString()!="" ){
System.out.println("ERROR:|" + error.toString() + "|"); //edit
process.destroy();
return -1;
}
resultat = Long.parseLong( output.toString() ); //catch error
}
else{
resultat = process.exitValue();
}
if(destroy){
process.destroy();
}
return resultat;
}
catch (InterruptedException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
return -1;
}
setvar() in active() don't work
long todayTraffic=0;
double procent;
try{
Thread.currentThread().sleep(1000);
todayTraffic=runScript(TRAFFICCHECK,true,true);
if(todayTraffic!=-1){
if( (limitTraffic>0) && (todayTraffic>=limitTraffic) ){
closeOperation();
}
}
else{
closeOperation();
}
procent = ( (double)todayTraffic/(double)limitTraffic )*100;
System.out.println("" + currentStatus +" "+ procent +" "+ reportTraffic(todayTraffic) ); //looks fine
monitor.setVar( currentStatus , procent , reportTraffic(todayTraffic) );
}
closeOperation();
}
varSet(): in main(..) Works
window = new SettingsGraphic();
monitor = new ActiveGraphic();
double a=0;
long c = 1000;
boolean b=true;
while(true){
for(int i=0;i<90;i++){
a=a+0.01;
c=c+108431258;
monitor.setVar(b,a,reportTraffic(c));
if(b){
b=false;
}
else{
b=true;
}
Thread.currentThread().sleep(1000);
}
a=0;
}
EDIT:
setVar();
private JLabel infoTraffic,status;
private boolean active;
private double procent;
public void setVar(boolean active, double procent,String traffic){
this.active=active;
this.procent=procent;
infoTraffic.setText("Traffic : " + traffic);
repaint();
}
I have added these lines to active(), but with no difference
NumberFormat form = new DecimalFormat("#.##");
form = NumberFormat.getInstance(Locale.US);
procent = Double.parseDouble( form.format( ( (double)todayTraffic/(double)limitTraffic )*100 ) );