Added a listener for incoming traffic from the server to the application
This commit is contained in:
parent
b08e459843
commit
e7f012dc5f
6 changed files with 148 additions and 82 deletions
|
|
@ -1,5 +1,7 @@
|
|||
package com.coder.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
|
@ -7,6 +9,8 @@ import javax.swing.JOptionPane;
|
|||
|
||||
import com.coder.client.LoginDialog.LoginDialogAction;
|
||||
import com.coder.server.CoderServer;
|
||||
import com.coder.server.message.CoderMessage;
|
||||
import com.coder.server.message.ProjectListData;
|
||||
|
||||
import zutil.log.LogUtil;
|
||||
|
||||
|
|
@ -16,15 +20,13 @@ public class CoderClient extends Thread{
|
|||
private String url;
|
||||
private int port;
|
||||
private Session session;
|
||||
private ClientWindow window;
|
||||
private LoginDialog loginDialog;
|
||||
private ProjectEditorWindow projectEditorWindow;
|
||||
|
||||
public CoderClient(String url, int port) {
|
||||
this.url = url;
|
||||
this.port = port;
|
||||
|
||||
this.window = new ClientWindow("CoderClient");
|
||||
|
||||
this.projectEditorWindow = new ProjectEditorWindow("CoderClient");
|
||||
|
||||
super.start();
|
||||
}
|
||||
|
|
@ -32,18 +34,37 @@ public class CoderClient extends Thread{
|
|||
public void run(){
|
||||
//save the previously typed user name
|
||||
String username = "";
|
||||
//keep track of the number of connection retries to the server
|
||||
int connectionRetries = 0;
|
||||
while(true){
|
||||
//close previou session if applicable
|
||||
//close previous session if applicable
|
||||
closeCurrentSession();
|
||||
|
||||
//setup a new session
|
||||
if(connectionRetries > 0){
|
||||
logger.info("This is reconnection try number: " + connectionRetries);
|
||||
}
|
||||
this.session = Session.setupConnection(url, port);
|
||||
if(session == null){
|
||||
logger.info("Could not setup a connection to " + url + ":" + port);
|
||||
continue;
|
||||
connectionRetries++;
|
||||
if(connectionRetries > 5){
|
||||
//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;
|
||||
}
|
||||
}
|
||||
|
||||
//ask for username and passwor din a dialog window
|
||||
//ask for username and password in a dialog window
|
||||
LoginDialog loginDialog = new LoginDialog(username, null);
|
||||
loginDialog.setVisible(true); //blocking
|
||||
loginDialog.dispose();
|
||||
|
|
@ -58,15 +79,35 @@ public class CoderClient extends Thread{
|
|||
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");
|
||||
logger.info("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) {
|
||||
//TODO
|
||||
}
|
||||
});
|
||||
|
||||
//start receiving traffic from the server
|
||||
session.start();
|
||||
|
||||
//show the user the main GUI
|
||||
this.window.setVisible(true);
|
||||
//add a listener to forward messages from the editor GUI to the server
|
||||
this.projectEditorWindow.addMessageSentListener(new GUIMessageSentListener(){
|
||||
@Override
|
||||
public void sendMessage(CoderMessage msg) {
|
||||
try {
|
||||
session.send(msg);
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.SEVERE, "could not forward message from editor to the server", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
//show the user the editor GUI
|
||||
this.projectEditorWindow.setVisible(true);
|
||||
try {Thread.sleep(1000);} catch (InterruptedException e) {}
|
||||
|
||||
//wait here until the session is closed for some reason
|
||||
|
|
@ -75,12 +116,13 @@ public class CoderClient extends Thread{
|
|||
}
|
||||
|
||||
//hide the main GUI
|
||||
logger.info("The socket was closed. terminating.");
|
||||
this.window.setVisible(false);
|
||||
logger.info("The socket was closed.");
|
||||
this.projectEditorWindow.setVisible(false);
|
||||
Thread.yield();
|
||||
}
|
||||
logger.info("The program till now terminate");
|
||||
closeCurrentSession();
|
||||
this.window.dispose();
|
||||
this.projectEditorWindow.dispose();
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue