Implemented SSDP client and is now using graphical list cells in ListViews

This commit is contained in:
Daniel Collin 2015-10-22 14:08:31 +00:00
parent 120dded24f
commit 34cf5865fe
16 changed files with 428 additions and 86 deletions

View file

@ -5,8 +5,6 @@ import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import com.coder.client.Session;
import com.coder.client.gui.editor.EditorWindow;
import com.coder.client.gui.editor.EditorWindowListener;
@ -25,6 +23,9 @@ import com.coder.server.message.ProjectRspMsg;
import zutil.log.CompactLogFormatter;
import zutil.log.LogUtil;
import zutil.net.ssdp.SSDPClient;
import zutil.net.ssdp.SSDPClient.SSDPServiceListener;
import zutil.net.ssdp.StandardSSDPInfo;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
@ -45,7 +46,12 @@ public class CoderClient extends Application{
private String serverURL = null;
private int serverPort = CoderServer.SERVER_PORT;
private String username = null;
private char[] password = null;
private char[] password = null; //should only be anything else than null if defined by program argument
private String loginErrorMessage = "";
private String project = null;
//services
SSDPClient ssdpClient;
public static void main(String[] args) {
Application.launch(args);
@ -74,13 +80,15 @@ public class CoderClient extends Application{
}catch(NumberFormatException e){
logger.warning("port argument to program is not of a integer type. using default port.");
}
}else if(key.equals("project")){
this.project = value;
}
}
if(this.username == null){ //ignore the password if no username was set
this.password = null;
}
//loading GUI
//setup GUI elements
this.mainStage = mainStage;
mainStage.setTitle("CoderClient");
try{
@ -90,6 +98,15 @@ public class CoderClient extends Application{
setupEditWindow();
}catch(IOException e){
logger.log(Level.SEVERE, "could not load all GUI elements", e);
System.exit(1);
}
//setup SSDP client
try{
setupSSDPClient();
}catch(IOException e){
logger.log(Level.SEVERE, "could not setup SSDP client", e);
System.exit(1);
}
//start program logic
@ -103,6 +120,13 @@ public class CoderClient extends Application{
@Override
public void willShow() {
closeCurrentSession();
ssdpClient.clearServices();
selectServerDialog.clearServerList();
if(ssdpClient != null){
ssdpClient.requestService("coder:discover");
}else{
logger.severe("could not send a SSDP request since the client is not setup");
}
selectServerDialog.setServerAddress(serverURL);
selectServerDialog.setServerPort(serverPort);
}
@ -121,13 +145,13 @@ public class CoderClient extends Application{
if(session == null){
logger.warning("Could not setup a connection to " + serverURL + ":" + port);
serverURL = null;
serverPort = CoderServer.SERVER_PORT;
selectServerDialog.showOnStage(mainStage);
}else{
loginDialog.showOnStage(mainStage);
}
}
});
}
private void setupLoginDialog() throws IOException {
@ -137,6 +161,7 @@ public class CoderClient extends Application{
public void willShow() {
loginDialog.setUsername(username);
loginDialog.setPassword(password);
loginDialog.setErrorMessage(loginErrorMessage);
}
@Override
public void cancel() {
@ -151,14 +176,16 @@ public class CoderClient extends Application{
//authenticate session
boolean authenticated = session.authenticate(username, password);
if(!authenticated){
JOptionPane.showMessageDialog(null, "Wrong username or password", "Authentication Failed", JOptionPane.INFORMATION_MESSAGE);
logger.severe("Authentication failed: wrong username or password");
password = null;
loginDialog.showOnStage(mainStage);
loginErrorMessage = "Wrong username or password";
selectServerDialog.showOnStage(mainStage);
}else{
loginErrorMessage = "";
setupSessionListener(); //resister a message listener to the session
session.start(); //start receiving traffic from the server
selectProjectDialog.showOnStage(mainStage);
}
setupSessionListener(); //resister a message listener to the session
session.start(); //start receiving traffic from the server
selectProjectDialog.showOnStage(mainStage);
}
private void setupSessionListener(){
session.addCoderMessageReceivedListener(new CoderMessageReceivedListener() {
@ -187,17 +214,22 @@ public class CoderClient extends Application{
}
});
}
private void setupSelectProjectDialog() throws IOException {
this.selectProjectDialog = new SelectProjectDialog();
this.selectProjectDialog.addSelectProjectDialogListener(new SelectProjectDialogListener() {
@Override
public void willShow() {
selectProjectDialog.clearProjectList();
sendProjectListReq();
if(project == null){
sendProjectListReq();
}else{
selectProjectDialog.setProject(project);
}
}
@Override
public void openProject(String selectedProjectName) {
public void open(String selectedProjectName) {
project = selectedProjectName;
editorWindow.showOnStage(mainStage);
}
@Override
@ -247,6 +279,25 @@ public class CoderClient extends Application{
});
}
private void setupSSDPClient() throws IOException{
this.ssdpClient = new SSDPClient();
ssdpClient.setListener(new SSDPServiceListener() {
@Override
public void newService(final StandardSSDPInfo service) {
if(selectServerDialog != null){
Platform.runLater(new Runnable() {
@Override
public void run() {
String ip = service.getInetAddress().getHostAddress();
selectServerDialog.addServerToList(ip);
}
});
}
}
});
ssdpClient.start();
}
private void closeCurrentSession(){
if(this.session != null){
logger.info("disconnecting from server");