Added task class to threads
This commit is contained in:
parent
eb1754e63e
commit
cedaf8f009
18 changed files with 1150 additions and 379 deletions
|
|
@ -45,6 +45,21 @@ public class AccountsManager {
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean checkPassword(String userName, String password) {
|
||||
synchronized(accounts) {
|
||||
Iterator<Account> i = accounts.iterator();
|
||||
while (i.hasNext()) {
|
||||
Account account = i.next();
|
||||
if (account.userName.equals(userName) && account.password.equals(password)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public int size(){
|
||||
return accounts.size();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ public class Auth extends UnicastRemoteObject implements IAuth {
|
|||
public Response login(String userName, String password) throws RemoteException {
|
||||
Logger.write("Invoked login with username=" + userName + " AND " + " password=" + password);
|
||||
if ((userName != null && !userName.isEmpty()) && (password != null && !password.equals(""))) {
|
||||
if(AccountsManager.getInstance().exists(userName)) {
|
||||
if(AccountsManager.getInstance().exists(userName) && AccountsManager.getInstance().checkPassword(userName, password)) {
|
||||
JsonObject data = new JsonObject();
|
||||
String token = nextSessionId();
|
||||
data.put("token", token);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
package com.texttwist.server.components;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* Created by loke on 19/06/2017.
|
||||
*/
|
||||
public class CheckOnlineUsers implements Callable<Boolean> {
|
||||
private final DefaultListModel<String> users;
|
||||
|
||||
public CheckOnlineUsers( DefaultListModel<String> users) {
|
||||
this.users = users;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean call() throws Exception {
|
||||
System.out.print("Check If users are online!");
|
||||
System.out.println(users);
|
||||
for(int i = 0; i < 1; i++){
|
||||
Thread.sleep(2000);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,10 +2,12 @@
|
|||
package com.texttwist.server.components;
|
||||
import com.sun.org.apache.xpath.internal.operations.Bool;
|
||||
import jdk.nashorn.internal.parser.JSONParser;
|
||||
import models.Message;
|
||||
import org.json.simple.JsonObject;
|
||||
import utilities.Logger;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
|
@ -75,9 +77,12 @@ public class GameServer implements Runnable{
|
|||
buffer.flip();
|
||||
String line = new String(buffer.array(), buffer.position(), buffer.remaining());
|
||||
|
||||
proxy = new ThreadProxy(JSONParser.quote(line));
|
||||
Thread t = new Thread(proxy);
|
||||
t.start();
|
||||
if (line.startsWith("MESSAGE")) {
|
||||
Message msg = Message.toMessage(line);
|
||||
proxy = new ThreadProxy(msg);
|
||||
Thread t = new Thread(proxy);
|
||||
t.start();
|
||||
}
|
||||
|
||||
if (line.startsWith("CLOSE")) {
|
||||
client.close();
|
||||
|
|
|
|||
|
|
@ -46,6 +46,20 @@ public class SessionsManager {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean isValidToken(String token) {
|
||||
synchronized(sessions) {
|
||||
Iterator<Session> i = sessions.iterator();
|
||||
while (i.hasNext()) {
|
||||
if (i.next().token.equals(token)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public int size(){
|
||||
return sessions.size();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
package com.texttwist.server.components;
|
||||
|
||||
import models.Message;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.*;
|
||||
|
|
@ -11,50 +15,41 @@ import java.util.concurrent.*;
|
|||
*/
|
||||
public class ThreadProxy implements Runnable {
|
||||
protected ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||
private String request;
|
||||
ThreadProxy(String request){
|
||||
private Message request;
|
||||
ThreadProxy(Message request){
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
private Callable<Boolean> checkIfUsersAreOnline = new Callable<Boolean>(){
|
||||
String message = "Check If users are online!";
|
||||
@Override
|
||||
public Boolean call() throws Exception {
|
||||
for(int i = 0; i < 1; i++){
|
||||
Thread.sleep(2000);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
private Boolean isValidToken(String token){
|
||||
return SessionsManager.getInstance().isValidToken(token);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
System.out.println("Selecting right task for new thread");
|
||||
/* byte[] buffer = new byte[100];
|
||||
try {
|
||||
InputStream clientInputStream = clientSocket.getInputStream();
|
||||
while (clientInputStream.read(buffer) != -1) {
|
||||
request += buffer;
|
||||
|
||||
if(isValidToken(request.token)){
|
||||
switch(request.message){
|
||||
case "START_GAME":
|
||||
Future<Boolean> newTask = threadPool.submit(new CheckOnlineUsers(request.data));
|
||||
Boolean returnedValue = null;
|
||||
try {
|
||||
returnedValue = newTask.get();
|
||||
System.out.println(returnedValue);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println(returnedValue);
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
System.out.println(request);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
*/
|
||||
//Assegna un threadWorker al task in arrivo
|
||||
System.out.print(request);
|
||||
Future<Boolean> callableFuture = threadPool.submit(checkIfUsersAreOnline);
|
||||
|
||||
try {
|
||||
// get() waits for the task to finish and then gets the result
|
||||
Boolean returnedValue = callableFuture.get();
|
||||
System.out.println(returnedValue);
|
||||
} catch (InterruptedException e) {
|
||||
// thrown if task was interrupted before completion
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
// thrown if the task threw an execption while executing
|
||||
e.printStackTrace();
|
||||
} else {
|
||||
System.out.print("TOKEN NON VALIDO");
|
||||
//RISPONDI ERRORE TOKEN NON VALIDO
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue