Skip to content

Commit

Permalink
Layout changed, disconnection improved, view update improved, baudrat…
Browse files Browse the repository at this point in the history
…e meter added.
  • Loading branch information
0fca committed Aug 14, 2017
1 parent 7339d33 commit 8b3cfd2
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 68 deletions.
2 changes: 0 additions & 2 deletions src/main/java/abstracts/LocalEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
*/
package abstracts;

import com.neology.net.NetController;
import com.neology.xml.XMLController;
import enums.Local;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/neology/net/BaudrateMeter.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void startMeasuringCycle() {
public void stopMeasuringCycle() {
measure = false;
long ms = System.currentTimeMillis() - start;
System.out.println("MS: "+ms);
if (0 == ms || bytes < 100) return; // skip with too small portion of data
double bps = bytes * 8. / (ms / 1000.);
// double bpms = bytes * 8. / ms;
Expand All @@ -61,5 +62,6 @@ public void stopMeasuringCycle() {
// exponential moving-average smoothing
ema = ALPHA * bps + (1. - ALPHA) * (0. == ema ? bps : ema);
bytes = 0;

}
}
4 changes: 4 additions & 0 deletions src/main/java/com/neology/net/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public boolean isConnected(){
return T.isConnected();
}

public boolean wasConnected(){
return T.wasConnected();
}

public Transport getTranportInstance(){
return T;
}
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/com/neology/net/Transport.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,11 @@ public class Transport {
DataOutputStream os;
InputStream origIs;
OutputStream origOs;
Socket s;
//Socket s;
private boolean WAS_CONNECTED = false;
private BaudrateMeter baudrateMeter;
private String IP;

public Transport(Socket socket) throws IOException {
this(socket.getInputStream(), socket.getOutputStream());
this.s = socket;
IP = socket.getInetAddress().getHostAddress();
}


public Transport(InputStream is) {
this(is, null);
Expand Down Expand Up @@ -82,7 +77,6 @@ void release() {

public void close() throws IOException{
release();
s.close();
IP = "";
}

Expand All @@ -102,6 +96,10 @@ public void setBaudrateMeter(BaudrateMeter baudrateMeter) {
this.baudrateMeter = baudrateMeter;
}

public BaudrateMeter getBaudrateMeter(){
return this.baudrateMeter;
}

public int readUInt8() throws TransportException {
return readByte() & 0x0ff;
}
Expand Down Expand Up @@ -355,7 +353,7 @@ public void setIp(String ip){
}

public boolean isConnected() {
return s.isConnected();
return true ? origIs != null && origOs != null : false;
}

public boolean wasConnected() {
Expand Down
155 changes: 98 additions & 57 deletions src/main/java/com/neology/todosaver/FXMLController.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
Expand All @@ -40,18 +39,16 @@
import javafx.collections.ObservableList;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.text.TextAlignment;
Expand Down Expand Up @@ -89,10 +86,11 @@ public String preparePath(String path) {
protected boolean IS_CONNECTED = false;
private int INDEX = 0;
private HashMap<String,Image> IMAGES = new HashMap<>();

private ArrayList<String> DATA = new ArrayList<>();
private HashMap<String,Service> THREADS = new HashMap<>();
final Executor EXEC = Executors.newSingleThreadExecutor();
ObservableList<Label> list = FXCollections.observableArrayList();
private ConnectionManager c = new ConnectionManager();

{
s = new Socket();
Expand Down Expand Up @@ -127,7 +125,7 @@ public void initialize(URL url, ResourceBundle rb) {
CONNECT.setDisable(true);
DISCONNECT.setDisable(true);
SETTINGS.setDisable(true);

});

SETTINGS.setOnAction(event ->{
Expand All @@ -140,18 +138,41 @@ public void initialize(URL url, ResourceBundle rb) {
});

CONNECT.setOnAction(event ->{
initConnectionManager();
if(c.getState() == Service.State.CANCELLED){
Platform.runLater(() ->{
c.restart();
});
s = new Socket();
if(c.getState() == Service.State.READY){
initConnectionManager();
}
}else{
initConnectionManager();
}
});

DISCONNECT.setOnAction(event ->{
IMAGES.clear();
THREADS.forEach((desc,serv) ->{
serv.cancel();
});
for(Service t : THREADS.values()){
t.cancel();
t = null;
}
THREADS.clear();
Platform.runLater(() ->{
VIEWER_PANEL.getItems().clear();
});
});
c.setOnCancelled(canc_evt ->{
try {
ss.close();
s.close();
ss = null;
s = null;
} catch (IOException ex) {
Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex);
}

});
c.cancel();
});

VIEWER_PANEL.setCellFactory(new CallbackImpl());
Expand Down Expand Up @@ -221,54 +242,12 @@ private boolean checkFolders() {
}

private void initConnectionManager() {
ConnectionManager c = new ConnectionManager();
c.start();
}


public class ConnectionManager extends Thread implements Runnable{
private Thread T;

@Override
public void start(){
if(T == null){
T = new Thread(this,"ConnectionManager");
T.setDaemon(true);
T.start();
}
}

@Override
public void run(){
try {
if(ss == null){
System.out.println(new InetSocketAddress("192.168.0.104",7998).getAddress().isReachable(500));

ss = new ServerSocket(PORT,16,new InetSocketAddress(n.getIp(),7999).getAddress());
System.out.println("ServerSocket prepared.");
}
while(!isInterrupted()){
s = ss.accept();
IS_CONNECTED = true;
System.out.println("Accepted.");
Connection c;
try {
c = initConnection();
TCPService t = new TCPService(c,INDEX);
t.start();

THREADS.put(c.getTranportInstance().getIp(), t);
INDEX++;
} catch (IOException ex) {
Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex);
}

}
} catch (IOException ex) {
Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
}

public class ConnectionManager extends Service {

private Connection initConnection() throws IOException{
Opened o = new Opened();
Connection c = new Connection();
Expand All @@ -277,6 +256,44 @@ private Connection initConnection() throws IOException{
c.setIp(s.getRemoteSocketAddress().toString());
return c;
}

@Override
protected Task createTask() {
return new Task<Void>(){
@Override
public Void call(){
try {
if(ss == null){
ss = new ServerSocket(PORT,16,new InetSocketAddress(n.getIp(),7999).getAddress());
System.out.println("ServerSocket prepared.");
}
while(!this.isCancelled()){
s = ss.accept();
IS_CONNECTED = true;
System.out.println("Accepted.");
Connection c;
try {
c = initConnection();
BaudrateMeter meter = new BaudrateMeter();
c.getTranportInstance().setBaudrateMeter(meter);
TCPService t = new TCPService(c,INDEX);
t.start();

THREADS.put(c.getTranportInstance().getIp(), t);
INDEX++;
} catch (IOException ex) {
Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex);
}

}
System.out.println("ConnectionManager stop.");
} catch (IOException ex) {
Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
};
}
}

public class TCPService extends Service{
Expand All @@ -285,10 +302,12 @@ public class TCPService extends Service{
private Connection CON;
private String NAME;
private int INDEX = 0;
private BaudrateMeter MTR;

public TCPService(Connection c,int index){
this.CON = c;
this.INDEX = index;
this.MTR = CON.getTranportInstance().getBaudrateMeter();
}

private Image processData(byte[] buffer) throws IOException {
Expand All @@ -306,13 +325,18 @@ private Image processData(byte[] buffer) throws IOException {
System.out.println("FILE_NAME_RECEIVED: "+name);
result = Arrays.copyOfRange(buffer,len,8192);
is = new ByteArrayInputStream(result);

BufferedImage bf = ImageIO.read(is);
System.out.println("AVAILABLE_BYTE_COUNT: "+is.available());
ImageIO.write(bf, "JPG", new FileOutputStream(TMP_DIR.getLocalVar(Local.TMP)+File.separator+name+".jpg"));
Image out = new Image("file:///"+TMP_DIR.getLocalVar(Local.TMP)+File.separator+NAME+".jpg",150,100,true,true);
System.err.println("ERROR: "+out.isError());
System.out.println("LDR_STATE: "+out.getProgress());


System.out.println("Buffer: "+buffer.length);
MTR.count(buffer.length);
MTR.stopMeasuringCycle();
return out;
}

Expand All @@ -322,6 +346,7 @@ protected Task createTask() {
@Override
protected Void call() throws Exception {
while(!this.isCancelled()){
MTR.startMeasuringCycle();
Established e = new Established();
CON.changeState(e);

Expand All @@ -333,13 +358,23 @@ protected Void call() throws Exception {
buffer = CON.read(t);
Image im = processData(buffer);
IMAGES.put("file:///"+TMP_DIR.getLocalVar(Local.TMP)+File.separator+NAME+".jpg", im);

String data = "";
data += "IP: "+t.getIp().substring(1)+",";
data += "Speed: "+t.getBaudrateMeter().kBPS()+"kB/s,";
data += "Is connected: "+t.isConnected()+",";
data += "Was connected earlier: "+t.wasConnected();
//System.out.println(data);
DATA.add(data);

Platform.runLater(() ->{
if(VIEWER_PANEL.getItems().size() == INDEX || VIEWER_PANEL.getItems().size() == 0){
if(VIEWER_PANEL.getItems().isEmpty() || VIEWER_PANEL.getItems().size() == INDEX){
VIEWER_PANEL.getItems().add("file:///"+TMP_DIR.getLocalVar(Local.TMP)+File.separator+NAME+".jpg");
}else{
VIEWER_PANEL.getItems().set(INDEX,"file:///"+TMP_DIR.getLocalVar(Local.TMP)+File.separator+NAME+".jpg");
}
});

} catch (TransportException | IOException ex) {
System.err.println("LOCALIZED_ERR_MSG:"+ex.getLocalizedMessage());
THREADS.forEach((desc,serv) ->{
Expand Down Expand Up @@ -384,6 +419,12 @@ public void updateItem(T item, boolean empty) {
System.out.println("LDR_STATE: "+out.getProgress());
setGraphic(new ImageView(out));
this.setOnMouseClicked(evt ->{
int index = VIEWER_PANEL.getSelectionModel().getSelectedIndex();
String data = DATA.get(index);

String[] splitted = data.split(",");
INFO_VIEW.getItems().clear();
INFO_VIEW.getItems().addAll(Arrays.asList(splitted));

});
this.setTextAlignment(TextAlignment.CENTER);
Expand Down

0 comments on commit 8b3cfd2

Please sign in to comment.