Added some comments and some stability fixes
This commit is contained in:
parent
2ff0009767
commit
b08e459843
4 changed files with 53 additions and 14 deletions
|
|
@ -3,6 +3,8 @@ package com.coder.client;
|
|||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
import com.coder.client.LoginDialog.LoginDialogAction;
|
||||
import com.coder.server.CoderServer;
|
||||
|
||||
|
|
@ -15,6 +17,7 @@ public class CoderClient extends Thread{
|
|||
private int port;
|
||||
private Session session;
|
||||
private ClientWindow window;
|
||||
private LoginDialog loginDialog;
|
||||
|
||||
public CoderClient(String url, int port) {
|
||||
this.url = url;
|
||||
|
|
@ -22,48 +25,72 @@ public class CoderClient extends Thread{
|
|||
|
||||
this.window = new ClientWindow("CoderClient");
|
||||
|
||||
|
||||
super.start();
|
||||
}
|
||||
|
||||
public void run(){
|
||||
//save the previously typed user name
|
||||
String username = "";
|
||||
while(true){
|
||||
//close previou session if applicable
|
||||
closeCurrentSession();
|
||||
|
||||
//setup a new session
|
||||
this.session = Session.setupConnection(url, port);
|
||||
if(session == null){
|
||||
logger.info("Could not setup a connection to " + url + ":" + port);
|
||||
continue;
|
||||
}
|
||||
|
||||
//ask for username and passwor din a dialog window
|
||||
LoginDialog loginDialog = new LoginDialog(username, null);
|
||||
loginDialog.setVisible(true);
|
||||
loginDialog.setVisible(true); //blocking
|
||||
loginDialog.dispose();
|
||||
if(loginDialog.getAction() == LoginDialogAction.CANCEL){
|
||||
logger.info("Login dialog closed or canceled by the user. terminating.");
|
||||
break;
|
||||
}
|
||||
|
||||
//authenticate the user
|
||||
username = loginDialog.getUsername();
|
||||
String password = loginDialog.getPassword();
|
||||
String password = new String(loginDialog.getPassword());
|
||||
boolean authenticated = session.authenticate(username, password);
|
||||
if(!authenticated){
|
||||
JOptionPane.showMessageDialog(null, "Wrong username or password", "Authentication Failed", JOptionPane.INFORMATION_MESSAGE);
|
||||
System.out.println("Authentication failed");
|
||||
continue;
|
||||
}
|
||||
|
||||
//start receiving traffic from the server
|
||||
session.start();
|
||||
|
||||
//show the user the main GUI
|
||||
this.window.setVisible(true);
|
||||
try {Thread.sleep(1000);} catch (InterruptedException e) {}
|
||||
|
||||
//wait here until the session is closed for some reason
|
||||
while(session.isConnected()){
|
||||
Thread.yield();
|
||||
}
|
||||
|
||||
//hide the main GUI
|
||||
logger.info("The socket was closed. terminating.");
|
||||
this.window.setVisible(false);
|
||||
Thread.yield();
|
||||
}
|
||||
closeCurrentSession();
|
||||
this.window.dispose();
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
private void closeCurrentSession(){
|
||||
if(this.session != null){
|
||||
session.close();
|
||||
session = null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
int port = CoderServer.SERVER_PORT;
|
||||
new CoderClient("127.0.0.1", port);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue