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-12 12:29:24 +00:00
|
|
|
import javax.swing.JOptionPane;
|
|
|
|
|
|
2015-10-20 14:26:58 +00:00
|
|
|
import com.coder.client.Session;
|
|
|
|
|
import com.coder.client.gui.editor.EditorWindow;
|
|
|
|
|
import com.coder.client.gui.editor.EditorWindowListener;
|
|
|
|
|
import com.coder.client.gui.login.LoginDialog;
|
2015-10-20 15:52:23 +00:00
|
|
|
import com.coder.client.gui.login.LoginDialogListener;
|
|
|
|
|
import com.coder.client.gui.selectProject.SelectProjectDialog;
|
|
|
|
|
import com.coder.client.gui.selectProject.SelectProjectDialogListener;
|
|
|
|
|
import com.coder.client.gui.selectServer.SelectServerDialog;
|
|
|
|
|
import com.coder.client.gui.selectServer.SelectServerDialogListener;
|
2015-10-09 13:37:45 +00:00
|
|
|
import com.coder.server.CoderServer;
|
2015-10-13 08:13:23 +00:00
|
|
|
import com.coder.server.message.CoderMessage;
|
2015-10-21 14:23:00 +00:00
|
|
|
import com.coder.server.message.ConfigData;
|
|
|
|
|
import com.coder.server.message.ProjectListData;
|
2015-10-20 06:56:14 +00:00
|
|
|
import com.coder.server.message.ProjectListReqMsg;
|
2015-10-21 14:23:00 +00:00
|
|
|
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-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-10-21 14:23:00 +00:00
|
|
|
|
2015-10-09 13:37:45 +00:00
|
|
|
private Session session;
|
2015-10-20 14:26:58 +00:00
|
|
|
private Stage mainStage;
|
2015-10-20 15:52:23 +00: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-09 13:37:45 +00:00
|
|
|
|
2015-10-21 14:23:00 +00:00
|
|
|
//state variables
|
|
|
|
|
private String serverURL = null;
|
|
|
|
|
private int serverPort = CoderServer.SERVER_PORT;
|
|
|
|
|
private String username = null;
|
|
|
|
|
private char[] password = null;
|
|
|
|
|
|
2015-10-20 14:26:58 +00:00
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Application.launch(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void start(Stage mainStage) throws Exception {
|
2015-10-12 12:29:24 +00:00
|
|
|
|
2015-10-20 14:26:58 +00:00
|
|
|
//setup logging
|
2015-10-14 08:21:24 +00:00
|
|
|
LogUtil.setGlobalLevel(Level.INFO);
|
2015-10-13 08:53:07 +00:00
|
|
|
LogUtil.setGlobalFormatter(new CompactLogFormatter());
|
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-21 14:23:00 +00:00
|
|
|
this.username = value;
|
2015-10-20 14:26:58 +00:00
|
|
|
}else if(key.equals("password")){
|
2015-10-21 14:23:00 +00:00
|
|
|
this.password = value.toCharArray();
|
2015-10-20 15:52:23 +00:00
|
|
|
}else if(key.equals("url")){
|
2015-10-21 14:23:00 +00:00
|
|
|
this.serverURL = value;
|
|
|
|
|
}else if(key.equals("port")){
|
|
|
|
|
try{
|
|
|
|
|
this.serverPort = Integer.parseInt(value);
|
|
|
|
|
}catch(NumberFormatException e){
|
|
|
|
|
logger.warning("port argument to program is not of a integer type. using default port.");
|
|
|
|
|
}
|
2015-10-20 14:26:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
2015-10-21 14:23:00 +00:00
|
|
|
if(this.username == null){ //ignore the password if no username was set
|
|
|
|
|
this.password = null;
|
2015-10-20 14:26:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//loading GUI
|
|
|
|
|
this.mainStage = mainStage;
|
|
|
|
|
mainStage.setTitle("CoderClient");
|
2015-10-21 14:23:00 +00:00
|
|
|
try{
|
|
|
|
|
setupSelectServerDialog();
|
|
|
|
|
setupLoginDialog();
|
|
|
|
|
setupSelectProjectDialog();
|
|
|
|
|
setupEditWindow();
|
|
|
|
|
}catch(IOException e){
|
|
|
|
|
logger.log(Level.SEVERE, "could not load all GUI elements", e);
|
|
|
|
|
}
|
2015-10-20 14:26:58 +00:00
|
|
|
|
|
|
|
|
//start program logic
|
2015-10-20 15:52:23 +00:00
|
|
|
selectServerDialog.showOnStage(mainStage);
|
2015-10-21 14:23:00 +00:00
|
|
|
|
2015-10-09 13:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
2015-10-21 14:23:00 +00:00
|
|
|
private void setupSelectServerDialog() throws IOException{
|
|
|
|
|
this.selectServerDialog = new SelectServerDialog();
|
2015-10-20 15:52:23 +00:00
|
|
|
this.selectServerDialog.addSelectProjectDialogListener(new SelectServerDialogListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void willShow() {
|
2015-10-21 14:23:00 +00:00
|
|
|
closeCurrentSession();
|
|
|
|
|
selectServerDialog.setServerAddress(serverURL);
|
|
|
|
|
selectServerDialog.setServerPort(serverPort);
|
2015-10-20 15:52:23 +00:00
|
|
|
}
|
|
|
|
|
@Override
|
2015-10-21 16:13:45 +00:00
|
|
|
public void exit() {
|
2015-10-21 14:23:00 +00:00
|
|
|
logger.info("terminating");
|
|
|
|
|
System.exit(0);
|
2015-10-20 15:52:23 +00:00
|
|
|
}
|
|
|
|
|
@Override
|
2015-10-21 14:23:00 +00:00
|
|
|
public void connect(String address, int port) {
|
2015-10-20 15:52:23 +00:00
|
|
|
//selectServerDialog.hide();
|
2015-10-21 14:23:00 +00:00
|
|
|
serverURL = address;
|
|
|
|
|
serverPort = port;
|
2015-10-20 15:52:23 +00:00
|
|
|
//connect session
|
2015-10-21 14:23:00 +00:00
|
|
|
session = Session.setupConnection(serverURL, serverPort);
|
2015-10-20 15:52:23 +00:00
|
|
|
if(session == null){
|
2015-10-21 14:23:00 +00:00
|
|
|
logger.warning("Could not setup a connection to " + serverURL + ":" + port);
|
|
|
|
|
serverURL = null;
|
2015-10-20 15:52:23 +00:00
|
|
|
selectServerDialog.showOnStage(mainStage);
|
2015-10-21 14:23:00 +00:00
|
|
|
}else{
|
|
|
|
|
loginDialog.showOnStage(mainStage);
|
2015-10-20 15:52:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2015-10-21 14:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setupLoginDialog() throws IOException {
|
|
|
|
|
this.loginDialog = new LoginDialog();
|
2015-10-20 15:52:23 +00:00
|
|
|
this.loginDialog.addLoginDialogListener(new LoginDialogListener(){
|
|
|
|
|
@Override
|
|
|
|
|
public void willShow() {
|
|
|
|
|
loginDialog.setUsername(username);
|
|
|
|
|
loginDialog.setPassword(password);
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void cancel() {
|
2015-10-21 14:23:00 +00:00
|
|
|
serverURL = null;
|
|
|
|
|
serverPort = CoderServer.SERVER_PORT;
|
|
|
|
|
selectServerDialog.showOnStage(mainStage);
|
2015-10-20 15:52:23 +00:00
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void login(String uname, char[] paswd) {
|
|
|
|
|
username = uname;
|
|
|
|
|
password = paswd;
|
|
|
|
|
//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);
|
|
|
|
|
}
|
2015-10-21 14:23:00 +00:00
|
|
|
setupSessionListener(); //resister a message listener to the session
|
|
|
|
|
session.start(); //start receiving traffic from the server
|
2015-10-20 15:52:23 +00:00
|
|
|
selectProjectDialog.showOnStage(mainStage);
|
|
|
|
|
}
|
2015-10-21 14:23:00 +00:00
|
|
|
private void setupSessionListener(){
|
|
|
|
|
session.addCoderMessageReceivedListener(new CoderMessageReceivedListener() {
|
|
|
|
|
@Override
|
2015-10-21 16:13:45 +00:00
|
|
|
public void projectListRspReceived(final Map<String, ProjectListData> projectListRsp) {
|
|
|
|
|
Platform.runLater(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
for(String projectName : projectListRsp.keySet()){
|
|
|
|
|
ProjectListData projectData = projectListRsp.get(projectName);
|
|
|
|
|
selectProjectDialog.addProjectToList(projectName, projectData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-10-21 14:23:00 +00:00
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void projectRspReceived(ProjectRspMsg projectRspMsg) {
|
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void projectTypeRspReceived(Map<String, ConfigData> projectTypeRsp) {
|
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-10-20 15:52:23 +00:00
|
|
|
});
|
2015-10-21 14:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setupSelectProjectDialog() throws IOException {
|
|
|
|
|
this.selectProjectDialog = new SelectProjectDialog();
|
2015-10-20 14:26:58 +00:00
|
|
|
this.selectProjectDialog.addSelectProjectDialogListener(new SelectProjectDialogListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void willShow() {
|
|
|
|
|
selectProjectDialog.clearProjectList();
|
2015-10-21 14:23:00 +00:00
|
|
|
sendProjectListReq();
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void openProject(String selectedProjectName) {
|
|
|
|
|
editorWindow.showOnStage(mainStage);
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void newProject() {
|
|
|
|
|
//TODO
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void cancel() {
|
|
|
|
|
serverURL = null;
|
|
|
|
|
serverPort = CoderServer.SERVER_PORT;
|
|
|
|
|
selectServerDialog.showOnStage(mainStage);
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void refresh() {
|
|
|
|
|
selectProjectDialog.clearProjectList();
|
|
|
|
|
sendProjectListReq();
|
|
|
|
|
}
|
|
|
|
|
private void sendProjectListReq(){
|
2015-10-20 14:26:58 +00:00
|
|
|
//send a request for a new list of projects
|
|
|
|
|
CoderMessage msg = new CoderMessage();
|
|
|
|
|
msg.ProjectListReq = new ProjectListReqMsg();
|
|
|
|
|
try {
|
|
|
|
|
session.send(msg);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
logger.log(Level.SEVERE, "Unable to send ProjectListReqMsg", e);
|
|
|
|
|
closeCurrentSession();
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-21 14:23:00 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setupEditWindow() throws IOException {
|
|
|
|
|
this.editorWindow = new EditorWindow();
|
|
|
|
|
this.editorWindow.addEditorWindowListener(new EditorWindowListener() {
|
2015-10-20 14:26:58 +00:00
|
|
|
@Override
|
2015-10-21 14:23:00 +00:00
|
|
|
public void willShow() {
|
|
|
|
|
//TODO
|
2015-10-20 14:26:58 +00:00
|
|
|
}
|
|
|
|
|
@Override
|
2015-10-21 14:23:00 +00:00
|
|
|
public void compile() {
|
2015-10-20 14:26:58 +00:00
|
|
|
//TODO
|
|
|
|
|
}
|
|
|
|
|
@Override
|
2015-10-21 14:23:00 +00:00
|
|
|
public void run() {
|
2015-10-20 15:52:23 +00:00
|
|
|
//TODO
|
2015-10-20 14:26:58 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-10-21 14:23:00 +00:00
|
|
|
|
2015-10-20 14:26:58 +00:00
|
|
|
private void closeCurrentSession(){
|
|
|
|
|
if(this.session != null){
|
2015-10-21 16:13:45 +00:00
|
|
|
logger.info("disconnecting from server");
|
2015-10-20 14:26:58 +00:00
|
|
|
session.close();
|
|
|
|
|
session = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-09 13:37:45 +00:00
|
|
|
|
|
|
|
|
}
|