Added registration client/server

This commit is contained in:
Lorenzo Iovino 2017-06-17 18:12:56 +02:00
parent dd799f6d99
commit 546e341433
18 changed files with 817 additions and 418 deletions

816
.idea/workspace.xml generated

File diff suppressed because it is too large Load diff

View file

@ -20,6 +20,6 @@ public class Main {
System.out.println("Font non caricato correttamente");
}
Entrypoint entrypoint = new Entrypoint();
App entrypoint = new App();
}
}

View file

@ -16,6 +16,9 @@ public class Palette {
public static Color registerLblBtn_onmouseover_color = new Color(95,0,0, 127);
public static Color registerLblBtn_onmouseclick_color = new Color(53,66,32);
public static Color scrollPanel_backgroundColor = new Color(220,229,207);
public static Color dialog_alert = Color.red;
public static Color dialog_success = new Color(53,66,32);
public static Font button_font = new Font("DK Trained Monkey", Font.BOLD, 30);

View file

@ -8,17 +8,23 @@ import java.awt.*;
/**
* Created by loke on 13/06/2017.
*/
public class Entrypoint extends JFrame {
public class App extends JFrame {
protected TTContainer root;
public static AuthService authService;
public static SessionService sessionService;
public Entrypoint(){
public App(){
setPreferredSize( new Dimension( 640, 480 ));
setSize(new Dimension(640,480));
setLocation(100,100);
setResizable( false );
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Init services
authService = new AuthService();
sessionService = new SessionService();
Home home = new Home(this);
}
}

View file

@ -1,4 +1,5 @@
package com.texttwist.client.pages;
import com.texttwist.client.constants.Config;
import interfaces.ITTAuth;
import models.TTResponse;
@ -11,21 +12,25 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by loke on 15/06/2017.
* Created by loke on 17/06/2017.
*/
public class HomeManager {
ExecutorService executor = Executors.newSingleThreadExecutor();
String baseUrl = Config.getRMIServerAddress().concat("/auth");
public class AuthService {
public HomeManager(){
protected String baseUrl = Config.getRMIServerAddress().concat("/auth");
public AuthService(){
}
public TTResponse login(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException {
System.out.print(password);
ITTAuth auth = (ITTAuth) Naming.lookup(baseUrl);
return auth.login(userName, password);
}
public TTResponse register(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException {
ITTAuth auth = (ITTAuth) Naming.lookup(baseUrl);
return auth.register(userName, password);
}
public TTResponse logout(String userName, String token) throws RemoteException, NotBoundException, MalformedURLException {
ITTAuth auth = (ITTAuth) Naming.lookup(baseUrl);
return auth.logout(userName, token);

View file

@ -1,6 +1,7 @@
package com.texttwist.client.pages;
import com.texttwist.client.constants.Palette;
import com.texttwist.client.ui.*;
import com.texttwist.client.ui.TTDialog;
import models.TTResponse;
import javax.swing.*;
@ -10,12 +11,12 @@ import java.util.concurrent.Callable;
public class Home extends Page {
private TTContainer loginDataContainer;
private HomeManager homeManager;
private HomeController homeController;
private TTContainer logoContainer;
public Home(JFrame window) {
super(window);
homeManager = new HomeManager();
homeController = new HomeController();
createUIComponents();
window.setVisible(true);
}
@ -49,13 +50,18 @@ public class Home extends Page {
@Override
public Object call() throws Exception {
//TODO CHIAMA API PER LOGIN E SE TUTTO OKEY MANDA A PAGINA DEL MENU
TTResponse res = homeManager.login(usernameField.getText(), String.valueOf(passwordField.getPassword()));
TTResponse res = homeController.login(usernameField.getText(), String.valueOf(passwordField.getPassword()));
if (res.code == 200){
//OK, go to next page and show popup
return new Menu(window);
} else {
JOptionPane.showMessageDialog(null, "Login Failes");
return null;
return new TTDialog("alert", res.message,
new Callable() {
@Override
public Object call() throws Exception {
return new Home(window);
}
},null);
}
}
},

View file

@ -0,0 +1,28 @@
package com.texttwist.client.pages;
import com.texttwist.client.constants.Config;
import interfaces.ITTAuth;
import models.TTResponse;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by loke on 15/06/2017.
*/
public class HomeController {
public HomeController(){
}
public TTResponse login(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException {
TTResponse res = App.authService.login(userName,password);
if (res.code == 200){
App.sessionService.create(userName, password);
}
return res;
}
}

View file

@ -13,10 +13,12 @@ import java.util.concurrent.Callable;
public class Menu extends Page{
private TTContainer menuBar;
private MenuController menuController;
public Menu(JFrame window) throws IOException {
super(window);
createUIComponents();
menuController = new MenuController();
window.setVisible(true);
}
@ -24,27 +26,36 @@ public class Menu extends Page{
@Override
public void createUIComponents() throws IOException {
addLogo(root);
menuBar = new TTContainer(
null,
new Dimension(1150,220),
new Dimension(1150,280),
Palette.root_backgroundColor,
-1,
root);
TTLabel playerToSendInvite_flavourText = new TTLabel(
new Point(25,15),
new Dimension(350,20),
"Welcome back, " + App.sessionService.account.userName + "!",
new Font(Palette.inputBox_font.getFontName(), Font.ITALIC, 24),
null,
menuBar);
TTButton newMatch = new TTButton(
new Point(25,30),
new Point(25,70),
new Dimension(250,75),
"New Match!",
new Callable<Object>() {
@Override
public Object call() throws Exception {
//TODO CHIAMA API PER REGISTRAZIONE E SE TUTTO OKEY MANDA A PAGINA LOGIN
return new MatchSetup(window);
}
},
menuBar);
TTButton matchRequests = new TTButton(
new Point(290,30),
new Point(290,70),
new Dimension(250,75),
"In pending",
new Callable<Object>() {
@ -56,13 +67,13 @@ public class Menu extends Page{
},
menuBar);
TTCircleCounter circleCounter = new TTCircleCounter(
new Point(290,30),
new Point(290,70),
new Dimension(25,25),
menuBar.getGraphics(),
menuBar);
TTButton highscores = new TTButton(
new Point(25, 115),
new Point(25, 155),
new Dimension(250, 75),
"Highscores",
new Callable<Object>() {
@ -74,25 +85,17 @@ public class Menu extends Page{
menuBar);
TTButton logout = new TTButton(
new Point(290, 115),
new Point(290, 155),
new Dimension(250, 75),
"Logout",
new Callable<Object>() {
@Override
public Object call() throws Exception {
menuController.logout("","");
return new Home(Page.window);
}
},
menuBar);
addFooter(root);
addBack(footer,
new Callable<Object>() {
@Override
public Object call() throws Exception {
return new Home(Page.window);
}
});
}
}

View file

@ -0,0 +1,27 @@
package com.texttwist.client.pages;
import com.texttwist.client.constants.Config;
import interfaces.ITTAuth;
import models.TTResponse;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
/**
* Created by loke on 17/06/2017.
*/
public class MenuController {
public MenuController(){
}
public TTResponse logout(String userName, String token) throws RemoteException, NotBoundException, MalformedURLException {
TTResponse res = App.authService.logout(userName,token);
if (res.code == 200){
App.sessionService.remove();
}
return res;
}
}

View file

@ -2,6 +2,7 @@ package com.texttwist.client.pages;
import com.texttwist.client.constants.Palette;
import com.texttwist.client.ui.*;
import models.TTResponse;
import javax.swing.*;
import java.awt.*;
@ -13,9 +14,11 @@ import java.util.concurrent.Callable;
public class Register extends Page {
private TTContainer registerDataContainer;
private RegisterController registerController;
public Register(JFrame window) {
super(window);
createUIComponents();
registerController = new RegisterController();
window.setVisible(true);
}
@ -51,9 +54,25 @@ public class Register extends Page {
new Callable<Object>() {
@Override
public Object call() throws Exception {
//TODO CHIAMA API PER REGISTRAZIONE E SE TUTTO OKEY MANDA A PAGINA LOGIN
return new Home(window);
//TODO CHIAMA API PER LOGIN E SE TUTTO OKEY MANDA A PAGINA DEL MENU
TTResponse res = registerController.register(usernameField.getText(), String.valueOf(passwordField.getPassword()));
if (res.code == 200){
return new TTDialog("success", res.message,
new Callable() {
@Override
public Object call() throws Exception {
return new Home(window);
}
},null);
} else {
return new TTDialog("alert", res.message,
new Callable() {
@Override
public Object call() throws Exception {
return null;
}
},null);
}
}
},
registerDataContainer);

View file

@ -0,0 +1,23 @@
package com.texttwist.client.pages;
import com.texttwist.client.constants.Config;
import interfaces.ITTAuth;
import models.TTResponse;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
/**
* Created by loke on 17/06/2017.
*/
public class RegisterController {
public RegisterController(){
}
public TTResponse register(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException {
return App.authService.register(userName,password);
}
}

View file

@ -0,0 +1,28 @@
package com.texttwist.client.pages;
import interfaces.ITTAuth;
import models.TTAccount;
import models.TTResponse;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
/**
* Created by loke on 17/06/2017.
*/
public class SessionService {
protected TTAccount account;
public SessionService(){}
public void create(String userName, String token) {
account = new TTAccount(userName, token);
}
public void remove(){
account = null;
}
}

View file

@ -7,6 +7,7 @@ import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.concurrent.Callable;
/**

View file

@ -0,0 +1,142 @@
package com.texttwist.client.ui;
import com.texttwist.client.constants.Palette;
import com.texttwist.client.pages.Home;
import com.texttwist.client.ui.TTContainer;
import com.texttwist.client.ui.TTLabel;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.io.IOException;
import java.util.concurrent.Callable;
/**
* Created by loke on 17/06/2017.
*/
public class TTDialog extends JFrame {
TTContainer root;
public TTDialog(String type, String message, Callable okHandler, Callable cancelHandler) {
setPreferredSize( new Dimension( 450, 200 ));
setSize(new Dimension(450,200));
setLocation(200,300);
setResizable(false);
setAlwaysOnTop(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setUndecorated(true);
setContentPane(new ShadowPane());
root = new TTContainer(
new Point(40,20),
new Dimension(0,0),
Palette.root_backgroundColor,
-1,
null);
switch (type){
case "alert":{
root.setBorder(BorderFactory.createLineBorder(Palette.dialog_alert));
}
case "success": {
root.setBorder(BorderFactory.createLineBorder(Palette.dialog_success));
}
}
add(root);
TTLabel msg = new TTLabel(
new Point(60,20),
new Dimension(350,50),
message,
new Font(Palette.inputBox_font.getFontName(), Font.ITALIC, 38),
null,
root);
if(okHandler != null && cancelHandler != null){
TTButton okBtn = new TTButton(
new Point(60,100),
new Dimension(150,50),
"Ok",
new Callable<Object>() {
@Override
public Object call() throws Exception {
okHandler.call();
dispose();
return null;
}
},
root);
TTButton cancelBtn = new TTButton(
new Point(250,100),
new Dimension(150,50),
"Cancel",
new Callable<Object>() {
@Override
public Object call() throws Exception {
cancelHandler.call();
dispose();
return null;
}
},
root);
} else {
if(cancelHandler != null) {
TTButton cancelBtn = new TTButton(
new Point(150, 100),
new Dimension(150, 50),
"Cancel",
new Callable<Object>() {
@Override
public Object call() throws Exception {
cancelHandler.call();
dispose();
return null;
}
},
root);
}
if(okHandler != null) {
TTButton okBtn = new TTButton(
new Point(150,100),
new Dimension(150,50),
"Ok",
new Callable<Object>() {
@Override
public Object call() throws Exception {
okHandler.call();
dispose();
return null;
}
},
root);
}
}
setVisible(true);
}
public class ShadowPane extends JPanel {
public ShadowPane() {
setLayout(new BorderLayout());
setOpaque(false);
setBackground(Color.BLACK);
setBorder(new EmptyBorder(0, 0, 5, 5));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
g2d.fillRect(5, 5, getWidth(), getHeight());
g2d.dispose();
}
}
}

View file

@ -1,8 +1,6 @@
package com.texttwist.client.ui;
import com.texttwist.client.constants.Palette;
import com.texttwist.client.pages.Entrypoint;
import com.texttwist.client.pages.Page;
import javax.swing.*;
import java.awt.*;

View file

@ -9,5 +9,7 @@ import java.rmi.RemoteException;
*/
public interface ITTAuth extends Remote {
TTResponse login(String userName, String password) throws RemoteException;
TTResponse register(String userName, String password) throws RemoteException;
TTResponse logout(String userName, String token) throws RemoteException;
}

View file

@ -0,0 +1,14 @@
package models;
/**
* Created by loke on 17/06/2017.
*/
public class TTAccount {
public String userName;
public String token;
public TTAccount(String userName, String token){
this.userName = userName;
this.token = token;
}
}

View file

@ -16,37 +16,59 @@ public class TTAuth extends UnicastRemoteObject implements ITTAuth {
private SecureRandom random = new SecureRandom();
public String nextSessionId() {
return new BigInteger(130, random).toString(32);
}
public TTAuth() throws RemoteException{
}
@Override
public TTResponse register(String userName, String password) throws RemoteException {
TTLogger.write("LOGGER: Invoked register with username=" + userName + " AND " + " password=" + password);
if ((userName != null && !userName.isEmpty()) && (password != null && !password.equals(""))) {
boolean res = true;//TODO Inserisci nel db e verifica se c'è un utente con lo stesso nome
if(userName.equals("ciro")){ //TODO rimuovere
res = false;
}
if(res) {
TTLogger.write("LOGGER: Registration successfull");
return new TTResponse("Registration successfull", 200, null);
} else {
TTLogger.write("LOGGER: Registration unsuccessfull");
return new TTResponse("Registration unsuccessfull: Username exist!", 400, null);
}
}
return new TTResponse("Registration unsuccessfull! All fields are mandatories", 400, null);
}
@Override
public TTResponse login(String userName, String password) throws RemoteException {
TTLogger.write("LOGGER: Invoked login with username=" + userName + " AND " + " password=" + password);
if ((userName != null && !userName.isEmpty())
&& (password != null && !password.equals(""))) {
if ((userName != null && !userName.isEmpty()) && (password != null && !password.equals(""))) {
if((userName.equalsIgnoreCase("admin"))
&& (password.equals("admin"))) {
JsonObject data = new JsonObject();
data.put("token", nextSessionId());
System.out.println("LOGGER: Login successfull");
TTLogger.write("LOGGER: Login successfull");
return new TTResponse("Login successfull", 200, data);
}
}
System.out.println("LOGGER: Login unsuccessfull");
return new TTResponse("Login unsuccessfull", 400, null);
}
@Override
public TTResponse logout(String userName, String token) throws RemoteException {
System.out.println("LOGGER: Invoked logout with username=" + userName + " AND " + " token=" + token);
if ((userName != null && !userName.isEmpty())
&& (token != null && !token.isEmpty())) {
TTLogger.write("LOGGER: Invoked logout with username=" + userName + " AND " + " token=" + token);
if ((userName != null && !userName.isEmpty()) && (token != null && !token.isEmpty())) {
return new TTResponse("Logout successfull", 200, null);
}
return new TTResponse("Logout successfull (but something go wrong)", 200, null);
return new TTResponse("Logout successfull (but something gone wrong)", 200, null);
}
public String nextSessionId() {
return new BigInteger(130, random).toString(32);
}
}