2015-10-09 13:37:45 +00:00
|
|
|
package com.coder.client;
|
|
|
|
|
|
2015-10-13 08:13:23 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Map;
|
2015-10-09 13:37:45 +00:00
|
|
|
import java.util.logging.Level;
|
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
2015-10-20 14:26:58 +00:00
|
|
|
import com.coder.client.gui.editor.EditorWindow;
|
|
|
|
|
import com.coder.client.gui.login.LoginDialog;
|
2015-10-23 13:44:11 +00:00
|
|
|
import com.coder.client.gui.newProject.NewProjectDialog;
|
2015-10-20 15:52:23 +00:00
|
|
|
import com.coder.client.gui.selectProject.SelectProjectDialog;
|
|
|
|
|
import com.coder.client.gui.selectServer.SelectServerDialog;
|
2015-11-11 16:13:01 +01:00
|
|
|
import com.coder.client.project.ProjectHandler;
|
|
|
|
|
import com.coder.client.project.ProjectListener;
|
2015-11-10 15:16:10 +01:00
|
|
|
import com.coder.client.session.ProjectRspMsgListener;
|
2015-11-11 16:13:01 +01:00
|
|
|
import com.coder.client.session.SessionHandler;
|
2015-11-10 15:16:10 +01:00
|
|
|
import com.coder.client.session.SessionListener;
|
2015-11-11 16:13:01 +01:00
|
|
|
import com.coder.server.message.CoderMessage;
|
|
|
|
|
import com.coder.server.message.ProjectCreateReqMsg;
|
|
|
|
|
import com.coder.server.message.ProjectReqMsg;
|
|
|
|
|
import com.coder.server.message.ProjectRspMsg;
|
2015-10-09 13:37:45 +00:00
|
|
|
|
2015-10-13 08:53:07 +00:00
|
|
|
import zutil.log.CompactLogFormatter;
|
2015-10-09 13:37:45 +00:00
|
|
|
import zutil.log.LogUtil;
|
2015-10-22 14:08:31 +00:00
|
|
|
import zutil.net.ssdp.SSDPClient;
|
|
|
|
|
import zutil.net.ssdp.SSDPClient.SSDPServiceListener;
|
|
|
|
|
import zutil.net.ssdp.StandardSSDPInfo;
|
2015-10-20 14:26:58 +00:00
|
|
|
import javafx.application.Application;
|
2015-10-21 16:13:45 +00:00
|
|
|
import javafx.application.Platform;
|
2015-10-20 14:26:58 +00:00
|
|
|
import javafx.stage.Stage;
|
2015-10-09 13:37:45 +00:00
|
|
|
|
2015-10-20 14:26:58 +00:00
|
|
|
public class CoderClient extends Application{
|
2015-10-09 13:37:45 +00:00
|
|
|
public static final Logger logger = LogUtil.getLogger();
|
2015-11-11 16:13:01 +01:00
|
|
|
private SessionHandler sessionHandler = new SessionHandler();
|
|
|
|
|
private ProjectHandler projectHandler = new ProjectHandler();
|
2015-10-27 12:18:22 +01:00
|
|
|
|
2015-10-21 14:23:00 +00:00
|
|
|
//GUI elements
|
2015-10-20 14:26:58 +00:00
|
|
|
private EditorWindow editorWindow;
|
|
|
|
|
private LoginDialog loginDialog;
|
|
|
|
|
private SelectProjectDialog selectProjectDialog;
|
2015-10-20 15:52:23 +00:00
|
|
|
private SelectServerDialog selectServerDialog;
|
2015-10-23 13:44:11 +00:00
|
|
|
private NewProjectDialog newProjectDialog;
|
2015-10-27 12:18:22 +01:00
|
|
|
|
2015-10-22 14:08:31 +00:00
|
|
|
//services
|
|
|
|
|
SSDPClient ssdpClient;
|
2015-10-27 12:18:22 +01:00
|
|
|
|
2015-10-20 14:26:58 +00:00
|
|
|
public static void main(String[] args) {
|
2015-10-27 12:18:22 +01:00
|
|
|
Application.launch(args);
|
|
|
|
|
}
|
2015-10-20 14:26:58 +00:00
|
|
|
|
|
|
|
|
@Override
|
2015-11-11 16:13:01 +01:00
|
|
|
public void start(final Stage mainStage) throws Exception {
|
2015-10-27 12:18:22 +01:00
|
|
|
|
2015-10-20 14:26:58 +00:00
|
|
|
//setup logging
|
2015-10-28 12:56:22 +01:00
|
|
|
CompactLogFormatter formatter = new CompactLogFormatter();
|
2015-10-29 12:52:11 +01:00
|
|
|
LogUtil.setLevel("com.coder", Level.FINEST);
|
2015-10-28 12:56:22 +01:00
|
|
|
LogUtil.setFormatter("com.coder", formatter);
|
2015-10-29 12:52:11 +01:00
|
|
|
LogUtil.setLevel("zutil", Level.FINEST);
|
|
|
|
|
LogUtil.setFormatter("zutil", formatter);
|
|
|
|
|
LogUtil.setGlobalFormatter(formatter);
|
2015-10-27 12:18:22 +01:00
|
|
|
|
|
|
|
|
//setup GUI elements
|
|
|
|
|
mainStage.setTitle("CoderClient");
|
|
|
|
|
try{
|
2015-11-11 16:13:01 +01:00
|
|
|
this.selectServerDialog = new SelectServerDialog(this);
|
|
|
|
|
this.loginDialog = new LoginDialog(this);
|
|
|
|
|
this.selectProjectDialog = new SelectProjectDialog(this);
|
|
|
|
|
this.newProjectDialog = new NewProjectDialog(this);
|
|
|
|
|
this.editorWindow = new EditorWindow(this);
|
2015-10-27 12:18:22 +01:00
|
|
|
}catch(IOException e){
|
|
|
|
|
logger.log(Level.SEVERE, "could not load all GUI elements", e);
|
|
|
|
|
System.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-20 14:26:58 +00:00
|
|
|
//parse program arguments
|
|
|
|
|
Map<String, String> params = this.getParameters().getNamed();
|
|
|
|
|
for(String key : params.keySet()){
|
|
|
|
|
String value = params.get(key);
|
|
|
|
|
if(key.equals("username")){
|
2015-10-27 12:18:22 +01:00
|
|
|
loginDialog.setUsername(value);
|
2015-10-20 15:52:23 +00:00
|
|
|
}else if(key.equals("url")){
|
2015-10-27 12:18:22 +01:00
|
|
|
selectServerDialog.setServerAddress(value);
|
2015-10-21 14:23:00 +00:00
|
|
|
}else if(key.equals("port")){
|
|
|
|
|
try{
|
2015-10-27 12:18:22 +01:00
|
|
|
selectServerDialog.setServerPort(Integer.parseInt(value));
|
2015-10-21 14:23:00 +00:00
|
|
|
}catch(NumberFormatException e){
|
2015-11-05 13:58:45 +01:00
|
|
|
logger.warning("port argument to program is not of a integer type");
|
|
|
|
|
selectServerDialog.setServerPort(-1);
|
2015-10-21 14:23:00 +00:00
|
|
|
}
|
2015-10-22 14:08:31 +00:00
|
|
|
}else if(key.equals("project")){
|
2015-11-11 16:13:01 +01:00
|
|
|
projectHandler.setProject(value);
|
2015-10-20 14:26:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
2015-10-27 12:18:22 +01:00
|
|
|
|
2015-10-22 14:08:31 +00:00
|
|
|
//setup SSDP client
|
|
|
|
|
try{
|
|
|
|
|
setupSSDPClient();
|
|
|
|
|
}catch(IOException e){
|
|
|
|
|
logger.log(Level.SEVERE, "could not setup SSDP client", e);
|
|
|
|
|
System.exit(1);
|
2015-10-21 14:23:00 +00:00
|
|
|
}
|
2015-11-11 16:13:01 +01:00
|
|
|
|
|
|
|
|
//add session listeners
|
|
|
|
|
sessionHandler.addSessionListener(new SessionListener() {
|
2015-10-28 12:58:33 +01:00
|
|
|
@Override
|
2015-11-11 16:13:01 +01:00
|
|
|
public void sessionDisconnected(boolean retryToEstablishConnection) {
|
|
|
|
|
Platform.runLater(new Runnable(){
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
logger.info("session disconnected, will show the select server dialog on the main stage");
|
|
|
|
|
selectServerDialog.showOnStage(mainStage);
|
2015-10-28 12:58:33 +01:00
|
|
|
}
|
2015-11-11 16:13:01 +01:00
|
|
|
});
|
2015-10-21 14:23:00 +00:00
|
|
|
}
|
|
|
|
|
@Override
|
2015-11-11 16:13:01 +01:00
|
|
|
public void sessionConnectionSuccess() {
|
|
|
|
|
Platform.runLater(new Runnable(){
|
2015-11-10 15:16:10 +01:00
|
|
|
@Override
|
2015-11-11 16:13:01 +01:00
|
|
|
public void run() {
|
|
|
|
|
logger.info("session connection successfull, will show the login dialog on the main stage");
|
|
|
|
|
loginDialog.showOnStage(mainStage);
|
2015-11-10 15:16:10 +01:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-10-23 13:44:11 +00:00
|
|
|
@Override
|
2015-11-11 16:13:01 +01:00
|
|
|
public void sessionConnectionFailure() {
|
|
|
|
|
Platform.runLater(new Runnable(){
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
logger.info("session connection failure, will show the select server dialog on the main stage");
|
|
|
|
|
selectServerDialog.showOnStage(mainStage);
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-10-23 13:44:11 +00:00
|
|
|
}
|
|
|
|
|
@Override
|
2015-11-11 16:13:01 +01:00
|
|
|
public void sessionAuthenticationSuccess() {
|
|
|
|
|
Platform.runLater(new Runnable(){
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
logger.info("session authentication successfull, will show the select project dialog on the main stage");
|
|
|
|
|
selectProjectDialog.showOnStage(mainStage);
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-10-23 13:44:11 +00:00
|
|
|
}
|
|
|
|
|
@Override
|
2015-11-11 16:13:01 +01:00
|
|
|
public void sessionAuthenticationFailure() {
|
|
|
|
|
Platform.runLater(new Runnable(){
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
logger.info("session authentication failure, will show the select server dialog on the main stage");
|
|
|
|
|
selectServerDialog.showOnStage(mainStage);
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-10-23 13:44:11 +00:00
|
|
|
}
|
2015-11-10 15:16:10 +01:00
|
|
|
@Override
|
2015-11-11 16:13:01 +01:00
|
|
|
public void sessionAuthenticationCancel() {
|
|
|
|
|
Platform.runLater(new Runnable(){
|
2015-11-10 15:16:10 +01:00
|
|
|
@Override
|
2015-11-11 16:13:01 +01:00
|
|
|
public void run() {
|
|
|
|
|
logger.info("session authentication canceled, will show the select server dialog on the main stage");
|
|
|
|
|
selectServerDialog.showOnStage(mainStage);
|
2015-11-10 15:16:10 +01:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-11-11 16:13:01 +01:00
|
|
|
|
|
|
|
|
//add project listeners
|
|
|
|
|
projectHandler.addprojectListener(new ProjectListener() {
|
2015-10-20 14:26:58 +00:00
|
|
|
@Override
|
2015-11-11 16:13:01 +01:00
|
|
|
public void selectProject() {
|
|
|
|
|
Platform.runLater(new Runnable(){
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
logger.info("select project requested, will show the select project dialog on the main stage");
|
|
|
|
|
selectProjectDialog.showOnStage(mainStage);
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-10-20 14:26:58 +00:00
|
|
|
}
|
|
|
|
|
@Override
|
2015-11-11 16:13:01 +01:00
|
|
|
public void openProject(String projectName) {
|
2015-10-23 13:44:11 +00:00
|
|
|
CoderMessage msg = new CoderMessage();
|
|
|
|
|
msg.ProjectReq = new ProjectReqMsg();
|
2015-11-11 16:13:01 +01:00
|
|
|
msg.ProjectReq.name = projectName;
|
|
|
|
|
sessionHandler.sendMessage(msg);
|
2015-10-23 13:44:11 +00:00
|
|
|
}
|
2015-11-05 14:42:40 +01:00
|
|
|
@Override
|
2015-11-11 16:13:01 +01:00
|
|
|
public void createNewProject(String name, String type, String description) {
|
|
|
|
|
CoderMessage msg = new CoderMessage();
|
|
|
|
|
msg.ProjectCreateReq = new ProjectCreateReqMsg();
|
|
|
|
|
msg.ProjectCreateReq.name = name;
|
|
|
|
|
msg.ProjectCreateReq.type = type;
|
|
|
|
|
msg.ProjectCreateReq.description = description;
|
|
|
|
|
sessionHandler.sendMessage(msg);
|
2015-11-05 14:42:40 +01:00
|
|
|
}
|
2015-11-10 15:16:10 +01:00
|
|
|
@Override
|
2015-11-11 16:13:01 +01:00
|
|
|
public void createNewProject() {
|
|
|
|
|
Platform.runLater(new Runnable(){
|
2015-11-10 15:16:10 +01:00
|
|
|
@Override
|
2015-11-11 16:13:01 +01:00
|
|
|
public void run() {
|
|
|
|
|
logger.info("new project requested, will show the new project dialog on the main stage");
|
|
|
|
|
newProjectDialog.showOnStage(mainStage);
|
2015-11-10 15:16:10 +01:00
|
|
|
}
|
|
|
|
|
});
|
2015-11-11 16:13:01 +01:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//add message listeners
|
|
|
|
|
sessionHandler.addMessageListener(new ProjectRspMsgListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void messageReceived(final ProjectRspMsg msg) {
|
|
|
|
|
Platform.runLater(new Runnable() {
|
2015-11-10 15:16:10 +01:00
|
|
|
@Override
|
2015-11-11 16:13:01 +01:00
|
|
|
public void run() {
|
|
|
|
|
if(msg.error != null){
|
|
|
|
|
selectServerDialog.showOnStage(mainStage);
|
|
|
|
|
}else{
|
|
|
|
|
editorWindow.showOnStage(mainStage);
|
|
|
|
|
}
|
2015-11-10 15:16:10 +01:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-11-11 16:13:01 +01:00
|
|
|
|
|
|
|
|
//start program logic
|
|
|
|
|
selectServerDialog.setServerPort(-1);
|
|
|
|
|
selectServerDialog.showOnStage(mainStage);
|
|
|
|
|
|
2015-10-20 14:26:58 +00:00
|
|
|
}
|
2015-10-27 12:18:22 +01:00
|
|
|
|
2015-10-22 14:08:31 +00:00
|
|
|
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() {
|
2015-10-27 12:18:22 +01:00
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2015-10-22 14:08:31 +00:00
|
|
|
String ip = service.getInetAddress().getHostAddress();
|
2015-11-05 13:58:45 +01:00
|
|
|
selectServerDialog.addServerToList(ip, 1337);
|
2015-10-27 12:18:22 +01:00
|
|
|
}
|
2015-10-22 14:08:31 +00:00
|
|
|
});
|
2015-10-28 12:56:22 +01:00
|
|
|
} else {
|
|
|
|
|
logger.warning("New Service found ("+service.getLocation()+") when stage closed.");
|
|
|
|
|
}
|
2015-10-22 14:08:31 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
ssdpClient.start();
|
|
|
|
|
}
|
2015-10-27 12:18:22 +01:00
|
|
|
|
2015-11-11 16:13:01 +01:00
|
|
|
public void exit() {
|
|
|
|
|
logger.info("terminating");
|
|
|
|
|
Platform.exit();
|
|
|
|
|
System.exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SSDPClient getSSDPClient() {
|
|
|
|
|
return this.ssdpClient;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public SessionHandler getSessionHandler() {
|
|
|
|
|
return this.sessionHandler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ProjectHandler getProjectHandler(){
|
|
|
|
|
return this.projectHandler;
|
2015-10-20 14:26:58 +00:00
|
|
|
}
|
2015-10-09 13:37:45 +00:00
|
|
|
|
|
|
|
|
}
|