multithreading - Java GUI with Swing, MVC and Ssh threads -
i want code application in java following:
- connect remote devices through ssh or using ftdi chips (so i'll have thread each ssh connection, , thread each ftdi connection)
- execute test sequences , output result (either pass or fail): want able create new test sequences different types of devices
- ultimately goal store results in database
- there gui using swing
i want structure code in way permit great modularity. i've looked @ mvc pattern not sure how apply situation.
initially wanted give input , output stream each device instance, give either ssh io or ftdi io.
currently app running in console here current attempt structure code (which not satisfactory obviously) main.java
package testlib; import java.io.ioexception; import java.util.*; import jd2xx.jd2xx; import jd2xx.jd2xxevent; import jd2xx.jd2xxeventlistener; import jd2xx.jd2xxinputstream; import jd2xx.jd2xxoutputstream; public class main{ public static void main(string[] args) { //ftdiwrapper = new ftdiwrapper(); try{ ftdiwrapper.scanftdimodules(); for(int = 6; < 8; i++){ jd2xx device = ftdiwrapper.devices.get(i); stationthread stationthread = new stationthread(device); thread t = new thread(stationthread); t.start(); /**/ break; /**/ } //ftdiwrapper.closealldevices(); } catch(exception e){ system.out.println(e.tostring());} } } class stationthread implements runnable { jd2xx jd; public stationthread(jd2xx device){ this.jd = device; } public void run() { device dev1 = new device1(new jd2xxinputstream2(jd), new jd2xxoutputstream(jd)); dev1.flushboot(); } }
ftdiwrapper.java
package testlib; import java.util.arraylist; import java.util.list; import jd2xx.jd2xx; import jd2xx.jd2xx.deviceinfo; import jd2xx.jd2xx.programdata; public class ftdiwrapper { static protected list<programdata> devicesdata = new arraylist<programdata>(); static protected list<jd2xx> devices = new arraylist<jd2xx>(); protected jd2xx jd; //ftdi driver instance public ftdiwrapper() { } static void scanftdimodules() throws exception { //get number of ftdi devices available int nbdevices = (new jd2xx()).createdeviceinfolist(); //loop through each , data for(int = 0; < nbdevices; i++) { jd2xx jd = new jd2xx(); jd.open(i); jd.setbaudrate(115200); jd.setdatacharacteristics( 8, jd2xx.stop_bits_1, jd2xx.parity_none ); jd.setflowcontrol( jd2xx.flow_none, 0, 0 ); jd.settimeouts(5000, 2000); //read current device's data programdata devicedata = jd.eeread(); //add current device list devicesdata.add(devicedata); system.out.println(devicedata); //add current ftdi connection static devices list devices.add(jd); } } static void closealldevices() throws exception { for(jd2xx jd : devices) jd.close(); } }
device1.java
package testlib; import java.io.inputstream; import java.io.outputstream; import java.lang.reflect.array; public class device1 implements device { stringbuffer strbuf; //input & output streams allows communicate ftdi/ssh/etc. private inputstream input; private outputstream output; public device1(inputstream input, outputstream output) { this.input = input; this.output = output; this.strbuf = new stringbuffer(); recvthread recvthread = new recvthread(strbuf,input); thread t = new thread(recvthread); t.start(); } public void flushboot(){ try{ webpowerswitch.power(7, webpowerswitch.off); webpowerswitch.power(7, webpowerswitch.on); if(expect("booting in ",5000,true)) { output.write(";;".getbytes()); if(expect("=>",5000,true)) { output.write("d\n".getbytes()); } } }catch(exception e){ system.out.println(e.tostring()); } } public boolean expect(string data, int timeoutms, boolean flushbuffer) { long timestarted = system.currenttimemillis(); long currenttime = system.currenttimemillis(); long timeelapsed = 0; synchronized(strbuf) { boolean found = false; while( !found && timeelapsed < timeoutms ){ try{ //wait recvthread's notification //using given timeout strbuf.wait(timeoutms - timeelapsed); found = strbuf.tostring().contains(data); currenttime = system.currenttimemillis(); timeelapsed = currenttime - timestarted; } catch(exception e){} } if(!found) return false; else { if(flushbuffer) strbuf.setlength(0); return true; } } } } class recvthread implements runnable { private stringbuffer strbuf; private inputstream input; public recvthread(stringbuffer buffer, inputstream input) { this.strbuf = buffer; this.input = input; } public void run() { byte[] tmp=new byte[1024]; while(true){ tmp=new byte[1024]; try{ int available = input.available(); if(available>0){ int i=input.read(tmp,0,(available <= 1024 ? available : 1024)); system.out.println("read:" + i); synchronized(strbuf){ strbuf.append(new string(tmp,0,i)); system.out.println(strbuf.tostring()); strbuf.notify(); } } }catch(exception e){ system.out.println(e.tostring()); } try{thread.sleep(1000);}catch(exception ee){} } } }
any input appreciated.
- i know way handle sending/receiving questionable. there must better way? (the ftdi chips work half-duplex, can not blocking read otherwise blocks write also)
- how port such design swing gui
thank much
Comments
Post a Comment