added more program arguments and modified logger levels

This commit is contained in:
Daniel Collin 2015-10-14 08:21:24 +00:00
parent 45e0e3d8cb
commit 84f0d5f354
3 changed files with 74 additions and 28 deletions

View file

@ -17,6 +17,7 @@ import zutil.log.LogUtil;
public class CoderClient extends Thread{ public class CoderClient extends Thread{
public static final Logger logger = LogUtil.getLogger(); public static final Logger logger = LogUtil.getLogger();
private static final int DEFAULT_CONNECTION_RETRIES = 5;
private String url; private String url;
private int port; private int port;
@ -24,6 +25,7 @@ public class CoderClient extends Thread{
private ProjectEditorWindow projectEditorWindow; private ProjectEditorWindow projectEditorWindow;
private String username = null; private String username = null;
private char[] password = null; private char[] password = null;
private int connectionRetriesLimit = DEFAULT_CONNECTION_RETRIES; //if zero, try forever
public CoderClient(String url, int port) { public CoderClient(String url, int port) {
this.url = url; this.url = url;
@ -31,6 +33,7 @@ public class CoderClient extends Thread{
this.projectEditorWindow = new ProjectEditorWindow("CoderClient"); this.projectEditorWindow = new ProjectEditorWindow("CoderClient");
LogUtil.setGlobalLevel(Level.INFO);
LogUtil.setGlobalFormatter(new CompactLogFormatter()); LogUtil.setGlobalFormatter(new CompactLogFormatter());
} }
@ -43,14 +46,14 @@ public class CoderClient extends Thread{
//setup a new session //setup a new session
if(connectionRetries > 0){ if(connectionRetries > 0){
logger.info("This is reconnection try number: " + connectionRetries); logger.fine("This is reconnection try number: " + connectionRetries);
} }
logger.info("Setting up a connected session"); logger.fine("Setting up a connected 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.warning("Could not setup a connection to " + url + ":" + port);
connectionRetries++; connectionRetries++;
if(connectionRetries > 5){ if(connectionRetriesLimit > 0 && connectionRetries > connectionRetriesLimit){
//stop trying to connect //stop trying to connect
logger.severe("Was not able to conenct to the remote host."); logger.severe("Was not able to conenct to the remote host.");
break; break;
@ -69,30 +72,36 @@ public class CoderClient extends Thread{
//get user credentials //get user credentials
if(username != null && !username.isEmpty() && password != null && password.length > 0){ if(username != null && !username.isEmpty() && password != null && password.length > 0){
//do nothing, already have all credentials we need //do nothing, already have all credentials we need
logger.info("All login credentials have already been given. No need for a login dialog."); logger.fine("All login credentials have already been given. No need for a login dialog.");
}else{ }else{
//ask for username and password in a dialog window //ask for username and password in a dialog window
logger.info("All or some login credentials have not been given. Using a ogin dialog for input."); logger.info("All or some login credentials have not been given. Using a dialog for input.");
LoginDialog loginDialog = new LoginDialog(username, null); LoginDialog loginDialog = new LoginDialog(username, null);
loginDialog.setVisible(true); //blocking loginDialog.setVisible(true); //blocking
loginDialog.dispose(); loginDialog.dispose();
if(loginDialog.getAction() == LoginDialogAction.CANCEL){ if(loginDialog.getAction() == LoginDialogAction.CANCEL){
logger.info("Login dialog closed or canceled by the user. terminating."); logger.fine("Login dialog closed or canceled by the user. terminating.");
break; break;
} }
username = loginDialog.getUsername(); username = loginDialog.getUsername();
password = loginDialog.getPassword(); password = loginDialog.getPassword();
} }
logger.info("The username: \"" + username + "\" will be used as the credential holder"); logger.fine("The username: \"" + username + "\" will be used as the credential holder");
//authenticate the user //authenticate the user
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); JOptionPane.showMessageDialog(null, "Wrong username or password", "Authentication Failed", JOptionPane.INFORMATION_MESSAGE);
logger.info("Authentication failed: wrong username or password"); logger.severe("Authentication failed: wrong username or password");
continue; continue;
} }
//forget the password
for(int i = 0; i < password.length; ++i){
password[i] = '*';
}
password = null;
//resister a message listener to the session //resister a message listener to the session
session.addCoderMessageReceivedListener(new CoderMessageReceivedListener() { session.addCoderMessageReceivedListener(new CoderMessageReceivedListener() {
@Override @Override
@ -126,7 +135,7 @@ public class CoderClient extends Thread{
} }
//hide the main GUI //hide the main GUI
logger.info("The socket was closed."); logger.fine("The socket was closed.");
this.projectEditorWindow.setVisible(false); this.projectEditorWindow.setVisible(false);
Thread.yield(); Thread.yield();
} }
@ -151,16 +160,40 @@ public class CoderClient extends Thread{
this.username = username; this.username = username;
} }
private void setRetryConnectForever(boolean tryForever) {
if(tryForever){
this.connectionRetriesLimit = 0;
}else{
this.connectionRetriesLimit = DEFAULT_CONNECTION_RETRIES;
}
}
public static void main(String[] args){ public static void main(String[] args){
int port = CoderServer.SERVER_PORT; int port = CoderServer.SERVER_PORT;
CoderClient client = new CoderClient("127.0.0.1", port); String username = null;
char[] password = null;
boolean reconnectForever = false;
//parse program arguments
for(int i = 0; i < args.length; ++i){ for(int i = 0; i < args.length; ++i){
String arg = args[i]; String arg = args[i];
if(arg.equals("-u") || arg.equals("--username")){ if(arg.equals("-h") || arg.equals("--help")){
System.out.println("Usage: CoderClient [options]");
System.out.println("Options:");
System.out.println("-h|--help Print this help.");
System.out.println("-u|--username <username> Set the username to use when authenticating.");
System.out.println("-p|--password <password> Set the password used when authenticating. The password will not be safetly");
System.out.println(" stored and ths option should not be used in anything but debuging.");
System.out.println(" A password cannot be defined without a username. If booth");
System.out.println(" a username and a password has been defined by arguments the software will not.");
System.out.println(" prompt for this.");
System.out.println(" If not a port have been defined, the deafult port will be used.");
System.out.println("--retry-connect-forever Make the software never stop trying to reconnect to its peer.");
System.exit(0);
}else if(arg.equals("-u") || arg.equals("--username")){
i++; i++;
if(i < args.length){ if(i < args.length){
client.setUsername(args[i]); username = args[i];
}else{ }else{
System.err.println("missing username argument"); System.err.println("missing username argument");
System.exit(1); System.exit(1);
@ -168,14 +201,25 @@ public class CoderClient extends Thread{
}else if(arg.equals("-p") || arg.equals("--password")){ }else if(arg.equals("-p") || arg.equals("--password")){
i++; i++;
if(i < args.length){ if(i < args.length){
client.setPassword(args[i].toCharArray()); password = args[i].toCharArray();
}else{ }else{
System.err.println("missing password argument"); System.err.println("missing password argument");
System.exit(1); System.exit(1);
} }
}else if(arg.equals("--retry-connect-forever")){
reconnectForever = true;
} }
} }
//create a client instance
CoderClient client = new CoderClient("127.0.0.1", port);
if(username != null){
client.setUsername(username);
if(password != null){
client.setPassword(password);
}
}
client.setRetryConnectForever(reconnectForever);
client.start(); client.start();
} }

View file

@ -82,7 +82,7 @@ public class ProjectEditorWindow extends JFrame implements TreeSelectionListener
return; return;
Object nodeInfo = node.getUserObject(); Object nodeInfo = node.getUserObject();
if(node.isLeaf()){ if(node.isLeaf()){
logger.info("A file has been selected in the file tree"); logger.fine("A file has been selected in the file tree");
}else{ }else{
} }

View file

@ -43,7 +43,7 @@ public class Session extends Thread {
public static Session setupConnection(String url, int port){ public static Session setupConnection(String url, int port){
Session session = new Session(); Session session = new Session();
logger.info("Setting up a TCP socket to " + url + ":" + port); logger.fine("Setting up a TCP socket to " + url + ":" + port);
try { try {
session.socket = new Socket(url, port); session.socket = new Socket(url, port);
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
@ -82,18 +82,18 @@ public class Session extends Thread {
try { try {
msg = in.readGenericObject(); msg = in.readGenericObject();
} catch (IOException e) { } catch (IOException e) {
logger.log(Level.INFO, "socket is probably closed by the peer", e); logger.log(Level.SEVERE, "socket is probably closed by the peer", e);
break; break;
} }
if(msg == null){ if(msg == null){
logger.info("socket is probably closed by the peer"); logger.severe("socket is probably closed by the peer");
break; break;
}else{ }else{
handleMessage(msg); handleMessage(msg);
} }
Thread.yield(); Thread.yield();
} }
logger.info("the current session will be closed"); logger.fine("the current session will be closed");
close(); close();
return; return;
} }
@ -120,7 +120,7 @@ public class Session extends Thread {
} }
public boolean authenticate(String username, char[] clearTextPassword) { public boolean authenticate(String username, char[] clearTextPassword) {
logger.info("Authenticating session"); logger.fine("Authenticating session");
if(socket == null){ if(socket == null){
logger.severe("Cannot authenticate on a closed socket"); logger.severe("Cannot authenticate on a closed socket");
@ -128,7 +128,7 @@ public class Session extends Thread {
} }
if(authenticated){ if(authenticated){
logger.info("this session is already athenticated and cannot be reauthenticated."); logger.warning("this session is already athenticated and cannot be reauthenticated.");
return true; return true;
} }
@ -146,11 +146,11 @@ public class Session extends Thread {
CoderMessage authReq = new CoderMessage(); CoderMessage authReq = new CoderMessage();
authReq.AuthenticationReq = new AuthenticationReqMsg(); authReq.AuthenticationReq = new AuthenticationReqMsg();
authReq.AuthenticationReq.username = username; authReq.AuthenticationReq.username = username;
logger.info("Sending AuthenticationReq"); logger.fine("Sending AuthenticationReq");
send(authReq); send(authReq);
//Receive AuthenticationChallenge //Receive AuthenticationChallenge
logger.info("Waiting for AuthenticationChallenge"); logger.fine("Waiting for AuthenticationChallenge");
CoderMessage msg; CoderMessage msg;
msg = in.readGenericObject(); msg = in.readGenericObject();
if(msg == null || msg.AuthenticationChallenge == null){ if(msg == null || msg.AuthenticationChallenge == null){
@ -158,11 +158,11 @@ public class Session extends Thread {
close(); close();
return false; return false;
} }
logger.log(Level.INFO, "Received AuthenticationChallenge"); logger.fine("Received AuthenticationChallenge");
// Setting up encryption // Setting up encryption
/* /*
logger.info("Setting up encryption"); logger.fine("Setting up encryption");
String hashedPassword = Hasher.PBKDF2(new String(clearTextPassword), username, AUTH_HASH_ITERATIONS); String hashedPassword = Hasher.PBKDF2(new String(clearTextPassword), username, AUTH_HASH_ITERATIONS);
String key = Hasher.PBKDF2(hashedPassword, msg.AuthenticationChallenge.salt, AUTH_HASH_ITERATIONS); String key = Hasher.PBKDF2(hashedPassword, msg.AuthenticationChallenge.salt, AUTH_HASH_ITERATIONS);
Encrypter crypto; Encrypter crypto;
@ -185,17 +185,17 @@ public class Session extends Thread {
CoderMessage authRsp = new CoderMessage(); CoderMessage authRsp = new CoderMessage();
authRsp.AuthenticationRsp = new AuthenticationRspMsg(); authRsp.AuthenticationRsp = new AuthenticationRspMsg();
authRsp.AuthenticationRsp.timestamp = System.currentTimeMillis(); authRsp.AuthenticationRsp.timestamp = System.currentTimeMillis();
logger.info("Sending AuthenticationRsp"); logger.fine("Sending AuthenticationRsp");
send(authRsp); send(authRsp);
logger.info("Waiting for AuthenticationSuccess"); logger.fine("Waiting for AuthenticationSuccess");
msg = in.readGenericObject(); msg = in.readGenericObject();
if(msg == null || msg.AuthenticationSuccess == null){ if(msg == null || msg.AuthenticationSuccess == null){
logger.severe("Authentication failure"); logger.severe("Authentication failure");
close(); close();
return false; return false;
} }
logger.info("Received AuthenticationSuccess"); logger.fine("Received AuthenticationSuccess");
logger.info("Session authenticated"); logger.info("Session authenticated");
@ -209,7 +209,9 @@ public class Session extends Thread {
} }
private void handleMessage(CoderMessage msg){ private void handleMessage(CoderMessage msg){
logger.fine("Received a CoderMessage");
if(msg.ProjectListRsp != null){ if(msg.ProjectListRsp != null){
logger.fine("The message contains a ProjectListRspMsg");
for(CoderMessageReceivedListener listener : messageReceivedlisteners){ for(CoderMessageReceivedListener listener : messageReceivedlisteners){
listener.projectListRspReceived(msg.ProjectListRsp); listener.projectListRspReceived(msg.ProjectListRsp);
} }