Skip to content

Commit

Permalink
I dont know, really...
Browse files Browse the repository at this point in the history
  • Loading branch information
0fca committed Sep 22, 2017
1 parent a6f9ccb commit 0200bf1
Show file tree
Hide file tree
Showing 21 changed files with 486 additions and 152 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,10 @@
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
<dependency>
<groupId>org.controlsfx</groupId>
<artifactId>controlsfx</artifactId>
<version>8.40.13</version>
</dependency>
</dependencies>
</project>
27 changes: 27 additions & 0 deletions src/main/java/com/neology/controllers/Accessable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2017 zsel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.neology.controllers;

/**
*
* @author obsidiam
*/
public interface Accessable {
boolean checkViewUpdaterAccess(Thread th);
void sendNotificationSignal(SignalType s);
void commitSignalData(String msg);
}
63 changes: 63 additions & 0 deletions src/main/java/com/neology/controllers/AccessorImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2017 zsel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.neology.controllers;

import com.neology.controllers.FXMLController.ViewUpdater;
import com.neology.exceptions.AccessDeniedException;

/**
*
* @author obsidiam
*/
final public class AccessorImpl implements Accessable{
private ViewUpdater instance;
private boolean hasAccess = false;
private Thread th;

AccessorImpl(ViewUpdater v){
this.instance = v;
}

@Override
public void sendNotificationSignal(SignalType s) {
if(hasAccess){
instance.signal(s);
}else{
throw new AccessDeniedException("Access is denied for "+th.getName());
}
}


@Override
public boolean checkViewUpdaterAccess(Thread t) throws AccessDeniedException{
try{
instance.checkAccess();
hasAccess = true;
}finally{
if(hasAccess){
return true;
}
}
this.th = t;
return false;
}

@Override
public void commitSignalData(String msg) {
instance.commitSignalData(msg);
}
}
62 changes: 50 additions & 12 deletions src/main/java/com/neology/controllers/FXMLController.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
Expand All @@ -48,7 +49,9 @@
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.Duration;
import javax.xml.parsers.ParserConfigurationException;
import org.controlsfx.control.Notifications;
import org.xml.sax.SAXException;

public class FXMLController extends LocalEnvironment implements Initializable,Viewable {
Expand Down Expand Up @@ -151,6 +154,7 @@ private void setUpConfiguration() throws SAXException, IOException, ParserConfig
XMLController xml = new XMLController();
INIT = xml.parseInitFile();
PORT = Integer.parseInt(INIT.get(0));
ADDR = INIT.get(1);
}

