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);
|
leftHorizontalSpitPane.setResizeWeight(0.0);
|
||||||
this.add(leftHorizontalSpitPane);
|
this.add(leftHorizontalSpitPane);
|
||||||
|
|
||||||
//show this window
|
//pack the window
|
||||||
this.pack();
|
this.pack();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@ package com.coder.client;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import javax.swing.JOptionPane;
|
||||||
|
|
||||||
import com.coder.client.LoginDialog.LoginDialogAction;
|
import com.coder.client.LoginDialog.LoginDialogAction;
|
||||||
import com.coder.server.CoderServer;
|
import com.coder.server.CoderServer;
|
||||||
|
|
||||||
|
|
@ -15,6 +17,7 @@ public class CoderClient extends Thread{
|
||||||
private int port;
|
private int port;
|
||||||
private Session session;
|
private Session session;
|
||||||
private ClientWindow window;
|
private ClientWindow window;
|
||||||
|
private LoginDialog loginDialog;
|
||||||
|
|
||||||
public CoderClient(String url, int port) {
|
public CoderClient(String url, int port) {
|
||||||
this.url = url;
|
this.url = url;
|
||||||
|
|
@ -22,48 +25,72 @@ public class CoderClient extends Thread{
|
||||||
|
|
||||||
this.window = new ClientWindow("CoderClient");
|
this.window = new ClientWindow("CoderClient");
|
||||||
|
|
||||||
|
|
||||||
super.start();
|
super.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run(){
|
public void run(){
|
||||||
|
//save the previously typed user name
|
||||||
String username = "";
|
String username = "";
|
||||||
while(true){
|
while(true){
|
||||||
|
//close previou session if applicable
|
||||||
|
closeCurrentSession();
|
||||||
|
|
||||||
|
//setup a new session
|
||||||
this.session = Session.setupConnection(url, port);
|
this.session = Session.setupConnection(url, port);
|
||||||
if(session == null){
|
if(session == null){
|
||||||
logger.info("Could not setup a connection to " + url + ":" + port);
|
logger.info("Could not setup a connection to " + url + ":" + port);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//ask for username and passwor din a dialog window
|
||||||
LoginDialog loginDialog = new LoginDialog(username, null);
|
LoginDialog loginDialog = new LoginDialog(username, null);
|
||||||
loginDialog.setVisible(true);
|
loginDialog.setVisible(true); //blocking
|
||||||
|
loginDialog.dispose();
|
||||||
if(loginDialog.getAction() == LoginDialogAction.CANCEL){
|
if(loginDialog.getAction() == LoginDialogAction.CANCEL){
|
||||||
logger.info("Login dialog closed or canceled by the user. terminating.");
|
logger.info("Login dialog closed or canceled by the user. terminating.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//authenticate the user
|
||||||
username = loginDialog.getUsername();
|
username = loginDialog.getUsername();
|
||||||
String password = loginDialog.getPassword();
|
String password = new String(loginDialog.getPassword());
|
||||||
boolean authenticated = session.authenticate(username, password);
|
boolean authenticated = session.authenticate(username, password);
|
||||||
if(!authenticated){
|
if(!authenticated){
|
||||||
|
JOptionPane.showMessageDialog(null, "Wrong username or password", "Authentication Failed", JOptionPane.INFORMATION_MESSAGE);
|
||||||
System.out.println("Authentication failed");
|
System.out.println("Authentication failed");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//start receiving traffic from the server
|
||||||
session.start();
|
session.start();
|
||||||
|
|
||||||
|
//show the user the main GUI
|
||||||
this.window.setVisible(true);
|
this.window.setVisible(true);
|
||||||
try {Thread.sleep(1000);} catch (InterruptedException e) {}
|
try {Thread.sleep(1000);} catch (InterruptedException e) {}
|
||||||
|
|
||||||
|
//wait here until the session is closed for some reason
|
||||||
while(session.isConnected()){
|
while(session.isConnected()){
|
||||||
Thread.yield();
|
Thread.yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//hide the main GUI
|
||||||
logger.info("The socket was closed. terminating.");
|
logger.info("The socket was closed. terminating.");
|
||||||
this.window.setVisible(false);
|
this.window.setVisible(false);
|
||||||
Thread.yield();
|
Thread.yield();
|
||||||
}
|
}
|
||||||
|
closeCurrentSession();
|
||||||
|
this.window.dispose();
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void closeCurrentSession(){
|
||||||
|
if(this.session != null){
|
||||||
|
session.close();
|
||||||
|
session = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
int port = CoderServer.SERVER_PORT;
|
int port = CoderServer.SERVER_PORT;
|
||||||
new CoderClient("127.0.0.1", port);
|
new CoderClient("127.0.0.1", port);
|
||||||
|
|
|
||||||
|
|
@ -99,6 +99,8 @@ public class LoginDialog extends JDialog {
|
||||||
public void keyPressed(KeyEvent e) {
|
public void keyPressed(KeyEvent e) {
|
||||||
if(e.getKeyCode() == KeyEvent.VK_ENTER){
|
if(e.getKeyCode() == KeyEvent.VK_ENTER){
|
||||||
btnLogin.doClick();
|
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) {
|
public void keyPressed(KeyEvent e) {
|
||||||
if(e.getKeyCode() == KeyEvent.VK_ENTER){
|
if(e.getKeyCode() == KeyEvent.VK_ENTER){
|
||||||
btnLogin.doClick();
|
btnLogin.doClick();
|
||||||
|
}else if(e.getKeyCode() == KeyEvent.VK_ESCAPE){
|
||||||
|
btnCancel.doClick();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -134,8 +138,14 @@ public class LoginDialog extends JDialog {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//pack the dialog
|
||||||
pack();
|
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);
|
setResizable(false);
|
||||||
setLocationRelativeTo(parent);
|
setLocationRelativeTo(parent);
|
||||||
}
|
}
|
||||||
|
|
@ -144,8 +154,8 @@ public class LoginDialog extends JDialog {
|
||||||
return tfUsername.getText().trim();
|
return tfUsername.getText().trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPassword() {
|
public char[] getPassword() {
|
||||||
return new String(pfPassword.getPassword());
|
return pfPassword.getPassword();
|
||||||
}
|
}
|
||||||
|
|
||||||
public LoginDialogAction getAction() {
|
public LoginDialogAction getAction() {
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ public class Session extends Thread {
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void close(){
|
public void close(){
|
||||||
if(socket != null){
|
if(socket != null){
|
||||||
try {
|
try {
|
||||||
socket.close();
|
socket.close();
|
||||||
|
|
@ -81,18 +81,20 @@ public class Session extends Thread {
|
||||||
try {
|
try {
|
||||||
msg = in.readGenericObject();
|
msg = in.readGenericObject();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
close();
|
logger.log(Level.INFO, "socket is probably closed by the peer", e);
|
||||||
return;
|
break;
|
||||||
}
|
}
|
||||||
if(msg == null){
|
if(msg == null){
|
||||||
close();
|
logger.info("socket is probably closed by the peer");
|
||||||
return;
|
break;
|
||||||
}
|
}else{
|
||||||
if(msg != null){
|
|
||||||
handleMessage(msg);
|
handleMessage(msg);
|
||||||
}
|
}
|
||||||
Thread.yield();
|
Thread.yield();
|
||||||
}
|
}
|
||||||
|
logger.info("the current session will be closed");
|
||||||
|
close();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void send(CoderMessage msg) throws IOException{
|
public void send(CoderMessage msg) throws IOException{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue