Browse Source

Registering is now possible

Allexit 4 years ago
parent
commit
7e2583b6c0

+ 1
- 1
nbproject/project.properties View File

@@ -53,7 +53,7 @@ javadoc.noindex=false
53 53
 javadoc.nonavbar=false
54 54
 javadoc.notree=false
55 55
 javadoc.private=false
56
-javadoc.reference.kryonet-2.21-all.jar=/home/somersby/programming/NeptulonBank/libs/javadoc
56
+javadoc.reference.kryonet-2.21-all.jar=libs/javadoc
57 57
 javadoc.splitindex=true
58 58
 javadoc.use=true
59 59
 javadoc.version=false

+ 5
- 2
src/com/saltosion/neptulon/ClientMain.java View File

@@ -7,6 +7,7 @@ import com.saltosion.neptulon.net.NetRegisterer;
7 7
 import com.saltosion.neptulon.net.PackageDistributor;
8 8
 import com.saltosion.neptulon.net.PackageHandler;
9 9
 import com.saltosion.neptulon.net.packages.BankInformationPackage;
10
+import com.saltosion.neptulon.net.packages.LoginResponsePackage;
10 11
 import com.saltosion.neptulon.net.packages.NetPackage;
11 12
 import com.saltosion.neptulon.ui.UI;
12 13
 import com.saltosion.neptulon.ui.UICreator;
