the project list will now be populated with data from the server
This commit is contained in:
parent
3634a61ee5
commit
bd77c2de28
10 changed files with 207 additions and 201 deletions
|
|
@ -18,7 +18,10 @@ import com.coder.client.gui.selectServer.SelectServerDialog;
|
|||
import com.coder.client.gui.selectServer.SelectServerDialogListener;
|
||||
import com.coder.server.CoderServer;
|
||||
import com.coder.server.message.CoderMessage;
|
||||
import com.coder.server.message.ConfigData;
|
||||
import com.coder.server.message.ProjectListData;
|
||||
import com.coder.server.message.ProjectListReqMsg;
|
||||
import com.coder.server.message.ProjectRspMsg;
|
||||
|
||||
import zutil.log.CompactLogFormatter;
|
||||
import zutil.log.LogUtil;
|
||||
|
|
@ -27,21 +30,22 @@ import javafx.stage.Stage;
|
|||
|
||||
public class CoderClient extends Application{
|
||||
public static final Logger logger = LogUtil.getLogger();
|
||||
private int port = CoderServer.SERVER_PORT;
|
||||
private static final int DEFAULT_CONNECTION_RETRIES = 5;
|
||||
|
||||
private Session session;
|
||||
private Stage mainStage;
|
||||
private int connectionRetriesLimit = DEFAULT_CONNECTION_RETRIES; //if zero, try forever
|
||||
|
||||
private String url;
|
||||
private String username = null;
|
||||
private char[] password = null;
|
||||
|
||||
//GUI elements
|
||||
private EditorWindow editorWindow;
|
||||
private LoginDialog loginDialog;
|
||||
private SelectProjectDialog selectProjectDialog;
|
||||
private SelectServerDialog selectServerDialog;
|
||||
|
||||
//state variables
|
||||
private String serverURL = null;
|
||||
private int serverPort = CoderServer.SERVER_PORT;
|
||||
private String username = null;
|
||||
private char[] password = null;
|
||||
|
||||
public static void main(String[] args) {
|
||||
Application.launch(args);
|
||||
}
|
||||
|
|
@ -55,80 +59,78 @@ public class CoderClient extends Application{
|
|||
|
||||
//parse program arguments
|
||||
Map<String, String> params = this.getParameters().getNamed();
|
||||
String url = null;
|
||||
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;
|
||||
this.username = value;
|
||||
}else if(key.equals("password")){
|
||||
password = value.toCharArray();
|
||||
}else if(key.equals("retry-connect-forever")){
|
||||
reconnectForever = true;
|
||||
this.password = value.toCharArray();
|
||||
}else if(key.equals("url")){
|
||||
url = value;
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//create a client instance
|
||||
if(url != null){
|
||||
this.url = url;
|
||||
}
|
||||
if(username != null){
|
||||
this.username = username;
|
||||
if(password != null){
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
if(reconnectForever){
|
||||
this.connectionRetriesLimit = 0;
|
||||
if(this.username == null){ //ignore the password if no username was set
|
||||
this.password = null;
|
||||
}
|
||||
|
||||
//loading GUI
|
||||
this.mainStage = mainStage;
|
||||
mainStage.setTitle("CoderClient");
|
||||
this.editorWindow = new EditorWindow();
|
||||
this.loginDialog = new LoginDialog();
|
||||
this.selectProjectDialog = new SelectProjectDialog();
|
||||
this.selectServerDialog = new SelectServerDialog();
|
||||
|
||||
//register listeners
|
||||
registerListeners();
|
||||
try{
|
||||
setupSelectServerDialog();
|
||||
setupLoginDialog();
|
||||
setupSelectProjectDialog();
|
||||
setupEditWindow();
|
||||
}catch(IOException e){
|
||||
logger.log(Level.SEVERE, "could not load all GUI elements", e);
|
||||
}
|
||||
|
||||
//start program logic
|
||||
selectServerDialog.showOnStage(mainStage);
|
||||
|
||||
//run();
|
||||
|
||||
}
|
||||
|
||||
private void registerListeners() {
|
||||
|
||||
private void setupSelectServerDialog() throws IOException{
|
||||
this.selectServerDialog = new SelectServerDialog();
|
||||
this.selectServerDialog.addSelectProjectDialogListener(new SelectServerDialogListener() {
|
||||
@Override
|
||||
public void willShow() {
|
||||
selectServerDialog.setURL(url);
|
||||
closeCurrentSession();
|
||||
selectServerDialog.setServerAddress(serverURL);
|
||||
selectServerDialog.setServerPort(serverPort);
|
||||
}
|
||||
@Override
|
||||
public void cancel() {
|
||||
//TODO
|
||||
logger.info("terminating");
|
||||
System.exit(0);
|
||||
}
|
||||
@Override
|
||||
public void connect(String serverUrl) {
|
||||
public void connect(String address, int port) {
|
||||
//selectServerDialog.hide();
|
||||
url = serverUrl;
|
||||
serverURL = address;
|
||||
serverPort = port;
|
||||
//connect session
|
||||
session = Session.setupConnection(url, port);
|
||||
session = Session.setupConnection(serverURL, serverPort);
|
||||
if(session == null){
|
||||
logger.warning("Could not setup a connection to " + url + ":" + port);
|
||||
url = null;
|
||||
logger.warning("Could not setup a connection to " + serverURL + ":" + port);
|
||||
serverURL = null;
|
||||
selectServerDialog.showOnStage(mainStage);
|
||||
}else{
|
||||
loginDialog.showOnStage(mainStage);
|
||||
}
|
||||
loginDialog.showOnStage(mainStage);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void setupLoginDialog() throws IOException {
|
||||
this.loginDialog = new LoginDialog();
|
||||
this.loginDialog.addLoginDialogListener(new LoginDialogListener(){
|
||||
@Override
|
||||
public void willShow() {
|
||||
|
|
@ -137,7 +139,9 @@ public class CoderClient extends Application{
|
|||
}
|
||||
@Override
|
||||
public void cancel() {
|
||||
//TODO
|
||||
serverURL = null;
|
||||
serverPort = CoderServer.SERVER_PORT;
|
||||
selectServerDialog.showOnStage(mainStage);
|
||||
}
|
||||
@Override
|
||||
public void login(String uname, char[] paswd) {
|
||||
|
|
@ -151,25 +155,40 @@ public class CoderClient extends Application{
|
|||
password = null;
|
||||
loginDialog.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() {
|
||||
@Override
|
||||
public void projectListRspReceived(Map<String, ProjectListData> projectListRsp) {
|
||||
for(String projectName : projectListRsp.keySet()){
|
||||
ProjectListData projectData = projectListRsp.get(projectName);
|
||||
selectProjectDialog.addProjectToList(projectName, projectData);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void projectRspReceived(ProjectRspMsg projectRspMsg) {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
@Override
|
||||
public void projectTypeRspReceived(Map<String, ConfigData> projectTypeRsp) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void setupSelectProjectDialog() throws IOException {
|
||||
this.selectProjectDialog = new SelectProjectDialog();
|
||||
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();
|
||||
}
|
||||
sendProjectListReq();
|
||||
}
|
||||
@Override
|
||||
public void openProject(String selectedProjectName) {
|
||||
|
|
@ -181,122 +200,52 @@ public class CoderClient extends Application{
|
|||
}
|
||||
@Override
|
||||
public void cancel() {
|
||||
serverURL = null;
|
||||
serverPort = CoderServer.SERVER_PORT;
|
||||
selectServerDialog.showOnStage(mainStage);
|
||||
}
|
||||
@Override
|
||||
public void refresh() {
|
||||
selectProjectDialog.clearProjectList();
|
||||
sendProjectListReq();
|
||||
}
|
||||
private void sendProjectListReq(){
|
||||
//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();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setupEditWindow() throws IOException {
|
||||
this.editorWindow = new EditorWindow();
|
||||
this.editorWindow.addEditorWindowListener(new EditorWindowListener() {
|
||||
@Override
|
||||
public void willShow() {
|
||||
//TODO
|
||||
}
|
||||
@Override
|
||||
public void compile() {
|
||||
//TODO
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
//TODO
|
||||
}
|
||||
});
|
||||
|
||||
this.editorWindow.addMessageSentListener(new EditorWindowListener() {
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void closeCurrentSession(){
|
||||
if(this.session != null){
|
||||
session.close();
|
||||
session = null;
|
||||
}
|
||||
}
|
||||
/*
|
||||
public void run(){
|
||||
//keep track of the number of connection retries to the server
|
||||
int connectionRetries = 0;
|
||||
while(true){
|
||||
//close previous session if applicable
|
||||
closeCurrentSession();
|
||||
|
||||
//setup a new session
|
||||
if(connectionRetries > 0){
|
||||
logger.fine("This is reconnection try number: " + connectionRetries);
|
||||
}
|
||||
logger.fine("Setting up a connected session");
|
||||
this.session = Session.setupConnection(url, port);
|
||||
if(session == null){
|
||||
logger.warning("Could not setup a connection to " + url + ":" + port);
|
||||
connectionRetries++;
|
||||
if(connectionRetriesLimit > 0 && connectionRetries > connectionRetriesLimit){
|
||||
//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;
|
||||
}
|
||||
}
|
||||
|
||||
//get user credentials
|
||||
if(username != null && !username.isEmpty() && password != null && password.length > 0){
|
||||
//do nothing, already have all credentials we need
|
||||
logger.fine("All login credentials have already been given. No need for a login dialog.");
|
||||
}else{
|
||||
//ask for username and password in a dialog window
|
||||
logger.info("All or some login credentials have not been given. Using a dialog for input.");
|
||||
loginDialog.setUsername(username);
|
||||
loginDialog.showModal(this.mainStage);
|
||||
if(loginDialog.getAction() == LoginDialogAction.CANCEL){
|
||||
logger.fine("Login dialog closed or canceled by the user. terminating.");
|
||||
break;
|
||||
}
|
||||
username = loginDialog.getUsername();
|
||||
password = loginDialog.getPassword();
|
||||
}
|
||||
logger.fine("The username: \"" + username + "\" will be used as the credential holder");
|
||||
|
||||
//authenticate the user
|
||||
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");
|
||||
continue;
|
||||
}
|
||||
|
||||
//resister a message listener to the session
|
||||
session.addCoderMessageReceivedListener(new CoderMessageReceivedListener() {
|
||||
@Override
|
||||
public void projectListRspReceived(Map<String, ProjectListData> projectListRsp) {
|
||||
for(String projectName : projectListRsp.keySet()){
|
||||
ProjectListData projectData = projectListRsp.get(projectName);
|
||||
selectProjectDialog.addProjectToList(projectName, projectData.type);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void projectTypeRspReceived(Map<String, ConfigData> projectTypeRsp) {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
@Override
|
||||
public void projectRspReceived(ProjectRspMsg projectRspMsg) {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
});
|
||||
|
||||
//start receiving traffic from the server
|
||||
session.start();
|
||||
|
||||
//show the project selector dialog
|
||||
this.selectProjectDialog.showModal(mainStage);
|
||||
|
||||
//wait here until the session is closed for some reason
|
||||
|
||||
while(session.isConnected()){
|
||||
Thread.yield();
|
||||
}
|
||||
|
||||
//hide the main GUI
|
||||
logger.fine("The socket was closed.");
|
||||
mainStage.close();
|
||||
Thread.yield();
|
||||
}
|
||||
logger.info("The program till now terminate");
|
||||
closeCurrentSession();
|
||||
// this.projectEditorWindow.dispose();
|
||||
System.exit(0);
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue