A simple client for CoderServer
This commit is contained in:
parent
34a65faf9d
commit
a414f42e58
11 changed files with 844 additions and 0 deletions
80
src/com/coder/client/CoderClient.java
Normal file
80
src/com/coder/client/CoderClient.java
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
package com.coder.client;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.coder.client.LoginDialog.LoginDialogAction;
|
||||
import com.coder.server.CoderServer;
|
||||
|
||||
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;
|
||||
private ClientWindow window;
|
||||
//private Scanner userInput = new Scanner(System.in);
|
||||
|
||||
public CoderClient(String url, int port) {
|
||||
this.url = url;
|
||||
this.port = port;
|
||||
|
||||
this.window = new ClientWindow("CoderClient");
|
||||
|
||||
super.start();
|
||||
}
|
||||
|
||||
public void run(){
|
||||
String username = "";
|
||||
while(true){
|
||||
this.session = Session.setupConnection(url, port);
|
||||
if(session == null){
|
||||
logger.info("Could not setup a connection to " + url + ":" + port);
|
||||
continue;
|
||||
}
|
||||
|
||||
LoginDialog loginDialog = new LoginDialog(username, null);
|
||||
loginDialog.setVisible(true);
|
||||
if(loginDialog.getAction() == LoginDialogAction.CANCEL){
|
||||
logger.info("Login dialog closed or canceled by the user. terminating.");
|
||||
break;
|
||||
}
|
||||
username = loginDialog.getUsername();
|
||||
String password = loginDialog.getPassword();
|
||||
/*
|
||||
System.out.println("username:");
|
||||
String username = userInput.next();
|
||||
System.out.println("password:");
|
||||
String password = userInput.next();
|
||||
*/
|
||||
boolean authenticated = session.authenticate(username, password);
|
||||
if(!authenticated){
|
||||
System.out.println("Authentication failed");
|
||||
continue;
|
||||
}
|
||||
|
||||
session.start();
|
||||
|
||||
this.window.setVisible(true);
|
||||
try {Thread.sleep(1000);} catch (InterruptedException e) {}
|
||||
|
||||
while(session.isConnected()){
|
||||
Thread.yield();
|
||||
}
|
||||
|
||||
logger.info("The socket was closed. terminating.");
|
||||
this.window.setVisible(false);
|
||||
Thread.yield();
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
int port = CoderServer.SERVER_PORT;
|
||||
new CoderClient("127.0.0.1", port);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue