2015-10-09 13:37:45 +00:00
|
|
|
package com.coder.client;
|
|
|
|
|
|
2015-10-13 08:13:23 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Map;
|
2015-10-09 13:37:45 +00:00
|
|
|
import java.util.logging.Level;
|
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
2015-10-12 12:29:24 +00:00
|
|
|
import javax.swing.JOptionPane;
|
|
|
|
|
|
2015-10-09 13:37:45 +00:00
|
|
|
import com.coder.client.LoginDialog.LoginDialogAction;
|
|
|
|
|
import com.coder.server.CoderServer;
|
2015-10-13 08:13:23 +00:00
|
|
|
import com.coder.server.message.CoderMessage;
|
|
|
|
|
import com.coder.server.message.ProjectListData;
|
2015-10-09 13:37:45 +00:00
|
|
|
|
|
|
|
|
import zutil.log.LogUtil;
|
|
|
|
|
|
|
|
|
|
public class CoderClient extends Thread{
|
|
|
|
|
public static final Logger logger = LogUtil.getLogger();
|
|
|
|
|
|
|
|
|
|
private String url;
|
|
|
|
|
private int port;
|
|
|
|
|
private Session session;
|
2015-10-13 08:13:23 +00:00
|
|
|
private ProjectEditorWindow projectEditorWindow;
|
2015-10-09 13:37:45 +00:00
|
|
|
|
|
|
|
|
public CoderClient(String url, int port) {
|
|
|
|
|
this.url = url;
|
|
|
|
|
this.port = port;
|
|
|
|
|
|
2015-10-13 08:13:23 +00:00
|
|
|
this.projectEditorWindow = new ProjectEditorWindow("CoderClient");
|
2015-10-12 12:29:24 +00:00
|
|
|
|
2015-10-09 13:37:45 +00:00
|
|
|
super.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void run(){
|
2015-10-12 12:29:24 +00:00
|
|
|
//save the previously typed user name
|
2015-10-09 13:37:45 +00:00
|
|
|
String username = "";
|
2015-10-13 08:13:23 +00:00
|
|
|
//keep track of the number of connection retries to the server
|
|
|
|
|
int connectionRetries = 0;
|
2015-10-09 13:37:45 +00:00
|
|
|
while(true){
|
2015-10-13 08:13:23 +00:00
|
|
|
//close previous session if applicable
|
2015-10-12 12:29:24 +00:00
|
|
|
closeCurrentSession();
|
|
|
|
|
|
|
|
|
|
//setup a new session
|
2015-10-13 08:13:23 +00:00
|
|
|
if(connectionRetries > 0){
|
|
|
|
|
logger.info("This is reconnection try number: " + connectionRetries);
|
|
|
|
|
}
|
2015-10-09 13:37:45 +00:00
|
|
|
this.session = Session.setupConnection(url, port);
|
|
|
|
|
if(session == null){
|
|
|
|
|
logger.info("Could not setup a connection to " + url + ":" + port);
|
2015-10-13 08:13:23 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2015-10-09 13:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
2015-10-13 08:13:23 +00:00
|
|
|
//ask for username and password in a dialog window
|
2015-10-09 13:37:45 +00:00
|
|
|
LoginDialog loginDialog = new LoginDialog(username, null);
|
2015-10-12 12:29:24 +00:00
|
|
|
loginDialog.setVisible(true); //blocking
|
|
|
|
|
loginDialog.dispose();
|
2015-10-09 13:37:45 +00:00
|
|
|
if(loginDialog.getAction() == LoginDialogAction.CANCEL){
|
|
|
|
|
logger.info("Login dialog closed or canceled by the user. terminating.");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-10-12 12:29:24 +00:00
|
|
|
|
|
|
|
|
//authenticate the user
|
2015-10-09 13:37:45 +00:00
|
|
|
username = loginDialog.getUsername();
|
2015-10-12 12:29:24 +00:00
|
|
|
String password = new String(loginDialog.getPassword());
|
2015-10-09 13:37:45 +00:00
|
|
|
boolean authenticated = session.authenticate(username, password);
|
|
|
|
|
if(!authenticated){
|
2015-10-12 12:29:24 +00:00
|
|
|
JOptionPane.showMessageDialog(null, "Wrong username or password", "Authentication Failed", JOptionPane.INFORMATION_MESSAGE);
|
2015-10-13 08:13:23 +00:00
|
|
|
logger.info("Authentication failed: wrong username or password");
|
2015-10-09 13:37:45 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-13 08:13:23 +00:00
|
|
|
//resister a message listener to the session
|
|
|
|
|
session.addCoderMessageReceivedListener(new CoderMessageReceivedListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void projectListRspReceived(Map<String, ProjectListData> projectListRsp) {
|
|
|
|
|
//TODO
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2015-10-12 12:29:24 +00:00
|
|
|
//start receiving traffic from the server
|
2015-10-09 13:37:45 +00:00
|
|
|
session.start();
|
|
|
|
|
|
2015-10-13 08:13:23 +00:00
|
|
|
//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);
|
2015-10-09 13:37:45 +00:00
|
|
|
try {Thread.sleep(1000);} catch (InterruptedException e) {}
|
|
|
|
|
|
2015-10-12 12:29:24 +00:00
|
|
|
//wait here until the session is closed for some reason
|
2015-10-09 13:37:45 +00:00
|
|
|
while(session.isConnected()){
|
|
|
|
|
Thread.yield();
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-12 12:29:24 +00:00
|
|
|
//hide the main GUI
|
2015-10-13 08:13:23 +00:00
|
|
|
logger.info("The socket was closed.");
|
|
|
|
|
this.projectEditorWindow.setVisible(false);
|
2015-10-09 13:37:45 +00:00
|
|
|
Thread.yield();
|
|
|
|
|
}
|
2015-10-13 08:13:23 +00:00
|
|
|
logger.info("The program till now terminate");
|
2015-10-12 12:29:24 +00:00
|
|
|
closeCurrentSession();
|
2015-10-13 08:13:23 +00:00
|
|
|
this.projectEditorWindow.dispose();
|
2015-10-09 13:37:45 +00:00
|
|
|
System.exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-12 12:29:24 +00:00
|
|
|
private void closeCurrentSession(){
|
|
|
|
|
if(this.session != null){
|
|
|
|
|
session.close();
|
|
|
|
|
session = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-09 13:37:45 +00:00
|
|
|
public static void main(String[] args){
|
|
|
|
|
int port = CoderServer.SERVER_PORT;
|
|
|
|
|
new CoderClient("127.0.0.1", port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|