private void loginUser() throws ClassNotFoundException {
Expand Down Expand Up @@ -203,6 +207,7 @@ private boolean checkFolders() {
}

private void initSession() {
mgr.setAccessorInstance(new AccessorImpl(v));
c.start();
tcp.start();
v.start();
Expand All @@ -216,10 +221,9 @@ private void transitToDisconnectedMode() {
tcp.interrupt();
mgr.interruptThread();
CDH.getData().clear();


CDH.getConnectionList().clear();

CDH.setFree(true);
Platform.runLater(() ->{
VIEWER_PANEL.getItems().clear();
INFO_VIEW.getItems().clear();
Expand All @@ -235,9 +239,7 @@ private void transitToConnectedMode() {
System.out.println("ConnectionManager -> "+c.getState().toString());
if(c.getState() == Service.State.CANCELLED || c.getState() == Service.State.SUCCEEDED){
Platform.runLater(() ->{
c.restart();
tcp.start();
mgr.start();
reinitSession();
});
}
System.out.println("ConnectionManager -> "+c.getState().toString());
Expand All @@ -262,6 +264,12 @@ private void setInfoViewData(String line) {
public void viewCustom() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

private void reinitSession() {
c.restart();
tcp.start();
mgr.start();
}


private class CallbackImpl implements Callback<ListView<String>, ListCell<String>> {
Expand All @@ -280,9 +288,8 @@ public void updateItem(T item, boolean empty) {
setText(null);
setGraphic(null);
} else {
String fn = Paths.get(item.toString()).getFileName().toString();
setText(fn);

String fn = Paths.get(item.toString().split("-")[1]).getFileName().toString();
//setText(fn);
Image out = IDH.getImagesMap().get(fn.split("\\.")[0]);

if(out != null){
Expand All @@ -305,9 +312,12 @@ public void updateItem(T item, boolean empty) {
}
}

private class ViewUpdater extends Thread implements Runnable{
class ViewUpdater extends Thread implements Runnable{
private Thread T = null;

private boolean wasSignaled = false;
private SignalType type;
private String msg = null;

@Override
public void start(){
if(T == null){
Expand All @@ -324,16 +334,18 @@ public void run(){

Platform.runLater(() ->{
VIEWER_PANEL.getItems().clear();
//System.out.println(map.size());
if(map.size() > 0){
if(c.getState() == javafx.concurrent.ScheduledService.State.RUNNING){

map.forEach((x,y) ->{
VIEWER_PANEL.getItems().add("file://"+getLocalVar(Local.TMP)+File.separator+x+".jpg:"+x);
VIEWER_PANEL.getItems().add("file://"+getLocalVar(Local.TMP)+File.separator+x+".jpg-"+x);
});

if(SELECTED > -1 && VIEWER_PANEL.getItems().size() > SELECTED){
data.forEach((x,y) ->{
VIEWER_PANEL.getSelectionModel().select(SELECTED);
Object item = VIEWER_PANEL.getItems().get(SELECTED).toString().split(":")[2];
Object item = VIEWER_PANEL.getItems().get(SELECTED).toString().split("-")[1];
if(item != null){
if(CDH.findConnectionName(item.toString()) != null){
setInfoViewData(CDH.getData().get(CDH.findConnectionName(item.toString())));
Expand All @@ -344,6 +356,11 @@ public void run(){
}
}
});

if(wasSignaled && msg != null){
Platform.runLater(() -> showNotification(type.toString(), msg));
wasSignaled = false;
}
try {
Thread.sleep(1000);
System.gc();
Expand All @@ -352,5 +369,26 @@ public void run(){
}
}
}

synchronized void signal(SignalType type){
this.type = type;
wasSignaled = true;
}

synchronized void commitSignalData(String msg){
this.msg = msg;
}

private synchronized void showNotification(String title, String text) {

Notifications notificationBuilder = Notifications.create()
.title(title)
.text(text)
.graphic(null)
.hideAfter(Duration.seconds(10))
.darkStyle()
.position(Pos.BOTTOM_RIGHT);
notificationBuilder.show();
}
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/neology/controllers/SignalType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2017 zsel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.neology.controllers;

/**
*
* @author obsidiam
*/
public enum SignalType {
INFO,
WARNING,
ERROR
}
21 changes: 15 additions & 6 deletions src/main/java/com/neology/data/ConnectionDataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@
package com.neology.data;

import com.neology.net.Connection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;


/**
*
* @author obsidiam
*/
public class ConnectionDataHandler {
public final class ConnectionDataHandler{
private volatile HashMap<String,String> DATA = new HashMap<>();
private volatile HashSet<String> IPS_MAP = new HashSet<>();
private static volatile ConnectionDataHandler INSTANCE = new ConnectionDataHandler();
private volatile List<Connection> SOCKET_LIST = Collections.synchronizedList(new ArrayList<>());
private final ObservableList<Connection> SOCKET_LIST = FXCollections.synchronizedObservableList(FXCollections.observableArrayList());
private volatile HashMap<String,String> CONN_USER_DATA = new HashMap<>();
private volatile boolean isFree = true;

private ConnectionDataHandler(){}

Expand Down Expand Up @@ -55,7 +56,7 @@ public synchronized void removeFromIpMap(String ip){
IPS_MAP.remove(ip);
}

public synchronized List<Connection> getConnectionList(){
public synchronized ObservableList<Connection> getConnectionList(){
return SOCKET_LIST;
}

Expand All @@ -72,5 +73,13 @@ public synchronized void putConnectionName(String userName, String ip){

public synchronized void removeConnectionName(String userName, String ip){
CONN_USER_DATA.remove(userName, ip);
}

public void setFree(boolean free){
this.isFree = free;
}

public boolean isFree(){
return isFree;
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/neology/exceptions/AccessDeniedException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2017 zsel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.neology.exceptions;

/**
*
* @author obsidiam
*/
public final class AccessDeniedException extends SecurityException{
public AccessDeniedException(String s){
super(s);
}

public AccessDeniedException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
@SuppressWarnings("serial")
public class ClosedConnectionException extends TransportException {

public ClosedConnectionException(Throwable exception) {
super(exception);
public ClosedConnectionException(String msg, Throwable exception) {
super(msg ,exception);
}

}
Loading

0 comments on commit 0200bf1

Please sign in to comment.