@@ -44,9 +45,11 @@ public class ClientMain implements PackageHandler {
44 45
 		currentUI = creator.create();
45 46
 
46 47
 		//Initialize PackageDistributors
47
-		PackageDistributor.addHandlerFor(this, BankInformationPackage.class);
48
-		PackageDistributor.addHandlerFor(currentUI.getPackageHandler(), 
48
+		PackageDistributor.addHandlerFor(this, 
49 49
 				BankInformationPackage.class);
50
+		PackageDistributor.addHandlerFor(currentUI.getPackageHandler(), 
51
+				BankInformationPackage.class,
52
+				LoginResponsePackage.class);
50 53
 
51 54
 		//Initialize properties
52 55
 		properties = JsonLoader.loadClientProperties();

+ 24
- 10
src/com/saltosion/neptulon/ConnectionManager.java View File

@@ -4,7 +4,8 @@ import com.esotericsoftware.kryonet.Connection;
4 4
 import com.esotericsoftware.kryonet.Listener;
5 5
 import com.saltosion.neptulon.net.PackageHandler;
6 6
 import com.saltosion.neptulon.net.packages.LoginPackage;
7
-import com.saltosion.neptulon.net.packages.LoginPackage.LoginAction;
7
+import com.saltosion.neptulon.net.packages.LoginResponsePackage;
8
+import com.saltosion.neptulon.net.packages.LoginResponsePackage.LoginResponse;
8 9
 import com.saltosion.neptulon.net.packages.NetPackage;
9 10
 import com.saltosion.neptulon.util.Encrypter;
10 11
 import com.saltosion.neptulon.util.Problem;
@@ -36,16 +37,22 @@ public class ConnectionManager extends Listener implements PackageHandler {
36 37
 	public void handle(NetPackage obj, Connection connection) {
37 38
 		if (obj instanceof LoginPackage) {
38 39
 			LoginPackage loginPackage = (LoginPackage) obj;
40
+			String BID = bankManager.getAccountBID(loginPackage.username);
41
+			LoginResponsePackage responsePackage = new LoginResponsePackage();
42
+			responsePackage.BID = Problem.BID_NOT_FOUND;
43
+			responsePackage.attemptedAction = loginPackage.action;
44
+			responsePackage.usedUsername = loginPackage.username;
45
+			
39 46
 			switch (loginPackage.action) {
40 47
 				case LOGIN: {
41 48
 					// Test username
42
-					String BID = bankManager.getAccountBID(loginPackage.username);
43 49
 					if (BID.equals(Problem.BID_NOT_FOUND)) {
44 50
 						logger.log(Level.WARNING, "Connection {0}: username not found ''{1}''",
45 51
 								new Object[]{
46 52
 									connection.getID(),
47 53
 									loginPackage.username});
48
-						return;
54
+						responsePackage.response = LoginResponse.BAD_USERNAME;
55
+						break;
49 56
 					}
50 57
 
51 58
 					// Get encrypted password from sent password
@@ -55,7 +62,7 @@ public class ConnectionManager extends Listener implements PackageHandler {
55 62
 								serverMain.getProperties().getEncryptKey());
56 63
 					} catch (GeneralSecurityException ex) {
57 64
 						logger.log(Level.SEVERE, "Connection {0}: {1}",
58
-								new Object[] {
65
+								new Object[]{
59 66
 									connection.getID(),
60 67
 									ex.getMessage()
61 68
 								});
@@ -65,17 +72,20 @@ public class ConnectionManager extends Listener implements PackageHandler {
65 72
 					// Test password
66 73
 					if (bankManager.comparePassword(loginPackage.username, encryptedPassword)) {
67 74
 						connectedUsers.put(connection, BID);
68
-						logger.log(Level.INFO, "Connection {0} logged in as {1}", 
69
-								new Object[] {
75
+						logger.log(Level.INFO, "Connection {0} logged in as {1}",
76
+								new Object[]{
70 77
 									connection.getID(),
71 78
 									loginPackage.username
72 79
 								});
80
+						responsePackage.response = LoginResponse.SUCCESS;
81
+						responsePackage.BID = BID;
73 82
 					} else {
74 83
 						logger.log(Level.WARNING, "Connection {0}: Incorrect password login on {1}",
75
-								new Object[] {
84
+								new Object[]{
76 85
 									connection.getID(),
77 86
 									loginPackage.username
78 87
 								});
88
+						responsePackage.response = LoginResponse.BAD_PASSWORD;
79 89
 					}
80 90
 					break;
81 91
 				}
@@ -89,7 +99,8 @@ public class ConnectionManager extends Listener implements PackageHandler {
89 99
 									connection.getID(),
90 100
 									loginPackage.username
91 101
 								});
92
-						return;
102
+						responsePackage.response = LoginResponse.BAD_USERNAME;
103
+						break;
93 104
 					}
94 105
 
95 106
 					// Get encrypted password from sent password
@@ -106,17 +117,20 @@ public class ConnectionManager extends Listener implements PackageHandler {
106 117
 						return;
107 118
 					}
108 119
 
109
-					String BID = bankManager.createAccount(
120
+					BID = bankManager.createAccount(
110 121
 							loginPackage.username,
111 122
 							encryptedPassword);
112 123
 					logger.log(Level.INFO, "Connection {0}: Registered a new bank account {1}",
113
-							new Object[] {
124
+							new Object[]{
114 125
 								connection.getID(),
115 126
 								loginPackage.username
116 127
 							});
128
+					responsePackage.response = LoginResponse.SUCCESS;
117 129
 					break;
118 130
 				}
119 131
 			}
132
+			serverMain.getServer().sendToTCP(connection.getID(),
133
+					responsePackage);
120 134
 		}
121 135
 	}
122 136
 

+ 2
- 0
src/com/saltosion/neptulon/net/NetRegisterer.java View File

@@ -9,8 +9,10 @@ public class NetRegisterer {
9 9
 		kryo.register(NetPackage.class);
10 10
 		kryo.register(BankInformationPackage.class);
11 11
 		kryo.register(LoginPackage.class);
12
+		kryo.register(LoginResponsePackage.class);
12 13
 
13 14
 		kryo.register(LoginPackage.LoginAction.class);
15
+		kryo.register(LoginResponsePackage.LoginResponse.class);
14 16
 	}
15 17
 
16 18
 }

+ 15
- 0
src/com/saltosion/neptulon/net/packages/LoginResponsePackage.java View File

@@ -0,0 +1,15 @@
1
+package com.saltosion.neptulon.net.packages;
2
+
3
+import com.saltosion.neptulon.net.packages.LoginPackage.LoginAction;
4
+
5
+public class LoginResponsePackage extends NetPackage {	
6
+	public enum LoginResponse {
7
+		SUCCESS, BAD_USERNAME, BAD_PASSWORD
8
+	};
9
+	
10
+	public String BID;
11
+	public String usedUsername;
12
+	public LoginResponse response;
13
+	public LoginAction attemptedAction;
14
+	
15
+}

+ 32
- 4
src/com/saltosion/neptulon/ui/ClientTBUICreator.java View File

@@ -7,6 +7,9 @@ import com.saltosion.neptulon.ServerMain;
7 7
 import com.saltosion.neptulon.net.PackageHandler;
8 8
 import com.saltosion.neptulon.net.packages.BankInformationPackage;
9 9
 import com.saltosion.neptulon.net.packages.LoginPackage;
10
+import com.saltosion.neptulon.net.packages.LoginPackage.LoginAction;
11
+import com.saltosion.neptulon.net.packages.LoginResponsePackage;
12
+import com.saltosion.neptulon.net.packages.LoginResponsePackage.LoginResponse;
10 13
 import com.saltosion.neptulon.net.packages.NetPackage;
11 14
 import com.saltosion.neptulon.ui.textbasedcommands.Command;
12 15
 import com.saltosion.neptulon.ui.textbasedcommands.CommandManager;
@@ -20,10 +23,12 @@ public class ClientTBUICreator implements UICreator<TextBasedUI>, PackageHandler
20 23
 
21 24
 	private ClientMain clientMain;
22 25
 
26
+	private Logger logger;
27
+
23 28
 	public TextBasedUI create() {
24 29
 		TextBasedUI ui = new TextBasedUI(this);
25 30
 		CommandManager commandManager = ui.getCommandManager();
26
-		Logger logger = TextBasedUI.logger;
31
+		logger = Logger.getLogger(ui.getClass().getName());
27 32
 
28 33
 		commandManager.getCommandList().add(
29 34
 				new Command("about",
@@ -136,9 +141,9 @@ public class ClientTBUICreator implements UICreator<TextBasedUI>, PackageHandler
136 141
 						loginPackage.username = args.get(0);
137 142
 						loginPackage.password = encryptedPassword;
138 143
 						loginPackage.action = LoginPackage.LoginAction.LOGIN;
139
-						clientMain.sendPackage(loginPackage);
140 144
 						logger.log(Level.INFO, "Attempting to log in as {0}..",
141 145
 								username);
146
+						clientMain.sendPackage(loginPackage);
142 147
 					}
143 148
 
144 149
 				});
@@ -166,9 +171,9 @@ public class ClientTBUICreator implements UICreator<TextBasedUI>, PackageHandler
166 171
 						loginPackage.username = args.get(0);
167 172
 						loginPackage.password = pass;
168 173
 						loginPackage.action = LoginPackage.LoginAction.REGISTER;
169
-						clientMain.sendPackage(loginPackage);
170
-						logger.log(Level.INFO, "Attempting to register as {0}..", 
174
+						logger.log(Level.INFO, "Attempting to register as {0}..",
171 175
 								args.get(0));
176
+						clientMain.sendPackage(loginPackage);
172 177
 					}
173 178
 
174 179
 				});
@@ -187,6 +192,29 @@ public class ClientTBUICreator implements UICreator<TextBasedUI>, PackageHandler
187 192
 			BankInformationPackage information = (BankInformationPackage) obj;
188 193
 			TextBasedUI.logger.log(Level.INFO, "Entered {0}, and changed used currency to {1}",
189 194
 					new Object[]{information.bankName, information.currencyName});
195
+		} else if (obj instanceof LoginResponsePackage) {
196
+			LoginResponsePackage response = (LoginResponsePackage) obj;
197
+			switch (response.attemptedAction) {
198
+				case LOGIN: {
199
+					if (response.response == LoginResponse.BAD_PASSWORD
200
+							|| response.response == LoginResponse.BAD_USERNAME) {
201
+						logger.log(Level.WARNING, "Invalid username or password.");
202
+					} else if (response.response == LoginResponse.SUCCESS) {
203
+						logger.log(Level.INFO, "Logged in as {0}. Welcome!", 
204
+								response.usedUsername);
205
+					}
206
+					break;
207
+				}
208
+				case REGISTER: {
209
+					if (response.response == LoginResponse.BAD_USERNAME) {
210
+						logger.log(Level.WARNING, "Username ''{0}'' is already taken.", 
211
+								response.usedUsername);
212
+					} else if (response.response == LoginResponse.SUCCESS) {
213
+						logger.log(Level.INFO, "Successfully created account with username {0}, log in with ''login''", 
214
+								response.usedUsername);
215
+					}
216
+				}
217
+			}
190 218
 		}
191 219
 	}
192 220
 

+ 2
- 1
src/com/saltosion/neptulon/ui/UI.java View File

@@ -22,7 +22,8 @@ public interface UI {
22 22
 	 * certain packages will differ on server and clientside.
23 23
 	 * Packages to handle clientside:
24 24
 	 * <li>
25
-	 * <ul> BankInformation </ul>
25
+	 * <ul> {@link com.saltosion.neptulon.net.packages.BankInformationPackage} </ul>
26
+	 * <ul> {@link com.saltosion.neptulon.net.packages.LoginResponsePackage} </ul>
26 27
 	 * </li>
27 28
 	 * @return PackageHandler must return packagehandler on conditions mentioned above
28 29
 	 */