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 static final Logger logger = LogUtil.getLogger();
private static final int DEFAULT_CONNECTION_RETRIES = 5;
private String url;
private int port;
@ -24,6 +25,7 @@ public class CoderClient extends Thread{
private ProjectEditorWindow projectEditorWindow;
private String username = null;
private char[] password = null;
private int connectionRetriesLimit = DEFAULT_CONNECTION_RETRIES; //if zero, try forever
public CoderClient(String url, int port) {
this.url = url;
@ -31,6 +33,7 @@ public class CoderClient extends Thread{
this.projectEditorWindow = new ProjectEditorWindow("CoderClient");
LogUtil.setGlobalLevel(Level.INFO);
LogUtil.setGlobalFormatter(new CompactLogFormatter());
}
@ -43,14 +46,14 @@ public class CoderClient extends Thread{
//setup a new session
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);
if(session == null){
logger.info("Could not setup a connection to " + url + ":" + port);
logger.warning("Could not setup a connection to " + url + ":" + port);
connectionRetries++;
if(connectionRetries > 5){
if(connectionRetriesLimit > 0 && connectionRetries > connectionRetriesLimit){
//stop trying to connect
logger.severe("Was not able to conenct to the remote host.");
break;
@ -69,30 +72,36 @@ public class CoderClient extends Thread{
//get user credentials
if(username != null && !username.isEmpty() && password != null && password.length > 0){
//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{
//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.setVisible(true); //blocking
loginDialog.dispose();
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;
}
username = loginDialog.getUsername();
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
boolean authenticated = session.authenticate(username, password);
if(!authenticated){
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;
}
//forget the password
for(int i = 0; i < password.length; ++i){
password[i] = '*';
}
password = null;
//resister a message listener to the session
session.addCoderMessageReceivedListener(new CoderMessageReceivedListener() {
@Override
@ -126,7 +135,7 @@ public class CoderClient extends Thread{
}
//hide the main GUI
logger.info("The socket was closed.");
logger.fine("The socket was closed.");
this.projectEditorWindow.setVisible(false);
Thread.yield();
}
@ -151,16 +160,40 @@ public class CoderClient extends Thread{
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){
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){
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++;
if(i < args.length){
client.setUsername(args[i]);
username = args[i];
}else{
System.err.println("missing username argument");
System.exit(1);
@ -168,14 +201,25 @@ public class CoderClient extends Thread{
}else if(arg.equals("-p") || arg.equals("--password")){
i++;
if(i < args.length){
client.setPassword(args[i].toCharArray());
password = args[i].toCharArray();
}else{
System.err.println("missing password argument");
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();
}