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
|
|
@ -66,7 +66,7 @@ public class ClientWindow extends JFrame implements TreeSelectionListener {
|
|||
leftHorizontalSpitPane.setResizeWeight(0.0);
|
||||
this.add(leftHorizontalSpitPane);
|
||||
|
||||
//show this window
|
||||
//pack the window
|
||||
this.pack();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -99,6 +99,8 @@ public class LoginDialog extends JDialog {
|
|||
public void keyPressed(KeyEvent e) {
|
||||
if(e.getKeyCode() == KeyEvent.VK_ENTER){
|
||||
btnLogin.doClick();
|
||||
}else if(e.getKeyCode() == KeyEvent.VK_ESCAPE){
|
||||
btnCancel.doClick();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -119,6 +121,8 @@ public class LoginDialog extends JDialog {
|
|||
public void keyPressed(KeyEvent e) {
|
||||
if(e.getKeyCode() == KeyEvent.VK_ENTER){
|
||||
btnLogin.doClick();
|
||||
}else if(e.getKeyCode() == KeyEvent.VK_ESCAPE){
|
||||
btnCancel.doClick();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -134,18 +138,24 @@ public class LoginDialog extends JDialog {
|
|||
|
||||
});
|
||||
|
||||
|
||||
//pack the dialog
|
||||
pack();
|
||||
|
||||
//move focus to the password field if already a username has been defined. must be done after pack()
|
||||
if(username.isEmpty() == false){
|
||||
pfPassword.requestFocusInWindow();
|
||||
}
|
||||
|
||||
setResizable(false);
|
||||
setLocationRelativeTo(parent);
|
||||
setLocationRelativeTo(parent);
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return tfUsername.getText().trim();
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return new String(pfPassword.getPassword());
|
||||
public char[] getPassword() {
|
||||
return pfPassword.getPassword();
|
||||
}
|
||||
|
||||
public LoginDialogAction getAction() {
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ public class Session extends Thread {
|
|||
return session;
|
||||
}
|
||||
|
||||
private void close(){
|
||||
public void close(){
|
||||
if(socket != null){
|
||||
try {
|
||||
socket.close();
|
||||
|
|
@ -81,18 +81,20 @@ public class Session extends Thread {
|
|||
try {
|
||||
msg = in.readGenericObject();
|
||||
} catch (IOException e) {
|
||||
close();
|
||||
return;
|
||||
logger.log(Level.INFO, "socket is probably closed by the peer", e);
|
||||
break;
|
||||
}
|
||||
if(msg == null){
|
||||
close();
|
||||
return;
|
||||
}
|
||||
if(msg != null){
|
||||
logger.info("socket is probably closed by the peer");
|
||||
break;
|
||||
}else{
|
||||
handleMessage(msg);
|
||||
}
|
||||
Thread.yield();
|
||||
}
|
||||
logger.info("the current session will be closed");
|
||||
close();
|
||||
return;
|
||||
}
|
||||
|
||||
public void send(CoderMessage msg) throws IOException{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue