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-20 06:56:14 +00:00
|
|
|
import com.coder.server.message.ProjectListReqMsg;
|
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;
|
|
|
|
|
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-20 14:26:58 +00:00
|
|
|
private int port = CoderServer.SERVER_PORT;
|
2015-10-14 08:21:24 +00:00
|
|
|
private static final int DEFAULT_CONNECTION_RETRIES = 5;
|
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
|
|
|
private int connectionRetriesLimit = DEFAULT_CONNECTION_RETRIES; //if zero, try forever
|
|
|
|
|
|
|
|
|
|
private String url;
|
2015-10-13 08:53:07 +00:00
|
|
|
private String username = null;
|
|
|
|
|
private char[] password = null;
|
2015-10-20 15:52:23 +00:00
|
|
|
|
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-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();
|
2015-10-20 15:52:23 +00:00
|
|
|
String url = null;
|
2015-10-20 14:26:58 +00:00
|
|
|
String username = null;
|
|
|
|
|
char[] password = null;
|
|
|
|
|
boolean reconnectForever = false;
|
|
|
|
|
for(String key : params.keySet()){
|
|
|
|
|
String value = params.get(key);
|
|
|
|
|
if(key.equals("username")){
|
|
|
|
|
username = value;
|
|
|
|
|
}else if(key.equals("password")){
|
|
|
|
|
password = value.toCharArray();
|
|
|
|
|
}else if(key.equals("retry-connect-forever")){
|
|
|
|
|
reconnectForever = true;
|
2015-10-20 15:52:23 +00:00
|
|
|
}else if(key.equals("url")){
|
|
|
|
|
url = value;
|
2015-10-20 14:26:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//create a client instance
|
2015-10-20 15:52:23 +00:00
|
|
|
if(url != null){
|
|
|
|
|
this.url = url;
|
|
|
|
|
}
|
2015-10-20 14:26:58 +00:00
|
|
|
if(username != null){
|
|
|
|
|
this.username = username;
|
|
|
|
|
if(password != null){
|
|
|
|
|
this.password = password;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(reconnectForever){
|
|
|
|
|
this.connectionRetriesLimit = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//loading GUI
|
|
|
|
|
this.mainStage = mainStage;
|
|
|
|
|
mainStage.setTitle("CoderClient");
|
|
|
|
|
this.editorWindow = new EditorWindow();
|
|
|
|
|
this.loginDialog = new LoginDialog();
|
|
|
|
|
this.selectProjectDialog = new SelectProjectDialog();
|
2015-10-20 15:52:23 +00:00
|
|
|
this.selectServerDialog = new SelectServerDialog();
|
2015-10-20 14:26:58 +00:00
|
|
|
|
|
|
|
|
//register listeners
|
|
|
|
|
registerListeners();
|
|
|
|
|
|
|
|
|
|
//start program logic
|
2015-10-20 15:52:23 +00:00
|
|
|
selectServerDialog.showOnStage(mainStage);
|
|
|
|
|
|
|
|
|
|
//run();
|
2015-10-09 13:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
2015-10-20 14:26:58 +00:00
|
|
|
private void registerListeners() {
|
|
|
|
|
|
2015-10-20 15:52:23 +00:00
|
|
|
this.selectServerDialog.addSelectProjectDialogListener(new SelectServerDialogListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void willShow() {
|
|
|
|
|
selectServerDialog.setURL(url);
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void cancel() {
|
|
|
|
|
//TODO
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void connect(String serverUrl) {
|
|
|
|
|
//selectServerDialog.hide();
|
|
|
|
|
url = serverUrl;
|
|
|
|
|
//connect session
|
|
|
|
|
session = Session.setupConnection(url, port);
|
|
|
|
|
if(session == null){
|
|
|
|
|
logger.warning("Could not setup a connection to " + url + ":" + port);
|
|
|
|
|
url = null;
|
|
|
|
|
selectServerDialog.showOnStage(mainStage);
|
|
|
|
|
}
|
|
|
|
|
loginDialog.showOnStage(mainStage);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.loginDialog.addLoginDialogListener(new LoginDialogListener(){
|
|
|
|
|
@Override
|
|
|
|
|
public void willShow() {
|
|
|
|
|
loginDialog.setUsername(username);
|
|
|
|
|
loginDialog.setPassword(password);
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void cancel() {
|
|
|
|
|
//TODO
|
|
|
|
|
}
|
|
|
|
|
@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);
|
|
|
|
|
}
|
|
|
|
|
selectProjectDialog.showOnStage(mainStage);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2015-10-20 14:26:58 +00:00
|
|
|
this.selectProjectDialog.addSelectProjectDialogListener(new SelectProjectDialogListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void willShow() {
|
|
|
|
|
//clear current list of project
|
|
|
|
|
selectProjectDialog.clearProjectList();
|
|
|
|
|
|
|
|
|
|
//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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void openProject(String selectedProjectName) {
|
|
|
|
|
editorWindow.showOnStage(mainStage);
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void newProject() {
|
|
|
|
|
//TODO
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void cancel() {
|
2015-10-20 15:52:23 +00:00
|
|
|
//TODO
|
2015-10-20 14:26:58 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.editorWindow.addMessageSentListener(new EditorWindowListener() {
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void closeCurrentSession(){
|
|
|
|
|
if(this.session != null){
|
|
|
|
|
session.close();
|
|
|
|
|
session = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-20 15:52:23 +00:00
|
|
|
/*
|
2015-10-09 13:37:45 +00:00
|
|
|
public void run(){
|
2015-10-13 08:13:23 +00:00
|
|
|
//keep track of the number of connection retries to the server
|
|
|
|
|
int connectionRetries = 0;
|
2015-10-09 13:37:45 +00:00
|
|
|
while(true){
|
2015-10-13 08:13:23 +00:00
|
|
|
//close previous session if applicable
|
2015-10-12 12:29:24 +00:00
|
|
|
closeCurrentSession();
|
|
|
|
|
|
|
|
|
|
//setup a new session
|
2015-10-13 08:13:23 +00:00
|
|
|
if(connectionRetries > 0){
|
2015-10-14 08:21:24 +00:00
|
|
|
logger.fine("This is reconnection try number: " + connectionRetries);
|
2015-10-13 08:13:23 +00:00
|
|
|
}
|
2015-10-14 08:21:24 +00:00
|
|
|
logger.fine("Setting up a connected session");
|
2015-10-09 13:37:45 +00:00
|
|
|
this.session = Session.setupConnection(url, port);
|
|
|
|
|
if(session == null){
|
2015-10-14 08:21:24 +00:00
|
|
|
logger.warning("Could not setup a connection to " + url + ":" + port);
|
2015-10-13 08:13:23 +00:00
|
|
|
connectionRetries++;
|
2015-10-14 08:21:24 +00:00
|
|
|
if(connectionRetriesLimit > 0 && connectionRetries > connectionRetriesLimit){
|
2015-10-13 08:13:23 +00:00
|
|
|
//stop trying to connect
|
|
|
|
|
logger.severe("Was not able to conenct to the remote host.");
|
|
|
|
|
break;
|
|
|
|
|
}else{
|
|
|
|
|
//wait for awhile and try to connect one more
|
|
|
|
|
logger.info("Will retry to connect once more in 2 seconds.");
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(2000);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2015-10-09 13:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
2015-10-13 08:53:07 +00:00
|
|
|
//get user credentials
|
|
|
|
|
if(username != null && !username.isEmpty() && password != null && password.length > 0){
|
|
|
|
|
//do nothing, already have all credentials we need
|
2015-10-14 08:21:24 +00:00
|
|
|
logger.fine("All login credentials have already been given. No need for a login dialog.");
|
2015-10-13 08:53:07 +00:00
|
|
|
}else{
|
|
|
|
|
//ask for username and password in a dialog window
|
2015-10-14 08:21:24 +00:00
|
|
|
logger.info("All or some login credentials have not been given. Using a dialog for input.");
|
2015-10-20 14:26:58 +00:00
|
|
|
loginDialog.setUsername(username);
|
|
|
|
|
loginDialog.showModal(this.mainStage);
|
|
|
|
|
if(loginDialog.getAction() == LoginDialogAction.CANCEL){
|
2015-10-14 08:21:24 +00:00
|
|
|
logger.fine("Login dialog closed or canceled by the user. terminating.");
|
2015-10-13 08:53:07 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
username = loginDialog.getUsername();
|
|
|
|
|
password = loginDialog.getPassword();
|
2015-10-09 13:37:45 +00:00
|
|
|
}
|
2015-10-14 08:21:24 +00:00
|
|
|
logger.fine("The username: \"" + username + "\" will be used as the credential holder");
|
2015-10-12 12:29:24 +00:00
|
|
|
|
|
|
|
|
//authenticate the user
|
2015-10-09 13:37:45 +00:00
|
|
|
boolean authenticated = session.authenticate(username, password);
|
|
|
|
|
if(!authenticated){
|
2015-10-12 12:29:24 +00:00
|
|
|
JOptionPane.showMessageDialog(null, "Wrong username or password", "Authentication Failed", JOptionPane.INFORMATION_MESSAGE);
|
2015-10-14 08:21:24 +00:00
|
|
|
logger.severe("Authentication failed: wrong username or password");
|
2015-10-09 13:37:45 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-13 08:13:23 +00:00
|
|
|
//resister a message listener to the session
|
|
|
|
|
session.addCoderMessageReceivedListener(new CoderMessageReceivedListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void projectListRspReceived(Map<String, ProjectListData> projectListRsp) {
|
2015-10-20 06:56:14 +00:00
|
|
|
for(String projectName : projectListRsp.keySet()){
|
|
|
|
|
ProjectListData projectData = projectListRsp.get(projectName);
|
2015-10-20 14:26:58 +00:00
|
|
|
selectProjectDialog.addProjectToList(projectName, projectData.type);
|
2015-10-20 06:56:14 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void projectTypeRspReceived(Map<String, ConfigData> projectTypeRsp) {
|
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void projectRspReceived(ProjectRspMsg projectRspMsg) {
|
|
|
|
|
// TODO Auto-generated method stub
|
2015-10-13 08:13:23 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2015-10-12 12:29:24 +00:00
|
|
|
//start receiving traffic from the server
|
2015-10-09 13:37:45 +00:00
|
|
|
session.start();
|
|
|
|
|
|
2015-10-20 06:56:14 +00:00
|
|
|
//show the project selector dialog
|
2015-10-20 14:26:58 +00:00
|
|
|
this.selectProjectDialog.showModal(mainStage);
|
2015-10-09 13:37:45 +00:00
|
|
|
|
2015-10-12 12:29:24 +00:00
|
|
|
//wait here until the session is closed for some reason
|
2015-10-20 14:26:58 +00:00
|
|
|
|
2015-10-09 13:37:45 +00:00
|
|
|
while(session.isConnected()){
|
|
|
|
|
Thread.yield();
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-12 12:29:24 +00:00
|
|
|
//hide the main GUI
|
2015-10-14 08:21:24 +00:00
|
|
|
logger.fine("The socket was closed.");
|
2015-10-20 14:26:58 +00:00
|
|
|
mainStage.close();
|
2015-10-09 13:37:45 +00:00
|
|
|
Thread.yield();
|
|
|
|
|
}
|
2015-10-13 08:13:23 +00:00
|
|
|
logger.info("The program till now terminate");
|
2015-10-12 12:29:24 +00:00
|
|
|
closeCurrentSession();
|
2015-10-20 14:26:58 +00:00
|
|
|
// this.projectEditorWindow.dispose();
|
2015-10-09 13:37:45 +00:00
|
|
|
System.exit(0);
|
|
|
|
|
}
|
2015-10-20 15:52:23 +00:00
|
|
|
*/
|
2015-10-09 13:37:45 +00:00
|
|
|
|
|
|
|
|
}
|